1
0

btnAuth.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. navUrl: wx.getStorageSync("navUrl")
  16. })
  17. }else{
  18. that.setData({
  19. navUrl: '/pages/index/index'
  20. })
  21. }
  22. },
  23. bindGetUserInfo: function (e) {
  24. let that = this;
  25. // console.log(e.detail.userInfo)
  26. // console.log(that.data.navUrl);
  27. wx.login({
  28. success: function (res) {
  29. if (res.code) {
  30. wx.getUserInfo({
  31. withCredentials: true,
  32. success: function (succRes) {
  33. // console.log(res);
  34. //登录远程服务器
  35. wx.request({
  36. url: api.AuthLoginByWeixin,
  37. data: {
  38. code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  39. },
  40. method: 'POST',
  41. header: {
  42. 'Content-Type': 'application/json'
  43. },
  44. success: function (wxRes) {
  45. if (wxRes.data.errno === 0) {
  46. //存储用户信息
  47. if (wxRes.data.data.userInfo) {
  48. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  49. }
  50. wx.setStorageSync('token', wxRes.data.data.token);
  51. wx.setStorageSync('userId', wxRes.data.data.userId);
  52. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  53. wx.switchTab({
  54. url: that.data.navUrl,
  55. });
  56. } else if (that.data.navUrl) {
  57. wx.redirectTo({
  58. url: that.data.navUrl,
  59. });
  60. }
  61. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  62. console.log("登录成功");
  63. }
  64. },
  65. fail: function (err) {
  66. console.log("failed");
  67. }
  68. });
  69. },
  70. fail: function (err) {
  71. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  72. wx.switchTab({
  73. url: '/pages/index/index'
  74. });
  75. console.log("重新认证");
  76. }
  77. });
  78. } else {
  79. console.log("failed");
  80. }
  81. },
  82. fail: function (err) {
  83. console.log("failed");
  84. }
  85. });
  86. },
  87. bindCancel: function () {
  88. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  89. wx.switchTab({
  90. url: '/pages/index/index'
  91. });
  92. },
  93. onReady: function () {
  94. // 页面渲染完成
  95. },
  96. onShow: function () {
  97. // 页面显示
  98. },
  99. onHide: function () {
  100. // 页面隐藏
  101. },
  102. onUnload: function () {
  103. // 页面关闭
  104. }
  105. })