import store from '@/store' // #ifdef H5 // import jweixin from 'jweixin-module' // #endif // import { share } from '@/mixins' import CONSTANT from '@/constant' // import util from '@/utils/util' import { getStaffApi, getUserApi, getShopUserApi // checkLogin, } from '@/api/common' import { postWxCode,getDictionaries } from '@/api/mini' import util from './util' /** * 获取商城端用户信息 * @parmas update为true时会重新触发请求,重置vuex的数据 */ export async function getShopUser(update) { let userInfo = store.getters.getShopUser if (!util.isEmpty(userInfo) && !update) { return userInfo } await getShopUserApi().then((res) => { userInfo = res.data store.dispatch('setShopUser', userInfo) }).catch(() => { uni.removeStorageSync('token') }) return userInfo } /** * 获取后台商户用户信息 * @parmas update为true时会重新触发请求,重置vuex的数据 */ export async function getUser(update) { let userInfo = store.getters.getUser if (!util.isEmpty(userInfo) && !update) { return userInfo } await getUserApi().then((res) => { userInfo = res.data store.dispatch('setUser', userInfo) }).catch(() => { uni.removeStorageSync('token') }) return userInfo } /** * 获取登录员工的基本信息 * @parmas update为true时会重新触发请求,重置vuex的数据 */ export async function getSelfInfo(update) { let userInfo = store.getters.getSelfUser if (!util.isEmpty(userInfo) && !update) { return userInfo } await getStaffApi().then(res => { userInfo = res.data store.dispatch('setSelfInfo', userInfo) }).catch(() => { return false }) return userInfo } /** * 微信小程序登录 * @parmas update为true时会重新触发请求,重置vuex的数据 */ export async function wxLoginFn(update) { let dataInfo = null let loginInfo = store.state.login.loginInfo if (!util.isEmpty(loginInfo) && !update) { return loginInfo } return new Promise((resolve,reject) => { uni.login({ provider: 'weixin', success: res => { console.log('res',res) uni.setStorageSync('code', res.code) postWxCode({ code: res.code }).then(subRes => { if (util.isEmpty(subRes)) { return false } var pages = getCurrentPages(); var page = (pages[pages.length - 1]).route; resolve({ redirectPage: page, ...subRes }) uni.setStorageSync('token', subRes.data.token) uni.setStorageSync('shopId', subRes.data.admin.currentShopId) getDictionaries({ token: subRes.data.token }).then(res => { if (util.isEmpty(res)) { return false } store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data }) store.commit('setMyShopInfo', { ...res.data.shop, ...res.data }) }) store.commit('setLoginInfo', { ...subRes.data.admin, ...subRes.data }) //启动更新逻辑 shish 2020.5.18 if (subRes.data.update == 1) { const updateManager = wx.getUpdateManager() console.log('请求完新版本信息的回调') updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 console.log(res.hasUpdate) }) updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success(res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新版本下载失败 }) } }) }, fail:err=>{ reject(err) } }) }) } // export default { // getSelfInfo, // getUser, // resetAllUser, // getRoleReal // }