authorize.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // 当前授权窗口是否隐藏
  9. iShidden: {
  10. type: Boolean,
  11. value: true,
  12. },
  13. // 是否自动登录
  14. isAuto: {
  15. type: Boolean,
  16. value: true,
  17. },
  18. // 是否跳转到首页
  19. isGoIndex:{
  20. type: Boolean,
  21. value:true,
  22. },
  23. }, // properties
  24. data: {
  25. errorCount:0,
  26. errorMaxNum:3,
  27. code: null, // wx login code
  28. canIUseGetUserProfile: false, // 判断是否为最新获取用户信息函数
  29. canIUseGetUserInfo: false
  30. }, // data
  31. attached() {
  32. this.get_logo_url();
  33. this.setAuthStatus();
  34. if (wx.getUserProfile) {
  35. this.setData({
  36. canIUseGetUserProfile: true
  37. })
  38. }
  39. if (wx.getUserInfo) {
  40. this.setData({
  41. canIUseGetUserInfo: true
  42. })
  43. }
  44. }, // attached
  45. methods: {
  46. close() {
  47. let pages = getCurrentPages();
  48. let currPage = pages[pages.length - 1];
  49. if (this.data.isGoIndex) {
  50. wx.switchTab({url: '/pages/index/index'});
  51. } else {
  52. this.setData({
  53. iShidden: true
  54. });
  55. if (currPage && currPage.data.iShidden != undefined){
  56. currPage.setData({ iShidden:true});
  57. }
  58. }
  59. }, // close()
  60. get_logo_url: function () {
  61. var that = this;
  62. if (wx.getStorageSync('logo_url')) {
  63. return this.setData({ logo_url: wx.getStorageSync('logo_url') });
  64. }
  65. getLogo().then(res => {
  66. wx.setStorageSync('logo_url', res.data.logo_url);
  67. that.setData({ logo_url: res.data.logo_url });
  68. });
  69. }, // get_logo_url()
  70. //检测登录状态并执行自动登录
  71. setAuthStatus() {
  72. var that = this;
  73. Util.checkWxLogin().then((res) => {
  74. let pages = getCurrentPages();
  75. let currPage = pages[pages.length - 1];
  76. if (currPage && currPage.data.iShidden != undefined) {
  77. currPage.setData({ iShidden:true});
  78. }
  79. if (res.isLogin) {
  80. if (!Util.isLoginValid()) {
  81. return Promise.reject({ authSetting: true, msg: '用户token失效', userInfo: res.userInfo} );
  82. }
  83. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  84. } else {
  85. wx.showLoading({ title: '正在登录中' });
  86. that.setUserInfo(res.userInfo,true);
  87. }
  88. }).catch(res => {
  89. if (res.authSetting === false) {
  90. //没有授权不会自动弹出登录框
  91. if (that.data.isAuto === false) return;
  92. //自动弹出授权
  93. that.setData({ iShidden: false });
  94. } else if (res.authSetting) {
  95. //授权后登录token失效了
  96. that.setUserInfo(res.userInfo);
  97. }
  98. })
  99. }, // setAuthStatus()
  100. wxSilentLogin: function () {
  101. return new Promise((resolve, reject) => {
  102. wx.login({
  103. success (res) {
  104. resolve(res.code)
  105. },
  106. fail (err) {
  107. reject(err)
  108. }
  109. })
  110. })
  111. }, // wxSilentLogin
  112. // 获取用户信息
  113. agreeAndAuth() {
  114. let that = this;
  115. wx.showLoading({ title: '正在登录中' });
  116. let p1 = this.wxSilentLogin()
  117. let p2 = Util.wxGetUserProfile()
  118. Promise.all([p1, p2]).then((res) => {
  119. let userInfo = res[1];
  120. userInfo.code = res[0];
  121. // console.log(userInfo);
  122. that.loginAuth(userInfo);
  123. }).catch((err) => {
  124. console.log(err)
  125. })
  126. }, // agreeAndAuth
  127. //授权
  128. setUserInfo(userInfo, isLogin) {
  129. let that = this;
  130. wx.showLoading({ title: '正在登录中' });
  131. if (isLogin) {
  132. that.loginAuth(userInfo);
  133. } else {
  134. Util.getAuthCode((res) => {
  135. Util.wxGetUserProfile().then(userInfo=>{
  136. userInfo.code = res.code;
  137. that.loginAuth(userInfo);
  138. }).catch(res => {
  139. wx.hideLoading();
  140. });
  141. });
  142. }
  143. }, // setUserInfo()
  144. // 从后台获取详细信息
  145. loginAuth: function (userInfo) {
  146. let that = this;
  147. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  148. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  149. // 发送到后台
  150. login(userInfo).then(res => {
  151. app.globalData.token = res.data.token;
  152. app.globalData.isLog = true;
  153. app.globalData.userInfo = res.data.userInfo;
  154. app.globalData.expiresTime = res.data.expires_time;
  155. wx.setStorageSync(CACHE_TOKEN, res.data.token);
  156. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time);
  157. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo));
  158. if (res.data.cache_key) {
  159. wx.setStorage({ key: 'cache_key', data: res.data.cache_key });
  160. }
  161. //取消登录提示
  162. wx.hideLoading();
  163. //关闭登录弹出窗口
  164. that.setData({ iShidden: true, errorCount: 0 });
  165. //执行登录完成回调
  166. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  167. }).catch((err) => {
  168. wx.hideLoading();
  169. that.data.errorCount++;
  170. that.setData({ errorCount: that.data.errorCount });
  171. if (that.data.errorCount >= that.data.errorMaxNum) {
  172. Util.Tips({ title: err });
  173. } else {
  174. that.setUserInfo(userInfo);
  175. }
  176. });
  177. }, // loginAuth()
  178. // 点击打开用户协议
  179. onTabRegular: function() {
  180. wx.navigateTo({
  181. url: `/pages/user_license/license`
  182. })
  183. } //onTabRegular
  184. }, // methods
  185. })