btnAuth.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (that.data.code) {
  38. util.request(api.AuthLoginByWeixin, {
  39. code: that.data.code,
  40. userInfo: e.detail
  41. }, 'POST', 'application/json').then(res => {
  42. if (res.errno === 0) {
  43. //存储用户信息
  44. res.data.userInfo.userId = res.data.userId
  45. res.data.userInfo.isAdmin = res.data.isAdmin
  46. res.data.userInfo.sessionKey = res.data.sessionKey
  47. wx.setStorageSync('userInfo', res.data.userInfo);
  48. wx.setStorageSync('token', res.data.token);
  49. wx.setStorageSync('userId', res.data.userId);
  50. wx.setStorageSync('isLogin', true);
  51. wx.navigateBack({})
  52. } else {
  53. // util.showErrorToast(res.errmsg)
  54. // wx.showModal({
  55. // title: '提示',
  56. // content: res.errmsg,
  57. // showCancel: false
  58. // });
  59. }
  60. });
  61. }
  62. // if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  63. // wx.switchTab({
  64. // url: that.data.navUrl,
  65. // })
  66. // } else if (that.data.navUrl) {
  67. // wx.redirectTo({
  68. // url: that.data.navUrl,
  69. // })
  70. // }
  71. },
  72. onReady: function() {
  73. // 页面渲染完成
  74. },
  75. onShow: function() {
  76. // 页面显示
  77. },
  78. onHide: function() {
  79. // 页面隐藏
  80. },
  81. onUnload: function() {
  82. // 页面关闭
  83. }
  84. })