- 新增 cloud-functions/ 目录(6个云函数:auth/device/subscription/treatment/user/admin) - 云函数使用 wx-server-sdk + 云数据库(不再依赖 MySQL) - auth 云函数通过 cloud.getWXContext() 自动获取 openid,无需手动 wx.login - request.js 改为通过 wx.cloud.callFunction() 调用云函数 - 登录流程简化:点击授权 → 直接调 auth 云函数 → 获取用户信息 - 保留 HTTP 模式切换能力(USE_CLOUD 变量控制)
51 行
1.1 KiB
JavaScript
51 行
1.1 KiB
JavaScript
var http = require('./utils/request')
|
|
|
|
App({
|
|
globalData: {
|
|
userInfo: null,
|
|
userId: null,
|
|
connectedDevice: null,
|
|
currentTreatment: null
|
|
},
|
|
|
|
onLaunch: function () {
|
|
if (!wx.cloud) {
|
|
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
|
|
return
|
|
}
|
|
wx.cloud.init({
|
|
traceUser: true
|
|
})
|
|
this.checkLogin()
|
|
},
|
|
|
|
checkLogin: function () {
|
|
var token = wx.getStorageSync('token')
|
|
if (!token) {
|
|
this.globalData.userInfo = null
|
|
return
|
|
}
|
|
this.loadProfile()
|
|
},
|
|
|
|
loadProfile: function () {
|
|
var self = this
|
|
http.get('/api/v1/user/profile').then(function (profile) {
|
|
self.globalData.userInfo = profile
|
|
self.globalData.userId = String(profile.user_id)
|
|
}).catch(function () {
|
|
wx.removeStorageSync('token')
|
|
})
|
|
},
|
|
|
|
doLogin: function () {
|
|
return http.post('/api/v1/auth/login', {}).then(function (data) {
|
|
wx.setStorageSync('token', data.token)
|
|
var app = getApp()
|
|
app.globalData.userInfo = data.user_info
|
|
app.globalData.userId = data.user_id
|
|
return data
|
|
})
|
|
}
|
|
})
|