flower-mp/app.js
2025-12-08 15:09:41 +08:00

87 lines
2.5 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)
publisher.emit("login");
// 生成邀请码
api('/personal/inviteCode/code','GET').then(res => {
if (res.code === 200){
wx.setStorageSync('inviteCode', res.data)
}
})
} 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) {
console.log("succ");
} 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);
}
})
}
})