Skip to content

Commit

Permalink
feat 微信登录、支付失败返回错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
tumobi committed Nov 15, 2019
1 parent 79b881a commit f3d95d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/api/controller/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = class extends Base {
const clientIp = this.ctx.ip;

// 解释用户数据
const userInfo = await this.service('weixin', 'api').login(code, fullUserInfo);
if (think.isEmpty(userInfo)) {
return this.fail('登录失败');
const { errno, errmsg, data: userInfo } = await this.service('weixin', 'api').login(code, fullUserInfo);
if (errno !== 0) {
return this.fail(errno, errmsg);
}

// 根据openid查找用户是否已经注册
Expand Down Expand Up @@ -41,8 +41,8 @@ module.exports = class extends Base {
const TokenSerivce = this.service('token', 'api');
const sessionKey = await TokenSerivce.create({ user_id: userId });

if (think.isEmpty(newUserInfo) || think.isEmpty(sessionKey)) {
return this.fail('登录失败');
if (think.isEmpty(sessionKey)) {
return this.fail('生成 token 失败');
}

return this.success({ token: sessionKey, userInfo: newUserInfo });
Expand Down
4 changes: 2 additions & 2 deletions src/api/controller/pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = class extends Base {
}
const openid = await this.model('user').where({ id: orderInfo.user_id }).getField('weixin_openid', true);
if (think.isEmpty(openid)) {
return this.fail('微信支付失败');
return this.fail('微信支付失败 openid empty');
}
const WeixinSerivce = this.service('weixin', 'api');
try {
Expand All @@ -31,7 +31,7 @@ module.exports = class extends Base {
});
return this.success(returnParams);
} catch (err) {
return this.fail('微信支付失败');
return this.fail(400, `微信支付失败 ${err.err_code_des || err.return_msg}`);
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/api/service/weixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ module.exports = class extends think.Service {
let sessionData = await rp(options);
sessionData = JSON.parse(sessionData);
if (!sessionData.openid) {
return null;
return { errno: sessionData.errcode, errmsg: sessionData.errmsg, data: null };
}

// 验证用户信息完整性
const sha1 = crypto.createHash('sha1').update(fullUserInfo.rawData.toString() + sessionData.session_key).digest('hex');
if (fullUserInfo.signature !== sha1) {
return null;
return { errno: 400, errmsg: `signature 校验不一致`, data: null };
}

// 解析用户数据
const wechatUserInfo = await this.decryptUserInfoData(sessionData.session_key, fullUserInfo.encryptedData, fullUserInfo.iv);
if (think.isEmpty(wechatUserInfo)) {
return null;
}
return wechatUserInfo;
} catch (e) {
return null;
return { errno: 400, errmsg: '微信登录失败:' + e.message, data: null };
}
}

Expand All @@ -61,7 +58,7 @@ module.exports = class extends think.Service {
decoded += decipher.final('utf8');
const userInfo = JSON.parse(decoded);
if (userInfo.watermark.appid !== think.config('weixin.appid')) {
return null;
return { errno: 400, errmsg: 'watermark appid 错误', data: null };
}

// 解析后的数据格式
Expand All @@ -74,9 +71,9 @@ module.exports = class extends think.Service {
// country: 'China',
// avatarUrl: 'https://wx.qlogo.cn/mmopen/vi_32/9Otwibfa5VXR0ntXdlX84dibbulWLJ0EiacHeAfT1ShG2A7LQa2unfbZVohsWQlmXbwQGM6NnhGFWicY5icdxFVdpLQ/132',
// watermark: { timestamp: 1542639764, appid: 'wx262f4ac3b1c477dd' } }
return userInfo;
return { errno: 0, errmsg: '', data: userInfo };
} catch (err) {
return null;
return { errno: 500, errmsg: '解析用户数据错误:' + err.message, data: null };
}
}

Expand Down

0 comments on commit f3d95d6

Please sign in to comment.