Browse Source

2023-6-19

master
guoxing 2 years ago
parent
commit
fa63649224
  1. 130
      supervise-wx-mp/common/app.scss
  2. 14
      supervise-wx-mp/common/config.js
  3. 7
      supervise-wx-mp/common/mixin.js
  4. 2
      supervise-wx-mp/common/props.js
  5. 16
      supervise-wx-mp/common/req.js
  6. 12
      supervise-wx-mp/common/request.api.js
  7. 5
      supervise-wx-mp/main.js
  8. 2
      supervise-wx-mp/pages.json
  9. 399
      supervise-wx-mp/pages/index/enterpriseRisk.vue
  10. BIN
      supervise-wx-mp/static/yujing.png
  11. 13
      supervise-wx-mp/utils/auth.token.js
  12. 13
      supervise-wx-mp/utils/request/index.js
  13. 35
      supervise-wx-mp/utils/request/requestInterceptors.js
  14. 130
      supervise-wx-mp/utils/request/responseInterceptors.js
  15. 196
      supervise-wx-mp/utils/requester.js

130
supervise-wx-mp/common/app.scss

@ -0,0 +1,130 @@
/**
* 这里是uni-app内置的常用样式变量
*
* uni-app 官方扩展插件及插件市场https://ext.dcloud.net.cn上很多三方插件均使用了这些样式变量
* 如果你是插件开发者建议你使用scss预处理并在插件代码中直接使用这些变量无需 import 这个文件方便用户通过搭积木的方式开发整体风格一致的App
*
*/
/**
* 如果你是App开发者插件使用者你可以通过修改这些变量来定制自己的插件主题实现自定义主题功能
*
* 如果你的项目同样使用了scss预处理你也可以直接在你的 scss 代码中使用如下变量同时无需 import 这个文件
*/
/* 颜色变量 */
/* 行为相关颜色 */
$uni-color-primary: #007aff;
$uni-color-success: #4cd964;
$uni-color-warning: #f0ad4e;
$uni-color-error: #dd524d;
/* 文字基本颜色 */
$uni-text-color: #333; //基本色
$uni-text-color-inverse: #fff; //反色
$uni-text-color-grey: #999; //辅助灰色如加载更多的提示信息
$uni-text-color-placeholder: #808080;
$uni-text-color-disable: #c0c0c0;
/* 背景颜色 */
$uni-bg-color: #ffffff;
$uni-bg-color-grey: #f8f8f8;
$uni-bg-color-hover: #f1f1f1; //点击状态颜色
$uni-bg-color-mask: rgba(0, 0, 0, 0.4); //遮罩颜色
/* 边框颜色 */
$uni-border-color: #c8c7cc;
/* 尺寸变量 */
/* 文字尺寸 */
$uni-font-size-sm: 12px;
$uni-font-size-base: 14px;
$uni-font-size-lg: 16;
/* 图片尺寸 */
$uni-img-size-sm: 20px;
$uni-img-size-base: 26px;
$uni-img-size-lg: 40px;
/* Border Radius */
$uni-border-radius-sm: 2px;
$uni-border-radius-base: 3px;
$uni-border-radius-lg: 6px;
$uni-border-radius-circle: 50%;
/* 水平间距 */
$uni-spacing-row-sm: 5px;
$uni-spacing-row-base: 10px;
$uni-spacing-row-lg: 15px;
/* 垂直间距 */
$uni-spacing-col-sm: 4px;
$uni-spacing-col-base: 8px;
$uni-spacing-col-lg: 12px;
/* 透明度 */
$uni-opacity-disabled: 0.3; // 组件禁用态的透明度
/* 文章场景相关 */
$uni-color-title: #2c405a; // 文章标题颜色
$uni-font-size-title: 20px;
$uni-color-subtitle: #555555; // 二级标题颜色
$uni-font-size-subtitle: 26px;
$uni-color-paragraph: #3f536e; // 文章段落颜色
$uni-font-size-paragraph: 15px;
/**
* 下面为当前APP自己定义的公共样式
*/
.app-container {
padding: 20px;
font-size: 14px;
line-height: 24px;
}
.u-block {
padding: 14px;
&__section {
margin-bottom: 10px;
}
&__title {
margin-top: 10px;
font-size: 15px;
color: $u-content-color;
margin-bottom: 10px;
}
&__flex {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
}
}
// 使用了cell组件的icon图片样式
.u-cell-icon {
width: 36rpx;
height: 36rpx;
margin-right: 8rpx;
}
.u-page {
padding: 15px 15px 40px 15px;
}
.u-demo-block {
flex: 1;
margin-bottom: 23px;
&__content {
@include flex(column);
}
&__title {
font-size: 14px;
color: rgb(143, 156, 162);
margin-bottom: 8px;
@include flex;
}
}

14
supervise-wx-mp/common/config.js

@ -0,0 +1,14 @@
/**
* config 配置项说明
* baseUrl = "", // 接口的根地址
* tokenName = "Authorization", // 请求头中token的名字,与服务器端对应
* loginTimeoutCode : "5000", // 登录超时或失效的情况下,服务器端返回的错误码
* loginTimeoutPage = "/pages/login/index", // 登录超时或失效的情况下,跳转到的登录页面
*/
module.exports = {
baseUrl: 'http://192.168.1.105:8001',
// baseUrl: 'http://jianguan.yyundong.com/api',
tokenName: "Authorization", // 请求头中token的名字,与服务器端对应
loginTimeoutCode: "5001", // 登录超时或失效的情况下,服务器端返回的错误码
loginTimeoutPage: "/pages/login/index", // 登录超时或失效的情况下,跳转到的登录页面
}

7
supervise-wx-mp/common/mixin.js

@ -0,0 +1,7 @@
export default {
data() {
return {
}
}
}

2
supervise-wx-mp/common/props.js

@ -0,0 +1,2 @@
uni.$u.props.gap.bgColor = '#f3f4f6'
uni.$u.props.gap.height = '10'

16
supervise-wx-mp/common/req.js

@ -0,0 +1,16 @@
const {
http
} = uni.$u
export default {
login: (params, config = {}) => http.post('/login', params, {
custom: {
catchError: true,
showFailMessage: true
}
}),
sayhello: (params = {}) => http.get("/sayb", params),
// 查看预警信息
selectBusinessRiskInfo: (params = {}) => http.post("/v1/businessData/selectBusinessRiskInfo", params)
}

12
supervise-wx-mp/common/request.api.js

@ -0,0 +1,12 @@
import request from '@/utils/requester.js'
export default {
login: (params = {}) => request.post("/portal/v1/sysuser/login", params),
sayhello: (params = {}) => request.get("/sayb", params),
getSalesReport: (params = {}) => request.get("/system/reportCenter/getSalesReport", params),
getGoodsOnWay: (params = {}) => request.get("/system/reportCenter/getGoodsOnWay", params),
getThresholdAnalysis: (params = {}) => request.get("/system/risk/getThresholdAnalysis", params),
// 查看预警信息
selectBusinessRiskInfo: (params = {}) => request.post("/v1/businessData/selectBusinessRiskInfo", params)
}

5
supervise-wx-mp/main.js

@ -1,5 +1,10 @@
import App from './App'
import api from '@/common/request.api.js'
Vue.prototype.$api = api
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false

2
supervise-wx-mp/pages.json

@ -4,7 +4,7 @@
{
"path": "pages/index/enterpriseRisk",
"style": {
"navigationBarTitleText": "uni-app"
"navigationBarTitleText": "企业经营异常核查"
}
},
{

399
supervise-wx-mp/pages/index/enterpriseRisk.vue

@ -1,62 +1,63 @@
<template>
<view class="charts-box">
<view style="display: flex;justify-content: center; width: 100%; ">
<text style="padding-top: 10px;padding-bottom: 10px;color: blue;margin-left: 20px;"
@click="jump(1)">1</text>
<text style="padding-top: 10px;padding-bottom: 10px;color: blue;margin-left: 20px;"
@click="jump(2)">2</text>
<text style="padding-top: 10px;padding-bottom: 10px;color: blue;margin-left: 20px;"
@click="jump(3)">3</text>
<view class="top" style="margin-top: 15px;">
<text>企业名称</text>
<text>{{queryParams.businessName}}</text>
</view>
<view style="margin-top: 20px;margin-left: 20px;">
<text>企业数据异常信息{{title}}</text>
<text style="margin-left: 10px;">{{newDate}}</text>
<view class="top">
<text>核查日期</text>
<text>{{queryParams.executionTime}}</text>
</view>
<view style="margin-top: 20px;margin-left: 20px;">
<text>>>>>>>{{title}}</text>
<view class="top">
<text>核查结果</text>
<text>{{result}}</text>
</view>
<view style="margin-top: 15px;margin-left: 10px;">
<qiun-data-charts type="mix" :opts="opts" :chartData="chartData" />
</view>
<view v-show="isShow">
<view class="top">
<text>异常信息详情</text>
</view>
<view style="margin-top: 25px;margin-left: 15px;margin-right: 15px;">
<block wx:for="{{list}}" wx:key="index">
<uni-table ref="table" :border="true" stripe type="" emptyText="暂无更多数据">
<uni-tr>
<uni-th align="center">现金流量-经营活动产生的现金流量()</uni-th>
<uni-th align="center">总额</uni-th>
</uni-tr>
<uni-tr v-for="(item, index) in tableData" :key="index">
<uni-td align="center">{{ item.name }}</uni-td>
<uni-td align="center">{{ item.price }}</uni-td>
</uni-tr>
</uni-table>
<!-- 包裹图片+两行文字 id="{{index}}"是给按下了那个item记录-->
</view>
<view class="view_tupian_wenzi" @click="click" id="{{index}}">
<!-- 包裹两行文字 -->
<view class="view_wenzi2">
<view class="top2">
<text>列入日期</text>
<text>{{item.addDate}}</text>
</view>
<view class="top2">
<text>列入经营异常名录原因</text>
<text>{{item.addReason}}</text>
</view>
<view class="top2">
<text>作出决定机关</text>
<text>{{item.decisionOffice}}</text>
</view>
<view class="top2">
<text>移出日期</text>
<text>{{item.removeDate}}</text>
</view>
<view class="top2">
<text>移出经营异常名录原因</text>
<text>{{item.romoveReason}}</text>
</view>
<view class="top2">
<text>移出决定机关</text>
<text>{{item.removeDecisionOffice}}</text>
</view>
<view style="margin-top: 25px;margin-left: 15px;margin-right: 15px;">
</view>
<uni-table ref="table" :border="true" stripe type="" emptyText="暂无更多数据">
<uni-tr>
<uni-th width="200" align="center">销售渠道类别</uni-th>
<uni-th align="center">总额</uni-th>
<uni-th align="center">应收帐款</uni-th>
<uni-th align="center">扣除应收</uni-th>
</uni-tr>
<uni-tr v-for="(item, index) in tableData2" :key="index">
<uni-td align="center">{{ item.name }}</uni-td>
<uni-td align="center">{{ item.totalAmount }}</uni-td>
<uni-td align="center">{{ item.accountsReceivable }}</uni-td>
<uni-td align="center">{{ item.deductionAccountsReceivable }}</uni-td>
</uni-tr>
</uni-table>
</view>
</block>
</view>
@ -69,211 +70,135 @@
data() {
return {
title: "",
newDate: new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate(),
tableData: [{
name: "销售商品收到的现金(元)",
price: "2000000",
},
{
name: "收到其他与经营活动有关的现金(元)",
price: "28700",
},
{
name: "本项合计",
price: "123456",
},
],
tableData2: [{
name: "销售商品收到的现金(元)",
totalAmount: "2000000",
accountsReceivable: "31914",
deductionAccountsReceivable: "35793"
},
{
name: "连锁内加盟",
totalAmount: "2000000",
accountsReceivable: "31914",
deductionAccountsReceivable: "35793"
},
{
name: "配送中心",
totalAmount: "2000000",
accountsReceivable: "31914",
deductionAccountsReceivable: "35793",
}, {
name: "连锁外加盟",
totalAmount: "2000000",
accountsReceivable: "31914",
deductionAccountsReceivable: "35793",
}
],
chartData: {},
opts: {
color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
"#ea7ccc"
],
padding: [15, 15, 0, 15],
enableScroll: false,
legend: {},
xAxis: {
disableGrid: true,
title: ""
},
yAxis: {
disabled: false,
disableGrid: false,
splitNumber: 5,
gridType: "dash",
dashLength: 4,
gridColor: "#CCCCCC",
padding: 10,
showTitle: true,
data: [{
position: "left",
title: "折线"
},
{
position: "right",
min: 0,
max: 200,
title: "柱状图",
textAlign: "left"
},
]
},
extra: {
mix: {
column: {
width: 20
}
}
}
}
result: "",
isShow: false,
queryParams: {
businessName: "",
executionTime: ""
},
list: [],
};
},
onLoad(option) {
this.getServerData();
console.log('App onLoad',JSON.stringify(option))
// this.queryParams = JSON.stringify(option)
// console.log('App onLoad', JSON.stringify(option))
this.title = JSON.stringify(option)
wx.showToast({
title: 'onLoad成功>>>>>' + JSON.stringify(option),
icon: 'none',
duration: 5000 //
})
// wx.showToast({
// title: 'onLoad>>>>>' + JSON.stringify(option),
// icon: 'none',
// duration: 5000 //
// })
},
created() {
this.getData()
},
// onShow(option) {
// console.log('App onShow',JSON.stringify(option))
// this.title = JSON.stringify(option)
// wx.showToast({
// title: 'onShow>>>>>' + JSON.stringify(option),
// icon: 'none',
// duration: 5000 //
// })
// },
// created(option) {
// console.log('App created',JSON.stringify(option))
// this.title = JSON.stringify(option)
// wx.showToast({
// title: 'created>>>>>' + JSON.stringify(option),
// icon: 'none',
// duration: 5000 //
// })
// },
// onError(option) {
// console.log('App onError',JSON.stringify(option))
// this.title =JSON.stringify(option)
// wx.showToast({
// title: 'onError>>>>>' + JSON.stringify(option),
// icon: 'none',
// duration: 5000 //
// })
// },
methods: {
getServerData() {
//
setTimeout(() => {
//
let res = {
categories: ["2018", "2019", "2020", "2021", "2022", "2023"],
series: [{
name: "仓库货值",
index: 1,
type: "column",
data: [40, {
"value": 30,
"color": "#f04864"
}, 55, 110, 24, 58]
},
{
name: "在途货值",
index: 1,
type: "column",
data: [50, 20, 75, 60, 34, 38]
}, {
name: "门店货值",
index: 1,
type: "column",
data: [50, 20, 75, 60, 34, 38]
}, {
name: "账户余额",
index: 1,
type: "column",
data: [50, 20, 75, 60, 34, 38]
}, {
name: "应收账款",
index: 1,
type: "column",
data: [50, 20, 75, 60, 34, 38]
}, {
name: "借款金额",
index: 1,
type: "column",
data: [50, 20, 75, 60, 34, 38]
},
{
name: "折线",
type: "line",
color: "#D33682",
data: [120, 140, 105, 170, 95, 160]
},
]
};
this.chartData = JSON.parse(JSON.stringify(res));
}, 500);
getData() {
var _this = this
this.$api.selectBusinessRiskInfo(_this.queryParams).then((resp) => {
console.log('1111>>>>>>', resp)
this.list = resp
console.log('2222>>>>>>', this.list.length)
this.result = this.list.length == 0 ? "无异常信息" : "共有" + this.list.length + "条异常信息"
this.isShow = this.list.length == 0 ? false : true
}).catch(e => {
console.log('eeeee', e)
})
},
jump(index) {
console.log("index>>3", index)
switch (index) {
case 1:
uni.navigateTo({
url: 'index'
});
break;
case 2:
uni.navigateTo({
url: 'index2'
});
break;
case 3:
break;
}
click(e) {
console.log("按了:", e.currentTarget.id)
}
},
};
</script>
<style scoped>
/* 请根据实际需求修改父元素尺寸,组件自动识别宽高 */
.charts-box {
<style lang="scss">
.top {
margin-left: 25px;
margin-top: 5px;
}
/* 包裹图片和两行文字 */
.view_tupian_wenzi {
display: flex;
flex-direction: row;
align-items: center;
/* 圆角 */
border-radius: 20rpx;
/* 边 */
border: 3rpx solid #E0E3DA;
box-shadow: 5rpx 5rpx 5rpx 5rpx #E0E3DA;
background-color: #ffffff;
margin: 30rpx;
/* padding使得文字和图片不至于贴着边框 */
padding: 30rpx;
}
/* 图片 */
.image_1 {
width: 100rpx;
height: 100rpx;
}
/* 包裹两行文字 */
.view_wenzi2 {
width: 100%;
height: 300px;
margin-left: 25rpx;
display: flex;
flex-direction: column;
.top2 {
margin-top: 5px;
}
.text1 {
font-size: 15px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
.text2 {
flex: 1;
margin-top: 5px;
font-size: 12px;
}
}
/* 小字 */
.text_small {
font-size: 30rpx;
word-break: break-all;
color: #7a7878;
margin-top: 10rpx
}
.text_small2 {
font-size: 25rpx;
color: #f00;
margin-top: 10rpx
}
</style>

BIN
supervise-wx-mp/static/yujing.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

13
supervise-wx-mp/utils/auth.token.js

@ -0,0 +1,13 @@
const TokenKey = 'Global-Auth-Token'
const token = {
getToken: function() {
return uni.getStorageSync(TokenKey)
},
setToken: function(token) {
return uni.setStorageSync(TokenKey, token)
}
}
export default token

13
supervise-wx-mp/utils/request/index.js

@ -0,0 +1,13 @@
// 引入配置
import config from '@/common/config'
// 初始化请求配置
uni.$u.http.setConfig((defaultConfig) => {
/* defaultConfig 为默认全局配置 */
defaultConfig.baseURL = config.baseUrl /* 根域名 */
return defaultConfig
})
module.exports = (vm) => {
require('./requestInterceptors')(vm)
require('./responseInterceptors')(vm)
}

35
supervise-wx-mp/utils/request/requestInterceptors.js

@ -0,0 +1,35 @@
import conf from "@/common/config.js"
import token from '@/utils/auth.token.js'
/**
* 请求拦截
* @param {Object} http
*/
module.exports = (vm) => {
uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
config.data = config.data || {}
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
// console.log(vm.$store.state);
// 设置请求头中token的名字,以及token的值
let _token = token.getToken()
let _tokenName = 'Authorization'
if (conf.tokenName) {
_tokenName = conf.tokenName
}
config.header[_tokenName] = _token
// 设置针对响应结果的处理方式
let _cus = {
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
Object.assign(_cus, config.custom)
config.custom = _cus
return config
}, (config) => // 可使用async await 做异步操作
Promise.reject(config))
}

130
supervise-wx-mp/utils/request/responseInterceptors.js

@ -0,0 +1,130 @@
import conf from "@/common/config.js"
/**
* 响应拦截
* @param {Object} http
*/
module.exports = (vm) => {
uni.$u.http.interceptors.response.use((response) => {
/* 对响应成功做点什么 可使用async await 做异步操作*/
// 自定义参数
let _cus = {
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
if (response.config && response.config.custom)
Object.assign(_cus, response.config.custom)
if (200 == response.statusCode) {
let data = response.data
if (_cus.catchError) {
if (data.success) {
return data.data
} else {
let loginTimeoutCode = "5000"
let loginTimeoutPage = "/pages/login/index"
if (conf.loginTimeoutCode) {
loginTimeoutCode = "" + conf.loginTimeoutCode
}
if (conf.loginTimeoutPage) {
loginTimeoutPage = conf.loginTimeoutPage
}
if (loginTimeoutCode === data.code) {
uni.showModal({
title: '提示',
content: '登录过期或失效,请重新登录!',
showCancel: false,
success: function(res) {
uni.reLaunch({
url: loginTimeoutPage
})
}
});
} else {
if (_cus.showFailMessage) {
uni.showToast({
title: data.msg,
icon: 'none',
duration: 2000,
})
}
}
return Promise.reject(data)
}
} else {
return data
}
} else {
console.log('response.statusCode--', response)
let em = {
"code": "" + response.statusCode,
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (res.data) {
em.msg = res.data
}
if (_cus.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
return Promise.reject(em)
}
// const data = response.data
// if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
// // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
// if (custom.toast !== false) {
// uni.$u.toast(data.message)
// }
// // 如果需要catch返回,则进行reject
// if (custom?.catch) {
// return Promise.reject(data)
// } else {
// // 否则返回一个pending中的promise
// return new Promise(() => {})
// }
// }
// return data.data || {}
}, (error) => {
console.log('request-fail', error)
let _opts = {
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
if (error.config && error.config.custom)
Object.assign(_opts, response.config.custom)
let em = {
"code": "600",
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (404 == error.statusCode) {
em.code = "404"
em.msg = "404,请求地址错误"
}
if (500 == error.statusCode) {
em.code = "500"
em.msg = "500,内部服务错误"
}
if (_opts.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
return Promise.reject(em)
})
}

196
supervise-wx-mp/utils/requester.js

@ -0,0 +1,196 @@
/**
* config 配置项说明
* baseUrl = "", // 接口的根地址
* tokenName = "Authorization", // 请求头中token的名字,与服务器端对应
* loginTimeoutCode : "5000", // 登录超时或失效的情况下,服务器端返回的错误码
* loginTimeoutPage = "/pages/login/index", // 登录超时或失效的情况下,跳转到的登录页面
*/
import config from '@/common/config.js'
import token from '@/utils/auth.token.js'
/**
* request options配置项说明
* url: '', // url String 是 开发者服务器接口地址
* data: {}, // data Object/String/ArrayBuffer 否 请求的参数 App 3.3.7 以下不支持 ArrayBuffer 类型
* header: {}, // header Object 否 设置请求的 header,header 中不能设置 Referer。 App、H5端会自动带上cookie,且H5端不可手动修改
* method: 'GET', // method String 否 GET 有效值详见下方说明
* timeout: 6000, // timeout Number 否 60000 超时时间,单位 ms H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
* showLoading: true, // 是否显示加载等待框
* loadingTitle: '加载中', // 加载等待框的提示文字
* showFailMessage: true, // 返回失败信息是否显示
* catchError: true, // 是否集中管理返回的success为false的情况,如果为false,在请求处判断code值做业务处理
*/
const request = (options) => {
let _opts = {
url: '', // url String 是 开发者服务器接口地址
data: {}, // data Object/String/ArrayBuffer 否 请求的参数 App 3.3.7 以下不支持 ArrayBuffer 类型
header: {}, // header Object 否 设置请求的 header,header 中不能设置 Referer。 App、H5端会自动带上cookie,且H5端不可手动修改 {"content-type": "application/json"}
method: 'GET', // method String 否 GET 有效值详见下方说明
timeout: 60000, // timeout Number 否 60000 超时时间,单位 ms H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
// dataType String 否 json 如果设为 json,会尝试对返回的数据做一次 JSON.parse
// responseType String 否 text 设置响应的数据类型。合法值:text、arraybuffer 支付宝小程序不支持
// sslVerify Boolean 否 true 验证 ssl 证书 仅App安卓端支持(HBuilderX 2.3.3+),不支持离线打包
// withCredentials Boolean 否 false 跨域请求时是否携带凭证(cookies) 仅H5支持(HBuilderX 2.6.15+)
// firstIpv4 Boolean 否 false DNS解析时优先使用ipv4 仅 App-Android 支持 (HBuilderX 2.8.0+)
// success Function 否 收到开发者服务器成功返回的回调函数
// fail Function 否 接口调用失败的回调函数
// complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
showLoading: true, // 是否显示加载等待框
loadingTitle: '加载中', // 加载等待框的提示文字
showFailMessage: true, // 返回失败信息是否显示
catchError: true, // 是否集中管理Catch,
}
Object.assign(_opts, options)
let _token = token.getToken()
let _baseUrl = config.baseUrl
let _tokenName = config.tokenName
if (_tokenName) {
_opts.header[_tokenName] = _token
} else {
_opts.header['Authorization'] = _token
}
return new Promise((resolve, reject) => {
if (_opts.showLoading) {
uni.showLoading({
title: _opts.loadingTitle || '加载中',
mask: true
})
}
return uni.request({
url: (_baseUrl || '') + (_opts.url || ''),
method: _opts.method,
data: _opts.data || null,
header: _opts.header,
timeout: _opts.timeout || 60000,
success: res => {
if (200 === res.statusCode) {
let data = res.data
if (_opts.catchError) {
if (data.success) {
resolve(data.data)
} else {
let loginTimeoutCode = "5000"
let loginTimeoutPage = "/pages/login/index"
if (config.loginTimeoutCode) {
loginTimeoutCode = "" + config.loginTimeoutCode
}
if (config.loginTimeoutPage) {
loginTimeoutPage = config.loginTimeoutPage
}
if (loginTimeoutCode === data.code) {
uni.showModal({
title: '提示',
content: '登录过期或失效,请重新登录!',
showCancel: false,
success: function(res) {
uni.reLaunch({
url: loginTimeoutPage
})
}
});
} else {
if (_opts.showFailMessage) {
uni.showToast({
title: data.msg,
icon: 'none',
duration: 2000,
})
}
}
reject(data)
}
} else {
resolve(data)
}
} else {
let em = {
"code": "404",
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (res.data) {
if (res.data.error) {
if (res.data.path) {
em.msg = res.data.path + res.data.error
} else {
em.msg = res.data.error
}
}
if (res.data.status) {
em.code = "" + res.data.status
}
}
if (_opts.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
reject(em)
}
},
fail: error => {
console.log('request-fail: ', error)
let em = {
"code": "600",
"data": "",
"msg": "网络请求失败~",
"success": false,
"timestamp": new Date().getTime()
}
if (error.errMsg) {
// em.msg = error.errMsg
}
if (_opts.showFailMessage) {
uni.showToast({
title: em.msg,
icon: 'none',
duration: 2000,
})
}
reject(em)
},
complete() {
if (_opts.showLoading) {
uni.hideLoading()
}
}
})
})
}
const req = function(url, method = "GET", data = {}, header = {}, options = {}) {
let _opts = {
url: url,
method: method,
data: data,
header: header
}
Object.assign(options, _opts)
return request(options)
}
const get = function(url, data = {}, header = {}, options = {}) {
return req(url, "GET", data, header, options)
}
const post = function(url, data = {}, header = {}, options = {}) {
return req(url, "POST", data, header, options)
}
const formpost = function(url, data = {}, header = {}, options = {}) {
let _head = {
"content-type": "application/x-www-form-urlencoded"
}
Object.assign(header, _head)
return req(url, "POST", data, header, options)
}
request.get = get
request.post = post
request.formpost = formpost
request.req = req
export default request
Loading…
Cancel
Save