authorize.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { CACHE_USERINFO, CACHE_TOKEN, CACHE_EXPIRES_TIME } from '../../config.js';
  2. import Util from '../../utils/util.js';
  3. import { getLogo } from '../../api/api.js';
  4. import { login } from '../../api/user.js';
  5. let app = getApp();
  6. Component({
  7. properties: {
  8. iShidden: {
  9. type: Boolean,
  10. value: true,
  11. },
  12. //是否自动登录
  13. isAuto: {
  14. type: Boolean,
  15. value: true,
  16. },
  17. isGoIndex:{
  18. type: Boolean,
  19. value:true,
  20. },
  21. },
  22. data: {
  23. cloneIner: null,
  24. loading:false,
  25. errorSum:0,
  26. errorNum:3
  27. },
  28. attached() {
  29. this.get_logo_url();
  30. this.setAuthStatus();
  31. },
  32. methods: {
  33. close(){
  34. let pages = getCurrentPages();
  35. let currPage = pages[pages.length - 1];
  36. if(this.data.isGoIndex){
  37. wx.switchTab({url:'/pages/index/index'});
  38. }else{
  39. this.setData({
  40. iShidden: true
  41. });
  42. if (currPage && currPage.data.iShidden != undefined){
  43. currPage.setData({ iShidden:true});
  44. }
  45. }
  46. },
  47. get_logo_url: function () {
  48. var that = this;
  49. if (wx.getStorageSync('logo_url')) return this.setData({ logo_url: wx.getStorageSync('logo_url') });
  50. getLogo().then(res=>{
  51. wx.setStorageSync('logo_url', res.data.logo_url);
  52. that.setData({ logo_url: res.data.logo_url });
  53. });
  54. },
  55. //检测登录状态并执行自动登录
  56. setAuthStatus() {
  57. var that = this;
  58. Util.chekWxLogin().then((res)=> {
  59. let pages = getCurrentPages();
  60. let currPage = pages[pages.length - 1];
  61. if (currPage && currPage.data.iShidden != undefined) {
  62. currPage.setData({ iShidden:true});
  63. }
  64. if (res.isLogin) {
  65. if (!Util.checkLogin()) return Promise.reject({ authSetting: true, msg: '用户token失效', userInfo: res.userInfo});
  66. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  67. }else{
  68. wx.showLoading({ title: '正在登录中' });
  69. that.setUserInfo(res.userInfo,true);
  70. }
  71. }).catch(res=>{
  72. if (res.authSetting === false) {
  73. //没有授权不会自动弹出登录框
  74. if (that.data.isAuto === false) return;
  75. //自动弹出授权
  76. that.setData({ iShidden: false });
  77. } else if (res.authSetting){
  78. //授权后登录token失效了
  79. that.setUserInfo(res.userInfo);
  80. }
  81. })
  82. },
  83. //授权
  84. setUserInfo(userInfo,isLogin) {
  85. let that = this;
  86. wx.showLoading({ title: '正在登录中' });
  87. if (isLogin){
  88. that.getWxUserInfo(userInfo);
  89. }else{
  90. Util.getCodeLogin((res)=>{
  91. Util.wxgetUserInfo().then(userInfo=>{
  92. userInfo.code = res.code;
  93. that.getWxUserInfo(userInfo);
  94. }).catch(res=>{
  95. wx.hideLoading();
  96. });
  97. });
  98. }
  99. },
  100. getWxUserInfo: function (userInfo){
  101. let that = this;
  102. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  103. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  104. login(userInfo).then(res => {
  105. app.globalData.token = res.data.token;
  106. app.globalData.isLog = true;
  107. app.globalData.userInfo = res.data.userInfo;
  108. app.globalData.expiresTime = res.data.expires_time;
  109. wx.setStorageSync(CACHE_TOKEN, res.data.token);
  110. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time);
  111. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo));
  112. if (res.data.cache_key) wx.setStorage({ key: 'cache_key', data: res.data.cache_key });
  113. //取消登录提示
  114. wx.hideLoading();
  115. //关闭登录弹出窗口
  116. that.setData({ iShidden: true, errorSum: 0 });
  117. //执行登录完成回调
  118. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  119. }).catch((err) => {
  120. wx.hideLoading();
  121. that.data.errorSum++;
  122. that.setData({ errorSum: that.data.errorSum });
  123. if (that.data.errorSum >= that.data.errorNum) {
  124. Util.Tips({ title: err });
  125. } else {
  126. that.setUserInfo(userInfo);
  127. }
  128. });
  129. }
  130. },
  131. })