Files
MBTI_wang/douyin-miniprogram/utils/phoneAuth.js
Ghost ef19f6860e feat: CRM 报告与双端结果页、管理端用户/设置与默认企业配置
1、修复了用户详情与人脸/DISC 展示、手机号与资料校验、相机页与测试选择等体验问题。

2、新增了 SystemDefaultEnterprise、discDisplay、管理端设置扩展接口;CrmReport/Test/Analyze 等 API 与路由能力补强。

3、优化了 resultFormat、descriptions、payment、企业上下文;海报与 PDP/DISC 文案;超管 Commerce/Users 与 admin Settings/Users。

Made-with: Cursor
2026-04-01 17:16:20 +08:00

108 lines
3.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.

/**
* 手机号授权工具(抖音版)
* 抖音小程序同样支持 <button open-type="getPhoneNumber"> 获取手机号
* 回调中拿到 encryptedData + iv或 code传给后端解密
*/
function isProfileComplete() {
const app = getApp()
const user = app.globalData.userInfo || tt.getStorageSync('userInfo')
if (!user) return false
const avatar = (user.avatar || user.avatarUrl || '').trim()
const nickname = (user.nickname || user.nickName || '').trim()
const phone = (user.phone || user.phoneNumber || '').trim()
return avatar.length > 0 && nickname.length > 0 && phone.length > 0
}
function ensureProfileCompleteAndRedirect() {
const app = getApp()
const token = app.globalData.token || tt.getStorageSync('token')
if (!token) return true
if (isProfileComplete()) return true
tt.showToast({ title: '请先完善个人资料', icon: 'none' })
tt.navigateTo({ url: '/pages/user-profile/index' })
return false
}
function hasPhone() {
const app = getApp()
const user = app.globalData.userInfo || tt.getStorageSync('userInfo')
const phone = (user && (user.phone || user.phoneNumber)) ? String(user.phone || user.phoneNumber).trim() : ''
return phone.length > 0
}
/**
* 使用 getPhoneNumber 回调的数据调用后端换取手机号
* 抖音版:回调返回 encryptedData + iv或 code
*/
function bindPhoneByCode(code) {
return new Promise((resolve, reject) => {
if (!code) {
tt.showToast({ title: '获取手机号失败', icon: 'none' })
reject(new Error('empty code'))
return
}
const app = getApp()
const token = app.globalData.token || tt.getStorageSync('token')
if (!token) {
tt.showToast({ title: '请先登录', icon: 'none' })
reject(new Error('no token'))
return
}
const apiBase = app.globalData.apiBase || ''
if (!apiBase) {
tt.showToast({ title: '服务未配置', icon: 'none' })
reject(new Error('no api base'))
return
}
tt.showLoading({ title: '处理中...', mask: true })
tt.request({
url: `${apiBase.replace(/\/$/, '')}/api/auth/douyin/phone`,
method: 'POST',
header: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
},
data: { code },
success: (res) => {
tt.hideLoading()
if (res.statusCode === 200 && res.data && res.data.code === 200) {
const data = res.data.data || {}
const user = data.user || app.globalData.userInfo || {}
const phone = data.phone || user.phone || ''
const newUser = { ...user, phone }
app.globalData.userInfo = newUser
tt.setStorageSync('userInfo', newUser)
tt.showToast({ title: '授权成功', icon: 'success' })
resolve(newUser)
} else {
const msg = res.data && res.data.message ? res.data.message : '获取手机号失败'
tt.showToast({ title: msg, icon: 'none' })
reject(new Error(msg))
}
},
fail: () => {
tt.hideLoading()
tt.showToast({ title: '网络请求失败', icon: 'none' })
reject(new Error('network error'))
},
})
})
}
/** 与后端 Test::isWechatProfileComplete 一致(抖音侧已实现为头像+昵称+手机) */
function isReportProfileComplete() {
return isProfileComplete()
}
module.exports = {
hasPhone,
bindPhoneByCode,
isProfileComplete,
isReportProfileComplete,
ensureProfileCompleteAndRedirect,
}