Merge pull request 'feature/请求二次封装' (#2) from feature/请求二次封装 into dev

Reviewed-on: http://git.hchyun.com/feashow/pupil/pulls/2
This commit is contained in:
clay
2022-12-04 11:57:32 +00:00
4 changed files with 240 additions and 13 deletions

View File

@@ -29,14 +29,15 @@ module.exports = {
}
],
'vue/max-attributes-per-line': [
2,
'error',
{
singleline: 10,
multiline: {
max: 1,
allowFirstLine: true
}
'singleline': {
'max': 1
},
'multiline': {
'max': 1
}
},
],
'vue/singleline-html-element-content-newline': 'off',
'vue/multiline-html-element-content-newline': 'off',

View File

@@ -1,6 +1,5 @@
import App from './App'
//引入uView-ui组件库注意这两行要放在 import Vue 之后)
// 引入uView-ui组件库注意这两行要放在 import Vue 之后)
import uView from 'uview-ui'
Vue.use(uView)

View File

@@ -5,7 +5,9 @@
</template>
<script>
import {apiService} from '../../service/request.js'
export default {
computed: {},
data() {
return {
list: [{
@@ -24,7 +26,11 @@
}
},
onLoad() {
console.log('--------------')
apiService.getCategories().then(res => {
const {message} = res.data
console.log(message)
})
},
methods: {

221
service/request.js Normal file
View File

@@ -0,0 +1,221 @@
const serverHost = 'https://api-ugo-web.itheima.net'
function isOutTime(res) {
if (res.data.message === '请先登录') {
uni.showToast('登录信息已过期,请重新登录')
setTimeout(() => {
uni.redirectTo({
url: '/pages/login/login'
})
}, 1000)
uni.removeStorageSync('userInfo')
uni.removeStorageSync('token')
} else {
// uni.showToast(res.data.message);
}
}
const service = {
get(url) {
const header = {}
header['Authorization'] = 'Bearer ' + uni.getStorageSync('token')
header['content-type'] = 'application/json'
return new Promise((resolve, reject) => {
uni.request({
method: 'get',
url: serverHost + url,
header: header,
success: res => {
// // 调用接口成功
// if (!res.data.data) {
// isOutTime(res)
// reject(res)
// }
resolve(res)
},
fail: err => {
// 调用接口失败
reject(err)
}
})
})
},
post(url, data, isLogin) {
const header = {}
if (isLogin) {
// header['content-type'] = 'application/x-www-form-urlencoded';
console.log(isLogin)
header['Authorization'] = ''
} else {
header['Authorization'] = 'Bearer ' + uni.getStorageSync('token')
header['content-type'] = 'application/json'
}
return new Promise((resolve, reject) => {
uni.request({
method: 'post',
url: serverHost + url,
data: data,
header: header,
timeout: 30000,
success: res => {
console.log(1111, res)
uni.hideLoading()
// if (res.data.exception === 'UnAuthorizedException') {
// let pages = getCurrentPages()
// let page = (pages[pages.length - 1]).route
// if (!page.includes('login')) {
// uni.navigateTo({
// url: `/pages/login/login`
// })
// }
// }
// if (res.data.message) {
// isOutTime(res);
// toast.error(res.data.message)
// reject(res);
// }
// if (isLogin && !res.data.access_token) {
// reject(res);
// } else {
// resolve(res);
// }
resolve(res)
},
fail: err => {
console.log('err', err)
uni.hideLoading()
// 调用接口失败
try {
toast.error(err.data.message)
} catch (errr) {
toast.error('服务器出错')
}
reject(err)
}
})
})
},
put(url, data, isLogin) {
const header = {}
if (isLogin) {
header['content-type'] = 'application/x-www-form-urlencoded'
} else {
header['Authorization'] = 'Bearer ' + uni.getStorageSync('token')
header['content-type'] = 'application/json'
}
return new Promise((resolve, reject) => {
uni.request({
method: 'put',
url: serverHost + url,
header: header,
success: res => {
if (!res.data.flag) {
isOutTime(res)
reject(res)
}
resolve(res)
},
data: data,
fail: err => {
// 调用接口失败
// toast.error(res.data.message)
uni.hideLoading()
reject(err)
}
})
})
}
}
const toastDuration = 1500
const toast = {
// duration: 1500,
success: (text, dur) => {
// $wuxToptips().success({
// hidden: false,
// text: text || '成功',
// duration: dur || toastDuration
// });
uni.showToast({
title: text || '成功',
duration: dur || toastDuration
})
},
warn: text => {
// $wuxToptips().warn({
// hidden: false,
// text: text || '未知警告',
// duration: toastDuration
// });
uni.showToast({
icon: 'none',
title: text || '未知警告',
duration: toastDuration
})
},
error: text => {
// $wuxToptips().error({
// hidden: false,
// text: text || '未知错误',
// duration: toastDuration
// });
uni.showToast({
icon: 'none',
title: text || '未知错误',
duration: toastDuration
})
}
}
const apiService = {
uploadImgUrl: serverHost,
imgUrl: serverHost,
login: data => {
data = Object.assign(data || {}, {})
const url = `/api`
return new Promise((resolve, reject) => {
resolve(service.post(url, data, true))
})
},
// 获取
getCategories(data) {
const url = `/api/public/v1/categories`
return new Promise((resolve, reject) => {
resolve(service.get(url, data))
})
},
// 新增
addOutboundOrder(data) {
const url = `/api`
return new Promise((resolve, reject) => {
resolve(service.post(url, data))
})
},
// 提交
deliveryOrderConfirm(data) {
const url = `/api`
return new Promise((resolve, reject) => {
resolve(service.post(url, data))
})
},
// 解密
decrypt(data) {
const url = `/api`
return new Promise((resolve, reject) => {
resolve(service.post(url, { data }))
})
},
// 国密sm4加密
sm4Encrypt(data) {
const url = `/api`
return new Promise((resolve, reject) => {
resolve(service.sm(url, data))
})
}
}
export { apiService, toast }