|
|
@@ -8,11 +8,12 @@ import axios, {
|
|
|
InternalAxiosRequestConfig
|
|
|
} from '@ohos/axios';
|
|
|
import { IBestToast, YTLog } from '../../../../Index';
|
|
|
-import { reqString } from '../models';
|
|
|
-import { UserInfo, userInfo } from '../models/UserInfo';
|
|
|
+import { AppStorageKeyCollect } from '../constants';
|
|
|
+import { ReqString } from '../models';
|
|
|
+import { UserInfo } from '../models/UserInfo';
|
|
|
import { formatDate } from './FormatDate';
|
|
|
-import { huaweiAuthPlugin } from './HuaWeiAuthPlugin';
|
|
|
-import { yTRouter } from './YTRouter';
|
|
|
+import { HuaweiAuthPlugin } from './HuaWeiAuthPlugin';
|
|
|
+import { YTRouter } from './YTRouter';
|
|
|
|
|
|
|
|
|
export const baseURL: string = 'https://hm-test.ytpm.net/prod-api'
|
|
|
@@ -26,8 +27,8 @@ export const instance = axios.create({
|
|
|
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
|
|
|
|
// 对请求数据做点什么
|
|
|
- if (AppStorage.get<string>('token')) {
|
|
|
- config.headers.Authorization = AppStorage.get<string>('token')
|
|
|
+ if (AppStorage.get<string>(AppStorageKeyCollect.TOKEN)) {
|
|
|
+ config.headers.Authorization = AppStorage.get<string>(AppStorageKeyCollect.TOKEN)
|
|
|
}
|
|
|
return config;
|
|
|
}, (error: AxiosError) => {
|
|
|
@@ -43,8 +44,8 @@ instance.interceptors.response.use((response: AxiosResponse) => {
|
|
|
YTLog.info(response)
|
|
|
// 对响应错误做点什么
|
|
|
if (response.data.code == 401) {
|
|
|
- userInfo.logout()
|
|
|
- yTRouter.router2LoginPage()
|
|
|
+ UserInfo.getInstance().logout()
|
|
|
+ YTRouter.router2LoginPage()
|
|
|
return Promise.reject('401');
|
|
|
}
|
|
|
|
|
|
@@ -61,25 +62,25 @@ instance.interceptors.response.use((response: AxiosResponse) => {
|
|
|
});
|
|
|
|
|
|
|
|
|
-class YtRequest {
|
|
|
- private productName: string = 'chat'
|
|
|
+export class YTRequest {
|
|
|
+ private static productName: string = 'chat'
|
|
|
|
|
|
- get<T>(url: string, params?: Record<string, string | number | boolean>,
|
|
|
+ static get<T>(url: string, params?: Record<string, string | number | boolean>,
|
|
|
headers?: Record<string, string>): Promise<T> {
|
|
|
return instance.get<null, T, null>(url, { params, headers })
|
|
|
}
|
|
|
|
|
|
- post<T, D>(url: string, data?: D, params?: Record<string, string | number | boolean>, headers?: AxiosHeaders) {
|
|
|
+ static post<T, D>(url: string, data?: D, params?: Record<string, string | number | boolean>, headers?: AxiosHeaders) {
|
|
|
return instance.post<null, T, D>(url, data, { params, headers })
|
|
|
}
|
|
|
|
|
|
- upPost<T, D>(url: string, data: D, configs?: AxiosRequestConfig<D>) {
|
|
|
+ static upPost<T, D>(url: string, data: D, configs?: AxiosRequestConfig<D>) {
|
|
|
return instance.post<string, T, D>(url, data, configs)
|
|
|
}
|
|
|
|
|
|
//获取验证码
|
|
|
- getCaptcha(phonenumber: string, success: (res: string) => void, fail: (err: Error) => void) {
|
|
|
- yTRequest.post<reqString, reqString>(`/api/${this.productName}/member/sendSmsCode`,
|
|
|
+ static getCaptcha(phonenumber: string, success: (res: string) => void, fail: (err: Error) => void) {
|
|
|
+ YTRequest.post<ReqString, ReqString>(`/api/${YTRequest.productName}/member/sendSmsCode`,
|
|
|
{ 'phonenumber': phonenumber })
|
|
|
.then(res => {
|
|
|
success(res['uuid'])
|
|
|
@@ -90,42 +91,42 @@ class YtRequest {
|
|
|
}
|
|
|
|
|
|
//手机号登录
|
|
|
- phonenumberLogin(param: reqString) {
|
|
|
+ static phonenumberLogin(param: ReqString) {
|
|
|
const uuid = AppStorage.get<string>('uuid')
|
|
|
if (uuid !== undefined) {
|
|
|
- yTRequest.post<reqString, reqString>(`/api/${this.productName}/member/phoneLogin`, {
|
|
|
+ YTRequest.post<ReqString, ReqString>(`/api/${YTRequest.productName}/member/phoneLogin`, {
|
|
|
'phonenumber': param['phonenumber'],
|
|
|
'smsCode': param['captcha'],
|
|
|
'uuid': uuid
|
|
|
})
|
|
|
.then(res => {
|
|
|
- userInfo.setToken(res['token'])
|
|
|
- yTRequest.refreshUserInfo(() => {
|
|
|
+ UserInfo.getInstance().setToken(res[AppStorageKeyCollect.TOKEN])
|
|
|
+ YTRequest.refreshUserInfo(() => {
|
|
|
IBestToast.show({ message: '登录成功' })
|
|
|
- yTRouter.routerBack()
|
|
|
+ YTRouter.routerBack()
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//华为登录
|
|
|
- huaweiLogin() {
|
|
|
+ static huaweiLogin() {
|
|
|
try {
|
|
|
IBestToast.showLoading()
|
|
|
- huaweiAuthPlugin.requestAuth()
|
|
|
+ HuaweiAuthPlugin.requestAuth()
|
|
|
.then(res => {
|
|
|
- yTRequest.post<reqString, reqString>(`/api/${this.productName}/member/hmLogin`,
|
|
|
- { 'code': res } as reqString)
|
|
|
+ YTRequest.post<ReqString, ReqString>(`/api/${YTRequest.productName}/member/hmLogin`,
|
|
|
+ { 'code': res } as ReqString)
|
|
|
.then(data => {
|
|
|
const token = data['token']
|
|
|
- userInfo.setToken(token)
|
|
|
- yTRequest.refreshUserInfo((userInfo) => {
|
|
|
+ UserInfo.getInstance().setToken(token)
|
|
|
+ YTRequest.refreshUserInfo((userInfo) => {
|
|
|
// YTLog.info(userInfo)
|
|
|
IBestToast.hide()
|
|
|
setTimeout(() => {
|
|
|
IBestToast.show({ message: '登录成功' })
|
|
|
}, 100)
|
|
|
- yTRouter.routerBack()
|
|
|
+ YTRouter.routerBack()
|
|
|
})
|
|
|
})
|
|
|
.catch((err: Error) => {
|
|
|
@@ -147,20 +148,20 @@ class YtRequest {
|
|
|
}
|
|
|
|
|
|
//刷新用户信息
|
|
|
- refreshUserInfo(success?: (res: UserInfo) => void) {
|
|
|
- yTRequest.post<UserInfo, null>(`/api/${this.productName}/member/info`)
|
|
|
+ static refreshUserInfo(success?: (res: UserInfo) => void) {
|
|
|
+ YTRequest.post<UserInfo, null>(`/api/${YTRequest.productName}/member/info`)
|
|
|
.then(res => {
|
|
|
- userInfo.setUserInfoAndLogin(res)
|
|
|
- YTLog.info(userInfo)
|
|
|
+ UserInfo.getInstance().setUserInfoAndLogin(res)
|
|
|
+ YTLog.info(UserInfo.getInstance())
|
|
|
success?.(res)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
//上传文件
|
|
|
- uploadFile(context: Context, fullpath: string, success: (url: string) => void) {
|
|
|
+ static uploadFile(context: Context, fullpath: string, success: (url: string) => void) {
|
|
|
const formData = new FormData()
|
|
|
formData.append('file', fullpath)
|
|
|
- yTRequest.upPost<reqString, FormData>('/common/upload', formData, {
|
|
|
+ YTRequest.upPost<ReqString, FormData>('/common/upload', formData, {
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
context,
|
|
|
onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
|
|
|
@@ -175,11 +176,11 @@ class YtRequest {
|
|
|
}
|
|
|
|
|
|
// 修改用户头像
|
|
|
- uploadHeadImg(context: Context, fullpath: string, success: () => void) {
|
|
|
+ static uploadHeadImg(context: Context, fullpath: string, success: () => void) {
|
|
|
const formData = new FormData()
|
|
|
formData.append('file', fullpath)
|
|
|
|
|
|
- yTRequest.upPost<reqString, FormData>('/common/upload', formData, {
|
|
|
+ YTRequest.upPost<ReqString, FormData>('/common/upload', formData, {
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
context,
|
|
|
onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
|
|
|
@@ -189,7 +190,7 @@ class YtRequest {
|
|
|
})
|
|
|
.then(res => {
|
|
|
const url = res['url']
|
|
|
- yTRequest.post<null, reqString>(`/api/${this.productName}/member/modifyMemberIcon`, { 'memberIcon': url })
|
|
|
+ YTRequest.post<null, ReqString>(`/api/${YTRequest.productName}/member/modifyMemberIcon`, { 'memberIcon': url })
|
|
|
.then(() => {
|
|
|
success()
|
|
|
})
|
|
|
@@ -204,25 +205,25 @@ class YtRequest {
|
|
|
}
|
|
|
|
|
|
// 修改用户昵称
|
|
|
- changeNickname(name: string, success: () => void) {
|
|
|
- yTRequest.post<null, reqString>(`/api/${this.productName}/member/modifyMemberName`, { 'memberName': name })
|
|
|
+ static changeNickname(name: string, success: () => void) {
|
|
|
+ YTRequest.post<null, ReqString>(`/api/${YTRequest.productName}/member/modifyMemberName`, { 'memberName': name })
|
|
|
.then(() => {
|
|
|
- yTRequest.refreshUserInfo(() => {
|
|
|
+ YTRequest.refreshUserInfo(() => {
|
|
|
success()
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
//问题反馈
|
|
|
- questionBack(des: string, createBy: string) {
|
|
|
- yTRequest.post<null, reqString>(`/api/${this.productName}/question/saveQuestion`, {
|
|
|
+ static questionBack(des: string, createBy: string) {
|
|
|
+ YTRequest.post<null, ReqString>(`/api/${YTRequest.productName}/question/saveQuestion`, {
|
|
|
'backQuestion': des,
|
|
|
'createBy': createBy,
|
|
|
'createTime': formatDate(new Date()),
|
|
|
})
|
|
|
.then(() => {
|
|
|
IBestToast.show("反馈成功")
|
|
|
- yTRouter.routerBack()
|
|
|
+ YTRouter.routerBack()
|
|
|
})
|
|
|
.catch((e: Error) => {
|
|
|
YTLog.error(e)
|
|
|
@@ -230,5 +231,4 @@ class YtRequest {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const yTRequest = new YtRequest()
|
|
|
|