authorize.js 7.8 KB

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