fix checklogin & getinfo in app.ts
parent
1f0d4881f3
commit
574d3692cd
14
src/app.ts
14
src/app.ts
|
|
@ -6,17 +6,25 @@ import { getLocaleData } from './service/localeData';
|
|||
import { checkLogin, checkToken } from './service/checkLogin';
|
||||
import wechatUser from './wechat';
|
||||
import { loginAte } from './service/login';
|
||||
import { getMemberInfo } from './service/memberInfo';
|
||||
import { getInfo } from './service/getInfo';
|
||||
|
||||
function App({ children }: PropsWithChildren<any>) {
|
||||
useLaunch(() => {
|
||||
console.log('App launched.');
|
||||
if (checkLogin()) {
|
||||
// if (checkLogin()) {
|
||||
// wechatUser.setToken(checkToken());
|
||||
// if(getMemberInfo());
|
||||
// } else {
|
||||
// loginAte();
|
||||
// }
|
||||
checkLogin((success) => {
|
||||
if (success) {
|
||||
wechatUser.setToken(checkToken());
|
||||
getMemberInfo();
|
||||
getInfo();
|
||||
} else {
|
||||
loginAte();
|
||||
}
|
||||
});
|
||||
getLocaleData();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
export type FixStatus = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
||||
|
||||
export const defaultAvatar =
|
||||
'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
|
||||
|
||||
export type Info = {
|
||||
phone: string;
|
||||
name: string;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import Taro from '@tarojs/taro';
|
||||
import { getUrl } from '.';
|
||||
|
||||
export function checkLogin(): boolean {
|
||||
export function checkLogin(callback: (success: boolean) => void) {
|
||||
let token = checkToken();
|
||||
if (token.length !== 0) {
|
||||
Taro.request({
|
||||
|
|
@ -13,16 +13,17 @@ export function checkLogin(): boolean {
|
|||
})
|
||||
.then((res) => {
|
||||
if (res.data.data.islogin) {
|
||||
return true;
|
||||
callback(true);
|
||||
}
|
||||
return false;
|
||||
callback(false);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return false;
|
||||
callback(false);
|
||||
});
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function checkToken(): string {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
import Taro from '@tarojs/taro';
|
||||
import wechatUser, { UserInfo } from '@/wechat';
|
||||
import { defaultAvatar } from '@/common';
|
||||
import { getUrl } from '.';
|
||||
|
||||
export function getInfo() {
|
||||
Taro.request({
|
||||
url: getUrl('/member/login'),
|
||||
method: 'POST',
|
||||
data: {
|
||||
token: wechatUser.getToken(),
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
const data = res.data.data;
|
||||
if (res.data.success) {
|
||||
wechatUser.setInfo(data as UserInfo);
|
||||
wechatUser.setAccess(true);
|
||||
Taro.reLaunch({
|
||||
url: '/pages/member/member',
|
||||
});
|
||||
} else {
|
||||
Taro.request({
|
||||
url: getUrl('/user/info'),
|
||||
method: 'GET',
|
||||
data: {
|
||||
token: wechatUser.getToken(),
|
||||
},
|
||||
})
|
||||
.then((re) => {
|
||||
const info = re.data.data;
|
||||
const userInfo: UserInfo = {
|
||||
name: info.name,
|
||||
phone: info.phone,
|
||||
avatar: defaultAvatar,
|
||||
};
|
||||
wechatUser.setInfo(userInfo);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ export function loginAte() {
|
|||
console.log(err);
|
||||
});
|
||||
} else {
|
||||
console.log('登陆失败' + res.errMsg);
|
||||
console.log('Login Failed: ' + res.errMsg);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
import Taro from '@tarojs/taro';
|
||||
import wechatUser, { UserInfo } from '@/wechat';
|
||||
import { getUrl } from '.';
|
||||
|
||||
export function getMemberInfo() {
|
||||
Taro.request({
|
||||
url: getUrl('/member/login'),
|
||||
method: 'POST',
|
||||
data: {
|
||||
token: wechatUser.getToken(),
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.data.success) {
|
||||
wechatUser.setInfo(res.data.data as UserInfo);
|
||||
wechatUser.setAccess(true);
|
||||
Taro.reLaunch({
|
||||
url: '/pages/member/member',
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import RepairPage from '@/pages/repair/repair';
|
||||
import Taro from '@tarojs/taro';
|
||||
import pt from '@/plain-text';
|
||||
import wechatUser, { UserInfo } from '@/wechat';
|
||||
import { defaultAvatar } from '@/common';
|
||||
import { getUrl } from '.';
|
||||
|
||||
const relaunchInterval = 1000;
|
||||
|
|
@ -9,6 +11,35 @@ export function submitTicket(that: RepairPage) {
|
|||
that.setState({
|
||||
isLoading: true,
|
||||
});
|
||||
if (!wechatUser.getAccess()) {
|
||||
const userInfo = wechatUser.getInfo();
|
||||
if (userInfo.name == '' || userInfo.phone == '') {
|
||||
Taro.request({
|
||||
url: getUrl('/user/update'),
|
||||
method: 'POST',
|
||||
data: {
|
||||
token: 'token_test',
|
||||
name: that.state.owner,
|
||||
phone: that.state.phone,
|
||||
},
|
||||
})
|
||||
.then(() => {
|
||||
const userTicketInfo: UserInfo = {
|
||||
name: that.state.owner,
|
||||
phone: that.state.phone,
|
||||
avatar: defaultAvatar,
|
||||
};
|
||||
wechatUser.setInfo(userTicketInfo);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
Taro.atMessage({
|
||||
message: pt.get().button.submitText.error + err.toString(),
|
||||
type: 'error',
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Taro.request({
|
||||
url: getUrl('/tickets/create'),
|
||||
method: 'POST',
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { defaultAvatar } from '@/common';
|
||||
|
||||
export interface UserInfo {
|
||||
name: string;
|
||||
phone: string;
|
||||
|
|
@ -13,8 +15,7 @@ class WechatUser {
|
|||
this.userInfo = {
|
||||
name: '',
|
||||
phone: '',
|
||||
avatar:
|
||||
'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
|
||||
avatar: defaultAvatar,
|
||||
};
|
||||
this.isMember = false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue