authorize.js 5.9 KB

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