| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- var app = getApp();
- Component({
- properties: {
- iShidden: {
- type: Boolean,
- value: true,
- },
- //是否自动登录
- isAuto: {
- type: Boolean,
- value: true,
- },
- },
- data: {
- cloneIner: null,
- url: app.globalData.url,
- loading:false,
- },
- pageLifetimes: {
- hide: function () {
- //关闭页面时销毁定时器
- if (this.data.cloneIner) clearInterval(this.data.cloneIner);
- },
- show: function () {
- //打开页面销毁定时器
- if (this.data.cloneIner) clearInterval(this.data.cloneIner);
- },
- },
- detached() {
- if (this.data.cloneIner) clearInterval(this.data.cloneIner);
- },
- attached() {
- this.get_logo_url();
- this.setAuthStatus();
- },
- methods: {
- get_logo_url: function () {
- if (wx.getStorageSync('logo_url')) return this.setData({ logo_url: wx.getStorageSync('logo_url') });
- app.baseGet(app.U({ c: 'public_api', a: 'get_logo_url' }), function (res) {
- wx.setStorageSync('logo_url', res.data.logo_url);
- this.setData({ logo_url: res.data.logo_url });
- }.bind(this));
- },
- //监听登录状态
- WatchIsLogin: function () {
- this.data.cloneIner = setInterval(function () {
- //防止死循环,超过错误次数终止监听
- if (this.getErrorCount()) return clearInterval(this.data.cloneIner);
- if (app.globalData.token == '' && this.data.loading===false) this.setAuthStatus();
- }.bind(this),800);
- this.setData({ cloneIner: this.data.cloneIner });
- },
- //检测登录状态并执行自动登录
- setAuthStatus() {
- var that = this;
- that.setErrorCount();
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.userInfo']) {
- //没有授权不会自动弹出登录框
- if (that.data.isAuto === false) return;
- //自动弹出授权
- that.setData({ iShidden: false });
- } else {
- //自动登录
- that.setData({ iShidden: true });
- if (app.globalData.token) {
- that.triggerEvent('onLoadFun', app.globalData.token);
- that.WatchIsLogin();
- } else {
- wx.showLoading({ title: '正在登录中' });
- that.getUserInfoBydecryptCode();
- }
- }
- }
- })
- },
- //访问服务器获得cache_key
- setCode(code, successFn, errotFn) {
- var that = this;
- that.setData({ loading: true });
- app.basePost(app.U({ c: 'Login', a: 'setCode' }), { code: code }, function (res) {
- that.setData({ loading: false });
- wx.setStorage({ key: 'cache_key', data: res.data.cache_key});
- successFn && successFn(res);
- }, function (res) {
- that.setData({ loading: false });
- if (errotFn) errotFn(res);
- else return app.Tips({ title: '获取cache_key失败' });
- });
- },
- //获取code
- getSessionKey(code, successFn, errotFn) {
- var that = this;
- wx.checkSession({
- success: function (res) {
- wx.getStorage({
- key:'cache_key',
- success:function(res){
- if (res.data){
- successFn && successFn();
- }else{
- that.setCode(code, successFn, errotFn);
- }
- },
- fail(res){
- that.setCode(code, successFn, errotFn);
- },
- });
- },
- fail: function () {
- that.setCode(code, successFn, errotFn);
- }
- });
- },
- login:function(){
- var that=this;
- wx.login({
- success: function (res) {
- if (!res.code) return app.Tips({ title: '登录失败!' + res.errMsg });
- //获取cache_key并缓存
- that.getSessionKey(res.code, function () {
- that.getUserInfoBydecryptCode();
- });
- },
- fail() {
- wx.hideLoading();
- }
- })
- },
- //授权
- setUserInfo(e) {
- wx.showLoading({ title: '正在登录中' });
- this.login();
- },
- close: function () {
- if (this.data.isAuto) return;
- this.setData({ iShidden: true });
- },
- //登录获取访问权限
- getUserInfoBydecryptCode: function () {
- var that = this;
- if (this.getErrorCount()){
- this.setData({ iShidden: false, ErrorCount: 0 });
- return app.Tips({ title: '获取code失败,请重新授权尝试获取!' });
- }
- wx.getStorage({
- key:'cache_key',
- success:function(res){
- if(res.data){
- var cache_key = res.data;
- wx.getUserInfo({
- lang: 'zh_CN',
- success: function (res) {
- var pdata = {};
- pdata.spid = app.globalData.spid;//获取推广人ID
- pdata.code = app.globalData.code;//获取推广人分享二维码ID
- if (res.iv) {
- pdata.iv = encodeURI(res.iv);
- pdata.encryptedData = res.encryptedData;
- pdata.cache_key = cache_key;
- //获取用户信息生成访问token
- that.setData({ loading: true });
- app.basePost(app.U({ c: 'login', a: 'index' }), pdata, function (res) {
- that.setData({ loading: false });
- if (res.data.status == 0) return app.Tips({ title: '抱歉,您已被禁止登录!' });
- else if (res.data.status == 410) {
- wx.clearStorage();
- wx.hideLoading();
- that.setErrorCount();
- that.login();
- return false;
- }
- //取消登录提示
- wx.hideLoading();
- //关闭登录弹出窗口
- that.setData({ iShidden: true, ErrorCount: 0 });
- //保存token和记录登录状态
- app.globalData.token = res.data.token;
- app.globalData.isLog = true;
- //执行登录完成回调
- that.triggerEvent('onLoadFun', app.globalData.uid);
- //清除定时器
- if (that.data.cloneIner) clearInterval(that.data.cloneIner);
- //监听登录状态
- that.WatchIsLogin();
- }, function (res) {
- that.setData({ loading: false });
- wx.hideLoading();
- that.setErrorCount();
- wx.clearStorage();
- return app.Tips({ title: res.msg });
- });
- } else {
- wx.hideLoading();
- wx.clearStorage();
- that.setErrorCount();
- return app.Tips({ title: '用户信息获取失败!' });
- }
- },
- fail: function () {
- wx.hideLoading();
- wx.clearStorage();
- that.setErrorCount();
- if (that.data.isAuto) that.login();
- },
- })
- }else{
- wx.hideLoading();
- wx.clearStorage();
- that.setErrorCount();
- if (that.data.isAuto) that.login();
- return false;
- }
- },
- fail:function(){
- wx.hideLoading();
- wx.clearStorage();
- that.setErrorCount();
- if (that.data.isAuto) that.login();
- }
- })
- },
- /**
- * 处理错误次数,防止死循环
- *
- */
- setErrorCount: function () {
- if (!this.data.ErrorCount) this.data.ErrorCount = 1;
- else this.data.ErrorCount++;
- this.setData({ ErrorCount: this.data.ErrorCount });
- },
- /**
- * 获取错误次数,是否终止监听
- *
- */
- getErrorCount: function () {
- return this.data.ErrorCount >= 10 ? true : false;
- }
- },
- })
|