flower-mp/app.js
sdaduanbilei 98af0c8d3e init
2025-12-04 11:56:37 +08:00

76 lines
2.2 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.miniOpenId)
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')
}
} else {
wx.navigateTo({
url: '../login/index',
})
}
})
// location
wx.getFuzzyLocation({
type: 'wgs84',
success(res) {
const data = {
latitude: res.latitude + "",
longitude: res.longitude + ""
}
api('/auth/getLocation', 'GET', data).then(res => {
if (res.code === 200) {
wx.setStorageSync('city', res.data)
const tmp = wx.getStorageSync('weather')
if (tmp) {
publisher.emit("login");
} else {
api('/auth/getWeather', 'GET', res.data).then(res => {
if (res.code === 200) {
wx.setStorageSync('weather', res.data)
}
})
}
}
})
},
fail(err) {
console.log(err);
}
})
},
fail: err => {
console.log(err);
}
})
}
})