Files
JoyD/Claw/client/wechat_app/components/auth-prompt/auth-prompt.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2026-04-21 13:46:20 +08:00
// 授权提示组件
Component({
properties: {
show: {
type: Boolean,
value: false
},
authType: {
type: String,
value: 'userInfo' // userInfo, location, camera, record
}
},
data: {
authConfig: {
userInfo: {
title: '授权用户信息',
desc: '为了提供更好的服务,需要获取您的用户信息',
buttonText: '授权',
icon: '/assets/images/user-avatar.png'
},
location: {
title: '授权地理位置',
desc: '为了提供基于位置的服务,需要获取您的地理位置',
buttonText: '授权',
icon: '/assets/images/location-icon.png'
},
camera: {
title: '授权相机',
desc: '为了拍摄照片或视频,需要获取相机权限',
buttonText: '授权',
icon: '/assets/images/camera-icon.png'
},
record: {
title: '授权录音',
desc: '为了录制语音,需要获取录音权限',
buttonText: '授权',
icon: '/assets/images/mic-icon.png'
}
}
},
methods: {
// 授权按钮点击
handleAuth() {
const { authType } = this.properties
switch (authType) {
case 'userInfo':
this.authUserInfo()
break
case 'location':
this.authLocation()
break
case 'camera':
this.authCamera()
break
case 'record':
this.authRecord()
break
}
},
// 授权用户信息
authUserInfo() {
const app = getApp()
app.triggerUserAuth((userInfo) => {
if (userInfo) {
this.triggerEvent('authSuccess', { authType: 'userInfo', data: userInfo })
this.setData({ show: false })
} else {
console.error('用户信息授权失败')
this.triggerEvent('authFail', { authType: 'userInfo', error: '授权失败' })
}
})
},
// 授权地理位置
authLocation() {
wx.getLocation({
type: 'wgs84',
success: (res) => {
this.triggerEvent('authSuccess', { authType: 'location', data: res })
this.setData({ show: false })
},
fail: (err) => {
console.error('地理位置授权失败:', err)
this.triggerEvent('authFail', { authType: 'location', error: err })
}
})
},
// 授权相机
authCamera() {
wx.authorize({
scope: 'scope.camera',
success: () => {
this.triggerEvent('authSuccess', { authType: 'camera' })
this.setData({ show: false })
},
fail: (err) => {
console.error('相机授权失败:', err)
this.triggerEvent('authFail', { authType: 'camera', error: err })
}
})
},
// 授权录音
authRecord() {
wx.authorize({
scope: 'scope.record',
success: () => {
this.triggerEvent('authSuccess', { authType: 'record' })
this.setData({ show: false })
},
fail: (err) => {
console.error('录音授权失败:', err)
this.triggerEvent('authFail', { authType: 'record', error: err })
}
})
},
// 取消授权
handleCancel() {
this.triggerEvent('authCancel', { authType: this.properties.authType })
this.setData({ show: false })
},
// 打开设置
openSettings() {
wx.openSetting({
success: (res) => {
console.log('设置结果:', res.authSetting)
this.triggerEvent('settingsOpen', { authType: this.properties.authType, settings: res.authSetting })
}
})
}
}
})