// app.js import ajax from './utils/request.js' const currency = require('currency.js') const { requestPath } = require('./url.js') App({ globalData: { statBarHeight: wx.getSystemInfoSync()['statusBarHeight'], title: "", loadingCount: 0, logout: false, showModal: false, innerShow: false, changeCompanyName: wx.getStorageSync('changeCompanyName') || false, uploadPath: requestPath + '/upLoadOss', apiLink: requestPath, token: wx.getStorageSync('access_token') || "", company: wx.getStorageSync('company') || {}, userInfo: wx.getStorageSync('userInfo') || {}, tabbar: { // custom: true, // color: "#000000", // selectedColor: "#00BC8A", // backgroundColor: "#ffffff", // position: "bottom", // borderStyle: "white", // list: [{ // "pagePath": "/pages/index/index", // "text": "首页", // iconfont: 'icon-shouye2', // show: true // }, // { // "pagePath": "/pages/shenhe/shenhe", // "text": "审核", // iconfont: 'icon-shenhe', // show: true // }, // { // "pagePath": "/pages/cart/cart", // "text": "购物袋", // iconfont: 'icon-gouwudai2', // show: true // }, // { // "pagePath": "/pages/user/index/index", // "text": "我的", // iconfont: 'icon-wode', // show: true // } // ] }, }, currency: currency, ajax: ajax, async onLaunch() { // 展示本地存储能力 const logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) wx.setStorageSync('logout', false); let { data } = await ajax.gets("/admin/miniConfig/tabbar"); if(data.data && data.data.content){ this.globalData.tabbar = JSON.parse(data.data.content); } //console.log("this.globalData.tabbar",this.globalData.tabbar); // return await this.autoLogin(); if (this.indeCallBack) { this.indeCallBack(); } }, autoLogin: async function () { // if (this.globalData.userInfo && this.globalData.userInfo.userId) { // return // } // 登录 let res = await wx.login(); let userData = await ajax.post({ url: '/auth/mobile/token/social?grant_type=mobile&mobile=WX@' + res.code, method: "post", header: { isToken: false, TENANT_ID: "1", Authorization: "Basic bWluaTptaW5p" } }) // wx.clearStorageSync(); wx.setStorageSync('access_token', userData.data.access_token); wx.setStorageSync('refresh_token', userData.data.refresh_token); wx.setStorageSync('expires_in', userData.data.expires_in); wx.setStorageSync('isLock', false); wx.setStorageSync('lockPasswd', ''); await this.getUserInfo(); }, getUserInfo: async function (that, isLoading) { let user = await ajax.get({ url: "/admin/user/info", data: {}, // isLoading: isLoading != undefined ? isLoading : true }); this.globalData.userInfo = user.data.data; //console.log(this.globalData.userInfo) wx.setStorageSync('userInfo', user.data.data.sysUser); wx.setStorageSync('userRole', user.data.data.roles); wx.setStorageSync('roleList', user.data.data.roleList); wx.setStorageSync('companyId', user.data.data.sysUser.companyId); setTimeout(() => {}, 500) let company = await ajax.get({ url: `/admin/bxdClientCompany/${user.data.data.sysUser.companyId}`, data: {}, // isLoading: isLoading != undefined ? isLoading : true }); // this.globalData.tabbar.list[0].show = false; this.globalData.company = company.data.data; wx.setStorageSync('company', company.data.data) wx.setStorageSync('isStraumann', company.data.data.corporationCode == 'straumann'); let role = user.data.data.roleList.find(v => v.roleCode == 'SZMAdmin'); if (role) { if (!wx.getStorageSync('virtualInventoryId')) { wx.setStorageSync('virtualInventoryId', 0); } if (!wx.getStorageSync('companyName')) { wx.setStorageSync('companyName', ""); } if (!wx.getStorageSync('companyName') || !wx.getStorageSync('virtualInventoryId')) { wx.setStorageSync('showChangeCompanyName', true); this.globalData.changeCompanyName = true; } } else { wx.setStorageSync('virtualInventoryId', user.data.data.sysUser.virtualInventoryId || 0); wx.setStorageSync('companyName', user.data.data.sysUser.companyName); } if (that) { that.setData({ userInfo: user.data.data.sysUser, company: company.data.data }) return } // wx.switchTab({ // url: '/pages/index/index', // }) }, getApiAsync: function (name, data, method, topage) { wx.showLoading({ mask: true }); this.globalData.loadingCount++; let that = this; return that.getApiAsyncPromise(that, name, data, method, topage); }, getApiAsyncPromise: function (that, name, data, method, topage) { return new Promise((resolve, reject) => { try { if (data) { let dd = JSON.stringify(data); dd = dd.replace(/undefined/gi, '', '""'); dd = dd.replace(/'undefined'/gi, '', '""'); dd = dd.replace(/"undefined"/gi, '', '""'); dd = dd.replace(/null/gi, '', '""'); dd = dd.replace(/'null'/gi, '', '""'); dd = dd.replace(/"null"/gi, '', '""'); data = JSON.parse(dd); } } catch (cat) { } wx.request({ url: that.globalData.apiLink + name, data: method ? JSON.stringify(data) : data, method: "POST", header: { 'content-type': method ? 'application/json;charset=utf-8' : 'application/x-www-form-urlencoded', token: that.globalData.token }, success: function (res) { // //console.log('网络请求成功', res.data); that.globalData.loadingCount--; if (that.globalData.loadingCount === 0) { wx.hideLoading() } if (!res.data.success) { // if (failfun) { // failfun(res.data); // } else { if (res.data.message === 'Please Log In.') { that.globalData.userInfo = null; that.clearStorageSync(); that.globalData.token = ""; that.globalData.showModal = true; wx.showModal({ title: "提示", content: "暂未登录,请先登录!", confirmText: "去登录", confirmColor: "#004445", async success(res1) { if (res1.confirm) { that.globalData.innerShow = true; that.globalData.loginType = true; that.globalData.showModal = true; wx.switchTab({ url: '/pages/user/index', }); return; } else { wx.navigateBack({ data: 1 }) return; } }, complete() { that.globalData.showModal = false; } }); return; } else if (res.data.message === '登录已过期,请重新登录') { that.globalData.userInfo = null; that.clearStorageSync(); wx.showModal({ title: "提示", content: res.data.message, async success(res1) { if (res1.confirm) { that.globalData.loginType = true; wx.switchTab({ url: '/pages/user/index', }); return; } else { wx.navigateBack({ data: 1 }) return; } } }); return; } wx.showToast({ title: res.data.message ? res.data.message : '错误', mask: true, icon: 'none' }); if (res.data.needreg) { wx.redirectTo({ url: 'qustionlist' }); return false; } reject(res); // } } else { if (topage) { if (data.GH_page && data.datalistname) { if (res.data.nowpage <= 1) { topage.setData(res.data); } else { var nowdatalist = topage.data[data.datalistname]; topage.setData({ pagetotal: res.data.pagetotal, nowpage: res.data.nowpage, [data.datalistname]: nowdatalist.concat(res.data[data.datalistname]) }); } } else { topage.setData(res.data); } } resolve(res); } }, fail: function (res) { //console.log('网络请求失败', res); // wx.showToast({ // title: res.errMsg, // icon: 'none' // }); wx.hideLoading() that.globalData.loadingCount--; if (that.globalData.loadingCount === 0) { wx.hideLoading() } reject(res); } }); }); }, dialog: function (that, currentStatu) { var animation = wx.createAnimation({ duration: 200, timingFunction: "linear", delay: 0 }); that.animation = animation; animation.translateY(380).step(); that.setData({ animationData: animation.export() }) setTimeout(function () { animation.translateY(0).step() that.setData({ animationData: animation }) // 显示抽屉 if (currentStatu == "open") { that.setData({ showDialog: true }); } //关闭抽屉 if (currentStatu == "close") { that.setData({ showDialog: false }); } }.bind(that), 200) }, watch: function (variate, method) { var obj = this.globalData; let val = obj[variate]; Object.defineProperty(obj, variate, { set: function (value) { val = value; method(variate, value); }, get: function () { return val; } }) }, /** * 邮箱 * @param {*} s */ isEmail: function (s) { return /^([a-zA-Z0-9_-])+((\.)?[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s) }, /** * 手机号码 * @param {*} s */ isMobile: function (s) { return /^1[0-9]{10}$/.test(s) }, /** * 电话号码 * @param {*} s */ isPhone: function (s) { return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s) } })