util.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //公共js,主要做表单验证,以及基本方法封装
  2. const utils = {
  3. isNullOrEmpty: function (value) {
  4. //是否为空
  5. return (value === null || value === '' || value === undefined) ? true : false;
  6. },
  7. trim: function (value) {
  8. //去空格
  9. return value.replace(/(^\s*)|(\s*$)/g, "");
  10. },
  11. isMobile: function (value) {
  12. //是否为手机号
  13. return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
  14. },
  15. isFloat: function (value) {
  16. //金额,只允许保留两位小数
  17. return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
  18. },
  19. isNum: function (value) {
  20. //是否全为数字
  21. return /^[0-9]+$/.test(value);
  22. },
  23. formatNum: function (num) {
  24. //格式化手机号码
  25. if (utils.isMobile(num)) {
  26. num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
  27. }
  28. return num;
  29. },
  30. interfaceUrl: function () {
  31. //接口地址
  32. // return "https://mgvmapi.mengguostore.com/";
  33. return "http://127.0.0.1:8111/";
  34. },
  35. toast: function (text, duration, success) {
  36. wx.showToast({
  37. title: text,
  38. icon: success ? 'success' : 'none',
  39. duration: duration || 2000
  40. })
  41. },
  42. preventMultiple: function (fn, gapTime) {
  43. if (gapTime == null || gapTime == undefined) {
  44. gapTime = 200;
  45. }
  46. let lastTime = null;
  47. return function () {
  48. let now = +new Date();
  49. if (!lastTime || now - lastTime > gapTime) {
  50. fn.apply(this, arguments);
  51. lastTime = now;
  52. }
  53. }
  54. },
  55. request: function (url, postData, method, type, hideLoading,token) {
  56. //接口请求
  57. if (!hideLoading) {
  58. wx.showLoading({
  59. title: '请稍候...',
  60. mask: true
  61. })
  62. }
  63. return new Promise((resolve, reject) => {
  64. wx.request({
  65. url: this.interfaceUrl() + url,
  66. data: postData,
  67. header: {
  68. 'content-type': type ? 'application/x-www-form-urlencoded' : 'application/json',
  69. 'X-Access-Token': token ? token : ''
  70. },
  71. method: method, //'GET','POST'
  72. dataType: 'json',
  73. success: (res) => {
  74. !hideLoading && wx.hideLoading()
  75. if(res.statusCode && res.statusCode == 500){
  76. wx.reLaunch({
  77. url: '/pages/login/index',
  78. })
  79. }
  80. resolve(res.data)
  81. },
  82. fail: (res) => {
  83. !hideLoading && this.toast("网络不给力,请稍后再试~")
  84. //wx.hideLoading()
  85. reject(res)
  86. }
  87. })
  88. })
  89. },
  90. uploadFile: function (src) {
  91. const that = this
  92. wx.showLoading({
  93. title: '请稍候...',
  94. mask: true
  95. })
  96. return new Promise((resolve, reject) => {
  97. const uploadTask = wx.uploadFile({
  98. url: 'http://39.108.124.252:8081/fileServce/file/ ', //测试地址,暂不使用
  99. filePath: src,
  100. name: 'file',
  101. header: {
  102. 'content-type': 'multipart/form-data'
  103. },
  104. formData: {},
  105. success: function (res) {
  106. wx.hideLoading()
  107. let d = JSON.parse(res.data)
  108. if (d.code === 1) {
  109. let fileObj = JSON.parse(d.data)[0];
  110. //文件上传成功后把图片路径数据提交到服务器,数据提交成功后,再进行下张图片的上传
  111. resolve(fileObj)
  112. } else {
  113. that.toast(res.message);
  114. }
  115. },
  116. fail: function (res) {
  117. reject(res)
  118. wx.hideLoading();
  119. that.toast(res.message);
  120. }
  121. })
  122. })
  123. },
  124. generateClientID: function () {
  125. var sessions = [];
  126. var clientID = '';
  127. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  128. var numPossible = possible.length;
  129. do {
  130. for (var i = 0; i < 8; i++) {
  131. clientID += possible.charAt((Math.random() * numPossible) | 0);
  132. }
  133. } while (sessions['clientID'] != null);
  134. return clientID;
  135. },
  136. //timestamp 时间戳
  137. //option 格式(年月日 就输入YY-MM-DD 时分 就输入 hh-mm)
  138. //功能如下注释 ↓↓↓↓
  139. formatDate(timestamp, option) {
  140. var datatime= timestamp.replace(/\-/g, "/")
  141. var date = new Date(datatime);
  142. var year = date.getFullYear()
  143. var month = date.getMonth() + 1
  144. var day = date.getDate()
  145. var hour = function () {
  146. return date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  147. }
  148. var minute = function () {
  149. return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  150. }
  151. var second = function () {
  152. return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  153. }
  154. //获取 年月日
  155. if (option == 'yyyy-MM-dd') return " " + year + "-" + month + "-" + day;
  156. //获取年月
  157. if (option == 'yyyy-MM') return " " + year + "-" + month;
  158. //获取年
  159. if (option == 'yyyy') return " " + year;
  160. //获取月
  161. if (option == 'MM') return " " + month;
  162. //获取日
  163. if (option == 'dd') return " " + day;
  164. //获取昨天
  165. if (option == 'yesterday') return " " + day - 1;
  166. //获取时分秒
  167. if (option == 'hh:mm:ss') return " " + hour() + ":" + minute() + ":" + second();
  168. //获取时分
  169. if (option == 'hh:mm') return " " + hour() + ":" + minute();
  170. //获取分秒
  171. if (option == 'mm:ss') return minute() + ":" + second();
  172. //获取分
  173. if (option == 'mm') return minute();
  174. //获取秒
  175. if (option == 'ss') return second();
  176. //默认时分秒年月日
  177. return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ":" + second();
  178. },
  179. api:{
  180. deviceListPage: 'api/device/deviceListPage',
  181. storeListPage: 'mgvm/mgVmStoreApi/storeListPage',
  182. queryByDeviceId: 'api/device/queryByDeviceId',
  183. storeList: 'mgvm/mgVmStoreApi/storeList',
  184. addDevice: 'api/device/addDevice',
  185. vmProductListPage: '/mgvm/mgVmProductApi/vmProductListPage',
  186. MgVmProductSkuPage:'/mgvm/mgVmProductSkuApi/list' ,
  187. productListPage: 'mgvm/mgSysProduct/sysProductListPage',
  188. queryByProductId: 'mgvm/mgSysProduct/queryByProductId',
  189. queryByVmProductId: 'mgvm/mgVmProductApi/queryByVmProductId',
  190. addVmProduct: 'mgvm/mgVmProductApi/addVmProduct',
  191. editVmProduct: 'mgvm/mgVmProductApi/editVmProduct',
  192. schemeListPage:'mgvm/mgVmSchemeApi/schemeListPage',
  193. schemeList:'mgvm/mgVmSchemeApi/schemeList',
  194. queryBySchemeId:'mgvm/mgVmSchemeApi/queryBySchemeId',
  195. addScheme:'mgvm/mgVmSchemeApi/addScheme',
  196. editScheme:'mgvm/mgVmSchemeApi/editScheme',
  197. orderListPage: 'mgvm/mgVmOrder/orderListPage',
  198. repairsListPage: 'mgvm/mgVmRepairs/repairsListPage',
  199. addRepairs: 'mgvm/mgVmRepairs/addRepairs',
  200. queryByStoreId: 'mgvm/mgVmStoreApi/queryByStoreId',
  201. addStore: 'mgvm/mgVmStoreApi/addStore',
  202. editStore: 'mgvm/mgVmStoreApi/editStore',
  203. stockDeviceList: 'api/device/stockDeviceList',
  204. upDevice:'api/device/upDeviceByStoreIdAnddevnceSn',
  205. productStockList: 'mgvm/mgVmStoreProductSkuApi/productStockList',
  206. editVmProductStock: 'mgvm/mgVmStoreProductSkuApi/editVmProductStock',
  207. addMerchantOrder: 'mgvm/mgSysProductOrder/addMerchantOrder',
  208. deviceListByStoreId: 'api/device/deviceListByStoreId',
  209. productOrderListPage: 'mgvm/mgSysProductOrder/productOrderListPage',
  210. queryByRepairsId: 'mgvm/mgVmRepairs/queryByRepairsId',
  211. storeList: 'mgvm/mgVmStoreApi/storeList',
  212. merchantLogin:'mgvm/miniLogin/merchantLogin',
  213. miniAppLogin:'mgvm/miniLogin/miniAppLogin',
  214. stockWarning: 'api/device/stockWarning',
  215. editMgVmDeviceGridProduct: 'api/device/editMgVmDeviceGridProduct',
  216. storeInfo: 'mgvm/mgVmStoreApi/storeInfo',
  217. getDeviceNum: 'api/device/getDeviceNum',
  218. getStockDeviceNum: 'api/device/getStockDeviceNum',
  219. changePassword: 'mgvm/info/changePassword',
  220. updateBankInfo:'mgvm/merchant/updateBankInfo',
  221. updateInfo:'mgvm/merchant/updateInfo',
  222. roleListPage: 'mgvm/info/roleListPage',
  223. addRole: 'mgvm/info/addRole',
  224. queryByUserId: 'mgvm/info/queryByUserId',
  225. editRole: 'mgvm/info/editRole',
  226. productOrderDetailPage:'mgvm/mgSysProductOrder/productOrderDetailPage',
  227. logout: 'mgvm/merchant/logout',
  228. withdrawMoney:'mgvm/withdrawMoneyApi/withdrawMoney',
  229. deleteMgVmProductById:'mgvm/mgVmProductApi/deleteMgVmProductById',
  230. deleteSchemeById:'mgvm/mgVmSchemeApi/deleteSchemeById',
  231. mgVmBillList:'mgvm/mgVmBill/list',
  232. unbundling:'api/device/unbundling',
  233. withdrawList:'mgvm/mgVmBill/withdrawList',
  234. indexSalesOrderApp:'mgvm/orderStatisApi/indexSalesOrderApp',
  235. salesOrderApp:'mgvm/orderStatisApi/salesOrderApp',
  236. storeDelete:'/mgvm/mgVmStoreApi/delete',
  237. agentList:'/mgvm/mgSysAgentApi/list',
  238. agentAdd:'/mgvm/mgSysAgentApi/add',
  239. agentEdit:'/mgvm/mgSysAgentApi/edit',
  240. agentDelete:'/mgvm/mgSysAgentApi/delete',
  241. agentQueryById:'/mgvm/mgSysAgentApi/queryById',
  242. },
  243. staticDomain:'https://mgvm.oss-cn-beijing.aliyuncs.com/',
  244. }
  245. module.exports = {
  246. isNullOrEmpty: utils.isNullOrEmpty,
  247. trim: utils.trim,
  248. isMobile: utils.isMobile,
  249. isFloat: utils.isFloat,
  250. isNum: utils.isNum,
  251. interfaceUrl: utils.interfaceUrl,
  252. toast: utils.toast,
  253. request: utils.request,
  254. uploadFile: utils.uploadFile,
  255. formatNum: utils.formatNum,
  256. generateClientID: utils.generateClientID,
  257. formatDate: utils.formatDate,
  258. api:utils.api,
  259. staticDomain: utils.staticDomain
  260. }