people-reading/miniprogram/pages/palm/palm.js
2026-05-11 23:26:11 +08:00

97 lines
2.5 KiB
JavaScript

const { request, uploadPalm } = require('../../utils/request')
Page({
data: {
hasToken: false,
imagePath: '',
handSide: 'unknown',
submitting: false
},
onShow() {
this.setData({ hasToken: Boolean(getApp().globalData.token) })
},
chooseLeft() {
this.setData({ handSide: 'left' })
},
chooseRight() {
this.setData({ handSide: 'right' })
},
chooseUnknown() {
this.setData({ handSide: 'unknown' })
},
async loginDev() {
try {
const login = await wx.login()
const data = await request({ url: '/auth/wechat-login', method: 'POST', data: { code: login.code || 'dev' } })
getApp().setToken(data.access_token)
this.setData({ hasToken: true })
wx.showToast({ title: '已登录' })
} catch (error) {
wx.showToast({ title: error.message || '登录失败', icon: 'none' })
}
},
async loginWithPhone(event) {
try {
const login = await wx.login()
const phoneCode = event.detail && event.detail.code
const data = await request({
url: '/auth/wechat-login',
method: 'POST',
data: { code: login.code || 'dev', phone_code: phoneCode || null }
})
getApp().setToken(data.access_token)
this.setData({ hasToken: true })
wx.showToast({ title: '已登录' })
} catch (error) {
wx.showToast({ title: error.message || '登录失败', icon: 'none' })
}
},
async chooseImage() {
try {
const res = await wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
sizeType: ['compressed']
})
this.setData({ imagePath: res.tempFiles[0].tempFilePath })
} catch (error) {
if (error.errMsg && !error.errMsg.includes('cancel')) {
wx.showToast({ title: '选择照片失败', icon: 'none' })
}
}
},
async submit() {
if (!getApp().globalData.token) {
await this.loginDev()
}
if (!this.data.imagePath) return
this.setData({ submitting: true })
try {
const upload = await uploadPalm(this.data.imagePath)
const report = await request({
url: '/reports',
method: 'POST',
data: { image_id: upload.image_id, hand_side: this.data.handSide }
})
wx.navigateTo({ url: `/pages/generating/generating?id=${report.id}` })
} catch (error) {
wx.showToast({ title: error.message || '生成失败', icon: 'none' })
} finally {
this.setData({ submitting: false })
}
},
openLegal() {
wx.navigateTo({ url: '/pages/legal/legal' })
}
})