authorize.js 4.6 KB

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