Files
JoyD/Claw/client/wechat_app/app.js
2026-04-21 13:46:20 +08:00

287 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 应用入口文件
App({
onLaunch: function () {
console.log('智控未来小程序启动')
// 初始化系统信息
this.globalData.systemInfo = wx.getSystemInfoSync()
// 检查更新
this.checkForUpdate()
// 初始化网络监听
this.initNetworkListener()
// 初始化用户信息
this.initUserInfo()
},
onShow: function () {
console.log('小程序显示')
},
onHide: function () {
console.log('小程序隐藏')
},
onError: function (msg) {
console.error('小程序错误:', msg)
},
// 初始化用户信息
initUserInfo: function() {
// 从本地存储获取用户信息
const storedUserInfo = wx.getStorageSync('userInfo')
if (storedUserInfo) {
this.globalData.userInfo = storedUserInfo
console.log('从本地存储加载用户信息:', storedUserInfo)
}
// 检查授权状态
this.checkAuthStatus()
},
// 检查授权状态
checkAuthStatus: function() {
wx.getSetting({
success: (res) => {
console.log('授权状态:', res.authSetting)
// 检查用户信息授权
if (res.authSetting['scope.userInfo']) {
console.log('已授权用户信息')
} else {
console.log('未授权用户信息')
}
// 检查地理位置授权
if (res.authSetting['scope.userLocation']) {
console.log('已授权地理位置')
} else {
console.log('未授权地理位置')
}
}
})
},
// 获取用户信息
getUserInfo: function(cb) {
const that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
// 检查本地存储是否有用户信息
const storedUserInfo = wx.getStorageSync('userInfo')
if (storedUserInfo) {
that.globalData.userInfo = storedUserInfo
typeof cb == "function" && cb(storedUserInfo)
} else {
// 未授权,需要用户点击授权
typeof cb == "function" && cb(null)
}
}
},
// 触发用户授权
triggerUserAuth: function(cb) {
const that = this
wx.getUserProfile({
desc: '用于完善个人资料,提供个性化服务',
success: (res) => {
that.globalData.userInfo = res.userInfo
// 存储到本地
wx.setStorageSync('userInfo', res.userInfo)
// 获取全局唯一ID
that.getGlobalUserId((userId) => {
typeof cb == "function" && cb(res.userInfo)
})
},
fail: (err) => {
console.error('获取用户信息失败:', err)
typeof cb == "function" && cb(null)
}
})
},
// 获取全局唯一IDUnionID或OpenID
getGlobalUserId: function(cb) {
const that = this
// 尝试从本地存储获取
let userId = wx.getStorageSync('globalUserId')
if (userId) {
typeof cb == "function" && cb(userId)
return
}
// 调用微信登录获取code
wx.login({
success: (res) => {
if (res.code) {
// 这里应该调用后端接口获取UnionID
// 由于是模拟环境我们使用code生成一个假的UnionID
// 实际项目中应该发送code到后端后端调用微信接口获取UnionID
const mockUnionId = `union_${res.code}_${Date.now()}`
userId = mockUnionId
// 存储到本地
wx.setStorageSync('globalUserId', userId)
that.globalData.globalUserId = userId
typeof cb == "function" && cb(userId)
} else {
console.error('获取微信登录凭证失败:', res.errMsg)
// 失败时生成一个基于设备的ID作为备选
const deviceInfo = that.globalData.systemInfo
const fallbackId = `fallback_${deviceInfo.model.replace(/\s+/g, '_')}_${Date.now()}`
wx.setStorageSync('globalUserId', fallbackId)
that.globalData.globalUserId = fallbackId
typeof cb == "function" && cb(fallbackId)
}
},
fail: (err) => {
console.error('微信登录失败:', err)
// 失败时生成一个基于设备的ID作为备选
const deviceInfo = that.globalData.systemInfo
const fallbackId = `fallback_${deviceInfo.model.replace(/\s+/g, '_')}_${Date.now()}`
wx.setStorageSync('globalUserId', fallbackId)
that.globalData.globalUserId = fallbackId
typeof cb == "function" && cb(fallbackId)
}
})
},
// 获取手机号
getPhoneNumber: function(e, cb) {
const that = this
if (e.detail.encryptedData && e.detail.iv) {
// 调用网关接口解密手机号
wx.request({
url: `${that.globalData.gatewayUrl}/v1/wechat/phone`,
method: 'POST',
data: {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
code: e.detail.code // 有些版本会返回code
},
header: {
'Content-Type': 'application/json'
},
success: (res) => {
if (res.data && res.data.phoneNumber) {
const phoneNumber = res.data.phoneNumber
// 存储到本地
wx.setStorageSync('phoneNumber', phoneNumber)
that.globalData.phoneNumber = phoneNumber
typeof cb == "function" && cb(phoneNumber)
} else {
console.error('解密手机号失败:', res.data)
typeof cb == "function" && cb(null)
}
},
fail: (err) => {
console.error('请求网关失败:', err)
typeof cb == "function" && cb(null)
}
})
} else {
console.error('用户拒绝授权手机号或获取加密数据失败')
typeof cb == "function" && cb(null)
}
},
// 获取地理位置信息
getLocation: function(cb) {
wx.getLocation({
type: 'wgs84',
success: (res) => {
const locationInfo = {
latitude: res.latitude,
longitude: res.longitude,
speed: res.speed,
accuracy: res.accuracy
}
// 存储到本地
wx.setStorageSync('locationInfo', locationInfo)
typeof cb == "function" && cb(locationInfo)
},
fail: (err) => {
console.error('获取地理位置失败:', err)
typeof cb == "function" && cb(null)
}
})
},
// 获取设备信息
getDeviceInfo: function() {
return this.globalData.systemInfo
},
// 获取网络状态
getNetworkType: function(cb) {
wx.getNetworkType({
success: (res) => {
typeof cb == "function" && cb(res.networkType)
}
})
},
// 检查更新
checkForUpdate: function() {
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
console.log('检查更新结果:', res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
wx.showModal({
title: '更新提示',
content: '新版本下载失败',
showCancel: false
})
})
}
},
// 初始化网络监听
initNetworkListener: function() {
wx.onNetworkStatusChange(function(res) {
console.log('网络状态变化:', res)
if (!res.isConnected) {
wx.showToast({
title: '网络已断开',
icon: 'none'
})
}
})
},
// 全局数据
globalData: {
userInfo: null,
globalUserId: null,
phoneNumber: null,
systemInfo: null,
apiBase: 'https://pactgo.cn/api/v1',
websocketUrl: 'wss://pactgo.cn/ws/task',
gatewayUrl: 'http://localhost:8000',
version: '1.0.0'
}
})