authorize.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /**
  101. * 封装 wx.getUserInfo()
  102. */
  103. wxGetUserInfo: function() {
  104. return new Promise((resolve, reject) => {
  105. wx.getUserInfo({
  106. lang: 'zh_CN',
  107. desc: '用于完善会员资料',
  108. success(res) {
  109. resolve(res);
  110. },
  111. fail(res){
  112. reject(res);
  113. }
  114. })
  115. });
  116. },
  117. wxSilentLogin: function () {
  118. return new Promise((resolve, reject) => {
  119. wx.login({
  120. success (res) {
  121. resolve(res.code)
  122. },
  123. fail (err) {
  124. reject(err)
  125. }
  126. })
  127. })
  128. }, // wxSilentLogin
  129. // 获取用户信息
  130. agreeAndAuth() {
  131. let that = this;
  132. wx.showLoading({ title: '正在登录中' });
  133. let p1 = this.wxSilentLogin()
  134. let p2 = Util.wxGetUserProfile()
  135. Promise.all([p1, p2]).then((res) => {
  136. let userInfo = res[1];
  137. userInfo.code = res[0];
  138. // console.log(userInfo);
  139. that.loginAuth(userInfo);
  140. }).catch((err) => {
  141. console.log(err)
  142. })
  143. }, // agreeAndAuth
  144. //授权
  145. setUserInfo(userInfo, isLogin) {
  146. let that = this;
  147. wx.showLoading({ title: '正在登录中' });
  148. if (isLogin) {
  149. that.loginAuth(userInfo);
  150. } else {
  151. Util.getAuthCode((res) => {
  152. Util.wxGetUserProfile().then(userInfo=>{
  153. userInfo.code = res.code;
  154. that.loginAuth(userInfo);
  155. }).catch(res => {
  156. wx.hideLoading();
  157. });
  158. });
  159. }
  160. }, // setUserInfo()
  161. // 从后台获取详细信息
  162. loginAuth: function (userInfo) {
  163. let that = this;
  164. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  165. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  166. // 发送到后台
  167. login(userInfo).then(res => {
  168. app.globalData.token = res.data.token;
  169. app.globalData.isLog = true;
  170. app.globalData.userInfo = res.data.userInfo;
  171. app.globalData.expiresTime = res.data.expires_time;
  172. wx.setStorageSync(CACHE_TOKEN, res.data.token);
  173. wx.setStorageSync(CACHE_EXPIRES_TIME, res.data.expires_time);
  174. wx.setStorageSync(CACHE_USERINFO, JSON.stringify(res.data.userInfo));
  175. if (res.data.cache_key) {
  176. wx.setStorage({ key: 'cache_key', data: res.data.cache_key });
  177. }
  178. //取消登录提示
  179. wx.hideLoading();
  180. //关闭登录弹出窗口
  181. that.setData({ iShidden: true, errorCount: 0 });
  182. //执行登录完成回调
  183. that.triggerEvent('onLoadFun', app.globalData.userInfo);
  184. }).catch((err) => {
  185. wx.hideLoading();
  186. that.data.errorCount++;
  187. that.setData({ errorCount: that.data.errorCount });
  188. if (that.data.errorCount >= that.data.errorMaxNum) {
  189. Util.Tips({ title: err });
  190. } else {
  191. that.setUserInfo(userInfo);
  192. }
  193. });
  194. }, // loginAuth()
  195. // 点击打开用户协议
  196. onTabRegular: function() {
  197. wx.navigateTo({
  198. url: `/pages/user_license/license`
  199. })
  200. } //onTabRegular
  201. }, // methods
  202. })