You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1877 lines
58 KiB

3 months ago
// pages/index/index.js
const API = require('../../utils/api.js');
Page({
data: {
currentUserType: null,
showUserInfoForm: false,
avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
name: '',
3 months ago
showAuthModal: false,
showOneKeyLoginModal: false,
userInfo: {},
needPhoneAuth: false,
3 months ago
// 测试模式开关,用于在未完成微信认证时进行测试
testMode: true,
partnerstatus: '', // 用户入驻状态,用于显示入驻/未入驻
// 侧边栏相关
showSidebar: false,
isDragging: false,
startY: 0,
currentY: 0,
sidebarBtnTop: 500, // 初始位置,单位rpx
// 搜索相关
searchKeyword: '',
selectedRegion: '全国',
showRegionPicker: false,
regions: ['全国', '北京', '上海', '广州', '深圳', '天津', '重庆', '河北', '山西', '辽宁', '吉林', '黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '台湾', '内蒙古', '广西', '西藏', '宁夏', '新疆', '香港', '澳门'],
// 商品相关
goods: [],
filteredGoods: [],
selectedCategory: '全部',
loadingMore: false,
hasMoreData: true,
hasMore: true, // 用于显示"已加载全部商品"提示
page: 1,
pageSize: 10,
// 图片预览相关状态
previewImageUrls: [], // 预览的图片URL列表
previewImageIndex: 0, // 当前预览图片的索引
showImagePreview: false, // 控制图片预览弹窗显示
// 导航栏显示控制
showNavBar: true, // 控制导航栏显示/隐藏
lastScrollTop: 0, // 上一次滚动位置
// 回到顶部按钮
showBackToTop: false, // 控制回到顶部按钮显示/隐藏
3 months ago
},
// 跳转到聊天页面
navigateToChat() {
wx.navigateTo({
url: '/pages/chat/index',
success: function() {
console.log('成功跳转到聊天页面');
},
fail: function(error) {
console.error('跳转到聊天页面失败:', error);
wx.showToast({
title: '跳转失败,请稍后重试',
icon: 'none'
});
}
});
},
// 跳转到客服列表页面
navigateToCustomerService(e) {
// 获取按钮类型(seller=我要买蛋,buyer=我要卖蛋)
const userType = e.currentTarget.dataset.type;
wx.navigateTo({
url: `/pages/customer-service/index?type=${userType}`,
success: function() {
console.log('成功跳转到客服列表页面,类型:', userType);
},
fail: function(error) {
console.error('跳转到客服列表页面失败:', error);
wx.showToast({
title: '跳转失败,请稍后重试',
icon: 'none'
});
}
});
},
// 跳转到立即入驻页面
navigateToSettlement() {
wx.navigateTo({
url: '/pages/settlement/index',
success: function() {
console.log('成功跳转到立即入驻页面');
},
fail: function(error) {
console.error('跳转到立即入驻页面失败:', error);
wx.showToast({
title: '跳转失败,请稍后重试',
icon: 'none'
});
}
});
},
// 切换侧边栏显示
toggleSidebar() {
if (this.data.isDragging) {
return;
}
this.setData({
showSidebar: !this.data.showSidebar
});
},
// 触摸开始事件
handleTouchStart(e) {
this.setData({
isDragging: false,
startY: e.touches[0].clientY
});
},
// 触摸移动事件
handleTouchMove(e) {
const moveY = e.touches[0].clientY;
const diffY = moveY - this.data.startY;
// 如果移动距离超过20px,视为拖动
if (Math.abs(diffY) > 20) {
this.setData({
isDragging: true
});
// 更新按钮位置
let newTop = this.data.sidebarBtnTop + diffY * 2; // 转换为rpx
// 限制按钮在屏幕范围内
const screenHeight = wx.getSystemInfoSync().screenHeight * 2; // 转换为rpx
const btnHeight = 80; // 按钮高度,单位rpx
if (newTop < 100) {
newTop = 100;
} else if (newTop > screenHeight - btnHeight - 100) {
newTop = screenHeight - btnHeight - 100;
}
this.setData({
sidebarBtnTop: newTop,
startY: moveY
});
}
},
// 触摸结束事件
handleTouchEnd(e) {
// 如果拖动距离超过屏幕高度的1/3,隐藏按钮
const moveY = e.changedTouches[0].clientY;
const diffY = moveY - this.data.startY;
const screenHeight = wx.getSystemInfoSync().screenHeight * 2; // 转换为rpx
if (Math.abs(diffY) > screenHeight / 3) {
this.setData({
sidebarBtnTop: -100 // 隐藏按钮
});
}
this.setData({
isDragging: false
});
},
3 months ago
onLoad() {
console.log('首页初始化')
// 检查本地缓存并恢复登录状态
this.checkAndRestoreLoginStatus()
// 初始化加载商品数据
this.loadGoods()
3 months ago
},
onShow: function () {
// 页面显示
// 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 0
});
}
// 更新全局tab状态
const app = getApp();
app.updateCurrentTab('index');
// 重新显示tabBar
app.globalData.showTabBar = true;
// 检查并恢复登录状态
this.checkAndRestoreLoginStatus()
// 刷新商品数据
this.refreshGoodsList()
3 months ago
},
// 格式化毛重显示的辅助函数
formatGrossWeight: function(grossWeight, weight) {
if (grossWeight !== null && grossWeight !== undefined && grossWeight !== '') {
return grossWeight;
}
if (weight !== null && weight !== undefined && weight !== '') {
return weight;
}
return "";
},
// 提取地区中的省份信息
extractProvince: function(region) {
if (!region || typeof region !== 'string') {
return region;
}
const provinceEndIndex = region.indexOf('省');
const autonomousRegionEndIndex = region.indexOf('自治区');
const municipalityEndIndex = region.indexOf('市');
const specialRegionEndIndex = region.indexOf('特别行政区');
if (provinceEndIndex !== -1) {
return region.substring(0, provinceEndIndex + 1);
} else if (autonomousRegionEndIndex !== -1) {
return region.substring(0, autonomousRegionEndIndex + 3);
} else if (specialRegionEndIndex !== -1) {
return region.substring(0, specialRegionEndIndex + 5);
} else if (municipalityEndIndex === 2) {
return region.substring(0, municipalityEndIndex + 1);
}
return region;
},
// 加载商品数据
loadGoods: function(isLoadMore = false) {
if (isLoadMore && !this.data.hasMoreData) {
return
}
const currentPage = isLoadMore ? this.data.page : 1
const currentPageSize = this.data.pageSize
this.setData({
loadingMore: true
})
// 添加时间戳参数防止请求缓存
const timestamp = new Date().getTime();
API.getProductList('published', {
timestamp: timestamp,
viewMode: 'shopping',
page: currentPage,
pageSize: currentPageSize,
// 增加搜索关键词和分类参数,与buyer页面保持一致
keyword: this.data.searchKeyword,
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory
})
.then(res => {
wx.hideLoading();
if (res.success && res.products) {
let newGoods = res.products.map(product => {
// 处理grossWeight为null或无效的情况
const grossWeightValue = product.grossWeight !== null && product.grossWeight !== undefined ? product.grossWeight : '';
// 计算预约人数,增强逻辑确保能正确处理各种情况
const selectedValue = product.selected;
const reservedCountValue = product.reservedCount;
const reservationCountValue = product.reservationCount;
const finalReservationCount = selectedValue !== undefined && selectedValue !== null ? selectedValue :
(reservedCountValue !== undefined && reservedCountValue !== null ? reservedCountValue :
(reservationCountValue || 0));
// 转换supplyStatus字段值
let supplyStatusValue = product.supplyStatus || '';
if (['平台货源', '三方认证'].includes(supplyStatusValue)) {
supplyStatusValue = '现货';
} else if (supplyStatusValue === '三方未认证') {
supplyStatusValue = '预售';
}
// 处理图片URL,确保imageUrls字段存在且为数组
const imageUrls = product.imageUrls || product.images || [];
// 确保imageUrls是数组
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls];
return {
...product,
fullRegion: product.region || '', // 保存完整地区数据
region: product.region ? this.extractProvince(product.region) : '', // 只显示省份
grossWeight: grossWeightValue,
displayGrossWeight: this.formatGrossWeight(grossWeightValue, product.weight),
status: product.status || 'published',
createdAt: product.created_at || product.createTime || null,
reservedCount: finalReservationCount,
product_contact: product.product_contact || '',
contact_phone: product.contact_phone || '',
supplyStatus: supplyStatusValue,
sourceType: product.sourceType || '',
isReserved: false,
isFavorite: false,
currentImageIndex: 0,
// 确保imageUrls字段存在且为数组
imageUrls: formattedImageUrls
}
})
// 过滤掉hidden状态的商品
newGoods = newGoods.filter(item => {
const itemStatus = (item.status || '').toLowerCase()
return itemStatus !== 'hidden'
})
let updatedGoods = []
if (isLoadMore) {
// 加载更多:合并数据,但要去重
const existingIds = new Set(this.data.goods.map(item => item.id));
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id));
updatedGoods = [...this.data.goods, ...uniqueNewGoods];
} else {
updatedGoods = newGoods
}
// 应用筛选条件
const filteredGoods = this.applyFilters(updatedGoods)
// 计算是否还有更多数据
const totalGoods = res.total || 0;
const totalPages = res.totalPages || Math.ceil(totalGoods / currentPageSize);
const hasMoreData = currentPage < totalPages && newGoods.length > 0;
this.setData({
goods: updatedGoods,
filteredGoods: filteredGoods,
loadingMore: false,
page: currentPage + 1,
hasMoreData: hasMoreData,
hasMore: hasMoreData,
totalGoods: totalGoods,
totalPages: totalPages
})
} else {
this.setData({
loadingMore: false
})
}
})
.catch(err => {
console.error('加载商品数据失败:', err)
this.setData({
loadingMore: false
})
})
},
// 刷新商品列表
refreshGoodsList: function() {
this.setData({
page: 1,
hasMoreData: true,
goods: [],
filteredGoods: [],
loadingMore: false
}, () => {
this.loadGoods()
})
},
// 应用筛选条件
applyFilters: function(goods) {
let filtered = [...goods]
// 按品种筛选
if (this.data.selectedCategory !== '全部') {
const category = this.data.selectedCategory
let keyword = category
// 根据品种确定对应的关键字
if (category === '粉壳') {
keyword = '粉'
} else if (category === '绿壳') {
keyword = '绿'
} else if (category === '红壳') {
keyword = '红'
} else if (category === '白壳') {
keyword = '白'
}
filtered = filtered.filter(item => {
const name = item.name || ''
return name.includes(keyword)
})
}
// 按搜索关键词筛选(商品名称和地区都要匹配)
if (this.data.searchKeyword) {
const keyword = this.data.searchKeyword.toLowerCase()
filtered = filtered.filter(item => {
const name = item.name || ''
const region = item.region || ''
return name.toLowerCase().includes(keyword) || region.toLowerCase().includes(keyword)
})
}
// 按地区筛选
if (this.data.selectedRegion !== '全国') {
filtered = filtered.filter(item => {
return item.region === this.data.selectedRegion
})
}
// 优先排序:按收藏人数、价格、创建时间排序
filtered.sort((a, b) => {
// 首先按收藏人数降序排序
const reservedCountA = a.reservedCount || 0
const reservedCountB = b.reservedCount || 0
if (reservedCountB !== reservedCountA) {
return reservedCountB - reservedCountA
}
// 然后按价格升序排序
const priceA = parseFloat(a.price || 0)
const priceB = parseFloat(b.price || 0)
if (!isNaN(priceB) && !isNaN(priceA) && priceA !== priceB) {
return priceA - priceB
}
// 最后按创建时间降序排序
const createdAtA = new Date(a.createdAt || 0).getTime()
const createdAtB = new Date(b.createdAt || 0).getTime()
return createdAtB - createdAtA
})
return filtered
},
// 搜索输入
onSearchInput: function(e) {
this.setData({
searchKeyword: e.detail.value
})
},
// 搜索商品
searchGoods: function() {
// 重新显示tabBar
const app = getApp();
if (app && app.globalData) {
app.globalData.showTabBar = true;
}
const filteredGoods = this.applyFilters(this.data.goods)
this.setData({
filteredGoods: filteredGoods
})
},
// 切换地区选择器
toggleRegionPicker: function() {
this.setData({
showRegionPicker: !this.data.showRegionPicker
})
},
// 选择地区
selectRegion: function(e) {
const region = e.currentTarget.dataset.region
this.setData({
selectedRegion: region
})
},
// 确认地区选择
confirmRegion: function() {
// 重新显示tabBar
const app = getApp();
if (app && app.globalData) {
app.globalData.showTabBar = true;
}
const filteredGoods = this.applyFilters(this.data.goods)
this.setData({
filteredGoods: filteredGoods,
showRegionPicker: false
})
},
// 阻止事件冒泡
stopPropagation: function() {
// 空函数,用于阻止事件冒泡
},
// 选择品种
selectCategory: function(e) {
// 重新显示tabBar
const app = getApp();
if (app && app.globalData) {
app.globalData.showTabBar = true;
}
const category = e.currentTarget.dataset.category
this.setData({
selectedCategory: category
})
const filteredGoods = this.applyFilters(this.data.goods)
this.setData({
filteredGoods: filteredGoods
})
},
// 查看商品详情
viewGoodsDetail: function(e) {
const item = e.currentTarget.dataset.item
// 确保productId存在,优先使用id,其次使用productId
const productId = String(item.id || item.productId || '')
if (!productId) {
console.error('商品ID不存在,无法查看详情');
wx.showToast({
title: '商品信息有误',
icon: 'none',
duration: 2000
})
return;
}
// 将完整的商品数据传递给详情页,包含联系人信息,与buyer页面保持一致
wx.navigateTo({
url: `/pages/goods-detail/goods-detail?goodsData=${encodeURIComponent(JSON.stringify(item))}&productId=${productId}`
})
},
// 查看商品详情(与buyer页面保持一致的方法名)
showGoodsDetail: function (e) {
// 检查用户是否登录
const openid = wx.getStorageSync('openid');
const userId = wx.getStorageSync('userId');
if (!openid || !userId) {
console.log('用户未登录,直接显示授权登录弹窗');
// 直接显示授权登录弹窗
this.showOneKeyLogin();
return;
}
const goodsItem = e.currentTarget.dataset.item;
// 跳转到商品详情页面,并传递商品数据,使用encodeURIComponent编码JSON字符串
wx.navigateTo({
url: '/pages/goods-detail/goods-detail?goodsData=' + encodeURIComponent(JSON.stringify(goodsItem))
});
// 同时尝试直接更新tabBar的选中状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
const tabBar = this.getTabBar();
if (tabBar.setData) {
tabBar.setData({ show: false });
}
}
// 调用后端API执行商品联系人更新
console.log('开始调用API.updateProductContacts()');
API.updateProductContacts().then(function(res) {
console.log('商品联系人更新成功:', res);
}).catch(function(err) {
console.error('商品联系人更新失败:', err);
});
},
// 跳转到入驻页面
navigateToSettlement: function() {
wx.navigateTo({
url: '/pages/settlement/index'
})
},
// 预览图片
previewImage: function(e) {
// 阻止事件冒泡,避免触发商品点击事件
e.stopPropagation();
// 获取图片信息
const item = e.currentTarget.dataset.item;
const index = e.currentTarget.dataset.index;
if (!item) {
return;
}
// 确保图片URL存在且为数组
let imageUrls = item.imageUrls || item.images || [];
if (!Array.isArray(imageUrls)) {
imageUrls = [imageUrls];
}
// 过滤掉无效的图片URL
const validImageUrls = imageUrls.filter(url => url && typeof url === 'string' && url.trim() !== '');
if (validImageUrls.length === 0) {
return;
}
// 显示图片预览弹窗
this.setData({
previewImageUrls: validImageUrls,
previewImageIndex: parseInt(index || 0),
showImagePreview: true
});
},
// 关闭图片预览
closeImagePreview: function() {
this.setData({
showImagePreview: false
});
},
// 预览图片切换事件
onPreviewImageChange: function(e) {
this.setData({
previewImageIndex: e.detail.current
});
},
// 滚动事件处理
onScroll: function(e) {
// 获取滚动信息
const { scrollTop, scrollHeight, clientHeight } = e.detail;
// 显示/隐藏回到顶部按钮:当滚动距离超过500rpx时显示
if (scrollTop > 500) {
this.setData({
showBackToTop: true
});
} else {
this.setData({
showBackToTop: false
});
}
},
// 上拉加载更多
onReachBottom: function() {
if (this.data.hasMoreData && !this.data.loadingMore) {
this.loadGoods(true)
}
},
// 回到顶部功能
backToTop: function() {
wx.pageScrollTo({
scrollTop: 0,
duration: 300 // 动画持续时间,单位ms
});
},
// 切换图片
swiperChange(e) {
const current = e.detail.current
const itemId = e.currentTarget.dataset.itemId
// 更新对应商品项的currentImageIndex
this.setData({
[`filteredGoods[${itemId}].currentImageIndex`]: current
})
},
// 切换收藏状态
toggleFavorite(e) {
const goodsItem = e.currentTarget.dataset.item;
console.log('用户点击了收藏按钮,商品信息:', goodsItem);
// 检查用户登录状态
const openid = wx.getStorageSync('openid');
const userId = wx.getStorageSync('userId');
if (!openid || !userId) {
console.log('用户未登录,显示一键登录弹窗');
this.setData({
showOneKeyLoginModal: true
});
return;
}
// 获取商品ID
const productId = String(goodsItem.productId || goodsItem.id);
console.log('准备操作的商品ID:', productId);
// 根据当前收藏状态决定是添加还是取消收藏
if (goodsItem.isFavorite) {
this.cancelFavorite(e);
} else {
this.addFavorite(e);
}
},
// 添加收藏
addFavorite: function (e) {
const goodsItem = e.currentTarget.dataset.item;
console.log('用户点击了收藏按钮,商品信息:', goodsItem);
// 获取商品ID
const productId = String(goodsItem.productId || goodsItem.id);
console.log('准备收藏的商品ID:', productId);
wx.showLoading({ title: '正在收藏...' });
// 调用API添加收藏
API.addFavorite(productId)
.then(res => {
wx.hideLoading();
console.log('添加收藏成功:', res);
// 更新商品的收藏状态
this.updateGoodsFavoriteStatus(productId, true);
// 触发全局事件,通知其他页面收藏状态已更改
const app = getApp();
if (app.eventBus) {
app.eventBus.emit('favoriteChanged', {
productId: productId,
isFavorite: true
});
}
// 显示成功提示
wx.showToast({
title: '收藏成功',
icon: 'success',
duration: 1500
});
})
.catch(err => {
wx.hideLoading();
console.error('添加收藏失败:', err);
// 显示错误提示
wx.showToast({
title: '收藏失败,请稍后重试',
icon: 'none',
duration: 2000
});
});
},
// 取消收藏
cancelFavorite: function (e) {
const goodsItem = e.currentTarget.dataset.item;
console.log('用户点击了取消收藏按钮,商品信息:', goodsItem);
// 获取商品ID
const productId = String(goodsItem.productId || goodsItem.id);
console.log('准备取消收藏的商品ID:', productId);
wx.showLoading({ title: '正在取消收藏...' });
// 调用API取消收藏
API.cancelFavorite(productId)
.then(res => {
wx.hideLoading();
console.log('取消收藏成功:', res);
// 更新商品的收藏状态
this.updateGoodsFavoriteStatus(productId, false);
// 触发全局事件,通知其他页面收藏状态已更改
const app = getApp();
if (app.eventBus) {
app.eventBus.emit('favoriteChanged', {
productId: productId,
isFavorite: false
});
}
// 显示成功提示
wx.showToast({
title: '取消收藏成功',
icon: 'success',
duration: 1500
});
})
.catch(err => {
wx.hideLoading();
console.error('取消收藏失败:', err);
// 显示错误提示
wx.showToast({
title: '取消收藏失败,请稍后重试',
icon: 'none',
duration: 2000
});
});
},
// 更新商品收藏状态
updateGoodsFavoriteStatus: function (productId, isFavorite) {
// 找到商品在列表中的索引
const goodsIndex = this.data.filteredGoods.findIndex(item =>
String(item.id) === productId || String(item.productId) === productId
);
if (goodsIndex !== -1) {
// 更新商品的收藏状态
const updateData = {};
updateData[`filteredGoods[${goodsIndex}].isFavorite`] = isFavorite;
this.setData(updateData);
// 同时更新原始goods数组
const originalIndex = this.data.goods.findIndex(item =>
String(item.id) === productId || String(item.productId) === productId
);
if (originalIndex !== -1) {
updateData[`goods[${originalIndex}].isFavorite`] = isFavorite;
this.setData(updateData);
}
}
},
3 months ago
// 显示一键登录弹窗
showOneKeyLogin() {
this.setData({
showAuthModal: false,
showOneKeyLoginModal: true
})
},
// 关闭未授权提示弹窗
closeAuthModal() {
this.setData({ showAuthModal: false })
},
// 关闭一键登录弹窗
closeOneKeyLoginModal() {
this.setData({ showOneKeyLoginModal: false })
},
// 分享给朋友/群聊
onShareAppMessage() {
return {
title: '鸡蛋贸易平台 - 专业的鸡蛋交易小程序',
path: '/pages/index/index',
imageUrl: '/images/你有好蛋.png'
}
},
// 分享到朋友圈
onShareTimeline() {
return {
title: '鸡蛋贸易平台 - 专业的鸡蛋交易小程序',
query: '',
imageUrl: '/images/你有好蛋.png'
}
},
3 months ago
// 处理手机号授权
async onGetPhoneNumber(e) {
// 打印详细错误信息,方便调试
console.log('getPhoneNumber响应:', e.detail)
// 关闭手机号授权弹窗
this.setData({ showOneKeyLoginModal: false })
// 用户点击拒绝授权
if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
wx.showToast({
title: '需要授权手机号才能使用',
icon: 'none',
duration: 2000
})
return
}
// 处理没有权限的情况
if (e.detail.errMsg === 'getPhoneNumber:fail no permission') {
// 如果是测试模式,跳过真实授权流程
if (this.data.testMode) {
console.log('进入测试模式,跳过真实手机号授权')
await this.simulateLoginForTest()
return
}
wx.showToast({
title: '当前环境无法获取手机号权限',
icon: 'none',
duration: 3000
})
// 增加关于微信认证要求的说明
console.warn('获取手机号权限失败: 请注意,微信小程序获取手机号功能需要满足以下条件:1. 小程序必须完成微信企业认证;2. 需要在小程序后台配置相应权限;3. 必须使用button组件的open-type="getPhoneNumber"触发。')
return
}
// 检查是否已经登录,避免重复授权
const existingOpenid = wx.getStorageSync('openid')
const existingUserId = wx.getStorageSync('userId')
const existingUserInfo = wx.getStorageSync('userInfo')
if (existingOpenid && existingUserId && existingUserInfo && existingUserInfo.phoneNumber) {
3 months ago
console.log('用户已登录且手机号有效,直接完成身份设置')
// 直接完成身份设置,跳过重复授权
const currentUserType = this.data.pendingUserType || this.data.currentUserType || 'buyer'
this.finishSetUserType(currentUserType)
return
}
wx.showLoading({
title: '登录中...',
mask: true
})
// 引入API服务
const API = require('../../utils/api.js')
try {
if (e.detail.errMsg === 'getPhoneNumber:ok') {
// 用户同意授权,实际处理授权流程
console.log('用户同意授权获取手机号')
// 同时请求位置授权
console.log('同时请求位置授权');
wx.authorize({
scope: 'scope.userLocation',
success() {
// 位置授权成功,获取用户位置
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('登录时获取位置成功:', { latitude, longitude });
// 存储位置信息到本地
wx.setStorageSync('userLocation', { latitude, longitude });
// 位置获取成功提示
wx.showToast({
title: '位置获取成功',
icon: 'success',
duration: 1500
});
},
fail() {
console.error('登录时获取位置失败');
// 位置获取失败提示
wx.showToast({
title: '位置获取失败',
icon: 'none',
duration: 1500
});
}
});
},
fail() {
// 位置授权失败,不影响登录流程
console.log('登录时位置授权被拒绝');
// 位置授权失败提示
wx.showToast({
title: '位置授权已拒绝',
icon: 'none',
duration: 1500
});
}
});
3 months ago
// 1. 先执行微信登录获取code
const loginRes = await new Promise((resolve, reject) => {
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;
console.log('openidRes完整响应:', JSON.stringify(openidRes));
if (openidRes && typeof openidRes === 'object') {
// 适配服务器返回格式:{success: true, code: 200, message: '获取openid成功', data: {openid, userId}}
if (openidRes.data && typeof openidRes.data === 'object') {
console.log('识别到标准服务器返回格式,从data字段提取信息');
openid = openidRes.data.openid || openidRes.data.OpenID || null;
userId = openidRes.data.userId || null;
} else {
// 尝试从响应对象中直接提取openid,适配其他可能的格式
console.log('尝试从根对象直接提取openid');
openid = openidRes.openid || openidRes.OpenID || null;
userId = openidRes.userId || null;
}
}
if (!openid) {
console.error('无法从服务器响应中提取openid,完整响应:', JSON.stringify(openidRes));
// 增加更友好的错误信息,指导用户检查服务器配置
throw new Error(`获取openid失败: 服务器返回数据格式可能不符合预期,请检查服务器配置。响应数据为: ${JSON.stringify(openidRes)}`);
}
console.log('获取openid成功:', openid)
// 3. 存储openid和session_key
wx.setStorageSync('openid', openid)
// 从服务器返回中获取session_key
if (openidRes && openidRes.session_key) {
wx.setStorageSync('sessionKey', openidRes.session_key)
} else if (openidRes && openidRes.data && openidRes.data.session_key) {
wx.setStorageSync('sessionKey', openidRes.data.session_key)
}
// 优先使用从服务器响应data字段中提取的userId
if (userId) {
wx.setStorageSync('userId', userId)
console.log('使用从服务器data字段提取的userId:', userId)
} else if (openidRes && openidRes.userId) {
wx.setStorageSync('userId', openidRes.userId)
console.log('使用服务器根对象中的userId:', openidRes.userId)
} else {
// 生成临时userId
const tempUserId = 'user_' + Date.now()
wx.setStorageSync('userId', tempUserId)
console.log('生成临时userId:', tempUserId)
}
// 4. 上传手机号加密数据到服务器解密
const phoneData = {
...e.detail,
openid: openid
}
console.log('准备上传手机号加密数据到服务器')
const phoneRes = await API.uploadPhoneNumberData(phoneData)
// 改进手机号解密结果的处理逻辑
if (!phoneRes || (!phoneRes.success && !phoneRes.phoneNumber)) {
// 如果服务器返回格式不标准但包含手机号,也接受
if (phoneRes && phoneRes.phoneNumber) {
console.warn('服务器返回格式可能不符合预期,但成功获取手机号');
} else {
throw new Error('获取手机号失败: ' + (phoneRes && phoneRes.message ? phoneRes.message : '未知错误'))
}
}
// 检查是否有手机号冲突
const hasPhoneConflict = phoneRes.phoneNumberConflict || false
const isNewPhone = phoneRes.isNewPhone || true
const phoneNumber = phoneRes.phoneNumber || null
// 如果有手机号冲突且没有返回手机号,使用实际返回的手机号
const finalPhoneNumber = phoneNumber
3 months ago
console.log('手机号解密结果:', {
phoneNumber: finalPhoneNumber,
hasPhoneConflict: hasPhoneConflict,
isNewPhone: isNewPhone
})
// 5. 获取用户微信名称和头像
let userProfile = null;
try {
userProfile = await new Promise((resolve, reject) => {
wx.getUserProfile({
desc: '用于完善会员资料',
success: resolve,
fail: reject
});
});
console.log('获取用户信息成功:', userProfile);
} catch (err) {
console.warn('获取用户信息失败:', err);
// 如果获取失败,使用默认值
}
// 6. 创建用户信息
const tempUserInfo = {
name: userProfile ? (userProfile.userInfo.name || userProfile.userInfo.nickName) : '微信用户',
// 获取微信头像失败时使用微信默认头像,而不是本地头像
avatarUrl: userProfile ? userProfile.userInfo.avatarUrl : 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
3 months ago
gender: userProfile ? userProfile.userInfo.gender : 0,
country: userProfile ? userProfile.userInfo.country : '',
province: userProfile ? userProfile.userInfo.province : '',
city: userProfile ? userProfile.userInfo.city : '',
language: userProfile ? userProfile.userInfo.language : 'zh_CN',
phoneNumber: finalPhoneNumber
}
// 从本地存储获取userId(使用已声明的变量)
const storedUserId = wx.getStorageSync('userId')
// 优先使用用户之前选择的身份类型,如果没有则尝试获取已存储的或默认为买家
const users = wx.getStorageSync('users') || {}
const currentUserType = this.data.pendingUserType || this.data.currentUserType ||
(users[storedUserId] && users[storedUserId].type ? users[storedUserId].type : 'buyer')
console.log('用户身份类型:', currentUserType)
// 清除临时存储的身份类型
if (this.data.pendingUserType) {
this.setData({ pendingUserType: null })
}
// 保存用户信息并等待上传完成
console.log('开始保存用户信息并上传到服务器...')
const uploadResult = await this.saveUserInfo(tempUserInfo, currentUserType)
console.log('用户信息保存并上传完成')
wx.hideLoading()
// 根据服务器返回的结果显示不同的提示
if (uploadResult && uploadResult.phoneNumberConflict) {
wx.showModal({
title: '登录成功',
content: '您的手机号已被其他账号绑定',
showCancel: false,
confirmText: '我知道了',
success(res) {
if (res.confirm) {
console.log('用户点击了我知道了');
}
}
});
3 months ago
} else {
wx.showModal({
title: '登录成功',
content: '欢迎使用鸡蛋贸易平台',
showCancel: false,
confirmText: '开始使用',
success(res) {
if (res.confirm) {
console.log('用户点击了开始使用');
}
}
});
3 months ago
}
// 用户登录成功,但已移除类型选择和跳转功能
3 months ago
} else {
// 用户拒绝授权或其他情况
console.log('手机号授权失败:', e.detail.errMsg)
// 不再抛出错误,而是显示友好的提示
wx.hideLoading()
wx.showToast({
title: '需要授权手机号才能使用',
icon: 'none',
duration: 2000
})
return
}
} catch (error) {
wx.hideLoading()
console.error('登录过程中发生错误:', error)
// 更具体的错误提示
let errorMsg = '登录失败,请重试'
if (error.message.includes('网络')) {
errorMsg = '网络连接失败,请检查网络后重试'
} else if (error.message.includes('服务器')) {
errorMsg = '服务器连接失败,请稍后重试'
}
wx.showToast({
title: errorMsg,
icon: 'none',
duration: 3000
})
// 清除可能已经保存的不完整信息
try {
wx.removeStorageSync('openid')
wx.removeStorageSync('sessionKey')
wx.removeStorageSync('userId')
} catch (e) {
console.error('清除临时登录信息失败:', e)
}
}
},
// 处理用户基本信息授权
handleUserAuth(type) {
// 保存当前用户类型
this.setData({
currentUserType: type
})
// 先执行微信登录
this.doWechatLogin(type)
},
// 测试模式下模拟登录流程
async simulateLoginForTest() {
wx.showLoading({
title: '测试模式登录中...',
mask: true
})
try {
// 1. 模拟微信登录,生成测试用的code
const mockCode = 'test_code_' + Date.now()
console.log('模拟获取登录code:', mockCode)
// 2. 模拟获取openid和userId
const mockOpenid = 'test_openid_' + Date.now()
const mockUserId = 'test_user_' + Date.now()
console.log('模拟获取openid:', mockOpenid)
console.log('模拟获取userId:', mockUserId)
// 3. 存储测试数据
wx.setStorageSync('openid', mockOpenid)
wx.setStorageSync('userId', mockUserId)
// 4. 模拟手机号解密结果
const mockPhoneNumber = null
3 months ago
console.log('模拟手机号解密成功:', mockPhoneNumber)
// 5. 创建模拟用户信息
const mockUserInfo = {
name: '测试用户',
3 months ago
avatarUrl: this.data.avatarUrl,
gender: 0,
country: '测试国家',
province: '测试省份',
city: '测试城市',
language: 'zh_CN',
phoneNumber: mockPhoneNumber
}
// 6. 获取用户身份类型(优先使用pendingUserType)
const userId = wx.getStorageSync('userId')
const users = wx.getStorageSync('users') || {}
const currentUserType = this.data.pendingUserType || this.data.currentUserType ||
(users[userId] && users[userId].type ? users[userId].type : 'buyer')
console.log('测试模式用户身份类型:', currentUserType)
// 7. 清除临时存储的身份类型
if (this.data.pendingUserType) {
this.setData({ pendingUserType: null })
}
// 8. 保存用户信息并等待上传完成
console.log('测试模式开始保存用户信息...')
// 在测试模式下也会上传用户信息到服务器,用于连通性测试
await this.saveUserInfo(mockUserInfo, currentUserType)
console.log('测试模式用户信息保存完成')
wx.hideLoading()
// 9. 显示成功提示
wx.showToast({
title: '测试模式登录成功',
icon: 'success',
duration: 2000
})
// 测试登录成功,但已移除类型选择和跳转功能
3 months ago
} catch (error) {
wx.hideLoading()
console.error('测试模式登录过程中发生错误:', error)
wx.showToast({
title: '测试模式登录失败',
icon: 'none',
duration: 2000
})
}
},
// 执行微信登录并获取openid
async doWechatLogin(type) {
// 显示加载提示
wx.showLoading({
title: '登录中...',
mask: true
})
try {
// 调用微信登录接口
const loginRes = await new Promise((resolve, reject) => {
wx.login({
success: resolve,
fail: reject
})
})
if (loginRes.code) {
console.log('微信登录成功,code:', loginRes.code)
// 保存登录凭证
try {
wx.setStorageSync('loginCode', loginRes.code)
} catch (e) {
console.error('保存登录凭证失败:', e)
}
// 引入API服务
const API = require('../../utils/api.js')
// 发送code和用户类型到服务器换取openid和session_key
try {
const openidRes = await API.getOpenid(loginRes.code, type)
console.log('获取openid响应:', openidRes)
// 增强版响应处理逻辑,支持多种返回格式
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);
const serverMessage = openidRes && (openidRes.message || openidRes.msg);
if (isSuccess && !openid) {
console.warn('服务器返回成功状态,但未包含有效的openid:', openidRes);
}
// 打印获取到的信息,方便调试
console.log('提取到的登录信息:', { openid, userId, sessionKey, serverMessage });
if (openid) {
// 存储openid和session_key
wx.setStorageSync('openid', openid)
if (sessionKey) {
wx.setStorageSync('sessionKey', sessionKey)
}
// 如果有userId,也存储起来
if (userId) {
wx.setStorageSync('userId', userId)
}
console.log('获取openid成功并存储:', openid)
// 验证登录状态并获取用户信息
await this.validateLoginAndGetUserInfo(openid)
} else {
// 即使没有获取到openid,也要继续用户信息授权流程
console.warn('未获取到有效的openid,但继续用户信息授权流程:', openidRes);
// 设置一个临时的openid以便继续流程
wx.setStorageSync('openid', 'temp_' + Date.now())
}
} catch (error) {
console.error('获取openid失败:', error)
// 即使获取openid失败,也继续用户信息授权流程
}
// 继续用户信息授权流程,等待完成
await this.processUserInfoAuth(type)
} else {
wx.hideLoading()
console.error('微信登录失败:', loginRes)
wx.showToast({
title: '登录失败,请重试',
icon: 'none',
duration: 2000
})
}
} catch (err) {
wx.hideLoading()
console.error('wx.login失败:', err)
wx.showToast({
title: '获取登录状态失败',
icon: 'none',
duration: 2000
})
}
},
// 验证登录状态并获取用户信息
async validateLoginAndGetUserInfo(openid) {
try {
// 引入API服务
const API = require('../../utils/api.js')
// 调用服务器验证登录状态
const validateRes = await API.validateUserLogin()
if (validateRes.success && validateRes.userInfo) {
// 服务器返回了用户信息,同步到本地
const app = getApp()
const userInfo = validateRes.userInfo
// 更新全局用户信息
app.globalData.userInfo = userInfo
// 存储用户信息到本地
wx.setStorageSync('userInfo', userInfo)
console.log('验证登录状态成功,用户信息已同步:', userInfo)
// 如果手机号存在则清除重新授权标志
if (userInfo.phoneNumber) {
3 months ago
// 清除可能存在的重新授权标志
wx.removeStorageSync('needPhoneAuth')
console.log('手机号验证通过:', userInfo.phoneNumber)
}
return true
} else {
console.warn('服务器验证失败,可能是新用户或登录状态无效')
return false
}
} catch (error) {
console.error('验证登录状态失败:', error)
// 如果验证失败,清除可能存在的无效登录信息
try {
wx.removeStorageSync('openid')
wx.removeStorageSync('userId')
wx.removeStorageSync('userInfo')
} catch (e) {
console.error('清除无效登录信息失败:', e)
}
return false
}
},
// 处理用户信息授权
async processUserInfoAuth(type) {
const app = getApp()
// 如果已经有用户信息,不需要再进行跳转
3 months ago
if (app.globalData.userInfo) {
wx.hideLoading()
return
}
// 优化:首次登录时自动创建临时用户信息并完成登录,不再需要用户填写表单
// 获取已存储的userId或生成新的
let userId = wx.getStorageSync('userId')
if (!userId) {
userId = 'user_' + Date.now()
wx.setStorageSync('userId', userId)
}
// 创建临时用户信息
const tempUserInfo = {
name: '微信用户',
3 months ago
avatarUrl: this.data.avatarUrl,
gender: 0,
country: '',
province: '',
city: '',
language: 'zh_CN'
}
try {
// 保存临时用户信息并完成登录,等待数据上传完成
await this.saveUserInfo(tempUserInfo, type)
// 隐藏加载提示
wx.hideLoading()
// 数据上传完成,但已移除类型选择和跳转功能
3 months ago
} catch (error) {
console.error('处理用户信息授权失败:', error)
wx.hideLoading()
wx.showToast({
title: '登录失败,请重试',
icon: 'none',
duration: 2000
})
}
},
// 保存用户信息
async saveUserInfo(userInfo, type) {
// 确保userId存在
let userId = wx.getStorageSync('userId')
if (!userId) {
userId = 'user_' + Date.now()
wx.setStorageSync('userId', userId)
}
// 保存用户信息到本地存储 - 修复首次获取问题
let users = wx.getStorageSync('users')
// 如果users不存在或不是对象,初始化为空对象
if (!users || typeof users !== 'object') {
users = {}
}
// 初始化用户信息
users[userId] = users[userId] || {}
users[userId].info = userInfo
users[userId].type = type
// 确保存储操作成功
try {
wx.setStorageSync('users', users)
console.log('用户信息已成功保存到本地存储')
} catch (e) {
console.error('保存用户信息到本地存储失败:', e)
}
// 保存用户信息到全局变量
const app = getApp()
app.globalData.userInfo = userInfo
app.globalData.userType = type
console.log('用户信息已保存到全局变量:', userInfo)
// 额外保存一份单独的userInfo到本地存储,便于checkPhoneAuthSetting方法检查
try {
wx.setStorageSync('userInfo', userInfo)
console.log('单独的userInfo已保存')
} catch (e) {
console.error('保存单独的userInfo失败:', e)
}
// 上传用户信息到服务器
// 在测试模式下也上传用户信息,用于连通性测试
console.log('准备上传用户信息到服务器进行测试...')
// 确保测试数据包含服务器所需的所有字段
const completeUserInfo = {
...userInfo,
// 确保包含服务器需要的必要字段
name: userInfo.name || '测试用户'
3 months ago
}
try {
const uploadResult = await this.uploadUserInfoToServer(completeUserInfo, userId, type)
console.log('用户信息上传到服务器成功')
return uploadResult // 返回上传结果
} catch (error) {
console.error('用户信息上传到服务器失败:', error)
// 显示友好的提示,但不中断流程
wx.showToast({
title: '测试数据上传失败,不影响使用',
icon: 'none',
duration: 2000
})
// 不再抛出错误,而是返回默认成功结果,确保登录流程继续
return {
success: true,
message: '本地登录成功,服务器连接失败'
}
}
},
// 处理头像选择
onChooseAvatar(e) {
const { avatarUrl } = e.detail
this.setData({
avatarUrl
})
},
// 处理昵称提交
getUserName(e) {
const { name } = e.detail.value
3 months ago
const type = this.data.currentUserType
if (!name) {
3 months ago
wx.showToast({
title: '请输入昵称',
icon: 'none',
duration: 2000
})
return
}
// 创建用户信息对象
const userInfo = {
name: name,
3 months ago
avatarUrl: this.data.avatarUrl,
// 其他可能需要的字段
gender: 0,
country: '',
province: '',
city: '',
language: 'zh_CN'
}
// 保存用户信息
this.saveUserInfo(userInfo, type)
// 隐藏表单
this.setData({
showUserInfoForm: false
})
// 已移除类型选择和跳转功能
3 months ago
},
// 取消用户信息表单
cancelUserInfoForm() {
this.setData({
showUserInfoForm: false
})
wx.hideLoading()
},
// 检查本地缓存并恢复登录状态
checkAndRestoreLoginStatus() {
console.log('开始检查并恢复登录状态')
const app = getApp()
// 从本地存储获取用户信息
const localUserInfo = wx.getStorageSync('userInfo') || {}
const userId = wx.getStorageSync('userId')
const openid = wx.getStorageSync('openid')
console.log('恢复登录状态 - userId:', userId, 'openid:', openid ? '已获取' : '未获取')
// 优先使用全局用户信息,如果没有则使用本地存储的用户信息
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
needPhoneAuth: !app.globalData.userInfo.phoneNumber
})
} else {
app.globalData.userInfo = localUserInfo
this.setData({
userInfo: localUserInfo,
needPhoneAuth: !localUserInfo.phoneNumber
})
}
if (userId && openid) {
// 确保users存储结构存在
let users = wx.getStorageSync('users')
if (!users) {
users = {}
wx.setStorageSync('users', users)
}
if (!users[userId]) {
users[userId] = { type: '' }
wx.setStorageSync('users', users)
}
// 先显示本地存储的用户类型,但会被服务器返回的最新值覆盖
const user = users[userId]
const currentType = user.type
this.setData({ currentUserType: currentType })
console.log('恢复登录状态 - 当前本地存储的用户类型:', currentType)
// 从服务器获取最新的用户信息,确保身份由数据库决定
this.refreshUserInfoFromServer(openid, userId)
} else {
console.log('未找到有效的本地登录信息')
}
},
// 从服务器刷新用户信息并同步身份数据
refreshUserInfoFromServer(openid, userId) {
const API = require('../../utils/api.js')
API.getUserInfo(openid).then(res => {
console.log('从服务器获取用户信息成功:', res)
if (res.success && res.data) {
const serverUserInfo = res.data
// 更新本地用户信息
const app = getApp()
const updatedUserInfo = {
...app.globalData.userInfo,
...serverUserInfo
}
app.globalData.userInfo = updatedUserInfo
wx.setStorageSync('userInfo', updatedUserInfo)
// 设置用户入驻状态
this.setData({
userInfo: updatedUserInfo,
partnerstatus: serverUserInfo.partnerstatus || ''
})
// 同步更新用户身份信息(当前身份由数据库决定)
if (serverUserInfo.type) {
this.syncUserTypeFromServer(userId, serverUserInfo.type)
}
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type)
}
}).catch(err => {
console.error('从服务器获取用户信息失败:', err)
// 如果getUserInfo失败,尝试使用validateUserLogin作为备选
API.validateUserLogin().then(res => {
console.log('使用validateUserLogin获取用户信息成功:', res)
if (res.success && res.data) {
const serverUserInfo = res.data
// 更新本地用户信息
const app = getApp()
const updatedUserInfo = {
...app.globalData.userInfo,
...serverUserInfo
}
app.globalData.userInfo = updatedUserInfo
wx.setStorageSync('userInfo', updatedUserInfo)
// 设置用户入驻状态
this.setData({
userInfo: updatedUserInfo,
partnerstatus: serverUserInfo.partnerstatus || ''
})
// 同步更新用户身份信息(当前身份由数据库决定)
if (serverUserInfo.type) {
this.syncUserTypeFromServer(userId, serverUserInfo.type)
}
console.log('用户信息已更新(备选方案):', updatedUserInfo)
}
}).catch(validateErr => {
console.error('从服务器获取用户信息失败(包括备选方案):', validateErr)
// 如果服务器请求失败,继续使用本地缓存的信息
})
})
},
// 从服务器同步用户身份信息
syncUserTypeFromServer(userId, serverType) {
if (!userId || !serverType) {
console.error('同步用户身份信息失败: 参数不完整')
return
}
console.log('从服务器同步用户身份信息:', { userId, serverType })
// 更新本地存储的用户身份
let users = wx.getStorageSync('users') || {}
if (!users[userId]) {
users[userId] = {}
}
// 移除serverType中的customer(如果存在)
let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '')
// 构建新的用户类型
let newUserType = processedServerType
// 只有当新构建的用户类型与本地不同时才更新
if (users[userId].type !== newUserType) {
users[userId].type = newUserType
wx.setStorageSync('users', users)
// 更新全局用户类型
const app = getApp()
app.globalData.userType = newUserType
console.log('用户身份已从服务器同步并保留客服标识:', newUserType)
} else {
console.log('用户身份与服务器一致,无需更新:', newUserType)
}
},
3 months ago
// 上传用户信息到服务器
async uploadUserInfoToServer(userInfo, userId, type) {
// 引入API服务
const API = require('../../utils/api.js')
// 获取openid
const openid = wx.getStorageSync('openid')
// 构造上传数据(包含openid和session_key)
const uploadData = {
userId: userId,
openid: openid,
...userInfo,
type: type,
timestamp: Date.now()
}
// 调用API上传用户信息并返回Promise
try {
const res = await API.uploadUserInfo(uploadData)
console.log('用户信息上传成功:', res)
return res
} catch (err) {
console.error('用户信息上传失败:', err)
// 不再抛出错误,而是返回默认成功结果,确保登录流程继续
// 这样即使服务器连接失败,本地登录也能完成
return {
success: true,
message: '本地登录成功,服务器连接失败'
}
}
},
// 处理手机号授权结果(已重命名为onPhoneNumberResult,此方法已废弃)
processPhoneAuthResult: function () {
console.warn('processPhoneAuthResult方法已废弃,请使用onPhoneNumberResult方法')
},
// 手机号授权处理
async onPhoneNumberResult(e) {
console.log('手机号授权结果:', e)
if (e.detail.errMsg === 'getPhoneNumber:ok') {
// 用户同意授权,获取加密数据
const phoneData = e.detail
wx.showLoading({ title: '获取手机号中...' })
try {
// 引入API服务
const API = require('../../utils/api.js')
// 上传到服务器解密
const res = await API.uploadPhoneNumberData(phoneData)
wx.hideLoading()
if (res.success && res.phoneNumber) {
console.log('获取手机号成功:', res.phoneNumber)
// 保存手机号到用户信息
const app = getApp()
const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {}
userInfo.phoneNumber = res.phoneNumber
// 更新本地和全局用户信息
app.globalData.userInfo = userInfo
wx.setStorageSync('userInfo', userInfo)
// 获取userId
const userId = wx.getStorageSync('userId')
const users = wx.getStorageSync('users') || {}
const currentUserType = users[userId] && users[userId].type ? users[userId].type : ''
// 同时更新服务器用户信息,确保上传完成
console.log('开始更新服务器用户信息...')
if (!this.data.testMode) {
await this.uploadUserInfoToServer(userInfo, userId, currentUserType)
console.log('服务器用户信息更新完成')
} else {
console.log('测试模式下跳过服务器用户信息更新')
}
wx.showToast({ title: '手机号绑定成功', icon: 'success' })
} else {
console.error('获取手机号失败:', res)
wx.showToast({ title: '获取手机号失败', icon: 'none' })
}
} catch (err) {
wx.hideLoading()
console.error('获取手机号失败:', err)
wx.showToast({ title: '获取手机号失败', icon: 'none' })
}
} else {
console.log('用户拒绝授权手机号')
}
},
3 months ago
})