小程序授权登录

时间:2024-10-03 21:59:39

参考资料

1、小程序登录 | 微信开放文档

2、GitHub - zhuhuix/opensource_startup: SpringBoot实现微信小程序登录的后台源码

学习记录,以上是小程序授权登录流程图。

1、首先使用方法获取code

  1. //微信文档实例
  2. wx.login({
  3. success (res) {
  4. if (res.code) {
  5. //发起网络请求
  6. wx.request({
  7. url: '/onLogin',
  8. data: {
  9. code: res.code
  10. }
  11. })
  12. } else {
  13. console.log('登录失败!' + res.errMsg)
  14. }
  15. }
  16. })

2、 然后使用获取用户信息(非必须)

  1. // 必须是在用户已经授权的情况下调用
  2. wx.getUserInfo({
  3. success: function(res) {
  4. var userInfo = res.userInfo
  5. var nickName = userInfo.nickName
  6. var avatarUrl = userInfo.avatarUrl
  7. var gender = userInfo.gender //性别 0:未知、1:男、2:女
  8. var province = userInfo.province
  9. var city = userInfo.city
  10. var country = userInfo.country
  11. }
  12. })

3、其次使用 wx.request方法,请求服务端的登录接口

  1. wx.request({
  2. url: '', //仅为示例,并非真实的接口地址
  3. data: {
  4. x: '',
  5. y: ''
  6. },
  7. header: {
  8. 'content-type': 'application/json' // 默认值
  9. },
  10. success (res) {
  11. console.log(res.data)
  12. }
  13. })

4、后台接口调用微信接口auth.code2Session,登录凭证校验。通过  接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。

GET https:///sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

 

 下面是实际实现代码,获取code,并获取用户信息,然后请求后台接口授权登录,保存用户信息。

  1. // 公共登录动作
  2. doLogin: function (callback) {
  3. let that = this;
  4. wx.login({
  5. success: function (loginRes) {
  6. console.log(loginRes, "loginRes");
  7. if (loginRes.code) {
  8. /*
  9. * @desc: 获取用户信息 期望数据如下
  10. *
  11. * @param: userInfo [Object]
  12. * @param: rawData [String]
  13. * @param: signature [String]
  14. * @param: encryptedData [String]
  15. * @param: iv [String]
  16. **/
  17. wx.getUserInfo({
  18. withCredentials: true, // 非必填, 默认为true
  19. success: function (infoRes) {
  20. console.log("infoRes:", infoRes);
  21. // 请求服务端的登录接口
  22. wx.request({
  23. url: api.loginUrl,
  24. method: "POST",
  25. data: {
  26. authType: 1, //1代表微信端登录
  27. code: loginRes.code, // 临时登录凭证
  28. rawData: infoRes.rawData, // 用户非敏感信息
  29. signature: infoRes.signature, // 签名
  30. encryptedData: infoRes.encryptedData, // 用户敏感信息
  31. iv: infoRes.iv, // 解密算法的向量
  32. token: wx.getStorageSync("loginFlag"),
  33. },
  34. success: function (res) {
  35. console.log("login success:", res);
  36. res = res.data;
  37. if (res.success) {
  38. that.globalData.userInfo = res.module.userInfo;
  39. console.log(
  40. "",
  41. that.globalData.userInfo
  42. );
  43. wx.setStorageSync("userInfo", res.module.userInfo);
  44. wx.setStorageSync("loginFlag", res.module.token);
  45. if (callback) {
  46. callback();
  47. }
  48. } else {
  49. that.showInfo(res.errMsg);
  50. }
  51. },
  52. fail: function (error) {
  53. // 调用服务端登录接口失败
  54. that.showInfo("调用接口失败");
  55. console.log(error);
  56. },
  57. });
  58. },
  59. fail: function (error) {
  60. console.log(error);
  61. // 获取 userInfo 失败,去检查是否未开启权限
  62. wx.hideLoading();
  63. that.showInfo("调用request接口失败");
  64. console.log(error);
  65. wwx.navigateTo({
  66. url: "/pages/index/index",
  67. });
  68. },
  69. });
  70. } else {
  71. // 获取 code 失败
  72. that.showInfo("登录失败");
  73. console.log("调用获取code失败");
  74. }
  75. },
  76. fail: function (error) {
  77. // 调用 接口失败
  78. that.showInfo("接口调用失败");
  79. console.log(error);
  80. },
  81. });
  82. },

 服务端调用微信服务器进行登录凭证校验。

 将获取到的code,从配置里拿到appid,secret拼接到链接上。

  1. @Override
  2. public JSONObject authCode2Session(String appId, String secret, String jsCode) {
  3. String url = "/sns/jscode2session?apphljs-string">"&secret=" + secret + "&js_code=" + jsCode + "&grant_type=authorization_code";
  4. String str = (url, "GET", null);
  5. ("api/wx-mini/getSessionKey:" + str);
  6. if ((str)) {
  7. return null;
  8. } else {
  9. return (str);
  10. }
  11. }

以上就是微信小程序的授权流程。

具体代码大家可以到GitHub上去下载,链接在文章开头,只需要把数据库和redis连接换成自己的就可以成功运行