wzj-boot/mini-app/src/appManager.js

113 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-01-12 18:04:14 +08:00
import store from '@/store'
import eventHub from './common/eventHub'
2021-01-18 16:10:59 +08:00
import userApis from './apis/userApis'
2021-01-12 18:04:14 +08:00
2021-01-15 12:00:20 +08:00
class AppManager {
2021-01-18 16:10:59 +08:00
login(callBack) {
let self = this
wx.showLoading({ title: '正在连接...', mask: true })
wx.login({
async success(res) {
let req1 = await userApis.getSession(res.code)
console.log(req1)
if (!req1.data.openid) {
self.showToast('登录失败!' + res.errMsg)
wx.hideLoading()
}
self.saveOpenid(req1.data.openid)
let req2 = await userApis.loginByMini({ openid: self.getOpenid() })
if (!req1.data.openid) {
self.showToast('登录失败!' + res.errMsg)
wx.hideLoading()
}
if (req2.token) {
store.dispatch('setTokenAction', req2.token)
self.setCacheInfo()
self.setRemoteUserInfo(req2.userInfo)
2021-01-18 16:10:59 +08:00
}
2021-01-18 16:10:59 +08:00
wx.hideLoading()
if (callBack) {
callBack()
}
},
fail(res) {
self.showToast('登录失败,正在重试.')
wx.hideLoading()
self.login()
}
}
)
}
2021-01-12 18:04:14 +08:00
saveOpenid(openid) {
2021-01-19 14:02:55 +08:00
// console.log('saveOpenid:' + openid)
2021-01-12 18:04:14 +08:00
store.dispatch('setOpenidAction', openid)
}
getOpenid() {
return wx.getStorageSync('openid')
}
setRemoteUserInfo(userInfo) {
store.dispatch('setMobileAction', userInfo.mobile)
store.dispatch('setUserInfoAction', userInfo)
}
setCacheInfo() {
const mobile = wx.getStorageSync('mobile')
const openid = wx.getStorageSync('openid')
const userInfo = wx.getStorageSync('userInfo')
2021-01-19 14:02:55 +08:00
const shoppingCar = wx.getStorageSync('shoppingCar')
console.log('setCacheInfo:', mobile)
console.log('setCacheInfo:', openid)
console.log('setCacheInfo:', userInfo)
2021-01-19 14:02:55 +08:00
console.log('shoppingCar:', shoppingCar)
if (mobile) {
store.dispatch('setMobileAction', mobile)
}
if (openid) {
store.dispatch('setOpenidAction', openid)
}
if (userInfo) {
store.dispatch('setUserInfoAction', userInfo)
}
2021-01-19 14:02:55 +08:00
if (shoppingCar) {
store.dispatch('setShoppingCarAction', shoppingCar)
}
}
navigateTo(path) {
console.log('path:', path)
if (!store.state.user.token) {
eventHub.$emit('onShowDialogUserInfo')
return
}
if (!path) {
this.showToast('建设中')
return
}
2021-01-12 18:04:14 +08:00
wx.navigateTo({
url: path
2021-01-12 18:04:14 +08:00
})
}
showToast(msg) {
wx.showToast({ title: msg, icon: 'none' })
2021-01-12 18:04:14 +08:00
}
}
2021-01-15 12:00:20 +08:00
export default new AppManager()