const CONFIG = require('./config.js') App({ globalData:{ viewerId:null, userInfo:null }, onLaunch(){ this.register() }, login: function(callback) { let _m = this if (!viewerId && CONFIG.IS_DEBUG) { viewerId = wx.getStorageSync('user_id'); } if (viewerId) { _m.globalData.viewerId = viewerId; callback && callback(); } else { wx.checkSession({ success: (res) => { console.log('warning wx.checkSession OK, but no viewerId', res); }, fail: (res) => { console.log('wx.checkSession failed:', res); }, complete: () => { wx.login({ success: (res) => { console.log('wx.login success:', res); API.login.wechat({ js_code: res.code }, d => { console.log('private login response:', d); if (d.code === 0) { console.log('private login success:', d); let viewerId = d.data.user.user_id; _m.globalData.viewerId = viewerId; wx.setStorageSync('user_id', viewerId); callback && callback(); } else { console.error('get user_id error'); } }, { ignoreError: true }); }, fail: (res) => { console.log('wx.login failed:', res); } }); } }); } }, register: function(needTry, callback){ !callback && (callback = function(){}); this.login(()=>{
wx.getUserInfo({ success: (res) => { let params = {}; this.globalData.userInfo = res.userInfo; params.owner = { id: this.globalData.viewerId, connected_profile: { nickname : res.userInfo.nickName||'', profile_pic_url: res.userInfo.avatarUrl||'', language : res.userInfo.language||'', gender : res.userInfo.gender, geo: { country : res.userInfo.country, province: res.userInfo.province, city : res.userInfo.city } } } API.profile.update(params, (d) => { if(d.code === 0) { try { wx.setStorageSync('USERINFO.'+ this.globalData.viewerId, this.globalData.userInfo); wx.setStorageSync('REGISTED.'+ this.globalData.viewerId, (new Date).getTime()); } catch (e) {} callback(); } }, { ignoreError: true }); }, fail: () => { console.log('get user info failed: not authorized.', arguments); if (needTry) { wx.openSetting({ success: (res)=> { if (res.authSetting['scope.userInfo']) { wx.showToast({ title: LANG.AuthorizeSuccess, duration: CONFIG.SHOWTOAST_DURATION, }); } }, fail: (res)=> { console.log('user not permit to authorize.', arguments); } }); } }, withCredentials: false }); }); }, init: function(callback) { this.login(()=>{ let pageInstance = this.getCurrentPageInstance(), context, screenWidth, screenHeight;
context = { LANG : LANG, CDN : CONFIG.CDN_HOST, isNoContent : false, HashtagType : CONFIG.HashtagType, VerbType : CONFIG.VerbType, GridImageWidthMode : CONFIG.GridImageWidthMode, STICKER_MAKER_ENABLED: CONFIG.STICKER_MAKER_ENABLED, UGC_ENABLED : CONFIG.UGC_ENABLED, UGC_IMAGE_COUNT_LIMIT: CONFIG.UGC_IMAGE_COUNT_LIMIT, ReviewStateText : CONFIG.ReviewStateText,
networkType : this.globalData.device.network ? this.globalData.device.network.network_type : NetworkType.UNKNOWN,
IS_DEV : CONFIG.IS_DEV, IS_SHOW_CONSOLE : CONFIG.IS_SHOW_CONSOLE, DEBUG_DATA : [],
CONFIG : CONFIG,
videoPlayStatus : {}, CURRENT_PAGE : pageInstance.data.PAGE,
hideVideo : false, updated_time : (new Date).getTime() };
pageInstance.setData({ context: context });
this.sendLaunchEvent();
callback && callback(); }) } })
|