authorize.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. canIUseGetUserProfile: false // 判断是否为最新获取用户信息函数
  28. },
  29. attached() {
  30. this.get_logo_url();
  31. this.setAuthStatus();
  32. if (wx.getUserProfile) {
  33. this.setData({
  34. canIUseGetUserProfile: true
  35. })
  36. }
  37. },
  38. methods: {
  39. close(){
  40. let pages = getCurrentPages();
  41. let currPage = pages[pages.length - 1];
  42. if(this.data.isGoIndex){
  43. wx.switchTab({url:'/pages/index/index'});
  44. }else{
  45. this.setData({
  46. iShidden: true
  47. });
  48. if (currPage && currPage.data.iShidden != undefined){
  49. currPage.setData({ iShidden:true});
  50. }
  51. }
  52. },
  53. get_logo_url: function () {
  54. var that = this;
  55. if (wx.getStorageSync('logo_url')) return this.setData({ logo_url: wx.getStorageSync('logo_url') });
  56. getLogo().then(res=>{
  57. wx.setStorageSync('logo_url', res.data.logo_url);
  58. that.setData({ logo_url: res.data.logo_url });
  59. });
  60. },
  61. //检测登录状态并执行自动登录
  62. setAuthStatus() {
  63. var that = this;
  64. Util.chekWxLogin().then((res)=> {
  65. let pages = getCurrentPages();
  66. let currPage = pages[pages.length - 1];
  67. if (currPage && currPage.data.iShidden != undefined) {
  68. currPage.setData({ iShidden:true});
  69. }
  70. if (res.isLogin) {
  71. if (!Util.checkLogin()) return Promise.reject({ authSetting: true, msg: '用户token失效', userInfo: res.userInfo});
  72. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  73. }else{
  74. wx.showLoading({ title: '正在登录中' });
  75. that.setUserInfo(res.userInfo,true);
  76. }
  77. }).catch(res=>{
  78. if (res.authSetting === false) {
  79. //没有授权不会自动弹出登录框
  80. if (that.data.isAuto === false) return;
  81. //自动弹出授权
  82. that.setData({ iShidden: false });
  83. } else if (res.authSetting){
  84. //授权后登录token失效了
  85. that.setUserInfo(res.userInfo);
  86. }
  87. })
  88. },
  89. // 获取用户信息
  90. getUserProfile() {
  91. let that = this;
  92. wx.showLoading({ title: '正在登录中' });
  93. wx.getUserProfile({
  94. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  95. success: (res) => {
  96. Util.getCodeLogin((code)=>{
  97. let userInfo = res;
  98. userInfo.code = code.code;
  99. that.getWxUserInfo(userInfo);
  100. })
  101. },
  102. fail: (err) => {
  103. wx.hideLoading();
  104. }
  105. });
  106. },
  107. //授权
  108. setUserInfo(userInfo,isLogin) {
  109. let that = this;
  110. wx.showLoading({ title: '正在登录中' });
  111. if (isLogin){
  112. that.getWxUserInfo(userInfo);
  113. }else{
  114. Util.getCodeLogin((res)=>{
  115. Util.wxgetUserInfo().then(userInfo=>{
  116. userInfo.code = res.code;
  117. that.getWxUserInfo(userInfo);
  118. }).catch(res=>{
  119. wx.hideLoading();
  120. });
  121. });
  122. }
  123. },
  124. getWxUserInfo: function (userInfo){
  125. let that = this;
  126. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  127. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  128. login(userInfo).then(res => {
  129. app.globalData.token = res.data.token;
  130. app.globalData.isLog = true;
  131. app.globalData.userInfo = res.data.userInfo;
  132. app.globalData.expiresTime = res.data.expires_time;
  133. wx.setStorageSync(CACHE_TOKEN, res.data.token);
  134. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time);
  135. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo));
  136. if (res.data.cache_key) wx.setStorage({ key: 'cache_key', data: res.data.cache_key });
  137. //取消登录提示
  138. wx.hideLoading();
  139. //关闭登录弹出窗口
  140. that.setData({ iShidden: true, errorSum: 0 });
  141. //执行登录完成回调
  142. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  143. }).catch((err) => {
  144. wx.hideLoading();
  145. that.data.errorSum++;
  146. that.setData({ errorSum: that.data.errorSum });
  147. if (that.data.errorSum >= that.data.errorNum) {
  148. Util.Tips({ title: err });
  149. } else {
  150. that.setUserInfo(userInfo);
  151. }
  152. });
  153. }
  154. },
  155. })