authorize.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. let pages = getCurrentPages();
  56. let currPage = pages[pages.length - 1];
  57. if (this.data.isGoIndex) {
  58. wx.switchTab({
  59. url: '/pages/index/index'
  60. });
  61. } else {
  62. this.setData({
  63. iShidden: true
  64. });
  65. if (currPage && currPage.data.iShidden != undefined) {
  66. currPage.setData({
  67. iShidden: true
  68. });
  69. }
  70. }
  71. }, // close()
  72. get_logo_url: function () {
  73. var that = this;
  74. if (wx.getStorageSync('logo_url')) {
  75. return this.setData({
  76. logo_url: wx.getStorageSync('logo_url')
  77. });
  78. }
  79. getLogo().then(res => {
  80. wx.setStorageSync('logo_url', res.data.logo_url);
  81. that.setData({
  82. logo_url: res.data.logo_url
  83. });
  84. });
  85. }, // get_logo_url()
  86. //检测登录状态并执行自动登录
  87. setAuthStatus() {
  88. var that = this;
  89. Util.checkWxLogin().then((res) => {
  90. let pages = getCurrentPages();
  91. let currPage = pages[pages.length - 1];
  92. if (currPage && currPage.data.iShidden != undefined) {
  93. currPage.setData({
  94. iShidden: true
  95. });
  96. }
  97. if (res.isLogin) {
  98. if (!Util.isLoginValid()) {
  99. return Promise.reject({
  100. authSetting: true,
  101. msg: '用户token失效',
  102. userInfo: res.userInfo
  103. });
  104. }
  105. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  106. } else {
  107. wx.showLoading({
  108. title: '正在登录中'
  109. });
  110. that.setUserInfo(res.userInfo, true);
  111. }
  112. }).catch(res => {
  113. if (res.authSetting === false) {
  114. //没有授权不会自动弹出登录框
  115. if (that.data.isAuto === false) return;
  116. //自动弹出授权
  117. that.setData({
  118. iShidden: false
  119. });
  120. } else if (res.authSetting) {
  121. //授权后登录token失效了
  122. that.setUserInfo(res.userInfo);
  123. }
  124. })
  125. }, // setAuthStatus()
  126. wxSilentLogin: function () {
  127. return new Promise((resolve, reject) => {
  128. wx.login({
  129. success(res) {
  130. resolve(res.code)
  131. },
  132. fail(err) {
  133. reject(err)
  134. }
  135. })
  136. })
  137. }, // wxSilentLogin
  138. // 获取用户信息
  139. agreeAndAuth() {
  140. let that = this;
  141. wx.showLoading({
  142. title: '正在登录中'
  143. });
  144. let p1 = this.wxSilentLogin()
  145. let p2 = Util.wxGetUserProfile()
  146. Promise.all([p1, p2]).then((res) => {
  147. let userInfo = res[1];
  148. userInfo.code = res[0];
  149. // console.log(userInfo);
  150. that.loginAuth(userInfo);
  151. }).catch((err) => {
  152. console.log(err)
  153. })
  154. }, // agreeAndAuth
  155. //授权
  156. setUserInfo(userInfo, isLogin) {
  157. let that = this;
  158. wx.showLoading({
  159. title: '正在登录中'
  160. });
  161. if (isLogin) {
  162. that.loginAuth(userInfo);
  163. } else {
  164. Util.wxLogin((res) => {
  165. Util.wxGetUserProfile().then(userInfo => {
  166. userInfo.code = res.code;
  167. that.loginAuth(userInfo);
  168. }).catch(res => {
  169. wx.hideLoading();
  170. });
  171. });
  172. }
  173. }, // setUserInfo()
  174. // 从后台获取详细信息
  175. loginAuth: function (userInfo) {
  176. let that = this;
  177. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  178. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  179. // 发送到后台
  180. login(userInfo).then(res => {
  181. app.globalData.token = res.data.token;
  182. app.globalData.isLog = true;
  183. app.globalData.userInfo = res.data.userInfo;
  184. app.globalData.expiresTime = res.data.expires_time;
  185. wx.setStorageSync(CACHE_TOKEN, res.data.token);
  186. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time);
  187. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo));
  188. if (res.data.cache_key) {
  189. wx.setStorage({
  190. key: 'cache_key',
  191. data: res.data.cache_key
  192. });
  193. }
  194. //取消登录提示
  195. wx.hideLoading();
  196. //关闭登录弹出窗口
  197. that.setData({
  198. iShidden: true,
  199. errorCount: 0
  200. });
  201. //执行登录完成回调
  202. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  203. }).catch((err) => {
  204. wx.hideLoading();
  205. that.data.errorCount++;
  206. that.setData({
  207. errorCount: that.data.errorCount
  208. });
  209. if (that.data.errorCount >= that.data.errorMaxNum) {
  210. Util.Tips({
  211. title: err
  212. });
  213. } else {
  214. that.setUserInfo(userInfo);
  215. }
  216. });
  217. }, // loginAuth()
  218. // 点击打开用户协议
  219. onTabRegular: function () {
  220. wx.navigateTo({
  221. url: `/pages/user_license/license`
  222. })
  223. } //onTabRegular
  224. }, // methods
  225. })