From 2f66303367beafa3a1ab30757f9d59338176cc40 Mon Sep 17 00:00:00 2001 From: RIceWqy <1840169763@qq.com> Date: Mon, 25 Dec 2023 09:28:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shopro-auth-modal/shopro-auth-modal.vue | 14 +--- main.js | 6 +- pages/index/category.vue | 19 ++++- pages/index/category/one-catgory.vue | 6 ++ pages/index/category/takeout-catgory.vue | 1 + pages/index/category/three-catgory.vue | 13 ++- pages/index/category/two-catgory.vue | 12 ++- shopro/index.js | 2 +- shopro/request/index.js | 2 + shopro/request/indexJson.js | 82 +++++++++++++++++++ shopro/request/request.js | 55 +++++++++++++ 11 files changed, 188 insertions(+), 24 deletions(-) create mode 100644 shopro/request/indexJson.js diff --git a/components/shopro-auth-modal/shopro-auth-modal.vue b/components/shopro-auth-modal/shopro-auth-modal.vue index 48d7bab..cc0e076 100644 --- a/components/shopro-auth-modal/shopro-auth-modal.vue +++ b/components/shopro-auth-modal/shopro-auth-modal.vue @@ -340,7 +340,7 @@ /** * 登录提示页 */ -import jsonrpc from "@/shopro/jsonrpc/jsonrpc2.0.js" + import FormValidate from "@/shopro/validate/form"; import schema from "@/uview-ui/libs/util/async-validator"; import wechat from "@/shopro/wechat/wechat"; @@ -505,17 +505,7 @@ export default { // 校验数据 validation(key, callback = () => { }) { - console.log("validation") - let wqy = function(responce){ - console.log(responce) - } - let res = { - "method":"/account/login", - "jsonrpc":"2.0", - "params":{"username":"admin", - "password":"123"} - } - jsonrpc.send("http://192.168.10.151:5080/jsonrpc",res,wqy,wqy) + let that = this; //拿到需要校验的数据 let fieldValue = this.form[this.authType].data[key]; diff --git a/main.js b/main.js index 3a3e9a8..7166cfc 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,7 @@ import { import store from "@/shopro/store"; import uView from "uview-ui"; import shopro from "@/shopro"; - +import jsonrpc from "@/shopro/request/indexJson.js" async function bootstrap() { App.mpType = "app"; @@ -17,7 +17,7 @@ async function bootstrap() { Vue.use(uView); // 加载shopro Vue.use(shopro); - +Vue.use(jsonrpc) const app = new Vue({ store, ...App @@ -30,4 +30,4 @@ async function bootstrap() { // #endif } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/pages/index/category.vue b/pages/index/category.vue index 019790d..aaa2edc 100644 --- a/pages/index/category.vue +++ b/pages/index/category.vue @@ -21,6 +21,7 @@ import threeCatgory from './category/three-catgory.vue'; import twoCatgory from './category/two-catgory.vue'; import oneCatgory from './category/one-catgory.vue'; import { mapMutations, mapActions, mapState, mapGetters } from 'vuex'; +import jsonrpc from "@/shopro/jsonrpc/jsonrpc2.0.js" export default { components: { takeoutCatgory, @@ -46,9 +47,19 @@ export default { * type4:三级分类, type3:二级分类 ,type2:一级分类,type1:快速购买 */ getCategory() { - this.$http('category.info', { - id: this.$Route.query.id ? this.$Route.query.id : 0 - }).then(res => { + + this.$jsonrpc('category.info',{id: this.$Route.query.id ? this.$Route.query.id : 0}).then(res =>{ + + if (res.result?.type) { + this.categoryType = Number(res.result.type); + this.categoryStyleId = Number(res.result.id); + uni.setNavigationBarTitle({ + title: res.result?.name + }); + } + }) + /* this.$http('category.info', { id: this.$Route.query.id ? this.$Route.query.id : 0}).then(res => { + if (res.code === 1) { if (res.data?.type) { this.categoryType = Number(res.data.type); @@ -58,7 +69,7 @@ export default { }); } } - }); + }); */ } } }; diff --git a/pages/index/category/one-catgory.vue b/pages/index/category/one-catgory.vue index d37c0fd..50601a1 100644 --- a/pages/index/category/one-catgory.vue +++ b/pages/index/category/one-catgory.vue @@ -79,6 +79,8 @@ export default { methods: { // 获取分类 getCategory() { + + console.log("getCategory111") this.$http('category.detail', { id: this.categoryStyleId }).then(res => { @@ -92,6 +94,7 @@ export default { // 获取分类商品 getGoodsList() { + console.log("getGoodsList") let that = this; that.$http('goods.lists', { category_id: that.categoryID, @@ -108,6 +111,7 @@ export default { // 商品底部 loadMore() { + console.log("loadMore") if (this.currentPage < this.lastPage) { this.currentPage += 1; this.getGoodsList(); @@ -115,6 +119,7 @@ export default { }, onType(index) { + console.log("onType") this.listId = index; this.categoryID = this.categoryData[index].id; this.goodsList = []; @@ -123,6 +128,7 @@ export default { }, // 路由跳转 jump(path, parmas) { + console.log("jump") this.$Router.push({ path: path, query: parmas diff --git a/pages/index/category/takeout-catgory.vue b/pages/index/category/takeout-catgory.vue index e19b6f6..1c67d65 100644 --- a/pages/index/category/takeout-catgory.vue +++ b/pages/index/category/takeout-catgory.vue @@ -196,6 +196,7 @@ // 获取分类 async getCategory() { + console.log("getCategory222") const tabbarData = await this.$http('category.detail', { id: this.categoryStyleId }); diff --git a/pages/index/category/three-catgory.vue b/pages/index/category/three-catgory.vue index e3bad78..3e2c50a 100644 --- a/pages/index/category/three-catgory.vue +++ b/pages/index/category/three-catgory.vue @@ -68,13 +68,22 @@ export default { }, methods: { getCategory() { - this.$http('category.detail', { + console.log("getCategory222") + this.$jsonrpc('category.info', { + id: this.categoryStyleId + }).then(res => { + console.log("jsonrpc detail @@@",res) + + this.categoryData = res.result.children; + + }); + /* this.$http('category.detail', { id: this.categoryStyleId }).then(res => { if (res.code === 1) { this.categoryData = res.data.children; } - }); + }); */ }, onType(id) { this.listId = id; diff --git a/pages/index/category/two-catgory.vue b/pages/index/category/two-catgory.vue index a891e72..d0f8880 100644 --- a/pages/index/category/two-catgory.vue +++ b/pages/index/category/two-catgory.vue @@ -76,13 +76,21 @@ export default { }, methods: { getCategory() { - this.$http('category.detail', { + console.log("getCategory333") + this.$jsonrpc('category.detail', { + id: this.categoryStyleId + }).then(res => { + + this.categoryData = res.result.children; + + }); + /* this.$http('category.detail', { id: this.categoryStyleId }).then(res => { if (res.code === 1) { this.categoryData = res.data.children; } - }); + }); */ }, onType(id) { this.listId = id; diff --git a/shopro/index.js b/shopro/index.js index 9340350..572ba09 100644 --- a/shopro/index.js +++ b/shopro/index.js @@ -31,7 +31,7 @@ const install = Vue => { Vue.prototype.$tools = tools; Vue.mixin(mixin); - // #ifdef H5 + // #ifdef H5 // 微信H5,sdk挂载 Vue.prototype.$wxsdk = wxsdk; // 调试微信jssdk diff --git a/shopro/request/index.js b/shopro/request/index.js index 3d9b71a..9f1a843 100644 --- a/shopro/request/index.js +++ b/shopro/request/index.js @@ -71,3 +71,5 @@ function getApiPath(url) { }); return api; } + + diff --git a/shopro/request/indexJson.js b/shopro/request/indexJson.js new file mode 100644 index 0000000..ecb40f7 --- /dev/null +++ b/shopro/request/indexJson.js @@ -0,0 +1,82 @@ + + +import Request from './request' +import apiList from './apis.js' +import store from '@/shopro/store/index.js' + +const shoproRequest = new Request(); + +export default { + install(Vue) { + Vue.prototype.$jsonrpc = function ( + url, + data = {}, + toastBefore = '', // 请求前加载提示 + toastAfter = true, // 请求后错误提示 + ) { + + if (typeof url !== "string") { + return; + } + let api = getApiPath(url); + /* 请求之前拦截器 */ + shoproRequest.interceptor.request((config, cancel) => { + let token = uni.getStorageSync('token'); + if (api.auth && !token) { + store.dispatch('showAuthModal'); + uni.hideLoading(); + throw (`暂未登录,已阻止此次API请求: '${api.url}'`); + } + token && shoproRequest.setConfig(config => { + config.header.token = token; + }); + if (toastBefore !== '') { + uni.showLoading({ + title: toastBefore, + mask: true + }); + } + return config; + }); + + /* 请求之后拦截器 */ + shoproRequest.interceptor.response((response) => { + uni.hideLoading(); + if (response.code === 0) { + if (toastAfter) { + uni.showToast({ + title: response.msg || '请求出错,稍后重试', + icon: 'none', + duration: 1000, + mask: true + }); + } + } + + // token过期注销 + if (response.code === 401) { + store.dispatch('logout'); + store.dispatch('showAuthModal'); + throw (`登录已过期或注销,已阻止此次API请求: '${api.url}'`); + } + return response; + }); + + return shoproRequest.requestJson({ + url: api.url, + data, + method: api.method + }); + } + } +}; + +// 组装接口路径 +function getApiPath(url) { + let apiArray = url.split("."); + let api = apiList; + apiArray.forEach(v => { + api = api[v]; + }); + return api; +} diff --git a/shopro/request/request.js b/shopro/request/request.js index e784e1d..d47002a 100644 --- a/shopro/request/request.js +++ b/shopro/request/request.js @@ -91,25 +91,80 @@ export default class Request { * @returns {Promise} */ async request(options = {}) { + options = { ...options, ...this.config, ...this.requestBefore(options) } + return new Promise((resolve, reject) => { + /* console.log("data",options.data) + let createJsonRpc ={ + method: options.url + } + createJsonRpc.params ={ + ...options.data + } + options.url = "http://localhost:9001/jsonrpc" + options.data = createJsonRpc + options.method="POST" + + console.log(createJsonRpc) */ let mergeUrl = Request.isUrl(options.url) ? options.url : (options.baseUrl + options.url) if (JSON.stringify(options.params) !== '{}') { let query = Request.addQueryString(options.params); mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}` } + options.url = mergeUrl options.success = res => { + + resolve(this.requestAfter(res.data)) + } + options.fail = err => { + reject(this.requestAfter(err)) + } + uni.request(options) + + }) + } +async requestJson(options = {}) { + + options = { + ...options, + ...this.config, + ...this.requestBefore(options) + } + + return new Promise((resolve, reject) => { + let createJsonRpc ={ + method: '/'+options.url + } + createJsonRpc.params ={ + ...options.data + } + options.url = "http://localhost:9001/jsonrpc" + options.data = createJsonRpc + options.method="POST" + + console.log(createJsonRpc) + /* let mergeUrl = Request.isUrl(options.url) ? options.url : (options.baseUrl + options.url) + if (JSON.stringify(options.params) !== '{}') { + let query = Request.addQueryString(options.params); + mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}` + } + + options.url = mergeUrl */ + options.success = res => { + console.log(res,"res") resolve(this.requestAfter(res.data)) } options.fail = err => { reject(this.requestAfter(err)) } uni.request(options) + }) }