authorize.js 5.9 KB

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