feat:first commit

This commit is contained in:
2024-08-16 14:04:26 +08:00
commit f276677d09
72 changed files with 33361 additions and 0 deletions

132
src/request/index.ts Normal file
View File

@@ -0,0 +1,132 @@
import { HttpMethod } from '@/utils/https'
import { CodeStatus } from '@/utils/common'
import { API_URL, getToken, setToken, showToast } from '@/utils/util'
import store from '@/store'
interface RequestData {
type: HttpMethod
url: string
data?: any
header?: any
notice?: boolean
returnData?: boolean
}
export const ContentTypeEnum = {
JSON: 'application/json;charset=UTF-8',
FORM: 'application/x-www-form-urlencoded',
FORM_DATA: 'multipart/form-data'
}
let requestCount = 0 // 请求计数器
const showLoading = () => {
if (requestCount === 0) {
uni.showLoading({ mask: true })
}
requestCount++
}
const hideLoading = () => {
if (requestCount <= 1) {
uni.hideLoading()
}
if (requestCount > 0) {
requestCount--
}
}
export const request = ({ type, url, data, header = {}, returnData, notice }: RequestData) => {
return new Promise((resolve) => {
if (notice) {
showLoading()
}
uni.request({
method: type,
url: API_URL + url,
data,
header: {
// 医生小程序token
assessToken: getToken(),
// 医院管理后台token
// adminAuthToken: getToken(),
...header
},
success: async (res: any) => {
if (res.data.code === CodeStatus.SUCCESS) {
return resolve(returnData ? res : res.data)
}
if (res.data.code === CodeStatus.NO_LOGIN || res.data.code === CodeStatus.SYSTEM_ERROR) {
setToken('')
await store.dispatch('UserStore/setUserInfo', null)
await uni.reLaunch({ url: '/pages/login/login' })
return resolve(null)
}
return resolve(null)
},
fail: (err) => {
return resolve(null)
},
complete: (res: any) => {
if (notice) {
hideLoading()
}
if (res.data.code !== CodeStatus.SUCCESS) {
showToast(res.data.msg)
}
}
})
})
}
interface RequestParams {
url: string
data?: any
header?: any
returnData?: boolean
notice?: boolean
}
export const get = ({ url, data, header, returnData = false, notice = true }: RequestParams): Promise<null | any> => {
return request({ type: HttpMethod.GET, url, data, header, returnData, notice })
}
export const post = ({ url, data, header, returnData = false, notice = true }: RequestParams): Promise<null | any> => {
return request({ type: HttpMethod.POST, url, data, header, returnData, notice })
}
export const del = ({ url, data, header, returnData = false, notice = true }: RequestParams): Promise<null | any> => {
return request({ type: HttpMethod.DELETE, url, data, header, returnData, notice })
}
export const put = ({ url, data, header, returnData = false, notice = true }: RequestParams): Promise<null | any> => {
return request({ type: HttpMethod.PUT, url, data, header, returnData, notice })
}
export const uploadFile = ({ url = '', tempFilePaths = [] }): Promise<null | any> => {
return new Promise((resolve) => {
uni.showLoading({
mask: true
})
uni.uploadFile({
url: API_URL + url, // 仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
header: {
assessToken: getToken()
},
success: (uploadFileRes) => {
uni.hideLoading()
const result = JSON.parse(uploadFileRes.data)
return resolve(result)
},
fail: (err) => {
uni.hideLoading()
showToast(err.errMsg)
return resolve(null)
}
})
})
}

View File

@@ -0,0 +1,14 @@
import { post } from '@/request'
const userApi = {
login: '/api/login',
}
type loginParams = {
username: string
password: string
}
export const login = (data: loginParams) => post({ url: userApi.login, data })
export default userApi