util.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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://automatapi.mengguohotel.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) || (res.statusCode && res.statusCode == 401)){
  76. wx.reLaunch({
  77. url: '/pages/login/index',
  78. })
  79. }
  80. // if(res.statusCode && res.statusCode == 200 && res.data.message.indexOf('未获取到用户')>-1){
  81. // wx.reLaunch({
  82. // url: '/pages/login/index',
  83. // })
  84. // }
  85. resolve(res.data)
  86. },
  87. fail: (res) => {
  88. !hideLoading && this.toast("网络不给力,请稍后再试~")
  89. //wx.hideLoading()
  90. reject(res)
  91. }
  92. })
  93. })
  94. },
  95. uploadFile: function (src) {
  96. const that = this
  97. wx.showLoading({
  98. title: '请稍候...',
  99. mask: true
  100. })
  101. return new Promise((resolve, reject) => {
  102. const uploadTask = wx.uploadFile({
  103. url: 'http://39.108.124.252:8081/fileServce/file/ ', //测试地址,暂不使用
  104. filePath: src,
  105. name: 'file',
  106. header: {
  107. 'content-type': 'multipart/form-data'
  108. },
  109. formData: {},
  110. success: function (res) {
  111. wx.hideLoading()
  112. let d = JSON.parse(res.data)
  113. if (d.code === 1) {
  114. let fileObj = JSON.parse(d.data)[0];
  115. //文件上传成功后把图片路径数据提交到服务器,数据提交成功后,再进行下张图片的上传
  116. resolve(fileObj)
  117. } else {
  118. that.toast(res.message);
  119. }
  120. },
  121. fail: function (res) {
  122. reject(res)
  123. wx.hideLoading();
  124. that.toast(res.message);
  125. }
  126. })
  127. })
  128. },
  129. generateClientID: function () {
  130. var sessions = [];
  131. var clientID = '';
  132. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  133. var numPossible = possible.length;
  134. do {
  135. for (var i = 0; i < 8; i++) {
  136. clientID += possible.charAt((Math.random() * numPossible) | 0);
  137. }
  138. } while (sessions['clientID'] != null);
  139. return clientID;
  140. },
  141. //timestamp 时间戳
  142. //option 格式(年月日 就输入YY-MM-DD 时分 就输入 hh-mm)
  143. //功能如下注释 ↓↓↓↓
  144. formatDate(timestamp, option) {
  145. var datatime= timestamp.replace(/\-/g, "/")
  146. var date = new Date(datatime);
  147. var year = date.getFullYear()
  148. var month = date.getMonth() + 1
  149. var day = date.getDate()
  150. var hour = function () {
  151. return date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  152. }
  153. var minute = function () {
  154. return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  155. }
  156. var second = function () {
  157. return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  158. }
  159. //获取 年月日
  160. if (option == 'yyyy-MM-dd') return " " + year + "-" + month + "-" + day;
  161. //获取年月
  162. if (option == 'yyyy-MM') return " " + year + "-" + month;
  163. //获取年
  164. if (option == 'yyyy') return " " + year;
  165. //获取月
  166. if (option == 'MM') return " " + month;
  167. //获取日
  168. if (option == 'dd') return " " + day;
  169. //获取昨天
  170. if (option == 'yesterday') return " " + day - 1;
  171. //获取时分秒
  172. if (option == 'hh:mm:ss') return " " + hour() + ":" + minute() + ":" + second();
  173. //获取时分
  174. if (option == 'hh:mm') return " " + hour() + ":" + minute();
  175. //获取分秒
  176. if (option == 'mm:ss') return minute() + ":" + second();
  177. //获取分
  178. if (option == 'mm') return minute();
  179. //获取秒
  180. if (option == 'ss') return second();
  181. //默认时分秒年月日
  182. return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ":" + second();
  183. },
  184. api:{
  185. deviceListPage: 'api/device/deviceListPage',
  186. storeListPage: 'mgvm/mgVmStoreApi/storeListPage',
  187. queryByDeviceId: 'api/device/queryByDeviceId',
  188. storeList: 'mgvm/mgVmStoreApi/storeList',
  189. addDevice: 'api/device/addDevice',
  190. vmProductListPage: '/mgvm/mgVmProductApi/vmProductListPage',
  191. MgVmProductSkuPage:'/mgvm/mgVmProductSkuApi/list' ,
  192. productListPage: 'mgvm/mgSysProduct/sysProductListPage',
  193. queryByProductId: 'mgvm/mgSysProduct/queryByProductId',
  194. queryByVmProductId: 'mgvm/mgVmProductApi/queryByVmProductId',
  195. addVmProduct: 'mgvm/mgVmProductApi/addVmProduct',
  196. editVmProduct: 'mgvm/mgVmProductApi/editVmProduct',
  197. schemeListPage:'mgvm/mgVmSchemeApi/schemeListPage',
  198. schemeList:'mgvm/mgVmSchemeApi/schemeList',
  199. queryBySchemeId:'mgvm/mgVmSchemeApi/queryBySchemeId',
  200. addScheme:'mgvm/mgVmSchemeApi/addScheme',
  201. editScheme:'mgvm/mgVmSchemeApi/editScheme',
  202. orderListPage: 'mgvm/mgVmOrder/orderListPage',
  203. repairsListPage: 'mgvm/mgVmRepairs/repairsListPage',
  204. addRepairs: 'mgvm/mgVmRepairs/addRepairs',
  205. queryByStoreId: 'mgvm/mgVmStoreApi/queryByStoreId',
  206. addStore: 'mgvm/mgVmStoreApi/addStore',
  207. editStore: 'mgvm/mgVmStoreApi/editStore',
  208. stockDeviceList: 'api/device/stockDeviceList',
  209. upDevice:'api/device/upDeviceByStoreIdAnddevnceSn',
  210. productStockList: 'api/mgVmStoreProductSku/productStockList',
  211. editVmProductStock: 'api/mgVmStoreProductSku/editVmProductStock',
  212. addMerchantOrder: 'mgvm/mgSysProductOrder/addMerchantOrder',
  213. deviceListByStoreId: 'api/device/deviceListByStoreId',
  214. productOrderListPage: 'mgvm/mgSysProductOrder/productOrderListPage',
  215. queryByRepairsId: 'mgvm/mgVmRepairs/queryByRepairsId',
  216. storeList: 'mgvm/mgVmStoreApi/storeList',
  217. merchantLogin:'mgvm/miniLogin/merchantLogin',
  218. miniAppLogin:'mgvm/miniLogin/miniAppLogin',
  219. stockWarning: 'api/device/stockWarning',
  220. editMgVmDeviceGridProduct: 'api/device/editMgVmDeviceGridProduct',
  221. editMgVmDeviceGridProductQc: 'api/device/editMgVmDeviceGridProductQc',
  222. storeInfo: 'mgvm/mgVmStoreApi/storeInfo',
  223. getDeviceNum: 'api/device/getDeviceNum',
  224. getStockDeviceNum: 'api/device/getStockDeviceNum',
  225. changePassword: 'mgvm/info/changePassword',
  226. updateBankInfo:'mgvm/merchant/updateBankInfo',
  227. updateInfo:'mgvm/merchant/updateInfo',
  228. roleListPage: 'mgvm/info/roleListPage',
  229. addRole: 'mgvm/info/addRole',
  230. queryByUserId: 'mgvm/info/queryByUserId',
  231. editRole: 'mgvm/info/editRole',
  232. productOrderDetailPage:'mgvm/mgSysProductOrder/productOrderDetailPage',
  233. logout: 'mgvm/merchant/logout',
  234. withdrawMoney:'mgvm/withdrawMoneyApi/withdrawMoney',
  235. deleteMgVmProductById:'mgvm/mgVmProductApi/deleteMgVmProductById',
  236. deleteSchemeById:'mgvm/mgVmSchemeApi/deleteSchemeById',
  237. mgVmBillList:'mgvm/mgVmBill/list',
  238. unbundling:'api/device/unbundling',
  239. withdrawList:'mgvm/mgVmBill/withdrawList',
  240. indexSalesOrderApp:'mgvm/orderStatisApi/indexSalesOrderApp',
  241. salesOrderApp:'mgvm/orderStatisApi/salesOrderApp',
  242. storeDelete:'/mgvm/mgVmStoreApi/delete',
  243. agentList:'/mgvm/mgSysAgentApi/list',
  244. allAgentList:'/mgvm/mgSysAgentApi/agentList',
  245. agentAdd:'/mgvm/mgSysAgentApi/add',
  246. agentEdit:'/mgvm/mgSysAgentApi/edit',
  247. agentDelete:'/mgvm/mgSysAgentApi/delete',
  248. agentQueryById:'/mgvm/mgSysAgentApi/queryById',
  249. orderGoods: 'mgvm/mgVmProductSkuApi/orderGoods',
  250. reserve: 'mgvm/mgVmProductSkuApi/reserve',
  251. deviceTypeList:'/api/mgVmDeviceType/list',
  252. getDictByCode:'/api/sysDict/getDictByCode',
  253. replenishment: 'api/device/replenishment',
  254. mgVmStoreProductSkuList:'/api/mgVmStoreProductSku/list',
  255. childListByMtLei:'/sys/category/childListByMtLei',
  256. queryDetailListByOrderSn:'/mgvm/mgVmOrder/queryDetailListByOrderSn'
  257. },
  258. staticDomain:'https://mgvm.oss-cn-beijing.aliyuncs.com/',
  259. }
  260. module.exports = {
  261. isNullOrEmpty: utils.isNullOrEmpty,
  262. trim: utils.trim,
  263. isMobile: utils.isMobile,
  264. isFloat: utils.isFloat,
  265. isNum: utils.isNum,
  266. interfaceUrl: utils.interfaceUrl,
  267. toast: utils.toast,
  268. request: utils.request,
  269. uploadFile: utils.uploadFile,
  270. formatNum: utils.formatNum,
  271. generateClientID: utils.generateClientID,
  272. formatDate: utils.formatDate,
  273. api:utils.api,
  274. staticDomain: utils.staticDomain
  275. }