authorize.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var app = getApp();
  2. Component({
  3. properties: {
  4. iShidden: {
  5. type: Boolean,
  6. value: true,
  7. },
  8. //是否自动登录
  9. isAuto: {
  10. type: Boolean,
  11. value: true,
  12. },
  13. },
  14. data: {
  15. cloneIner: null,
  16. url: app.globalData.url,
  17. loading:false,
  18. },
  19. pageLifetimes: {
  20. hide: function () {
  21. //关闭页面时销毁定时器
  22. if (this.data.cloneIner) clearInterval(this.data.cloneIner);
  23. },
  24. show: function () {
  25. //打开页面销毁定时器
  26. if (this.data.cloneIner) clearInterval(this.data.cloneIner);
  27. },
  28. },
  29. detached() {
  30. if (this.data.cloneIner) clearInterval(this.data.cloneIner);
  31. },
  32. attached() {
  33. this.get_logo_url();
  34. this.setAuthStatus();
  35. },
  36. methods: {
  37. get_logo_url: function () {
  38. if (wx.getStorageSync('logo_url')) return this.setData({ logo_url: wx.getStorageSync('logo_url') });
  39. app.baseGet(app.U({ c: 'public_api', a: 'get_logo_url' }), function (res) {
  40. wx.setStorageSync('logo_url', res.data.logo_url);
  41. this.setData({ logo_url: res.data.logo_url });
  42. }.bind(this));
  43. },
  44. //监听登录状态
  45. WatchIsLogin: function () {
  46. this.data.cloneIner = setInterval(function () {
  47. //防止死循环,超过错误次数终止监听
  48. if (this.getErrorCount()) return clearInterval(this.data.cloneIner);
  49. if (app.globalData.token == '' && this.data.loading===false) this.setAuthStatus();
  50. }.bind(this),800);
  51. this.setData({ cloneIner: this.data.cloneIner });
  52. },
  53. //检测登录状态并执行自动登录
  54. setAuthStatus() {
  55. var that = this;
  56. that.setErrorCount();
  57. wx.getSetting({
  58. success(res) {
  59. if (!res.authSetting['scope.userInfo']) {
  60. //没有授权不会自动弹出登录框
  61. if (that.data.isAuto === false) return;
  62. //自动弹出授权
  63. that.setData({ iShidden: false });
  64. } else {
  65. //自动登录
  66. that.setData({ iShidden: true });
  67. if (app.globalData.token) {
  68. that.triggerEvent('onLoadFun', app.globalData.token);
  69. that.WatchIsLogin();
  70. } else {
  71. wx.showLoading({ title: '正在登录中' });
  72. that.getUserInfoBydecryptCode();
  73. }
  74. }
  75. }
  76. })
  77. },
  78. //访问服务器获得cache_key
  79. setCode(code, successFn, errotFn) {
  80. var that = this;
  81. that.setData({ loading: true });
  82. app.basePost(app.U({ c: 'Login', a: 'setCode' }), { code: code }, function (res) {
  83. that.setData({ loading: false });
  84. wx.setStorage({ key: 'cache_key', data: res.data.cache_key});
  85. successFn && successFn(res);
  86. }, function (res) {
  87. that.setData({ loading: false });
  88. if (errotFn) errotFn(res);
  89. else return app.Tips({ title: '获取cache_key失败' });
  90. });
  91. },
  92. //获取code
  93. getSessionKey(code, successFn, errotFn) {
  94. var that = this;
  95. wx.checkSession({
  96. success: function (res) {
  97. wx.getStorage({
  98. key:'cache_key',
  99. success:function(res){
  100. if (res.data){
  101. successFn && successFn();
  102. }else{
  103. that.setCode(code, successFn, errotFn);
  104. }
  105. },
  106. fail(res){
  107. that.setCode(code, successFn, errotFn);
  108. },
  109. });
  110. },
  111. fail: function () {
  112. that.setCode(code, successFn, errotFn);
  113. }
  114. });
  115. },
  116. login:function(){
  117. var that=this;
  118. wx.login({
  119. success: function (res) {
  120. if (!res.code) return app.Tips({ title: '登录失败!' + res.errMsg });
  121. //获取cache_key并缓存
  122. that.getSessionKey(res.code, function () {
  123. that.getUserInfoBydecryptCode();
  124. });
  125. },
  126. fail() {
  127. wx.hideLoading();
  128. }
  129. })
  130. },
  131. //授权
  132. setUserInfo(e) {
  133. wx.showLoading({ title: '正在登录中' });
  134. this.login();
  135. },
  136. close: function () {
  137. if (this.data.isAuto) return;
  138. this.setData({ iShidden: true });
  139. },
  140. //登录获取访问权限
  141. getUserInfoBydecryptCode: function () {
  142. var that = this;
  143. if (this.getErrorCount()){
  144. this.setData({ iShidden: false, ErrorCount: 0 });
  145. return app.Tips({ title: '获取code失败,请重新授权尝试获取!' });
  146. }
  147. wx.getStorage({
  148. key:'cache_key',
  149. success:function(res){
  150. if(res.data){
  151. var cache_key = res.data;
  152. wx.getUserInfo({
  153. lang: 'zh_CN',
  154. success: function (res) {
  155. var pdata = {};
  156. pdata.spid = app.globalData.spid;//获取推广人ID
  157. pdata.code = app.globalData.code;//获取推广人分享二维码ID
  158. if (res.iv) {
  159. pdata.iv = encodeURI(res.iv);
  160. pdata.encryptedData = res.encryptedData;
  161. pdata.cache_key = cache_key;
  162. //获取用户信息生成访问token
  163. that.setData({ loading: true });
  164. app.basePost(app.U({ c: 'login', a: 'index' }), pdata, function (res) {
  165. that.setData({ loading: false });
  166. if (res.data.status == 0) return app.Tips({ title: '抱歉,您已被禁止登录!' });
  167. else if (res.data.status == 410) {
  168. wx.clearStorage();
  169. wx.hideLoading();
  170. that.setErrorCount();
  171. that.login();
  172. return false;
  173. }
  174. //取消登录提示
  175. wx.hideLoading();
  176. //关闭登录弹出窗口
  177. that.setData({ iShidden: true, ErrorCount: 0 });
  178. //保存token和记录登录状态
  179. app.globalData.token = res.data.token;
  180. app.globalData.isLog = true;
  181. //执行登录完成回调
  182. that.triggerEvent('onLoadFun', app.globalData.uid);
  183. //清除定时器
  184. if (that.data.cloneIner) clearInterval(that.data.cloneIner);
  185. //监听登录状态
  186. that.WatchIsLogin();
  187. }, function (res) {
  188. that.setData({ loading: false });
  189. wx.hideLoading();
  190. that.setErrorCount();
  191. wx.clearStorage();
  192. return app.Tips({ title: res.msg });
  193. });
  194. } else {
  195. wx.hideLoading();
  196. wx.clearStorage();
  197. that.setErrorCount();
  198. return app.Tips({ title: '用户信息获取失败!' });
  199. }
  200. },
  201. fail: function () {
  202. wx.hideLoading();
  203. wx.clearStorage();
  204. that.setErrorCount();
  205. if (that.data.isAuto) that.login();
  206. },
  207. })
  208. }else{
  209. wx.hideLoading();
  210. wx.clearStorage();
  211. that.setErrorCount();
  212. if (that.data.isAuto) that.login();
  213. return false;
  214. }
  215. },
  216. fail:function(){
  217. wx.hideLoading();
  218. wx.clearStorage();
  219. that.setErrorCount();
  220. if (that.data.isAuto) that.login();
  221. }
  222. })
  223. },
  224. /**
  225. * 处理错误次数,防止死循环
  226. *
  227. */
  228. setErrorCount: function () {
  229. if (!this.data.ErrorCount) this.data.ErrorCount = 1;
  230. else this.data.ErrorCount++;
  231. this.setData({ ErrorCount: this.data.ErrorCount });
  232. },
  233. /**
  234. * 获取错误次数,是否终止监听
  235. *
  236. */
  237. getErrorCount: function () {
  238. return this.data.ErrorCount >= 10 ? true : false;
  239. }
  240. },
  241. })