小程序
小游戏
企业微信
微信支付
扫描小程序码分享
https://developers.weixin.qq.com/s/DfX1SjmE74YA
上面链接是代码片段。
在本地使用phpstudy搭建的环境,后台使用wordpress,在函数中注册了几个端点。调试的过程中始终报错,未返回有效的openid,实际上在network中可以确定openid端口的状态码是200,也能看到返回的openid,我是新手,希望大佬给指教下,问题出现在哪里?
下面是调试窗口:
5 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
排查下这里 打印下
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
你这是在前端获取openid?
获取openid的接口需要在服务端调用,不支持在前端调用。
代码不是你写的?
不方便导入代码片段的大佬,可以看这里:
const app = getApp(); App({ globalData: { userInfo: null, // 当前用户信息 apiBase: 'http://localhost/new' // 统一API地址 }, onLaunch() { this.checkUserLogin(); // 全局错误捕获 wx.onError(function (error) { console.error('全局捕获的错误:', error); wx.showToast({ title: '程序异常,请重试', icon: 'none' }); }); }, // 检查用户登录状态 async checkUserLogin() { try { const loginRes = await wx.login(); const code = loginRes.code; console.log('%c[DEBUG]wx.login返回的code:','color:orange', code); if (!code) { throw new Error('无法获取 code'); } const openid = await this.getOpenid(code); if (!openid) { throw new Error('无法获取 openid'); } const userRes = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/user/check?openid=${openid}`, method: 'GET', header: { 'content-type': 'application/json' } }); console.log('[DEBUG]/user/check响应:', userRes); if (userRes.statusCode === 200 && userRes.data) { if (userRes.data.isRegistered) { this.globalData.userInfo = { openid, name: userRes.data.name }; } else { await this.showRegisterDialog(openid); } } else { throw new Error('用户检查失败'); } } catch (error) { console.error('登录检查失败:', error); // 如果已获取 openid,则尝试注册 if (this.globalData.openid) { try { await this.showRegisterDialog(this.globalData.openid); } catch (registerError) { console.error('注册过程中发生错误:', registerError); wx.showToast({ title: '注册失败,请重试', icon: 'none' }); } } else { wx.showToast({ title: '登录失败,请重试', icon: 'none' }); } } }, // 获取 openid async getOpenid(code) { try { const res = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/auth/openid`, method: 'POST', header: { 'content-type': 'application/json' }, data: { code } }); console.log('%c[DEBUG]auth/openid响应:','color:green', res); if (res.statusCode === 200 && res.data && res.data.openid) { const openid = res.data.openid; this.globalData.openid = openid; // 将 openid 存储到全局变量 return openid; } else { throw new Error(res.data?.errmsg || '未返回有效 openid'); } } catch (error) { console.error('获取 openid 失败:', error); wx.showToast({ title: '获取 openid 失败', icon: 'none' }); throw error; } }, // 显示注册弹窗 async showRegisterDialog(openid) { try { const name = await this.getUserName(); if (!name) { throw new Error('未输入姓名'); } await this.registerUser(openid, name); wx.showToast({ title: '注册成功', icon: 'none' }); this.globalData.userInfo = { openid, name }; } catch (error) { console.error('注册失败:', error); wx.showToast({ title: '注册失败,请重试', icon: 'none' }); throw error; // 继续抛出错误 } }, // 获取用户姓名 getUserName() { return new Promise((resolve) => { wx.showModal({ title: '输入姓名', editable: true, placeholderText: '请输入真实姓名', success: (res) => { if (res.confirm) { resolve(res.content); } else { resolve(null); } } }); }); }, // 注册用户 async registerUser(openid, name) { const res = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/user/register`, method: 'POST', data: { openid, name } }); // 根据后端返回的状态码处理不同情况 if (res.statusCode === 200) { this.globalData.userInfo = { openid, name }; return; } else if (res.statusCode === 409) { // 假设 409 表示用户已存在 wx.showToast({ title: '用户已存在,自动登录', icon: 'none' }); this.globalData.userInfo = { openid, name: res.data.name }; // 假设后端返回用户信息 } else { throw new Error(res.data?.message || '注册失败'); } } });
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
你这是在前端获取openid?
获取openid的接口需要在服务端调用,不支持在前端调用。
代码不是你写的?
不方便导入代码片段的大佬,可以看这里:
const app = getApp(); App({ globalData: { userInfo: null, // 当前用户信息 apiBase: 'http://localhost/new' // 统一API地址 }, onLaunch() { this.checkUserLogin(); // 全局错误捕获 wx.onError(function (error) { console.error('全局捕获的错误:', error); wx.showToast({ title: '程序异常,请重试', icon: 'none' }); }); }, // 检查用户登录状态 async checkUserLogin() { try { const loginRes = await wx.login(); const code = loginRes.code; console.log('%c[DEBUG]wx.login返回的code:','color:orange', code); if (!code) { throw new Error('无法获取 code'); } const openid = await this.getOpenid(code); if (!openid) { throw new Error('无法获取 openid'); } const userRes = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/user/check?openid=${openid}`, method: 'GET', header: { 'content-type': 'application/json' } }); console.log('[DEBUG]/user/check响应:', userRes); if (userRes.statusCode === 200 && userRes.data) { if (userRes.data.isRegistered) { this.globalData.userInfo = { openid, name: userRes.data.name }; } else { await this.showRegisterDialog(openid); } } else { throw new Error('用户检查失败'); } } catch (error) { console.error('登录检查失败:', error); // 如果已获取 openid,则尝试注册 if (this.globalData.openid) { try { await this.showRegisterDialog(this.globalData.openid); } catch (registerError) { console.error('注册过程中发生错误:', registerError); wx.showToast({ title: '注册失败,请重试', icon: 'none' }); } } else { wx.showToast({ title: '登录失败,请重试', icon: 'none' }); } } }, // 获取 openid async getOpenid(code) { try { const res = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/auth/openid`, method: 'POST', header: { 'content-type': 'application/json' }, data: { code } }); console.log('%c[DEBUG]auth/openid响应:','color:green', res); if (res.statusCode === 200 && res.data && res.data.openid) { const openid = res.data.openid; this.globalData.openid = openid; // 将 openid 存储到全局变量 return openid; } else { throw new Error(res.data?.errmsg || '未返回有效 openid'); } } catch (error) { console.error('获取 openid 失败:', error); wx.showToast({ title: '获取 openid 失败', icon: 'none' }); throw error; } }, // 显示注册弹窗 async showRegisterDialog(openid) { try { const name = await this.getUserName(); if (!name) { throw new Error('未输入姓名'); } await this.registerUser(openid, name); wx.showToast({ title: '注册成功', icon: 'none' }); this.globalData.userInfo = { openid, name }; } catch (error) { console.error('注册失败:', error); wx.showToast({ title: '注册失败,请重试', icon: 'none' }); throw error; // 继续抛出错误 } }, // 获取用户姓名 getUserName() { return new Promise((resolve) => { wx.showModal({ title: '输入姓名', editable: true, placeholderText: '请输入真实姓名', success: (res) => { if (res.confirm) { resolve(res.content); } else { resolve(null); } } }); }); }, // 注册用户 async registerUser(openid, name) { const res = await wx.request({ url: `${this.globalData.apiBase}/wp-json/api/v1/user/register`, method: 'POST', data: { openid, name } }); // 根据后端返回的状态码处理不同情况 if (res.statusCode === 200) { this.globalData.userInfo = { openid, name }; return; } else if (res.statusCode === 409) { // 假设 409 表示用户已存在 wx.showToast({ title: '用户已存在,自动登录', icon: 'none' }); this.globalData.userInfo = { openid, name: res.data.name }; // 假设后端返回用户信息 } else { throw new Error(res.data?.message || '注册失败'); } } });