调整登录,调整部分接口路径,调整上传图片集合到基本请求中去
This commit is contained in:
parent
decbccedce
commit
79bdca1412
|
|
@ -1,14 +1,18 @@
|
||||||
import { client } from '@/utils/client'
|
|
||||||
import request from '@/utils/request'
|
import request from '@/utils/request'
|
||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
export function login(data: Record<string, any>) {
|
export function mobileLogin(data: Record<string, any>) {
|
||||||
return request.post({ url: '/login/check', data: { ...data, client } })
|
return request.post({ url: '/login/mobileLogin', data })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录
|
||||||
|
export function accountLogin(data: Record<string, any>) {
|
||||||
|
return request.post({ url: '/login/accountLogin', data })
|
||||||
}
|
}
|
||||||
|
|
||||||
//注册
|
//注册
|
||||||
export function register(data: Record<string, any>) {
|
export function register(data: Record<string, any>) {
|
||||||
return request.post({ url: '/login/register', data: { ...data, client } })
|
return request.post({ url: '/login/register', data })
|
||||||
}
|
}
|
||||||
|
|
||||||
//忘记密码
|
//忘记密码
|
||||||
|
|
@ -21,6 +25,13 @@ export function getWxCodeUrl() {
|
||||||
return request.get({ url: '/login/codeUrl', data: { url: location.href } })
|
return request.get({ url: '/login/codeUrl', data: { url: location.href } })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function OALogin(data: Record<string, any>) {
|
// 微信小程序登录
|
||||||
return request.get({ url: '/login/oaLogin', data })
|
|
||||||
|
export function mnpLogin(data: Record<string, any>) {
|
||||||
|
return request.post({ url: '/login/mnpLogin', data })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 公众号登录
|
||||||
|
export function OALogin(data: Record<string, any>) {
|
||||||
|
return request.post({ url: '/login/oaLogin', data })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,25 @@ import request from '@/utils/request'
|
||||||
|
|
||||||
//发送短信
|
//发送短信
|
||||||
export function smsSend(data: any) {
|
export function smsSend(data: any) {
|
||||||
return request.post({ url: '/sms/send', data: data })
|
return request.post({ url: '/index/sendSms', data: data })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getConfig() {
|
export function getConfig() {
|
||||||
return request.get({ url: '/config' })
|
return request.get({ url: '/index/config' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPolicy(data: any) {
|
export function getPolicy(data: any) {
|
||||||
return request.get({ url: '/policy', data: data })
|
return request.get({ url: '/index/policy', data: data })
|
||||||
|
}
|
||||||
|
|
||||||
|
export function uploadImage(file: any, token?: string) {
|
||||||
|
return request.uploadFile({
|
||||||
|
url: '/upload/image',
|
||||||
|
filePath: file,
|
||||||
|
name: 'file',
|
||||||
|
header: {
|
||||||
|
token
|
||||||
|
},
|
||||||
|
fileType: 'image'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ import request from '@/utils/request'
|
||||||
|
|
||||||
//首页数据
|
//首页数据
|
||||||
export function getIndex() {
|
export function getIndex() {
|
||||||
return request.get({ url: '/index' })
|
return request.get({ url: '/index/index' })
|
||||||
}
|
}
|
||||||
|
|
||||||
// 装修页面
|
// 装修页面
|
||||||
export function getDecorate(data: any) {
|
export function getDecorate(data: any) {
|
||||||
return request.get({ url: '/decorate', data })
|
return request.get({ url: '/index/decorate', data })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -15,14 +15,5 @@ export function getDecorate(data: any) {
|
||||||
* @return { Promise }
|
* @return { Promise }
|
||||||
*/
|
*/
|
||||||
export function getHotSearch() {
|
export function getHotSearch() {
|
||||||
return request.get({ url: '/hotSearch' })
|
return request.get({ url: '/index/hotSearch' })
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description 搜索
|
|
||||||
* @param { string } keyword 关键词
|
|
||||||
* @return { Promise }
|
|
||||||
*/
|
|
||||||
export function getSearch(data: { keyword: string; pageNo: number; pageSize: number }) {
|
|
||||||
return request.get({ url: '/search', data })
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,3 +26,8 @@ export enum RequestCodeEnum {
|
||||||
REQUEST_404_ERROR = 404, //请求接口不存在
|
REQUEST_404_ERROR = 404, //请求接口不存在
|
||||||
SYSTEM_ERROR = 500 //系统错误
|
SYSTEM_ERROR = 500 //系统错误
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum RequestErrMsgEnum {
|
||||||
|
ABORT = 'request:fail abort',
|
||||||
|
TIMEOUT = 'request:fail timeout'
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { login } from '@/api/account'
|
import { mobileLogin, accountLogin, mnpLogin } from '@/api/account'
|
||||||
import { smsSend } from '@/api/app'
|
import { smsSend } from '@/api/app'
|
||||||
import { SMSEnum } from '@/enums/appEnums'
|
import { SMSEnum } from '@/enums/appEnums'
|
||||||
import { BACK_URL } from '@/enums/cacheEnums'
|
import { BACK_URL } from '@/enums/cacheEnums'
|
||||||
|
|
@ -174,8 +174,7 @@ import { onLoad, onShow } from '@dcloudio/uni-app'
|
||||||
import { computed, reactive, ref, shallowRef, watch } from 'vue'
|
import { computed, reactive, ref, shallowRef, watch } from 'vue'
|
||||||
enum LoginTypeEnum {
|
enum LoginTypeEnum {
|
||||||
MOBILE = 'mobile',
|
MOBILE = 'mobile',
|
||||||
ACCOUNT = 'account',
|
ACCOUNT = 'account'
|
||||||
MNP = 'mnp'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LoginWayEnum {
|
enum LoginWayEnum {
|
||||||
|
|
@ -242,28 +241,34 @@ const isOpenAgreement = computed(() => appStore.getLoginConfig.openAgreement ==
|
||||||
const isOpenOtherAuth = computed(() => appStore.getLoginConfig.openOtherAuth == 1)
|
const isOpenOtherAuth = computed(() => appStore.getLoginConfig.openOtherAuth == 1)
|
||||||
const isForceBindMobile = computed(() => appStore.getLoginConfig.forceBindMobile == 1)
|
const isForceBindMobile = computed(() => appStore.getLoginConfig.forceBindMobile == 1)
|
||||||
|
|
||||||
const loginFun = async (scene: LoginTypeEnum, code?: string) => {
|
const loginFun = async (scene: LoginTypeEnum) => {
|
||||||
if (!isCheckAgreement.value && isOpenAgreement.value)
|
|
||||||
return uni.$u.toast('请勾选已阅读并同意《服务协议》和《隐私协议》')
|
|
||||||
if (scene == LoginTypeEnum.ACCOUNT) {
|
|
||||||
if (!formData.username) return uni.$u.toast('请输入账号/手机号码')
|
|
||||||
if (!formData.password) return uni.$u.toast('请输入密码')
|
|
||||||
}
|
|
||||||
if (scene == LoginTypeEnum.MOBILE) {
|
|
||||||
if (!formData.mobile) return uni.$u.toast('请输入手机号码')
|
|
||||||
if (!formData.code) return uni.$u.toast('请输入验证码')
|
|
||||||
}
|
|
||||||
const params = {
|
|
||||||
...formData,
|
|
||||||
scene
|
|
||||||
}
|
|
||||||
if (code) params.code = code
|
|
||||||
uni.showLoading({
|
|
||||||
title: '请稍后...'
|
|
||||||
})
|
|
||||||
try {
|
try {
|
||||||
const data = await login(params)
|
await checkAgreement()
|
||||||
loginHandle(data)
|
if (scene == LoginTypeEnum.ACCOUNT) {
|
||||||
|
if (!formData.username) return uni.$u.toast('请输入账号/手机号码')
|
||||||
|
if (!formData.password) return uni.$u.toast('请输入密码')
|
||||||
|
}
|
||||||
|
if (scene == LoginTypeEnum.MOBILE) {
|
||||||
|
if (!formData.mobile) return uni.$u.toast('请输入手机号码')
|
||||||
|
if (!formData.code) return uni.$u.toast('请输入验证码')
|
||||||
|
}
|
||||||
|
uni.showLoading({
|
||||||
|
title: '请稍后...'
|
||||||
|
})
|
||||||
|
|
||||||
|
let data
|
||||||
|
switch (scene) {
|
||||||
|
case LoginTypeEnum.ACCOUNT:
|
||||||
|
data = await accountLogin(formData)
|
||||||
|
break
|
||||||
|
case LoginTypeEnum.MOBILE:
|
||||||
|
data = await mobileLogin(formData)
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (data) {
|
||||||
|
loginHandle(data)
|
||||||
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
uni.$u.toast(error)
|
uni.$u.toast(error)
|
||||||
|
|
@ -286,7 +291,7 @@ const loginHandle = async (data: any) => {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
const pages = getCurrentPages()
|
const pages = getCurrentPages()
|
||||||
if (pages.length > 1) {
|
if (pages.length > 1) {
|
||||||
const prevPage = pages.at(-2)
|
const prevPage = pages[pages.length - 2]
|
||||||
uni.navigateBack({
|
uni.navigateBack({
|
||||||
success: () => {
|
success: () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
@ -307,19 +312,34 @@ const loginHandle = async (data: any) => {
|
||||||
|
|
||||||
const { lockFn: handleLogin } = useLockFn(loginFun)
|
const { lockFn: handleLogin } = useLockFn(loginFun)
|
||||||
|
|
||||||
const wxLogin = async () => {
|
const checkAgreement = async () => {
|
||||||
// #ifdef MP-WEIXIN
|
if (!isCheckAgreement.value && isOpenAgreement.value)
|
||||||
const data: any = await uni.login({
|
return Promise.reject('请勾选已阅读并同意《服务协议》和《隐私协议》')
|
||||||
provider: 'weixin'
|
|
||||||
})
|
|
||||||
handleLogin(LoginTypeEnum.MNP, data.code)
|
|
||||||
// #endif
|
|
||||||
// #ifdef H5
|
|
||||||
if (isWeixin.value) {
|
|
||||||
wechatOa.getUrl()
|
|
||||||
}
|
|
||||||
// #endif
|
|
||||||
}
|
}
|
||||||
|
const { lockFn: wxLogin } = useLockFn(async () => {
|
||||||
|
try {
|
||||||
|
await checkAgreement()
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
uni.showLoading({
|
||||||
|
title: '请稍后...'
|
||||||
|
})
|
||||||
|
const { code }: any = await uni.login({
|
||||||
|
provider: 'weixin'
|
||||||
|
})
|
||||||
|
const data = await mnpLogin({
|
||||||
|
code
|
||||||
|
})
|
||||||
|
loginHandle(data)
|
||||||
|
// #endif
|
||||||
|
// #ifdef H5
|
||||||
|
if (isWeixin.value) {
|
||||||
|
wechatOa.getUrl()
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
} catch (error) {
|
||||||
|
uni.$u.toast(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => appStore.getLoginConfig,
|
() => appStore.getLoginConfig,
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,8 @@
|
||||||
import { ref, reactive, shallowRef } from 'vue'
|
import { ref, reactive, shallowRef } from 'vue'
|
||||||
import Suggest from './component/suggest.vue'
|
import Suggest from './component/suggest.vue'
|
||||||
import { HISTORY } from '@/enums/cacheEnums'
|
import { HISTORY } from '@/enums/cacheEnums'
|
||||||
import { getHotSearch, getSearch } from '@/api/shop'
|
import { getHotSearch } from '@/api/shop'
|
||||||
|
import { getArticleList } from '@/api/news'
|
||||||
import cache from '@/utils/cache'
|
import cache from '@/utils/cache'
|
||||||
|
|
||||||
interface Search {
|
interface Search {
|
||||||
|
|
@ -98,7 +99,7 @@ const handleClear = async (): Promise<void> => {
|
||||||
|
|
||||||
const queryList = async (pageNo, pageSize) => {
|
const queryList = async (pageNo, pageSize) => {
|
||||||
try {
|
try {
|
||||||
const { lists } = await getSearch({
|
const { lists } = await getArticleList({
|
||||||
keyword: keyword.value,
|
keyword: keyword.value,
|
||||||
pageNo,
|
pageNo,
|
||||||
pageSize
|
pageSize
|
||||||
|
|
|
||||||
|
|
@ -191,7 +191,7 @@ import { onShow, onUnload } from '@dcloudio/uni-app'
|
||||||
import { getUserInfo, userEdit, userBindMobile, userMnpMobile } from '@/api/user'
|
import { getUserInfo, userEdit, userBindMobile, userMnpMobile } from '@/api/user'
|
||||||
import { smsSend } from '@/api/app'
|
import { smsSend } from '@/api/app'
|
||||||
import { FieldType, SMSEnum } from '@/enums/appEnums'
|
import { FieldType, SMSEnum } from '@/enums/appEnums'
|
||||||
import { uploadFile } from '@/utils/util'
|
import { uploadImage } from '@/api/app'
|
||||||
|
|
||||||
// 用户信息
|
// 用户信息
|
||||||
const userInfo = ref<any>({})
|
const userInfo = ref<any>({})
|
||||||
|
|
@ -342,7 +342,7 @@ const uploadAvatar = (path: string) => {
|
||||||
title: '正在上传中...',
|
title: '正在上传中...',
|
||||||
mask: true
|
mask: true
|
||||||
})
|
})
|
||||||
uploadFile(path)
|
uploadImage(path)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
setUserInfoFun(res.url)
|
setUserInfoFun(res.url)
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,5 @@ import { TOKEN_KEY } from '@/enums/cacheEnums'
|
||||||
import cache from './cache'
|
import cache from './cache'
|
||||||
|
|
||||||
export function getToken() {
|
export function getToken() {
|
||||||
return cache.get(TOKEN_KEY)
|
return cache.get(TOKEN_KEY) || ''
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ const cache = {
|
||||||
try {
|
try {
|
||||||
uni.setStorageSync(key, data)
|
uni.setStorageSync(key, data)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
get(key: string) {
|
get(key: string) {
|
||||||
|
|
@ -22,16 +22,16 @@ const cache = {
|
||||||
try {
|
try {
|
||||||
const data = uni.getStorageSync(key)
|
const data = uni.getStorageSync(key)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
const { value, expire } = JSON.parse(data)
|
const { value, expire } = JSON.parse(data)
|
||||||
if (expire && expire < this.time()) {
|
if (expire && expire < this.time()) {
|
||||||
uni.removeStorageSync(key)
|
uni.removeStorageSync(key)
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
//获取当前时间
|
//获取当前时间
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,10 @@ export class RequestCancel {
|
||||||
}
|
}
|
||||||
add(url: string, requestTask: RequestTask) {
|
add(url: string, requestTask: RequestTask) {
|
||||||
this.remove(url)
|
this.remove(url)
|
||||||
if (!cancelerMap.has(url)) {
|
if (cancelerMap.has(url)) {
|
||||||
cancelerMap.set(url, requestTask)
|
cancelerMap.delete(url)
|
||||||
}
|
}
|
||||||
|
cancelerMap.set(url, requestTask)
|
||||||
}
|
}
|
||||||
remove(url: string) {
|
remove(url: string) {
|
||||||
if (cancelerMap.has(url)) {
|
if (cancelerMap.has(url)) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { merge } from 'lodash-es'
|
import { merge } from 'lodash-es'
|
||||||
import { isFunction } from '@vue/shared'
|
import { isFunction } from '@vue/shared'
|
||||||
import { HttpRequestOptions, RequestConfig, RequestOptions } from './type'
|
import { HttpRequestOptions, RequestConfig, RequestOptions, UploadFileOption } from './type'
|
||||||
import { RequestMethodsEnum } from '@/enums/requestEnums'
|
import { RequestErrMsgEnum, RequestMethodsEnum } from '@/enums/requestEnums'
|
||||||
import requestCancel from './cancel'
|
import requestCancel from './cancel'
|
||||||
|
|
||||||
export default class HttpRequest {
|
export default class HttpRequest {
|
||||||
|
|
@ -9,7 +9,25 @@ export default class HttpRequest {
|
||||||
constructor(options: HttpRequestOptions) {
|
constructor(options: HttpRequestOptions) {
|
||||||
this.options = options
|
this.options = options
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @description 重新请求
|
||||||
|
*/
|
||||||
|
retryRequest(options: RequestOptions, config: RequestConfig) {
|
||||||
|
const { retryCount, retryTimeout } = config
|
||||||
|
if (!retryCount || options.method?.toUpperCase() == RequestMethodsEnum.POST) {
|
||||||
|
return Promise.reject()
|
||||||
|
}
|
||||||
|
uni.showLoading({ title: '加载中...' })
|
||||||
|
config.hasRetryCount = config.hasRetryCount ?? 0
|
||||||
|
if (config.hasRetryCount >= retryCount) {
|
||||||
|
return Promise.reject()
|
||||||
|
}
|
||||||
|
config.hasRetryCount++
|
||||||
|
config.requestHooks.requestInterceptorsHook = (options) => options
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, retryTimeout))
|
||||||
|
.then(() => this.request(options, config))
|
||||||
|
.finally(() => uni.hideLoading())
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @description get请求
|
* @description get请求
|
||||||
*/
|
*/
|
||||||
|
|
@ -23,6 +41,49 @@ export default class HttpRequest {
|
||||||
post<T = any>(options: RequestOptions, config?: Partial<RequestConfig>): Promise<T> {
|
post<T = any>(options: RequestOptions, config?: Partial<RequestConfig>): Promise<T> {
|
||||||
return this.request({ ...options, method: RequestMethodsEnum.POST }, config)
|
return this.request({ ...options, method: RequestMethodsEnum.POST }, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description 上传图片
|
||||||
|
*/
|
||||||
|
uploadFile(options: UploadFileOption, config?: Partial<RequestConfig>) {
|
||||||
|
let mergeOptions: RequestOptions = merge({}, this.options.requestOptions, options)
|
||||||
|
const mergeConfig: RequestConfig = merge({}, this.options, config)
|
||||||
|
const { requestInterceptorsHook, responseInterceptorsHook, responseInterceptorsCatchHook } =
|
||||||
|
mergeConfig.requestHooks || {}
|
||||||
|
if (requestInterceptorsHook && isFunction(requestInterceptorsHook)) {
|
||||||
|
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig)
|
||||||
|
}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
uni.uploadFile({
|
||||||
|
...mergeOptions,
|
||||||
|
success: async (response) => {
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
response.data = JSON.parse(response.data)
|
||||||
|
if (responseInterceptorsHook && isFunction(responseInterceptorsHook)) {
|
||||||
|
try {
|
||||||
|
response = await responseInterceptorsHook(response, mergeConfig)
|
||||||
|
resolve(response)
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolve(response)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: async (err) => {
|
||||||
|
if (
|
||||||
|
responseInterceptorsCatchHook &&
|
||||||
|
isFunction(responseInterceptorsCatchHook)
|
||||||
|
) {
|
||||||
|
reject(await responseInterceptorsCatchHook(mergeOptions, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @description 请求函数
|
* @description 请求函数
|
||||||
*/
|
*/
|
||||||
|
|
@ -32,15 +93,15 @@ export default class HttpRequest {
|
||||||
const { requestInterceptorsHook, responseInterceptorsHook, responseInterceptorsCatchHook } =
|
const { requestInterceptorsHook, responseInterceptorsHook, responseInterceptorsCatchHook } =
|
||||||
mergeConfig.requestHooks || {}
|
mergeConfig.requestHooks || {}
|
||||||
if (requestInterceptorsHook && isFunction(requestInterceptorsHook)) {
|
if (requestInterceptorsHook && isFunction(requestInterceptorsHook)) {
|
||||||
mergeOptions = requestInterceptorsHook(options, mergeConfig)
|
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig)
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const requestTask = uni.request({
|
const requestTask = uni.request({
|
||||||
...mergeOptions,
|
...mergeOptions,
|
||||||
success(response) {
|
async success(response) {
|
||||||
if (responseInterceptorsHook && isFunction(responseInterceptorsHook)) {
|
if (responseInterceptorsHook && isFunction(responseInterceptorsHook)) {
|
||||||
try {
|
try {
|
||||||
response = responseInterceptorsHook(response, mergeConfig)
|
response = await responseInterceptorsHook(response, mergeConfig)
|
||||||
resolve(response)
|
resolve(response)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
|
|
@ -49,18 +110,25 @@ export default class HttpRequest {
|
||||||
}
|
}
|
||||||
resolve(response)
|
resolve(response)
|
||||||
},
|
},
|
||||||
fail(err) {
|
fail: async (err) => {
|
||||||
|
if (err.errMsg == RequestErrMsgEnum.TIMEOUT) {
|
||||||
|
this.retryRequest(mergeOptions, mergeConfig)
|
||||||
|
.then((res) => resolve(res))
|
||||||
|
.catch((err) => reject(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
responseInterceptorsCatchHook &&
|
responseInterceptorsCatchHook &&
|
||||||
isFunction(responseInterceptorsCatchHook)
|
isFunction(responseInterceptorsCatchHook)
|
||||||
) {
|
) {
|
||||||
reject(responseInterceptorsCatchHook(err, mergeConfig))
|
reject(await responseInterceptorsCatchHook(mergeOptions, err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reject(err)
|
reject(err)
|
||||||
},
|
},
|
||||||
complete(err) {
|
complete(err) {
|
||||||
if (err.errMsg !== 'request:fail abort') {
|
if (err.errMsg !== RequestErrMsgEnum.ABORT) {
|
||||||
requestCancel.remove(options.url)
|
requestCancel.remove(options.url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@ import HttpRequest from './http'
|
||||||
import { merge } from 'lodash-es'
|
import { merge } from 'lodash-es'
|
||||||
import { HttpRequestOptions, RequestHooks } from './type'
|
import { HttpRequestOptions, RequestHooks } from './type'
|
||||||
import { getToken } from '../auth'
|
import { getToken } from '../auth'
|
||||||
import { RequestCodeEnum } from '@/enums/requestEnums'
|
import { RequestCodeEnum, RequestMethodsEnum } from '@/enums/requestEnums'
|
||||||
import { useUserStore } from '@/stores/user'
|
import { useUserStore } from '@/stores/user'
|
||||||
|
import { client } from '../client'
|
||||||
|
|
||||||
const requestHooks: RequestHooks = {
|
const requestHooks: RequestHooks = {
|
||||||
requestInterceptorsHook(options, config) {
|
requestInterceptorsHook(options, config) {
|
||||||
|
|
@ -17,12 +18,15 @@ const requestHooks: RequestHooks = {
|
||||||
}
|
}
|
||||||
const token = getToken()
|
const token = getToken()
|
||||||
// 添加token
|
// 添加token
|
||||||
if (withToken && token) {
|
if (withToken) {
|
||||||
options.header.token = token
|
options.header['like-token'] = options.header.token || token
|
||||||
}
|
}
|
||||||
|
// 添加终端类型
|
||||||
|
options.header['terminal'] = client
|
||||||
|
delete options.header.token
|
||||||
return options
|
return options
|
||||||
},
|
},
|
||||||
responseInterceptorsHook(response, config) {
|
async responseInterceptorsHook(response, config) {
|
||||||
const { isTransformResponse, isReturnDefaultResponse, isAuth } = config
|
const { isTransformResponse, isReturnDefaultResponse, isAuth } = config
|
||||||
|
|
||||||
//返回默认响应,当需要获取响应头及其他数据时可使用
|
//返回默认响应,当需要获取响应头及其他数据时可使用
|
||||||
|
|
@ -34,7 +38,7 @@ const requestHooks: RequestHooks = {
|
||||||
return response.data
|
return response.data
|
||||||
}
|
}
|
||||||
const { logout } = useUserStore()
|
const { logout } = useUserStore()
|
||||||
const { code, data, msg } = response.data as any
|
const { code, data, msg, show } = response.data as any
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case RequestCodeEnum.SUCCESS:
|
case RequestCodeEnum.SUCCESS:
|
||||||
return data
|
return data
|
||||||
|
|
@ -48,6 +52,7 @@ const requestHooks: RequestHooks = {
|
||||||
case RequestCodeEnum.NO_PERMISSTION:
|
case RequestCodeEnum.NO_PERMISSTION:
|
||||||
case RequestCodeEnum.FAILED:
|
case RequestCodeEnum.FAILED:
|
||||||
case RequestCodeEnum.SYSTEM_ERROR:
|
case RequestCodeEnum.SYSTEM_ERROR:
|
||||||
|
case RequestCodeEnum.REQUEST_404_ERROR:
|
||||||
uni.$u.toast(msg)
|
uni.$u.toast(msg)
|
||||||
return Promise.reject(msg)
|
return Promise.reject(msg)
|
||||||
|
|
||||||
|
|
@ -64,6 +69,12 @@ const requestHooks: RequestHooks = {
|
||||||
default:
|
default:
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
async responseInterceptorsCatchHook(options, error) {
|
||||||
|
if (options.method?.toUpperCase() == RequestMethodsEnum.POST) {
|
||||||
|
uni.$u.toast('请求失败,请重试')
|
||||||
|
}
|
||||||
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,6 +94,8 @@ const defaultOptions: HttpRequestOptions = {
|
||||||
// 是否携带token
|
// 是否携带token
|
||||||
withToken: true,
|
withToken: true,
|
||||||
isAuth: false,
|
isAuth: false,
|
||||||
|
retryCount: 2,
|
||||||
|
retryTimeout: 1000,
|
||||||
requestHooks: requestHooks
|
requestHooks: requestHooks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
export type RequestOptions = UniApp.RequestOptions
|
export type RequestOptions = UniApp.RequestOptions
|
||||||
export type ResponseResult = UniApp.RequestSuccessCallbackResult
|
export type ResponseResult =
|
||||||
export type ResponseError = UniApp.GeneralCallbackResult
|
| UniApp.RequestSuccessCallbackResult
|
||||||
|
| UniApp.UploadFileSuccessCallbackResult
|
||||||
|
export type RequestOptionsResponseError = UniApp.GeneralCallbackResult
|
||||||
export type RequestTask = UniApp.RequestTask
|
export type RequestTask = UniApp.RequestTask
|
||||||
|
export type UploadFileOption = UniApp.UploadFileOption
|
||||||
export interface HttpRequestOptions extends RequestConfig {
|
export interface HttpRequestOptions extends RequestConfig {
|
||||||
requestOptions: Partial<RequestOptions>
|
requestOptions: Partial<RequestOptions>
|
||||||
}
|
}
|
||||||
|
|
@ -15,10 +18,13 @@ export interface RequestConfig {
|
||||||
ignoreCancel: boolean
|
ignoreCancel: boolean
|
||||||
withToken: boolean
|
withToken: boolean
|
||||||
isAuth: boolean
|
isAuth: boolean
|
||||||
|
retryCount: number
|
||||||
|
retryTimeout: number
|
||||||
|
hasRetryCount?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RequestHooks {
|
export interface RequestHooks {
|
||||||
requestInterceptorsHook?(options: RequestOptions, config: RequestConfig): RequestOptions
|
requestInterceptorsHook?(options: RequestOptions, config: RequestConfig): RequestOptions
|
||||||
responseInterceptorsHook?(response: ResponseResult, config: RequestConfig): any
|
responseInterceptorsHook?(response: ResponseResult, config: RequestConfig): any
|
||||||
responseInterceptorsCatchHook?(error: ResponseError, config: RequestConfig): any
|
responseInterceptorsCatchHook?(options: RequestOptions, error: any): any
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { isObject } from '@vue/shared'
|
import { isObject } from '@vue/shared'
|
||||||
import { getToken } from './auth'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 获取元素节点信息(在组件中的元素必须要传ctx)
|
* @description 获取元素节点信息(在组件中的元素必须要传ctx)
|
||||||
|
|
@ -9,11 +8,11 @@ import { getToken } from './auth'
|
||||||
*/
|
*/
|
||||||
export const getRect = (selector: string, all = false, context?: any) => {
|
export const getRect = (selector: string, all = false, context?: any) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let qurey = uni.createSelectorQuery()
|
let query = uni.createSelectorQuery()
|
||||||
if (context) {
|
if (context) {
|
||||||
qurey = uni.createSelectorQuery().in(context)
|
query = uni.createSelectorQuery().in(context)
|
||||||
}
|
}
|
||||||
qurey[all ? 'selectAll' : 'select'](selector)
|
query[all ? 'selectAll' : 'select'](selector)
|
||||||
.boundingClientRect(function (rect) {
|
.boundingClientRect(function (rect) {
|
||||||
if (all && Array.isArray(rect) && rect.length) {
|
if (all && Array.isArray(rect) && rect.length) {
|
||||||
return resolve(rect)
|
return resolve(rect)
|
||||||
|
|
@ -94,35 +93,3 @@ export function objectToQuery(params: Record<string, any>): string {
|
||||||
}
|
}
|
||||||
return query.slice(0, -1)
|
return query.slice(0, -1)
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @description 上传图片
|
|
||||||
* @param { String } path 选择的本地地址
|
|
||||||
*/
|
|
||||||
export function uploadFile(path: any) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const token = getToken()
|
|
||||||
uni.uploadFile({
|
|
||||||
url: `${import.meta.env.VITE_APP_BASE_URL}/api/upload/image`,
|
|
||||||
filePath: path,
|
|
||||||
name: 'file',
|
|
||||||
header: {
|
|
||||||
token
|
|
||||||
},
|
|
||||||
fileType: 'image',
|
|
||||||
success: (res) => {
|
|
||||||
console.log('uploadFile res ==> ', res)
|
|
||||||
const data = JSON.parse(res.data)
|
|
||||||
console.log('data.code', data.code)
|
|
||||||
if (data.code == 200) {
|
|
||||||
resolve(data.data)
|
|
||||||
} else {
|
|
||||||
reject()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail: (err) => {
|
|
||||||
console.log(err)
|
|
||||||
reject()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue