2026-04-30 19:35:55 +08:00
|
|
|
|
const app = getApp()
|
|
|
|
|
|
|
|
|
|
|
|
// 引入 SDK(实际使用时通过 npm 包或复制 dist 文件)
|
|
|
|
|
|
const { XuqmMiniProgramSDK } = require('../../utils/sdk')
|
|
|
|
|
|
|
|
|
|
|
|
Page({
|
|
|
|
|
|
data: {
|
|
|
|
|
|
userId: 'user_a',
|
|
|
|
|
|
password: '123456',
|
|
|
|
|
|
loading: false,
|
|
|
|
|
|
error: '',
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
|
|
const sdk = new XuqmMiniProgramSDK()
|
|
|
|
|
|
const { baseUrl, wsUrl } = app.globalData
|
|
|
|
|
|
sdk.init({ appKey: 'ak_demo_chat', appSecret: 'secret', debug: true, baseUrl, wsUrl })
|
|
|
|
|
|
app.globalData.sdk = sdk
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
onUserIdInput(e) {
|
|
|
|
|
|
this.setData({ userId: e.detail.value })
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
onPasswordInput(e) {
|
|
|
|
|
|
this.setData({ password: e.detail.value })
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
async onLogin() {
|
|
|
|
|
|
const { userId, password } = this.data
|
|
|
|
|
|
if (!userId || !password) {
|
|
|
|
|
|
this.setData({ error: '请输入用户ID和密码' })
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
this.setData({ loading: true, error: '' })
|
|
|
|
|
|
try {
|
|
|
|
|
|
const sdk = app.globalData.sdk
|
2026-05-02 12:53:19 +08:00
|
|
|
|
const config = sdk.requireConfig()
|
|
|
|
|
|
// 1. Call demo API to get userSig
|
|
|
|
|
|
const res = await new Promise((resolve, reject) => {
|
|
|
|
|
|
wx.request({
|
|
|
|
|
|
url: `${config.apiUrl}/api/demo/auth/login`,
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
header: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
data: { appId: config.appKey, userId, password },
|
|
|
|
|
|
success: (r) => resolve(r.data),
|
|
|
|
|
|
fail: (err) => reject(err),
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
if (res.code !== 200) {
|
|
|
|
|
|
throw new Error(res.message || '登录失败')
|
|
|
|
|
|
}
|
|
|
|
|
|
// 2. Login with userSig
|
|
|
|
|
|
await sdk.login(userId, res.data.imToken)
|
2026-04-30 19:35:55 +08:00
|
|
|
|
app.globalData.userId = userId
|
|
|
|
|
|
wx.navigateTo({ url: '/pages/chat/chat' })
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
this.setData({ error: err.message || '登录失败', loading: false })
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|