index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const util = require('../../../utils/util.js');
  2. const api = require('../../../config/api.js');
  3. const user = require('../../../services/user.js');
  4. const app = getApp();
  5. Page({
  6. data: {
  7. userInfo: {},
  8. showLoginDialog: false
  9. },
  10. onLoad: function(options) {
  11. // 页面初始化 options为页面跳转所带来的参数
  12. },
  13. onReady: function() {
  14. },
  15. onShow: function() {
  16. this.setData({
  17. userInfo: app.globalData.userInfo,
  18. });
  19. },
  20. onHide: function() {
  21. // 页面隐藏
  22. },
  23. onUnload: function() {
  24. // 页面关闭
  25. },
  26. onUserInfoClick: function() {
  27. if (wx.getStorageSync('token')) {
  28. } else {
  29. this.showLoginDialog();
  30. }
  31. },
  32. showLoginDialog() {
  33. this.setData({
  34. showLoginDialog: true
  35. })
  36. },
  37. onCloseLoginDialog () {
  38. this.setData({
  39. showLoginDialog: false
  40. })
  41. },
  42. onDialogBody () {
  43. // 阻止冒泡
  44. },
  45. onWechatLogin(e) {
  46. if (e.detail.errMsg !== 'getUserInfo:ok') {
  47. if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
  48. return false
  49. }
  50. wx.showToast({
  51. title: '微信登录失败',
  52. })
  53. return false
  54. }
  55. util.login().then((res) => {
  56. return util.request(api.AuthLoginByWeixin, {
  57. code: res,
  58. userInfo: e.detail
  59. }, 'POST');
  60. }).then((res) => {
  61. console.log(res)
  62. if (res.errno !== 0) {
  63. wx.showToast({
  64. title: '微信登录失败',
  65. })
  66. return false;
  67. }
  68. // 设置用户信息
  69. this.setData({
  70. userInfo: res.data.userInfo,
  71. showLoginDialog: false
  72. });
  73. app.globalData.userInfo = res.data.userInfo;
  74. app.globalData.token = res.data.token;
  75. wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo));
  76. wx.setStorageSync('token', res.data.token);
  77. }).catch((err) => {
  78. console.log(err)
  79. })
  80. },
  81. onOrderInfoClick: function(event) {
  82. wx.navigateTo({
  83. url: '/pages/ucenter/order/order',
  84. })
  85. },
  86. onSectionItemClick: function(event) {
  87. },
  88. // TODO 移到个人信息页面
  89. exitLogin: function() {
  90. wx.showModal({
  91. title: '',
  92. confirmColor: '#b4282d',
  93. content: '退出登录?',
  94. success: function(res) {
  95. if (res.confirm) {
  96. wx.removeStorageSync('token');
  97. wx.removeStorageSync('userInfo');
  98. wx.switchTab({
  99. url: '/pages/index/index'
  100. });
  101. }
  102. }
  103. })
  104. }
  105. })