btnAuth.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const util = require('../../../utils/util.js');
  2. const api = require('../../../config/api.js');
  3. //获取应用实例
  4. const app = getApp()
  5. Page({
  6. data: {
  7. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  8. navUrl: '',
  9. code: '',
  10. login_img: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/login-bg.png",
  11. login_logo: "/static/images/img_empower_logo@2x11.png"
  12. },
  13. onLoad: function(options) {
  14. let that = this;
  15. if (wx.getStorageSync("navUrl")) {
  16. that.setData({
  17. navUrl: wx.getStorageSync("navUrl")
  18. })
  19. } else {
  20. that.setData({
  21. navUrl: '/pages/index/index'
  22. })
  23. }
  24. wx.login({
  25. success: function(res) {
  26. if (res.code) {
  27. that.setData({
  28. code: res.code
  29. })
  30. }
  31. }
  32. });
  33. },
  34. bindGetUserInfo: function(e) {
  35. let that = this;
  36. //登录远程服务器
  37. debugger;
  38. if (that.data.code) {
  39. util.request(api.AuthLoginByWeixin, {
  40. code: that.data.code,
  41. userInfo: e.detail
  42. }, 'POST', 'application/json').then(res => {
  43. if (res.errno === 0) {
  44. debugger
  45. //存储用户信息
  46. res.data.userInfo.userId = res.data.userId
  47. res.data.userInfo.sessionKey = res.data.sessionKey
  48. res.data.userInfo.isAdmin = res.data.isAdmin
  49. wx.setStorageSync('userInfo', res.data.userInfo);
  50. wx.setStorageSync('token', res.data.token);
  51. wx.setStorageSync('userId', res.data.userId);
  52. wx.setStorageSync('isLogin', true);
  53. wx.navigateBack({})
  54. } else {
  55. // util.showErrorToast(res.errmsg)
  56. // wx.showModal({
  57. // title: '提示',
  58. // content: res.errmsg,
  59. // showCancel: false
  60. // });
  61. }
  62. });
  63. }
  64. // if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  65. // wx.switchTab({
  66. // url: that.data.navUrl,
  67. // })
  68. // } else if (that.data.navUrl) {
  69. // wx.redirectTo({
  70. // url: that.data.navUrl,
  71. // })
  72. // }
  73. },
  74. onReady: function() {
  75. // 页面渲染完成
  76. },
  77. onShow: function() {
  78. // 页面显示
  79. },
  80. onHide: function() {
  81. // 页面隐藏
  82. },
  83. onUnload: function() {
  84. // 页面关闭
  85. }
  86. })