From e0a55b8efeb069fae43f57774f3a11e20877e197 Mon Sep 17 00:00:00 2001 From: YeMingfei666 <1619116647@qq.com> Date: Mon, 6 Jan 2025 13:44:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=85=B1=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=96=B9=E6=B3=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 3 + commons/config.js | 46 +++++++++++ http/http.js | 177 ++++++++++++++++++++++++++++++++++++++++++ main.js | 17 ++-- package.json | 4 + pages/index/index.vue | 8 +- utils/encryptUtil.js | 64 +++++++++++++++ utils/infoBox.js | 58 ++++++++++++++ 9 files changed, 370 insertions(+), 8 deletions(-) create mode 100644 commons/config.js create mode 100644 http/http.js create mode 100644 utils/encryptUtil.js create mode 100644 utils/infoBox.js diff --git a/.gitignore b/.gitignore index 2ccbe46..7b3e6ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules/ +/unpackage/ diff --git a/README.md b/README.md index bd3fde0..4c14511 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ #new_app +#组件 +components目录下符合目录规范如 components/my-test/my-test.vue +在页面中无需导入即可直接使用 my-test diff --git a/commons/config.js b/commons/config.js new file mode 100644 index 0000000..0634ca3 --- /dev/null +++ b/commons/config.js @@ -0,0 +1,46 @@ + +//打包时修改env的值即可 +const env='test' //test , production,local + +export const encryptKey='1234567890123456' // http数据加解密的key + +export const apiUrl='/czg/' +export const h5Config = { + production: 'https://dj-h5.hnsiyao.cn', + test: 'https://web-api.hnsiyao.cn', + local: 'http://192.168.1.41:8100' +} + +export const AppConfig = { + production: 'https://dj-api.hnsiyao.cn', + test: 'https://video-h5.hnsiyao.cn', + local: 'http://192.168.1.41:8100' +} + +function returnShareUrl(){ + if(env==='test'){ + return 'https://video-h5.hnsiyao.cn' + } + if(env==='production'){ + return 'https://dj-h5.hnsiyao.cn' + } + if(env==='local'){ + return AppConfig[env] + } +} +// #ifdef H5 +export default{ + baseUrl:h5Config[env], + baseApiUrl:h5Config[env]+apiUrl, + shareUrl:returnShareUrl() +} +// #endif + + +// #ifdef APP +export default{ + baseUrl:AppConfig[env], + baseApiUrl:AppConfig[env]+apiUrl, + shareUrl:returnShareUrl() +} +// #endif \ No newline at end of file diff --git a/http/http.js b/http/http.js new file mode 100644 index 0000000..c80941b --- /dev/null +++ b/http/http.js @@ -0,0 +1,177 @@ + +// 导入全局属性 +import { + sm4DecryptByResData +} from '@/utils/encryptUtil.js' +import infoBox from "@/utils/infoBox.js" +import { reject } from 'lodash'; +import config from '@/commons/config.js' +// 测试服 +let baseUrl = config.baseApiUrl +const loadingShowTime = 200 +function getHeader(){ + const headerObject={} + headerObject["token"] = uni.getStorageSync('token') + return headerObject +} + +// 通用处理逻辑 +function commonsProcess(showLoading, httpReqCallback) { + // 判断是否请求完成(用作 是否loading ) + // 包括: 'ing', 'ingLoading', 'finish' + let reqState = 'ing' + // 是否已经提示的错误信息 + let isShowErrorToast = false + // 请求完成, 需要处理的动作 + let reqFinishFunc = () => { + if (reqState == 'ingLoading') { // 关闭loading弹层 + infoBox.hideLoading() + } + reqState = 'finish' // 请求完毕 + } + // 明确显示loading + if (showLoading) { + // xx ms内响应完成,不提示loading + setTimeout(() => { + if (reqState == 'ing') { + reqState = 'ingLoading' + infoBox.showLoading() + } + }, loadingShowTime) + } + + return httpReqCallback().then((httpData) => { + reqFinishFunc(); // 请求完毕的动作 + // 从http响应数据中解构响应数据 [ 响应码、 bodyData ] + let { + statusCode, + data + } = httpData + // 避免混淆重新命名 + let bodyData = data + if (statusCode == 500) { + isShowErrorToast = true + return Promise.reject(bodyData) // 跳转到catch函数 + } + if (statusCode == 401) { + // 提示信息 + isShowErrorToast = true + return Promise.reject(bodyData) // 跳转到catch函数 + } + // http响应码不正确 + if (statusCode != 200 && statusCode != 204 && statusCode != 201) { + isShowErrorToast = true + data.message=data.message=='Bad credentials'?'用户名或密码错误':data.message + infoBox.showToast(data.message || '服务器异常') + return Promise.reject(bodyData) // 跳转到catch函数 + } + + // 加密数据 + if (!bodyData.data && bodyData.encryptData) { + + return Promise.resolve({ + bizData: sm4DecryptByResData(bodyData.encryptData), + code: bodyData.code + }) + } + + // 构造请求成功的响应数据 + return Promise.resolve(bodyData) + + }).catch(res => { + if(res.status==401){ + infoBox.showErrorToast(res.message||'请登录').then(() => { + uni.redirectTo({url: '/pages/login/index'}) + reject() + }) + } + if(res.status==500){ + infoBox.showErrorToast(res.message||'服务器异常').then(() => { + }) + } + reqFinishFunc(); // 请求完毕的动作 + + // 如果没有提示错误, 那么此处提示 异常。 + if (!isShowErrorToast) { + infoBox.showErrorToast(`请求网络异常`) + } + + return Promise.reject(res) + + }).finally(() => { // finally 是 then结束后再执行, 此处不适用。 需要在请求完成后立马调用: reqFinishFunc() + + }); + +} + + +// 默认 显示loading(控制 xxs 内 不提示loading ) +function req(uri, data, method = "GET", showLoading = true, extParams = {}) { + return commonsProcess(showLoading, () => { + return uni.request( + Object.assign({ + url: baseUrl + uri, + data: data, + method: method, + header: getHeader() + }, extParams) + ) + }) +} + + +// 默认 显示loading(控制 xxs 内 不提示loading ) +function request(args) { + const { + url, + data, + params, + method = "GET", + showLoading = true, + extParams = {} + } = args + let headerObject = {} + return commonsProcess(showLoading, () => { + return uni.request( + Object.assign({ + url: baseUrl + url, + data: params||data, + method: method, + header: getHeader() + }, extParams) + + ) + }) +} + + + +// 上传 +function upload(uri, data, file, showLoading = true, extParams = {}) { + // 放置token + let headerObject = {} + return commonsProcess(showLoading, () => { + return uni.uploadFile( + Object.assign({ + url: baseUrl + uri, + formData: data, + name: "file", + filePath: file.path||file.url, + header: getHeader() + }, extParams) + ).then((httpData) => { + // uni.upload 返回bodyData 的是 string类型。 需要解析。 + httpData.data = JSON.parse(httpData.data) + return Promise.resolve(httpData) + }).catch(err=>{ + uni.hideLoading() + infoBox.showErrorToast(`上传失败`) + }) + }) +} + +export default { + req: req, + request, + upload: upload +} \ No newline at end of file diff --git a/main.js b/main.js index 0a1e17b..bd4b638 100644 --- a/main.js +++ b/main.js @@ -1,23 +1,26 @@ import App from './App' import uviewPlus from 'uview-plus' + // #ifndef VUE3 import Vue from 'vue' import './uni.promisify.adaptor' Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ - ...App + ...App }) app.$mount() // #endif // #ifdef VUE3 -import { createSSRApp } from 'vue' +import { + createSSRApp +} from 'vue' export function createApp() { - const app = createSSRApp(App) - app.use(uviewPlus) - return { - app - } + const app = createSSRApp(App) + app.use(uviewPlus) + return { + app + } } // #endif \ No newline at end of file diff --git a/package.json b/package.json index 892ce72..12cca06 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,10 @@ "dependencies": { "clipboard": "^2.0.11", "dayjs": "^1.11.13", + "gm-crypto": "^0.1.8", + "jsbn": "^1.1.0", + "lodash": "^4.17.21", + "to-arraybuffer": "^1.0.1", "uview-plus": "^3.3.61" } } diff --git a/pages/index/index.vue b/pages/index/index.vue index 48d48ea..732e274 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -1,7 +1,13 @@ -