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.
99 lines
2.8 KiB
99 lines
2.8 KiB
|
1 week ago
|
// 测试脚本:验证登录时用户类型是否被覆盖
|
||
|
|
// 运行方法:在微信开发者工具的控制台中执行
|
||
|
|
|
||
|
|
// 模拟删除缓存
|
||
|
|
function clearCache() {
|
||
|
|
console.log('=== 开始模拟删除缓存 ===');
|
||
|
|
try {
|
||
|
|
wx.removeStorageSync('userInfo');
|
||
|
|
wx.removeStorageSync('users');
|
||
|
|
wx.removeStorageSync('userId');
|
||
|
|
wx.removeStorageSync('openid');
|
||
|
|
wx.removeStorageSync('sessionKey');
|
||
|
|
wx.removeStorageSync('phoneNumber');
|
||
|
|
wx.removeStorageSync('userType');
|
||
|
|
console.log('✅ 缓存删除成功');
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 缓存删除失败:', e);
|
||
|
|
}
|
||
|
|
console.log('=== 缓存删除完成 ===');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 模拟登录流程
|
||
|
|
async function simulateLogin() {
|
||
|
|
console.log('=== 开始模拟登录流程 ===');
|
||
|
|
|
||
|
|
// 模拟获取登录code
|
||
|
|
const loginRes = await new Promise((resolve) => {
|
||
|
|
wx.login({
|
||
|
|
success: resolve
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('✅ 获取登录code成功:', loginRes.code);
|
||
|
|
|
||
|
|
// 模拟获取openid
|
||
|
|
const API = require('./utils/api.js');
|
||
|
|
const openidRes = await API.getOpenid(loginRes.code);
|
||
|
|
console.log('✅ 获取openid成功:', openidRes);
|
||
|
|
|
||
|
|
const openid = openidRes.data?.openid || openidRes.openid;
|
||
|
|
const userId = openidRes.data?.userId || openidRes.userId;
|
||
|
|
|
||
|
|
console.log('提取到的openid:', openid);
|
||
|
|
console.log('提取到的userId:', userId);
|
||
|
|
|
||
|
|
// 存储openid和userId
|
||
|
|
wx.setStorageSync('openid', openid);
|
||
|
|
if (userId) {
|
||
|
|
wx.setStorageSync('userId', userId);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('=== 登录流程模拟完成 ===');
|
||
|
|
return { openid, userId };
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试用户类型是否被覆盖
|
||
|
|
async function testUserType() {
|
||
|
|
console.log('=== 开始测试用户类型是否被覆盖 ===');
|
||
|
|
|
||
|
|
// 1. 清除缓存
|
||
|
|
clearCache();
|
||
|
|
|
||
|
|
// 2. 模拟登录
|
||
|
|
const { openid, userId } = await simulateLogin();
|
||
|
|
|
||
|
|
// 3. 模拟获取手机号(实际登录时会有授权流程)
|
||
|
|
const phoneNumber = '13800138000'; // 模拟手机号
|
||
|
|
wx.setStorageSync('phoneNumber', phoneNumber);
|
||
|
|
|
||
|
|
// 4. 模拟用户信息
|
||
|
|
const userInfo = {
|
||
|
|
name: '测试用户',
|
||
|
|
avatarUrl: 'https://example.com/avatar.jpg',
|
||
|
|
phoneNumber: phoneNumber
|
||
|
|
};
|
||
|
|
|
||
|
|
// 5. 模拟用户类型(空值,模拟删除缓存后的情况)
|
||
|
|
const currentUserType = '';
|
||
|
|
|
||
|
|
// 6. 调用uploadUserInfoToServer
|
||
|
|
const page = getCurrentPages()[0];
|
||
|
|
if (page && page.uploadUserInfoToServer) {
|
||
|
|
console.log('=== 调用uploadUserInfoToServer ===');
|
||
|
|
console.log('传入的用户类型:', currentUserType);
|
||
|
|
|
||
|
|
const result = await page.uploadUserInfoToServer(userInfo, userId || 'test_user_id', currentUserType);
|
||
|
|
console.log('上传结果:', result);
|
||
|
|
} else {
|
||
|
|
console.error('❌ 无法找到uploadUserInfoToServer方法');
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('=== 测试完成 ===');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
testUserType().catch(err => {
|
||
|
|
console.error('测试失败:', err);
|
||
|
|
});
|