| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import store from '@/store'
- import UTIL from '@/utils/util'
- import { getStaffApi, getUserApi } from '@/api/common'
- import { postWxCode,getDictionaries } from '@/api/mini'
- import { currentInfo } from '@/api/user'
- import { TOKEN_STORAGE_KEY } from '@/constant/storageKeys'
- /**
- * 拉取商城登录用户信息。
- * update=true 时强制请求 /user/current-info,并同步 shopUser 与 loginInfo。
- * 「我的」页展示的是 loginInfo.name,不写 loginInfo 的话改昵称返回后不会刷新。
- */
- export async function getShopUser(update) {
- let userInfo = store.getters.getShopUser
- if (!UTIL.isEmpty(userInfo) && !update) {
- return userInfo
- }
- await currentInfo().then((res) => {
- if (res && res.code == 1 && res.data && res.data.info) {
- userInfo = res.data.info
- store.dispatch('setShopUser', userInfo)
- const loginInfo = store.getters.getLoginInfo || {}
- store.commit('setLoginInfo', Object.assign({}, loginInfo, userInfo))
- }
- }).catch(() => {})
- 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_STORAGE_KEY)
- })
- 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
- }
- let getObj = await new Promise(resolve => {
- uni.login({
- provider: 'weixin',
- success: res => {
- uni.setStorageSync('code', res.code)
- resolve(res)
- }
- })
- })
- await postWxCode({ code: getObj.code }).then(subRes => {
- if(subRes.code && subRes.code == 1){
- uni.setStorageSync(TOKEN_STORAGE_KEY, subRes.data.token)
- uni.setStorageSync('miniOpenId', subRes.data.miniOpenId)
- if(subRes.data.user && !UTIL.isEmpty(subRes.data.user)){
- store.commit('setLoginInfo', subRes.data.user)
- }
- getDictionaries({ token: subRes.data.token }).then(res => {
- if (UTIL.isEmpty(res)) {
- return false
- }
- store.commit('setDictionariesInfo', { ...res.data.dict, ...res.data })
- })
- //启动更新逻辑 shish 2020.5.18
- if (subRes.data.update == 1) {
- const updateManager = uni.getUpdateManager();
- updateManager.onCheckForUpdate(function(res) {
- // 请求完新版本信息的回调
- if (res.hasUpdate) {
- updateManager.onUpdateReady(function(res2) {
- uni.showModal({
- title: '更新提示',
- content: '发现新版本,请更新应用',
- cancelColor:'#DDDDDD',
- confirmColor:'#FF0000',
- success(res2) {
- if (res2.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate();
- }
- }
- });
- });
- }
- });
- updateManager.onUpdateFailed(function(res) {
- // 新的版本下载失败
- uni.showModal({
- title: '提示',
- content: '检查到有新版本,但下载失败,请检查网络连接',
- success(res) {
- if (res.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate();
- }
- }
- });
- });
- }
- }
- })
- return dataInfo
- }
|