Files
JoyD/Claw/client/wechat_app/pages/index/index.js
2026-03-16 15:47:55 +08:00

224 lines
5.5 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.

// 首页逻辑
const app = getApp()
Page({
data: {
userInfo: null,
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
websocketConnected: false,
version: app.globalData.version
},
onLoad() {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
// 初始化WebSocket连接
this.initWebSocket()
},
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
// 发送用户信息到服务器
this.sendUserInfoToServer(res.userInfo)
}
})
},
getUserInfo(e) {
// 不推荐使用getUserInfo获取用户信息预计自2021年4月13日起getUserInfo将不再弹出弹窗并直接返回匿名的用户个人信息
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
// 发送用户信息到服务器
this.sendUserInfoToServer(e.detail.userInfo)
},
// 发送用户信息到服务器
sendUserInfoToServer(userInfo) {
wx.request({
url: `${app.globalData.apiBase}/user/info`,
method: 'POST',
data: {
userInfo: userInfo,
deviceId: app.globalData.systemInfo.model
},
header: {
'content-type': 'application/json'
},
success: (res) => {
console.log('用户信息上传成功', res.data)
},
fail: (err) => {
console.error('用户信息上传失败', err)
}
})
},
// 初始化WebSocket连接
initWebSocket() {
const socket = wx.connectSocket({
url: app.globalData.websocketUrl,
header: {
'content-type': 'application/json'
}
})
socket.onOpen(() => {
console.log('WebSocket连接已打开')
this.setData({
websocketConnected: true
})
// 发送认证信息
socket.send({
data: JSON.stringify({
type: 'auth',
userId: app.globalData.userInfo ? app.globalData.userInfo.nickName : 'anonymous',
deviceId: app.globalData.systemInfo.model,
timestamp: Date.now()
})
})
})
socket.onMessage((res) => {
console.log('收到WebSocket消息', res.data)
try {
const data = JSON.parse(res.data)
this.handleWebSocketMessage(data)
} catch (e) {
console.error('解析WebSocket消息失败', e)
}
})
socket.onClose(() => {
console.log('WebSocket连接已关闭')
this.setData({
websocketConnected: false
})
// 3秒后尝试重连
setTimeout(() => {
this.initWebSocket()
}, 3000)
})
socket.onError((err) => {
console.error('WebSocket连接错误', err)
this.setData({
websocketConnected: false
})
})
},
// 处理WebSocket消息
handleWebSocketMessage(data) {
switch (data.type) {
case 'task_status':
// 处理任务状态更新
this.handleTaskStatusUpdate(data)
break
case 'message':
// 处理聊天消息
this.handleChatMessage(data)
break
default:
console.log('未知消息类型', data.type)
}
},
// 处理任务状态更新
handleTaskStatusUpdate(data) {
// 可以在这里更新任务列表或显示通知
if (data.status === 'completed') {
wx.showToast({
title: '任务完成',
icon: 'success'
})
} else if (data.status === 'failed') {
wx.showToast({
title: '任务失败',
icon: 'error'
})
}
},
// 处理聊天消息
handleChatMessage(data) {
// 可以在这里显示新消息通知
if (data.message) {
wx.showToast({
title: '新消息',
icon: 'none'
})
}
},
// 跳转到聊天页面
goToChat() {
wx.navigateTo({
url: '/pages/chat/chat'
})
},
// 跳转到任务页面
goToTask() {
wx.navigateTo({
url: '/pages/task/task'
})
},
// 显示设备信息
showDeviceInfo() {
const systemInfo = app.globalData.systemInfo
wx.showModal({
title: '设备信息',
content: `设备型号:${systemInfo.model}\n系统版本:${systemInfo.system}\n微信版本:${systemInfo.version}\n屏幕尺寸:${systemInfo.screenWidth}x${systemInfo.screenHeight}`,
showCancel: false
})
},
onShareAppMessage() {
return {
title: '智控未来 - 企业微信智能控制系统',
path: '/pages/index/index'
}
}
})