app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // app.js
  2. import ajax from './utils/request.js'
  3. const currency = require('currency.js')
  4. const {
  5. requestPath
  6. } = require('./url.js')
  7. App({
  8. globalData: {
  9. statBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
  10. title: "",
  11. loadingCount: 0,
  12. logout: false,
  13. showModal: false,
  14. innerShow: false,
  15. changeCompanyName: wx.getStorageSync('changeCompanyName') || false,
  16. uploadPath: requestPath + '/upLoadOss',
  17. apiLink: requestPath,
  18. token: wx.getStorageSync('access_token') || "",
  19. company: wx.getStorageSync('company') || {},
  20. userInfo: wx.getStorageSync('userInfo') || {},
  21. tabbar: {
  22. // custom: true,
  23. // color: "#000000",
  24. // selectedColor: "#00BC8A",
  25. // backgroundColor: "#ffffff",
  26. // position: "bottom",
  27. // borderStyle: "white",
  28. // list: [{
  29. // "pagePath": "/pages/index/index",
  30. // "text": "首页",
  31. // iconfont: 'icon-shouye2',
  32. // show: true
  33. // },
  34. // {
  35. // "pagePath": "/pages/shenhe/shenhe",
  36. // "text": "审核",
  37. // iconfont: 'icon-shenhe',
  38. // show: true
  39. // },
  40. // {
  41. // "pagePath": "/pages/cart/cart",
  42. // "text": "购物袋",
  43. // iconfont: 'icon-gouwudai2',
  44. // show: true
  45. // },
  46. // {
  47. // "pagePath": "/pages/user/index/index",
  48. // "text": "我的",
  49. // iconfont: 'icon-wode',
  50. // show: true
  51. // }
  52. // ]
  53. },
  54. },
  55. currency: currency,
  56. ajax: ajax,
  57. async onLaunch() {
  58. // 展示本地存储能力
  59. const logs = wx.getStorageSync('logs') || []
  60. logs.unshift(Date.now())
  61. wx.setStorageSync('logs', logs)
  62. wx.setStorageSync('logout', false);
  63. let {
  64. data
  65. } = await ajax.gets("/admin/miniConfig/tabbar");
  66. if(data.data && data.data.content){
  67. this.globalData.tabbar = JSON.parse(data.data.content);
  68. }
  69. //console.log("this.globalData.tabbar",this.globalData.tabbar);
  70. // return
  71. await this.autoLogin();
  72. if (this.indeCallBack) {
  73. this.indeCallBack();
  74. }
  75. },
  76. autoLogin: async function () {
  77. // if (this.globalData.userInfo && this.globalData.userInfo.userId) {
  78. // return
  79. // }
  80. // 登录
  81. let res = await wx.login();
  82. let userData = await ajax.post({
  83. url: '/auth/mobile/token/social?grant_type=mobile&mobile=WX@' + res.code,
  84. method: "post",
  85. header: {
  86. isToken: false,
  87. TENANT_ID: "1",
  88. Authorization: "Basic bWluaTptaW5p"
  89. }
  90. })
  91. // wx.clearStorageSync();
  92. wx.setStorageSync('access_token', userData.data.access_token);
  93. wx.setStorageSync('refresh_token', userData.data.refresh_token);
  94. wx.setStorageSync('expires_in', userData.data.expires_in);
  95. wx.setStorageSync('isLock', false);
  96. wx.setStorageSync('lockPasswd', '');
  97. await this.getUserInfo();
  98. },
  99. getUserInfo: async function (that, isLoading) {
  100. let user = await ajax.get({
  101. url: "/admin/user/info",
  102. data: {},
  103. // isLoading: isLoading != undefined ? isLoading : true
  104. });
  105. this.globalData.userInfo = user.data.data;
  106. //console.log(this.globalData.userInfo)
  107. wx.setStorageSync('userInfo', user.data.data.sysUser);
  108. wx.setStorageSync('userRole', user.data.data.roles);
  109. wx.setStorageSync('roleList', user.data.data.roleList);
  110. wx.setStorageSync('companyId', user.data.data.sysUser.companyId);
  111. setTimeout(() => {}, 500)
  112. let company = await ajax.get({
  113. url: `/admin/bxdClientCompany/${user.data.data.sysUser.companyId}`,
  114. data: {},
  115. // isLoading: isLoading != undefined ? isLoading : true
  116. });
  117. // this.globalData.tabbar.list[0].show = false;
  118. this.globalData.company = company.data.data;
  119. wx.setStorageSync('company', company.data.data)
  120. wx.setStorageSync('isStraumann', company.data.data.corporationCode == 'straumann');
  121. let role = user.data.data.roleList.find(v => v.roleCode == 'SZMAdmin');
  122. if (role) {
  123. if (!wx.getStorageSync('virtualInventoryId')) {
  124. wx.setStorageSync('virtualInventoryId', 0);
  125. }
  126. if (!wx.getStorageSync('companyName')) {
  127. wx.setStorageSync('companyName', "");
  128. }
  129. if (!wx.getStorageSync('companyName') || !wx.getStorageSync('virtualInventoryId')) {
  130. wx.setStorageSync('showChangeCompanyName', true);
  131. this.globalData.changeCompanyName = true;
  132. }
  133. } else {
  134. wx.setStorageSync('virtualInventoryId', user.data.data.sysUser.virtualInventoryId || 0);
  135. wx.setStorageSync('companyName', user.data.data.sysUser.companyName);
  136. }
  137. if (that) {
  138. that.setData({
  139. userInfo: user.data.data.sysUser,
  140. company: company.data.data
  141. })
  142. return
  143. }
  144. // wx.switchTab({
  145. // url: '/pages/index/index',
  146. // })
  147. },
  148. getApiAsync: function (name, data, method, topage) {
  149. wx.showLoading({
  150. mask: true
  151. });
  152. this.globalData.loadingCount++;
  153. let that = this;
  154. return that.getApiAsyncPromise(that, name, data, method, topage);
  155. },
  156. getApiAsyncPromise: function (that, name, data, method, topage) {
  157. return new Promise((resolve, reject) => {
  158. try {
  159. if (data) {
  160. let dd = JSON.stringify(data);
  161. dd = dd.replace(/undefined/gi, '', '""');
  162. dd = dd.replace(/'undefined'/gi, '', '""');
  163. dd = dd.replace(/"undefined"/gi, '', '""');
  164. dd = dd.replace(/null/gi, '', '""');
  165. dd = dd.replace(/'null'/gi, '', '""');
  166. dd = dd.replace(/"null"/gi, '', '""');
  167. data = JSON.parse(dd);
  168. }
  169. } catch (cat) {
  170. }
  171. wx.request({
  172. url: that.globalData.apiLink + name,
  173. data: method ? JSON.stringify(data) : data,
  174. method: "POST",
  175. header: {
  176. 'content-type': method ? 'application/json;charset=utf-8' : 'application/x-www-form-urlencoded',
  177. token: that.globalData.token
  178. },
  179. success: function (res) {
  180. // //console.log('网络请求成功', res.data);
  181. that.globalData.loadingCount--;
  182. if (that.globalData.loadingCount === 0) {
  183. wx.hideLoading()
  184. }
  185. if (!res.data.success) {
  186. // if (failfun) {
  187. // failfun(res.data);
  188. // } else {
  189. if (res.data.message === 'Please Log In.') {
  190. that.globalData.userInfo = null;
  191. that.clearStorageSync();
  192. that.globalData.token = "";
  193. that.globalData.showModal = true;
  194. wx.showModal({
  195. title: "提示",
  196. content: "暂未登录,请先登录!",
  197. confirmText: "去登录",
  198. confirmColor: "#004445",
  199. async success(res1) {
  200. if (res1.confirm) {
  201. that.globalData.innerShow = true;
  202. that.globalData.loginType = true;
  203. that.globalData.showModal = true;
  204. wx.switchTab({
  205. url: '/pages/user/index',
  206. });
  207. return;
  208. } else {
  209. wx.navigateBack({
  210. data: 1
  211. })
  212. return;
  213. }
  214. },
  215. complete() {
  216. that.globalData.showModal = false;
  217. }
  218. });
  219. return;
  220. } else if (res.data.message === '登录已过期,请重新登录') {
  221. that.globalData.userInfo = null;
  222. that.clearStorageSync();
  223. wx.showModal({
  224. title: "提示",
  225. content: res.data.message,
  226. async success(res1) {
  227. if (res1.confirm) {
  228. that.globalData.loginType = true;
  229. wx.switchTab({
  230. url: '/pages/user/index',
  231. });
  232. return;
  233. } else {
  234. wx.navigateBack({
  235. data: 1
  236. })
  237. return;
  238. }
  239. }
  240. });
  241. return;
  242. }
  243. wx.showToast({
  244. title: res.data.message ? res.data.message : '错误',
  245. mask: true,
  246. icon: 'none'
  247. });
  248. if (res.data.needreg) {
  249. wx.redirectTo({
  250. url: 'qustionlist'
  251. });
  252. return false;
  253. }
  254. reject(res);
  255. // }
  256. } else {
  257. if (topage) {
  258. if (data.GH_page && data.datalistname) {
  259. if (res.data.nowpage <= 1) {
  260. topage.setData(res.data);
  261. } else {
  262. var nowdatalist = topage.data[data.datalistname];
  263. topage.setData({
  264. pagetotal: res.data.pagetotal,
  265. nowpage: res.data.nowpage,
  266. [data.datalistname]: nowdatalist.concat(res.data[data.datalistname])
  267. });
  268. }
  269. } else {
  270. topage.setData(res.data);
  271. }
  272. }
  273. resolve(res);
  274. }
  275. },
  276. fail: function (res) {
  277. //console.log('网络请求失败', res);
  278. // wx.showToast({
  279. // title: res.errMsg,
  280. // icon: 'none'
  281. // });
  282. wx.hideLoading()
  283. that.globalData.loadingCount--;
  284. if (that.globalData.loadingCount === 0) {
  285. wx.hideLoading()
  286. }
  287. reject(res);
  288. }
  289. });
  290. });
  291. },
  292. dialog: function (that, currentStatu) {
  293. var animation = wx.createAnimation({
  294. duration: 200,
  295. timingFunction: "linear",
  296. delay: 0
  297. });
  298. that.animation = animation;
  299. animation.translateY(380).step();
  300. that.setData({
  301. animationData: animation.export()
  302. })
  303. setTimeout(function () {
  304. animation.translateY(0).step()
  305. that.setData({
  306. animationData: animation
  307. })
  308. // 显示抽屉
  309. if (currentStatu == "open") {
  310. that.setData({
  311. showDialog: true
  312. });
  313. }
  314. //关闭抽屉
  315. if (currentStatu == "close") {
  316. that.setData({
  317. showDialog: false
  318. });
  319. }
  320. }.bind(that), 200)
  321. },
  322. watch: function (variate, method) {
  323. var obj = this.globalData;
  324. let val = obj[variate];
  325. Object.defineProperty(obj, variate, {
  326. set: function (value) {
  327. val = value;
  328. method(variate, value);
  329. },
  330. get: function () {
  331. return val;
  332. }
  333. })
  334. },
  335. /**
  336. * 邮箱
  337. * @param {*} s
  338. */
  339. isEmail: function (s) {
  340. return /^([a-zA-Z0-9_-])+((\.)?[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
  341. },
  342. /**
  343. * 手机号码
  344. * @param {*} s
  345. */
  346. isMobile: function (s) {
  347. return /^1[0-9]{10}$/.test(s)
  348. },
  349. /**
  350. * 电话号码
  351. * @param {*} s
  352. */
  353. isPhone: function (s) {
  354. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
  355. }
  356. })