49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
import {
|
|
api
|
|
} from './utils/api';
|
|
import {
|
|
publisher
|
|
} from "./utils/login-sdk";
|
|
App({
|
|
onLaunch() {
|
|
// 登录
|
|
wx.login({
|
|
success: res => {
|
|
// 发送 res.code 到后台换取 openId, sessionKey, unionId
|
|
api("/auth/miniLogin?code=" + res.code).then(res => {
|
|
if (res.code === 200) {
|
|
wx.setStorageSync('openid', res.data.openid)
|
|
wx.setStorageSync('sessionKey', res.data.sessionKey)
|
|
if (res.data.id){
|
|
wx.setStorageSync('user', res.data)
|
|
wx.setStorageSync('token', res.data.token)
|
|
} else {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '登录过期,请重新登陆',
|
|
confirmText: '去登录',
|
|
complete: (res) => {
|
|
if (res.confirm) {
|
|
wx.navigateTo({
|
|
url: '../login/index',
|
|
})
|
|
}
|
|
}
|
|
})
|
|
wx.removeStorageSync('token')
|
|
wx.removeStorageSync('user')
|
|
}
|
|
publisher.emit("login");
|
|
} else {
|
|
wx.navigateTo({
|
|
url: '../login/index',
|
|
})
|
|
}
|
|
})
|
|
},
|
|
fail:err => {
|
|
console.log(err);
|
|
}
|
|
})
|
|
}
|
|
}) |