1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // app.js
- import Activity from './api/activity';
- App({
- onLaunch() {
- // 展示本地存储能力
- // const logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs)
- // 登录
- var openId = wx.getStorageSync('loginInfo').openId
- if(!openId){
- this.login();
- }
-
- },
- checkInvalid(param={},callBack='') {
- let _self = this;
- wx.checkSession({
- success () {
- //session_key 未过期,并且在本生命周期一直有效
- if(typeof(callBack)==='function'){
- callBack(param);
- }
- },
- fail () {
- _self.login(function(){
- // session_key 已经失效,需要重新执行登录流程
- if(typeof(callBack)==='function'){
- callBack(param);
- }
- });
-
- }
- })
- },
- // 登录
- login() {
- wx.login({
- success: res => {
- let code = res.code;
- let result = Activity.getSessionKeyFromApi(code);
- result.then(res=>{
- wx.setStorageSync('loginInfo',{
- openId:res.openId,
- sessionKey:res.sessionKey
- });
- });
- // if(typeof(call)=='function') {
- // call();
- // }
- }
- })
- },
- /**
- * 接口请求手机号
- * @param {*} encryptedData
- * @param {*} iv
- * @param call
- */
- async doDecodePhone(encryptedData,iv,call='') {
- let _self = this;
- //验证登录是否失效
- _self.checkInvalid({encryptedData:encryptedData,iv:iv,call:call},function(param){
- var params = {
- encryptedData: param.encryptedData,
- iv: param.iv,
- sessionKey:Activity.getSessionKey(),
- };
- try {
- let res = Activity.getAuthMobile(params);
- res.then(userInfo=>{
- let mobile = userInfo.phoneNumber;
- // @todo从缓存里面取openId
- let openId = Activity.getOpenId();
- //保存用户信息
- Activity.saveUser(openId,mobile);
- Activity.setMobileCache(mobile);
- if(param.call && typeof(param.call)=='function') {
- param.call();
- }
- })
- }catch(err) {
- console.log(err);
- }
- });
- },
- globalData: {
- userInfo: null,
- appCode:'HSAY_SHARE_COUPON',
- typeSwitchObj : {"C":'现金券',"D":"折扣券"}
- }
- })
|