|
|
@ -523,8 +523,19 @@ Page({ |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
const openid = wx.getStorageSync('openid'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
|
|
|
|
// 如果本地没有openid或用户信息,直接显示一键登录弹窗
|
|
|
|
|
|
if (!openid || !userId) { |
|
|
|
|
|
console.log('本地没有用户信息,显示一键登录弹窗'); |
|
|
|
|
|
// 保存当前表单数据
|
|
|
|
|
|
this.saveSettlementProgress(); |
|
|
|
|
|
// 直接显示一键登录弹窗
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showOneKeyLoginModal: true |
|
|
|
|
|
}); |
|
|
|
|
|
return; // 取消提交申请
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 如果本地有openid,查询数据库验证用户是否真的存在
|
|
|
// 如果本地有openid,查询数据库验证用户是否真的存在
|
|
|
if (openid) { |
|
|
|
|
|
try { |
|
|
try { |
|
|
const API = require('../../utils/api'); |
|
|
const API = require('../../utils/api'); |
|
|
console.log('查询数据库验证用户是否存在...'); |
|
|
console.log('查询数据库验证用户是否存在...'); |
|
|
@ -541,9 +552,9 @@ Page({ |
|
|
console.log('用户不存在于数据库中,需要登录'); |
|
|
console.log('用户不存在于数据库中,需要登录'); |
|
|
// 保存当前表单数据
|
|
|
// 保存当前表单数据
|
|
|
this.saveSettlementProgress(); |
|
|
this.saveSettlementProgress(); |
|
|
// 显示登录弹窗
|
|
|
// 直接显示一键登录弹窗
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
showAuthModal: true |
|
|
showOneKeyLoginModal: true |
|
|
}); |
|
|
}); |
|
|
return; // 取消提交申请
|
|
|
return; // 取消提交申请
|
|
|
} |
|
|
} |
|
|
@ -552,17 +563,6 @@ Page({ |
|
|
// 查询失败,可能是网络问题或其他原因,继续使用本地存储的信息
|
|
|
// 查询失败,可能是网络问题或其他原因,继续使用本地存储的信息
|
|
|
console.log('查询失败,继续使用本地存储的用户信息:', openid, userId); |
|
|
console.log('查询失败,继续使用本地存储的用户信息:', openid, userId); |
|
|
} |
|
|
} |
|
|
} else if (!openid || !userId) { |
|
|
|
|
|
// 本地没有openid或userId,显示登录弹窗
|
|
|
|
|
|
console.log('本地没有用户信息,显示授权弹窗'); |
|
|
|
|
|
// 保存当前表单数据
|
|
|
|
|
|
this.saveSettlementProgress(); |
|
|
|
|
|
// 显示登录弹窗
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showAuthModal: true |
|
|
|
|
|
}); |
|
|
|
|
|
return; // 取消提交申请
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('使用用户信息提交申请:', openid, userId); |
|
|
console.log('使用用户信息提交申请:', openid, userId); |
|
|
|
|
|
|
|
|
@ -697,27 +697,7 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查用户是否已经获取手机号
|
|
|
// 先检查所有必填表单字段
|
|
|
let userInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
if (!userInfo || !userInfo.phoneNumber || userInfo.phoneNumber === '未绑定') { |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '请先授权手机号', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
// 显示登录弹窗,引导用户授权手机号
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showAuthModal: true |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果用户已经获取手机号,确保手机号字段正确赋值
|
|
|
|
|
|
if (userInfo.phoneNumber && userInfo.phoneNumber !== '未绑定') { |
|
|
|
|
|
submitData.phoneNumber = userInfo.phoneNumber; |
|
|
|
|
|
contactPhone = userInfo.phoneNumber; |
|
|
|
|
|
console.log('使用登录获取的手机号:', contactPhone); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!this.data.collaborationid) { |
|
|
if (!this.data.collaborationid) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: '请选择合作商身份', |
|
|
title: '请选择合作商身份', |
|
|
@ -742,6 +722,21 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查用户是否已经获取手机号
|
|
|
|
|
|
let userInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
if (!userInfo || !userInfo.phoneNumber || userInfo.phoneNumber === '未绑定') { |
|
|
|
|
|
// 直接显示一键登录弹窗,引导用户授权手机号
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showOneKeyLoginModal: true |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果用户已经获取手机号,确保手机号字段正确赋值
|
|
|
|
|
|
submitData.phoneNumber = userInfo.phoneNumber; |
|
|
|
|
|
contactPhone = userInfo.phoneNumber; |
|
|
|
|
|
console.log('使用登录获取的手机号:', contactPhone); |
|
|
|
|
|
|
|
|
// 强制要求手机号
|
|
|
// 强制要求手机号
|
|
|
if (!contactPhone) { |
|
|
if (!contactPhone) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -751,10 +746,6 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 更新submitData中的手机号
|
|
|
|
|
|
submitData.phoneNumber = contactPhone; |
|
|
|
|
|
console.log('使用的手机号:', contactPhone); |
|
|
|
|
|
|
|
|
|
|
|
// 验证省市区字段是否填写完整(用于构建region字段)
|
|
|
// 验证省市区字段是否填写完整(用于构建region字段)
|
|
|
if (!this.data.province || !this.data.city || !this.data.district) { |
|
|
if (!this.data.province || !this.data.city || !this.data.district) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -764,6 +755,10 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新submitData中的手机号
|
|
|
|
|
|
submitData.phoneNumber = contactPhone; |
|
|
|
|
|
console.log('使用的手机号:', contactPhone); |
|
|
|
|
|
|
|
|
// 记录省市区字段内容
|
|
|
// 记录省市区字段内容
|
|
|
console.log('省市区字段内容:', this.data.province, this.data.city, this.data.district); |
|
|
console.log('省市区字段内容:', this.data.province, this.data.city, this.data.district); |
|
|
|
|
|
|
|
|
@ -873,14 +868,26 @@ Page({ |
|
|
console.log('用户完整数据:', userRes.data); |
|
|
console.log('用户完整数据:', userRes.data); |
|
|
// 更新用户的申请状态
|
|
|
// 更新用户的申请状态
|
|
|
if (userRes.data && userRes.data.partnerstatus) { |
|
|
if (userRes.data && userRes.data.partnerstatus) { |
|
|
this.setData({ |
|
|
// 构建要更新的数据对象,确保只设置存在的值
|
|
|
partnerstatus: userRes.data.partnerstatus, |
|
|
const updateData = { |
|
|
applicationId: userRes.data.applicationId |
|
|
partnerstatus: userRes.data.partnerstatus |
|
|
}); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 只有当applicationId存在且不为undefined时才设置
|
|
|
|
|
|
if (userRes.data.applicationId) { |
|
|
|
|
|
updateData.applicationId = userRes.data.applicationId; |
|
|
|
|
|
} else { |
|
|
|
|
|
updateData.applicationId = null; // 设置为null而不是undefined
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.setData(updateData); |
|
|
|
|
|
|
|
|
// 保存最新状态到本地存储
|
|
|
// 保存最新状态到本地存储
|
|
|
wx.setStorageSync('settlementStatus', userRes.data.partnerstatus); |
|
|
wx.setStorageSync('settlementStatus', userRes.data.partnerstatus); |
|
|
if (userRes.data.applicationId) { |
|
|
if (userRes.data.applicationId) { |
|
|
wx.setStorageSync('applicationId', userRes.data.applicationId); |
|
|
wx.setStorageSync('applicationId', userRes.data.applicationId); |
|
|
|
|
|
} else { |
|
|
|
|
|
wx.removeStorageSync('applicationId'); // 如果不存在则移除本地存储
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
@ -960,10 +967,19 @@ Page({ |
|
|
|
|
|
|
|
|
// 关闭登录弹窗
|
|
|
// 关闭登录弹窗
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
showAuthModal: false |
|
|
showOneKeyLoginModal: false |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 首先检查用户是否拒绝授权
|
|
|
|
|
|
if (e.detail.errMsg !== 'getPhoneNumber:ok') { |
|
|
|
|
|
console.log('用户拒绝授权手机号'); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '您已拒绝授权,操作已取消', |
|
|
|
|
|
icon: 'none' |
|
|
}); |
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (e.detail.errMsg === 'getPhoneNumber:ok') { |
|
|
|
|
|
// 用户同意授权
|
|
|
// 用户同意授权
|
|
|
wx.showLoading({ |
|
|
wx.showLoading({ |
|
|
title: '登录中...', |
|
|
title: '登录中...', |
|
|
@ -971,17 +987,92 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
// 调用API解密手机号
|
|
|
// 引入API服务
|
|
|
const app = getApp(); |
|
|
const API = require('../../utils/api.js'); |
|
|
if (app && app.uploadPhoneNumberData) { |
|
|
|
|
|
const result = await app.uploadPhoneNumberData(e.detail); |
|
|
|
|
|
|
|
|
|
|
|
if (result && result.success) { |
|
|
// 1. 先执行微信登录获取code
|
|
|
// 保存用户信息到全局和本地存储
|
|
|
const loginRes = await new Promise((resolve, reject) => { |
|
|
const userInfo = { |
|
|
wx.login({ |
|
|
|
|
|
success: resolve, |
|
|
|
|
|
fail: reject |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!loginRes.code) { |
|
|
|
|
|
throw new Error('获取登录code失败'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('获取登录code成功:', loginRes.code); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 使用code换取openid
|
|
|
|
|
|
const openidRes = await API.getOpenid(loginRes.code); |
|
|
|
|
|
|
|
|
|
|
|
// 增强版响应处理逻辑,支持多种返回格式
|
|
|
|
|
|
let openid = null; |
|
|
|
|
|
let userId = null; |
|
|
|
|
|
let sessionKey = null; |
|
|
|
|
|
|
|
|
|
|
|
// 优先从data字段获取数据
|
|
|
|
|
|
if (openidRes && openidRes.data && typeof openidRes.data === 'object') { |
|
|
|
|
|
openid = openidRes.data.openid || openidRes.data.OpenID || null; |
|
|
|
|
|
userId = openidRes.data.userId || openidRes.data.userid || null; |
|
|
|
|
|
sessionKey = openidRes.data.session_key || openidRes.data.sessionKey || null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果data为空或不存在,尝试从响应对象直接获取
|
|
|
|
|
|
if (!openid && openidRes && typeof openidRes === 'object') { |
|
|
|
|
|
console.warn('服务器返回格式可能不符合预期,data字段为空或不存在,但尝试从根对象提取信息:', openidRes); |
|
|
|
|
|
openid = openidRes.openid || openidRes.OpenID || null; |
|
|
|
|
|
userId = openidRes.userId || openidRes.userid || null; |
|
|
|
|
|
sessionKey = openidRes.session_key || openidRes.sessionKey || null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查服务器状态信息
|
|
|
|
|
|
const isSuccess = openidRes && (openidRes.success === true || openidRes.code === 200); |
|
|
|
|
|
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
throw new Error('获取openid失败: ' + (openidRes && openidRes.message ? openidRes.message : '未知错误')); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 存储openid和session_key
|
|
|
|
|
|
wx.setStorageSync('openid', openid); |
|
|
|
|
|
if (sessionKey) { |
|
|
|
|
|
wx.setStorageSync('sessionKey', sessionKey); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 确保始终使用从服务器获取的正式用户ID
|
|
|
|
|
|
if (userId) { |
|
|
|
|
|
wx.setStorageSync('userId', userId); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('获取openid成功并存储:', openid); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 上传手机号加密数据到服务器解密
|
|
|
|
|
|
const phoneData = { |
|
|
|
|
|
...e.detail, |
|
|
|
|
|
openid: openid, |
|
|
|
|
|
sessionKey: sessionKey || '' |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
console.log('准备上传手机号加密数据到服务器'); |
|
|
|
|
|
const phoneRes = await API.uploadPhoneNumberData(phoneData); |
|
|
|
|
|
|
|
|
|
|
|
// 改进手机号解密结果的处理逻辑
|
|
|
|
|
|
if (!phoneRes || (!phoneRes.success && !phoneRes.phoneNumber)) { |
|
|
|
|
|
// 如果服务器返回格式不标准但包含手机号,也接受
|
|
|
|
|
|
if (!(phoneRes && phoneRes.phoneNumber)) { |
|
|
|
|
|
throw new Error('获取手机号失败: ' + (phoneRes && phoneRes.message ? phoneRes.message : '未知错误')); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const phoneNumber = phoneRes.phoneNumber || '未绑定'; |
|
|
|
|
|
console.log('获取手机号成功:', phoneNumber); |
|
|
|
|
|
|
|
|
|
|
|
// 4. 获取用户信息
|
|
|
|
|
|
let userInfo = { |
|
|
name: '微信用户', |
|
|
name: '微信用户', |
|
|
avatarUrl: '/images/default-avatar.png', |
|
|
avatarUrl: '/images/default-avatar.png', |
|
|
phoneNumber: result.phoneNumber || '未绑定', |
|
|
phoneNumber: phoneNumber, |
|
|
gender: 0, |
|
|
gender: 0, |
|
|
country: '', |
|
|
country: '', |
|
|
province: '', |
|
|
province: '', |
|
|
@ -989,13 +1080,48 @@ Page({ |
|
|
language: 'zh_CN' |
|
|
language: 'zh_CN' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 保存到全局数据
|
|
|
try { |
|
|
|
|
|
// 尝试获取用户微信头像和昵称
|
|
|
|
|
|
const userProfileRes = await new Promise((resolve, reject) => { |
|
|
|
|
|
wx.getUserProfile({ |
|
|
|
|
|
desc: '用于完善会员资料', |
|
|
|
|
|
success: resolve, |
|
|
|
|
|
fail: reject |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (userProfileRes && userProfileRes.userInfo) { |
|
|
|
|
|
userInfo = { |
|
|
|
|
|
...userInfo, |
|
|
|
|
|
...userProfileRes.userInfo |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
console.warn('获取用户头像昵称失败:', err); |
|
|
|
|
|
// 不影响主流程,使用默认值
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 5. 更新用户信息到服务器
|
|
|
|
|
|
try { |
|
|
|
|
|
await API.updateUserInfo({ |
|
|
|
|
|
openid: openid, |
|
|
|
|
|
phoneNumber: phoneNumber, |
|
|
|
|
|
...userInfo |
|
|
|
|
|
}); |
|
|
|
|
|
console.log('用户信息已更新到服务器'); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
console.warn('更新用户信息到服务器失败:', err); |
|
|
|
|
|
// 不影响主流程,继续执行
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 6. 保存用户信息到全局和本地存储
|
|
|
|
|
|
const app = getApp(); |
|
|
|
|
|
if (app) { |
|
|
app.globalData.userInfo = userInfo; |
|
|
app.globalData.userInfo = userInfo; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 保存到本地存储
|
|
|
|
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
|
|
|
console.log('用户信息已保存到本地存储:', userInfo); |
|
|
console.log('用户信息已保存:', userInfo); |
|
|
|
|
|
|
|
|
|
|
|
wx.hideLoading(); |
|
|
wx.hideLoading(); |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -1003,20 +1129,15 @@ Page({ |
|
|
icon: 'success', |
|
|
icon: 'success', |
|
|
duration: 1500, |
|
|
duration: 1500, |
|
|
complete: () => { |
|
|
complete: () => { |
|
|
// 登录成功后关闭弹窗,让用户决定是否继续提交
|
|
|
// 登录成功后关闭弹窗,让用户继续提交申请
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
this.closeOneKeyLoginModal(); |
|
|
this.closeOneKeyLoginModal(); |
|
|
this.setData({ showAuthModal: false }); |
|
|
// 用户登录成功后,可以选择自动提交申请
|
|
|
|
|
|
// 这里保持原逻辑,让用户手动点击提交
|
|
|
}, 1500); |
|
|
}, 1500); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
|
|
|
throw new Error(result.message || '登录失败'); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// 备用方案:模拟登录
|
|
|
|
|
|
await this.performBackupLogin(); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
wx.hideLoading(); |
|
|
wx.hideLoading(); |
|
|
console.error('手机号授权登录失败:', error); |
|
|
console.error('手机号授权登录失败:', error); |
|
|
@ -1025,13 +1146,6 @@ Page({ |
|
|
icon: 'none' |
|
|
icon: 'none' |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
|
|
|
// 用户拒绝授权
|
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '需要手机号授权才能继续', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 备用登录方案
|
|
|
// 备用登录方案
|
|
|
@ -1182,15 +1296,15 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 跳转到seller页面,不显示提示
|
|
|
// 跳转到index页面,不显示提示
|
|
|
wx.reLaunch({ |
|
|
wx.reLaunch({ |
|
|
url: '/pages/seller/index' |
|
|
url: '/pages/index/index' |
|
|
}); |
|
|
}); |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('获取用户数据失败:', err); |
|
|
console.error('获取用户数据失败:', err); |
|
|
// 即使获取失败也跳转到seller页面
|
|
|
// 即使获取失败也跳转到index页面
|
|
|
wx.reLaunch({ |
|
|
wx.reLaunch({ |
|
|
url: '/pages/seller/index' |
|
|
url: '/pages/index/index' |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
@ -1263,18 +1377,10 @@ Page({ |
|
|
completeApplication() { |
|
|
completeApplication() { |
|
|
// 更新入驻状态为审核通过
|
|
|
// 更新入驻状态为审核通过
|
|
|
this.updateGlobalSettlementStatus('approved'); |
|
|
this.updateGlobalSettlementStatus('approved'); |
|
|
|
|
|
// 返回首页
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '感谢您的提交,我们将尽快与您联系!', |
|
|
|
|
|
icon: 'success' |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 延迟跳转到seller页面
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
wx.reLaunch({ |
|
|
wx.reLaunch({ |
|
|
url: '/pages/seller/index' |
|
|
url: '/pages/index/index' |
|
|
}); |
|
|
}); |
|
|
}, 1500); |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 重置申请流程
|
|
|
// 重置申请流程
|
|
|
|