btnAuth.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. },
  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. wx.login({
  23. success: function(res) {
  24. if (res.code) {
  25. that.setData({
  26. code: res.code
  27. })
  28. }
  29. }
  30. });
  31. },
  32. bindGetUserInfo: function(e) {
  33. let that = this;
  34. //登录远程服务器
  35. if (that.data.code) {
  36. util.request(api.AuthLoginByWeixin, {
  37. code: that.data.code,
  38. userInfo: e.detail
  39. }, 'POST', 'application/json').then(res => {
  40. if (res.errno === 0) {
  41. //存储用户信息
  42. wx.setStorageSync('userInfo', res.data.userInfo);
  43. wx.setStorageSync('token', res.data.token);
  44. wx.setStorageSync('userId', res.data.userId);
  45. } else {
  46. // util.showErrorToast(res.errmsg)
  47. wx.showModal({
  48. title: '提示',
  49. content: res.errmsg,
  50. showCancel: false
  51. });
  52. }
  53. });
  54. }
  55. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  56. wx.switchTab({
  57. url: that.data.navUrl,
  58. })
  59. } else if (that.data.navUrl) {
  60. wx.redirectTo({
  61. url: that.data.navUrl,
  62. })
  63. }
  64. },
  65. onReady: function() {
  66. // 页面渲染完成
  67. },
  68. onShow: function() {
  69. // 页面显示
  70. },
  71. onHide: function() {
  72. // 页面隐藏
  73. },
  74. onUnload: function() {
  75. // 页面关闭
  76. }
  77. })