1
0

btnAuth.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const util = require('../../../utils/util.js');
  2. const api = require('../../../config/api.js');
  3. const user = require('../../../services/user.js');
  4. //获取应用实例
  5. const app = getApp()
  6. Page({
  7. data: {
  8. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  9. navUrl: ''
  10. },
  11. onLoad: function (options) {
  12. let that = this;
  13. if (wx.getStorageSync("navUrl")) {
  14. that.setData(
  15. {
  16. navUrl: wx.getStorageSync("navUrl")
  17. }
  18. )
  19. }else{
  20. that.setData(
  21. {
  22. navUrl: '/pages/index/index'
  23. }
  24. )
  25. }
  26. },
  27. bindGetUserInfo: function (e) {
  28. let that = this;
  29. // console.log(e.detail.userInfo)
  30. // console.log(that.data.navUrl);
  31. wx.login({
  32. success: function (res) {
  33. if (res.code) {
  34. wx.getUserInfo({
  35. withCredentials: true,
  36. success: function (succRes) {
  37. // console.log(res);
  38. //登录远程服务器
  39. wx.request({
  40. url: api.AuthLoginByWeixin,
  41. data: {
  42. code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId')
  43. },
  44. method: 'POST',
  45. header: {
  46. 'Content-Type': 'application/json'
  47. },
  48. success: function (wxRes) {
  49. if (wxRes.data.errno === 0) {
  50. //存储用户信息
  51. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  52. wx.setStorageSync('token', wxRes.data.data.token);
  53. wx.setStorageSync('userId', wxRes.data.data.userId);
  54. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  55. wx.switchTab({
  56. url: that.data.navUrl,
  57. });
  58. } else if (that.data.navUrl) {
  59. wx.redirectTo({
  60. url: that.data.navUrl,
  61. });
  62. }
  63. console.log("登录成功");
  64. }
  65. },
  66. fail: function (err) {
  67. console.log("failed");
  68. }
  69. });
  70. },
  71. fail: function (err) {
  72. console.log("重新认证");
  73. }
  74. });
  75. } else {
  76. console.log("failed");
  77. }
  78. },
  79. fail: function (err) {
  80. console.log("failed");
  81. }
  82. });
  83. },
  84. onReady: function () {
  85. // 页面渲染完成
  86. },
  87. onShow: function () {
  88. // 页面显示
  89. },
  90. onHide: function () {
  91. // 页面隐藏
  92. },
  93. onUnload: function () {
  94. // 页面关闭
  95. }
  96. })