caimeiApi.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @Time 2019-12-12
  3. * @Author Zhengjingyi
  4. * @Action 全局公共方法
  5. */
  6. import requestUrl from './config.js'
  7. const caimeiApi = {
  8. /**
  9. * @封装公共get数据请求方法无加载动画
  10. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  11. * @自定义请求头信息
  12. */
  13. get:function(url,data,callback){
  14. uni.request({
  15. url: requestUrl + url,
  16. data:data,
  17. header: {
  18. 'Accept': 'application/json',
  19. 'Content-Type': 'application/x-www-form-urlencoded',
  20. 'Cookie': uni.getStorageSync('cookieKey')
  21. },
  22. method: 'GET',
  23. success: (response) => {
  24. if(response.statusCode !== 200){
  25. uni.showToast({icon: 'none',title:'网络链接超时',duration: 3000})
  26. }else{
  27. callback(response.data);
  28. }
  29. },
  30. fail: (error) => {
  31. if (error) {
  32. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 3000})
  33. }
  34. }
  35. });
  36. },
  37. /**
  38. * @封装公共get数据请求方法有加载动画
  39. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  40. * @自定义请求头信息
  41. */
  42. lodingGet:function(url,data,callback){
  43. uni.showLoading({mask: true,title:'加载中~',});
  44. uni.request({
  45. url: requestUrl + url,
  46. data:data,
  47. header: {
  48. 'Accept': 'application/json',
  49. 'Content-Type': 'application/x-www-form-urlencoded',
  50. 'Cookie': uni.getStorageSync('cookieKey')
  51. },
  52. method: 'GET',
  53. success: (response) => {
  54. if(response.statusCode !== 200){
  55. uni.showToast({icon: 'none',title: '网络链接超时',duration: 3000})
  56. }else{
  57. callback(response.data);
  58. }
  59. },
  60. fail: (error) => {
  61. if (error) {
  62. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 3000})
  63. }
  64. },
  65. complete: () => {
  66. setTimeout(function () {
  67. uni.hideLoading();
  68. }, 250);
  69. }
  70. });
  71. },
  72. /**
  73. * @封装公共post数据请求方法
  74. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  75. */
  76. post:function(url,data,callback){
  77. uni.showLoading({mask: true,title:'加载中~'})
  78. uni.request({
  79. url: requestUrl+url,
  80. data:data,
  81. header: {
  82. 'Accept': 'application/json',
  83. 'Content-Type': 'application/json',
  84. 'Cookie': uni.getStorageSync('cookieKey')
  85. },
  86. method: 'POST',
  87. success: (response) => {
  88. uni.hideLoading()
  89. const result = response.data
  90. callback(result)
  91. },
  92. fail: (error) => {
  93. uni.hideLoading()
  94. if (error) {
  95. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 3000})
  96. }
  97. }
  98. })
  99. },
  100. getStorage:function(){
  101. // 获取本地Storage
  102. return new Promise(function(resolve,reject) {
  103. uni.getStorage({
  104. key: 'user_key',
  105. success: function (res){
  106. resolve(res.data);
  107. }
  108. })
  109. });
  110. },
  111. getStorageAddressKey:function(){
  112. // 获取本地Storage
  113. return new Promise(function(resolve,reject) {
  114. uni.getStorage({
  115. key: 'address_key',
  116. success: function (res){
  117. resolve(res.data);
  118. }
  119. })
  120. });
  121. },
  122. loginStatus:function(){
  123. // 获取用户是否登陆 1:已登陆,否则未登陆
  124. return new Promise(function(resolve,reject) {
  125. uni.getStorage({
  126. key: 'user_key',
  127. success: function (res){
  128. if(res.data.code == '1'){
  129. resolve(true);
  130. } else {
  131. resolve(false);
  132. }
  133. }
  134. })
  135. });
  136. },
  137. navToListPage:function({type,value,id,lType} = {}){
  138. // 跳转到列表页
  139. if(lType=='4'){
  140. const pages = getCurrentPages();
  141. const prevPage = pages[pages.length-2];
  142. prevPage.refresh = true;
  143. prevPage.listData = {
  144. type: type,
  145. from: value,
  146. id: id
  147. }
  148. uni.navigateBack({
  149. delta: 1
  150. })
  151. }else{
  152. uni.navigateTo({
  153. url:`/pages/goods/goods?type=${type}&from=${value}&id=${id}`
  154. })
  155. }
  156. },
  157. navigateTo:function(url){
  158. //路由跳转:页面之间路由跳转
  159. uni.navigateTo({
  160. url:url
  161. })
  162. },
  163. redirectTo:function(url){
  164. //路由跳转:关闭当前页跳转到新页面
  165. uni.redirectTo({
  166. url:url
  167. })
  168. },
  169. switchTabTo:function(url){
  170. //路由跳转:底部 tab页
  171. uni.switchTab({
  172. url:url
  173. })
  174. },
  175. isNumber:function(value){
  176. //验证是否为数字
  177. var patrn = /^(-)?\d+(\.\d+)?$/;
  178. if (patrn.exec(value) == null || value == "") {
  179. return false
  180. } else {
  181. return true
  182. }
  183. },
  184. getWindowHeight:function(){
  185. // 获取窗口高度
  186. const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
  187. return windowHeight;
  188. },
  189. adaptRichTextImg:function(res){
  190. /**
  191. *@富文本实现图片自适应
  192. *@style再添加自适应样式
  193. */
  194. const html = res.replace(/<img[^>]*>/gi,function(match,capture){
  195. let match1 = match.replace(/<img*/gi, '<img style="width:100% !important;height:auto !important;float:left !important;"'),
  196. results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"');
  197. return results;
  198. })
  199. return html;
  200. },
  201. FormatMoney:function(num){
  202. // 金额千分位
  203. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  204. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) { // 对整数部分添加分隔符
  205. return $1 + ",";
  206. });
  207. });
  208. },
  209. formatDate:function(){
  210. //获取当前时间
  211. let date = new Date();
  212. let y = date.getFullYear();
  213. let MM = date.getMonth() + 1;
  214. MM = MM < 10 ? ('0' + MM) : MM;
  215. let d = date.getDate();
  216. d = d < 10 ? ('0' + d) : d;
  217. let h = date.getHours();
  218. h = h < 10 ? ('0' + h) : h;
  219. let m = date.getMinutes();
  220. m = m < 10 ? ('0' + m) : m;
  221. let s = date.getSeconds();
  222. s = s < 10 ? ('0' + s) : s;
  223. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  224. },
  225. regexSets:function() {
  226. let sets = {
  227. 'companyName': /^[\u4e00-\u9fa5\(\)()\s\da-zA-Z&]{2,50}$/gi,
  228. 'phoneAndTelephone': /^([1]\d{10}|([\((]?0[0-9]{2,3}[)\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/,
  229. 'bankNum': /^([1-9]{1})(\d{18})$/,
  230. 'invalidChar': /^[\s\u4e00-\u9fa5a-z0-9_-]{0,}$/
  231. };
  232. return sets;
  233. },
  234. timestampToTime:function(timestamp) {
  235. // 时间戳转日期
  236. let date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  237. let Y = date.getFullYear() + '-';
  238. let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  239. let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
  240. let h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
  241. let m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
  242. let s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
  243. return `${Y}${M}${D}${h}${m}${s}`;
  244. }
  245. }
  246. /**
  247. *@导出
  248. */
  249. module.exports = {
  250. get: caimeiApi.get,
  251. post: caimeiApi.post,
  252. lodingGet: caimeiApi.lodingGet,
  253. isNumber: caimeiApi.isNumber,
  254. FormatMoney: caimeiApi.FormatMoney,
  255. navigateTo: caimeiApi.navigateTo,
  256. redirectTo: caimeiApi.redirectTo,
  257. switchTabTo: caimeiApi.switchTabTo,
  258. formatDate: caimeiApi.formatDate,
  259. loginStatus: caimeiApi.loginStatus,
  260. getStorage: caimeiApi.getStorage,
  261. navToListPage: caimeiApi.navToListPage,
  262. getWindowHeight: caimeiApi.getWindowHeight,
  263. adaptRichTextImg: caimeiApi.adaptRichTextImg,
  264. getStorageAddressKey: caimeiApi.getStorageAddressKey,
  265. regexSets: caimeiApi.regexSets,
  266. timestampToTime: caimeiApi.timestampToTime
  267. };