180 lines
5.2 KiB
JavaScript
180 lines
5.2 KiB
JavaScript
import userApi from './api/user';
|
||
import commonApi from './api/common';
|
||
let token = wx.getStorageSync('accessToken');
|
||
App({
|
||
towxml:require('./miniprogram_npm/@beefast-wxmp/towxml/index'),
|
||
async onLaunch(options){
|
||
wx.getStorage({
|
||
key:'accessToken',
|
||
success:(res)=>{
|
||
this.globalData.accessToken = res.data;
|
||
}
|
||
});
|
||
wx.onAppRoute((res)=>{
|
||
const page = getCurrentPages();
|
||
const currentPage = page[page.length-1];
|
||
if(currentPage){
|
||
currentPage.onShareAppMessage = async()=>{
|
||
if(!(this.globalData.appConfig&&this.globalData.appConfig.share_card_title)){
|
||
await this.getAppConfig();
|
||
}
|
||
if(!(this.globalData.userInfo&&this.globalData.userInfo.user_code)){
|
||
await this.getUserInfo();
|
||
}
|
||
return {
|
||
title:this.globalData.appConfig.share_card_title,
|
||
imageUrl:'/assets/imgs/login/share.jpg',
|
||
path:`/pages/help/index/index?shared_user_code=${this.globalData.userInfo.user_code||''}`
|
||
}
|
||
}
|
||
}
|
||
})
|
||
},
|
||
onShow(options){
|
||
/**
|
||
* shared_user_code 通过分享进来的 分享者 的user_code
|
||
* 这里必须放到onshow 里 才能实时获取 code 先保存在这里,再跳转登录的时候通过 url 带过去,防止刷新丢失 code
|
||
* wx.getLaunchOptionsSync 也不行,比如用户开始自行打开了 app,然后再次点朋友的进来,就无法获取 code
|
||
*/
|
||
if(options.query.shared_user_code){
|
||
this.globalData.shared_user_code = options.query.shared_user_code;
|
||
}
|
||
},
|
||
navToLogin(){
|
||
wx.reLaunch({
|
||
url: '/pages/login/login',
|
||
})
|
||
},
|
||
forceGetUserInfo(){
|
||
this.globalData.userInfoGetTime = null;
|
||
return this.getUserInfo();
|
||
},
|
||
async getUserInfo(){
|
||
if(this.globalData.userInfoGetTime&&
|
||
this.globalData.userInfo&&
|
||
new Date()-this.globalData.userInfoGetTime<1000*60*5){
|
||
return this.globalData.userInfo;
|
||
}
|
||
const data = await userApi.info();
|
||
this.globalData.userInfo = data;
|
||
this.globalData.userInfoGetTime = new Date();
|
||
return data;
|
||
},
|
||
async getAppConfig(){
|
||
if(!this.globalData.appConfig){
|
||
const data = await commonApi.getConfig();
|
||
this.globalData.appConfig = {};
|
||
data.map((item)=>{
|
||
this.globalData.appConfig[item.key] = item.value;
|
||
})
|
||
}
|
||
return this.globalData.appConfig;
|
||
},
|
||
getLocation(){
|
||
return new Promise((rs,rj)=>{
|
||
if(this.globalData.locationGetTime&&
|
||
this.globalData.location&&
|
||
new Date()-this.globalData.locationGetTime<1000*60*1){
|
||
rs(this.globalData.location);
|
||
}
|
||
wx.authorize({
|
||
scope: 'scope.userLocation',
|
||
success:(res)=>{
|
||
wx.getLocation({
|
||
success:(_res)=>{
|
||
this.globalData.location = _res;
|
||
this.globalData.locationGetTime = new Date();
|
||
rs(_res)
|
||
},
|
||
fail(res){
|
||
rj();
|
||
}
|
||
});
|
||
},
|
||
fail:()=>{
|
||
rj();
|
||
}
|
||
})
|
||
})
|
||
},
|
||
globalData: {
|
||
userInfo: null,
|
||
accessToken:token,
|
||
appConfig:null
|
||
},
|
||
validateForm(rules,page){
|
||
const result = [];
|
||
for(var key in rules){
|
||
((rules[key] instanceof Array)?rules[key]:[rules[key]]).map((item)=>{
|
||
let valid = true;
|
||
let value = (page.data[key]+'').trim();
|
||
//非空
|
||
if(item.required){
|
||
if(value==''){
|
||
valid = false;
|
||
}
|
||
}else if(item.length){
|
||
//绝对长度
|
||
if(value.length!=item.length){
|
||
valid = false;
|
||
}
|
||
}else if(item.type=='phone'){
|
||
//验证电话号码
|
||
if(value.length!=11){
|
||
valid = false;
|
||
}
|
||
}else if(item.maxLength||item.minLength){
|
||
//字符串长度
|
||
if(value.length>(item.maxLength||Infinity)||value.length<item.minLength||0){
|
||
valid = false
|
||
}
|
||
}else if(item.min){
|
||
if(parseInt(value)<item.min){
|
||
valid = false;
|
||
}
|
||
}
|
||
if(valid){
|
||
page.setData({
|
||
[`${key}Message`]:''
|
||
});
|
||
}else{
|
||
page.setData({
|
||
[`${key}Message`]:item.message
|
||
});
|
||
result.push({
|
||
[key]:item.message,autoFocus:item.autoFocus,key:key,shake:item.shake
|
||
})
|
||
}
|
||
})
|
||
}
|
||
const focusInput = result.find((item)=>item.autoFocus);
|
||
if(focusInput){
|
||
page.setData({
|
||
[`${focusInput.key}Focus`]:true
|
||
})
|
||
}
|
||
const shakeInput = result.find((item)=>item.shake);
|
||
if(shakeInput){
|
||
if(!shakeInput.animation){
|
||
shakeInput.animation = wx.createAnimation({
|
||
duration: 20,
|
||
})
|
||
}
|
||
shakeInput.animation.translateX(10).step();
|
||
shakeInput.animation.translateX(-10).step();
|
||
shakeInput.animation.translateX(10).step();
|
||
shakeInput.animation.translateX(-10).step();
|
||
shakeInput.animation.translateX(5).step();
|
||
shakeInput.animation.translateX(0).step();
|
||
// needSetData[`${key}Animation`] = item.animation.export();
|
||
page.setData({
|
||
[`${shakeInput.key}Animation`]:shakeInput.animation.export()
|
||
})
|
||
}
|
||
return result;
|
||
},
|
||
getAddressList(){
|
||
|
||
}
|
||
})
|