btnAuth.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. wx.setStorageSync('userInfo', res.data.userInfo);
  46. wx.setStorageSync('token', res.data.token);
  47. wx.setStorageSync('userId', res.data.userId);
  48. wx.setStorageSync('isLogin', true);
  49. wx.navigateBack({})
  50. } else {
  51. // util.showErrorToast(res.errmsg)
  52. // wx.showModal({
  53. // title: '提示',
  54. // content: res.errmsg,
  55. // showCancel: false
  56. // });
  57. }
  58. });
  59. }
  60. // if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  61. // wx.switchTab({
  62. // url: that.data.navUrl,
  63. // })
  64. // } else if (that.data.navUrl) {
  65. // wx.redirectTo({
  66. // url: that.data.navUrl,
  67. // })
  68. // }
  69. },
  70. onReady: function() {
  71. // 页面渲染完成
  72. },
  73. onShow: function() {
  74. // 页面显示
  75. },
  76. onHide: function() {
  77. // 页面隐藏
  78. },
  79. onUnload: function() {
  80. // 页面关闭
  81. }
  82. })