flower-mp/app.js
2025-09-25 14:46:10 +08:00

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.user.openId)
wx.setStorageSync('sessionKey', res.data.user.sessionKey)
if (res.data.user.id){
wx.setStorageSync('user', res.data.user)
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);
}
})
}
})