Commit 2dd3b671 by chenwq

优化请求逻辑,调整组件交互,修复禁用状态处理

1 parent d0cca6a0
import sign from './sign.js' import sign from "./sign.js";
import { import {
// secretKey, // secretKey,
pathEnum, pathEnum,
domain, domain,
envIndex, envIndex,
} from '../env/index' } from "../env/index";
/**接口 */ /**接口 */
class Request { class Request {
get(params, returnAll = false) { get(params, returnAll = false) {
return this.request('GET', params, returnAll) return this.request("GET", params, returnAll);
} }
post(params, returnAll = false) { post(params, returnAll = false) {
return this.request('POST', params, returnAll) return this.request("POST", params, returnAll);
} }
fetch(method, data, returnAll = true) { fetch(method, data, returnAll = true) {
return this.post({ return this.post(
{
method, method,
...data, ...data,
}, },
returnAll returnAll,
); );
} }
wx_request({ wx_request({
url, url,
method = 'GET', method = "GET",
data = {}, data = {},
header = {}, header = {},
dataType = 'json', dataType = "json",
responseType = "text" responseType = "text",
}) { }) {
return new Promise((resolve) => { return new Promise((resolve) => {
wx.request({ wx.request({
...@@ -38,99 +39,109 @@ class Request { ...@@ -38,99 +39,109 @@ class Request {
dataType, dataType,
responseType, responseType,
success(response) { success(response) {
resolve(response.data) resolve(response.data);
} },
}) });
}) });
} }
reportTrack(params) { reportTrack(params) {
return this.request('POST', params, true) return this.request("POST", params, true);
} }
request(RequestMethods, params, returnAll) { request(RequestMethods, params, returnAll) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
wx.showNavigationBarLoading() wx.showNavigationBarLoading();
let header = RequestMethods === 'POST' ? { let header =
'content-type': 'application/json' RequestMethods === "POST"
} : { ? {
'Cache-Control': 'no-cache' "content-type": "application/json",
} }
let { token } = wx.p.storage.get() || {} : {
"Cache-Control": "no-cache",
};
let { token } = wx.p.storage.get() || {};
if (token) { if (token) {
header['Authorization'] = `Bearer ${token}` header["Authorization"] = `Bearer ${token}`;
} }
let projectName = params.projectName let projectName = params.projectName;
if (!projectName) projectName = this.getRopProject() if (!projectName) projectName = this.getRopProject();
const path = pathEnum[projectName] const path = pathEnum[projectName];
params = this.getData(params) params = this.getData(params);
wx.request({ wx.request({
url: `${domain}${path}${params.method}`, url: `${domain}${path}${params.method}`,
data: params, data: params,
method: RequestMethods, method: RequestMethods,
header, header,
dataType: 'json', dataType: "json",
responseType: 'text', responseType: "text",
success(response) { success(response) {
/**业务代码 */ /**业务代码 */
let { let {
data: { data: { code, message },
code, data,
message } = response;
}, if (code == "1006") {
data wx.hideLoading();
} = response
if (returnAll) {
resolve(data)
return
}
if (code == '401') {
wx.hideLoading()
const thisPage = wx.p.router.getPageInstance(), const thisPage = wx.p.router.getPageInstance(),
options = Object.keys(thisPage.options) options = Object.keys(thisPage.options);
let currentRoute = thisPage.route, let currentRoute = thisPage.route,
query = [] query = [];
if (options.length) { if (options.length) {
options.map(key => { options.map((key) => {
query.push(`${key}=${thisPage.options[key]}`) query.push(`${key}=${thisPage.options[key]}`);
}) });
} }
if (query.length) currentRoute += `?${query.join('&')}` if (query.length) currentRoute += `?${query.join("&")}`;
wx.p.storage.rm('token') wx.p.storage.rm("token");
wx.p.isLogin = false wx.p.isLogin = false;
wx.p.storage.rm('userInfo') wx.p.storage.rm("userInfo");
return wx.p.router.redirectTo('/pages/Login/Login') return wx.p.router.redirectTo("/pages/Login/Login");
} else { } else {
if (code == '200') { if (returnAll) {
resolve(data.data) resolve(data);
return;
}
if (code == "200") {
resolve(data.data);
} else { } else {
wx.showToast({ wx.showToast({
title: message || '系统异常', title: message || "系统异常",
icon: 'error' icon: "error",
}) });
wx.p.sentry.reportError(`接口异常:${message}`, {code,message,params}) wx.p.sentry.reportError(`接口异常:${message}`, {
code,
message,
params,
});
} }
} }
}, },
fail(error) { fail(error) {
wx.hideLoading() wx.hideLoading();
wx.showToast({ wx.showToast({
title: typeof error == 'string' ? error : JSON.stringify(error) || '系统异常', title:
icon: 'error' typeof error == "string"
}) ? error
console.error(`接口异常:`, error) : JSON.stringify(error) || "系统异常",
wx.p.sentry.reportError(`接口异常:${error.message}`, {error,params}) icon: "error",
reject(error) });
console.error(`接口异常:`, error);
wx.p.sentry.reportError(`接口异常:${error.message}`, {
error,
params,
});
reject(error);
}, },
complete() { complete() {
wx.stopPullDownRefresh() wx.stopPullDownRefresh();
wx.hideNavigationBarLoading() wx.hideNavigationBarLoading();
} },
}) });
}) });
} }
getData(params) { getData(params) {
let projectName = params.projectName let projectName = params.projectName;
if (!projectName) projectName = this.getRopProject() if (!projectName) projectName = this.getRopProject();
else delete params.projectName else delete params.projectName;
// let appKey = '48e5e13229b82c1b4e6e8c96151f0637' // let appKey = '48e5e13229b82c1b4e6e8c96151f0637'
// let secret = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b' // let secret = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
// if (secretKey[projectName]) { // if (secretKey[projectName]) {
...@@ -150,38 +161,38 @@ class Request { ...@@ -150,38 +161,38 @@ class Request {
// noSignAttr = params.noSignAttr // noSignAttr = params.noSignAttr
// delete params.noSignAttr // delete params.noSignAttr
// } // }
params.v = '1.0.0' params.v = "1.0.0";
params.format = 'json' params.format = "json";
params.locale = 'zh_CN' params.locale = "zh_CN";
params.source = 'uplus-wechat' params.source = "uplus-wechat";
// params.appKey = appKey // params.appKey = appKey
return params return params;
}
getMock(configModule = "uplus-fapai-mp") {
const env = {
1: "develop",
2: "test",
3: "production",
4: "beta",
}[envIndex],
configUrl = `https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=${configModule}&env=${env}`;
return new Promise((resolve) => {
wx.request({
url: configUrl,
data: {},
method: "GET",
header: {},
dataType: "json",
responseType: "text",
success(response) {
resolve(response.data);
},
});
});
} }
getMock(configModule = "uplus-fapai-mp") {
const env = {
1: "develop",
2: "test",
3: "production",
4: "beta",
}[envIndex],
configUrl = `https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=${configModule}&env=${env}`;
return new Promise((resolve) => {
wx.request({
url: configUrl,
data: {},
method: "GET",
header: {},
dataType: "json",
responseType: "text",
success(response) {
resolve(response.data);
},
});
});
}
getRopProject() { getRopProject() {
const projectName = 'vr' const projectName = "vr";
return projectName return projectName;
} }
} }
export default new Request()
\ No newline at end of file \ No newline at end of file
export default new Request();
...@@ -393,6 +393,7 @@ Component({ ...@@ -393,6 +393,7 @@ Component({
handleCellClick(e) { handleCellClick(e) {
let { target, idx } = e.currentTarget.dataset; let { target, idx } = e.currentTarget.dataset;
if (target.disabled || this.data.disable) return;
switch (target.type) { switch (target.type) {
case "brand": case "brand":
this.setData({ this.setData({
......
...@@ -73,7 +73,7 @@ ...@@ -73,7 +73,7 @@
range-key="{{ item.optionLabel }}" range-key="{{ item.optionLabel }}"
> >
<view class="picker"> <view class="picker">
<image class="arrow" src="/components/images/arrow-x.png" /> <image class="arrow" wx:if="{{!(item.disabled || disable)}}" src="/components/images/arrow-x.png" />
<view class="text-hide" wx:if="{{ formData[item.labelName] }}">{{ formData[item.labelName] }}</view> <view class="text-hide" wx:if="{{ formData[item.labelName] }}">{{ formData[item.labelName] }}</view>
<text class="placeholder" wx:else>请选择</text> <text class="placeholder" wx:else>请选择</text>
</view> </view>
...@@ -82,10 +82,10 @@ ...@@ -82,10 +82,10 @@
<block wx:if="{{ item.type == 'region' }}"> <block wx:if="{{ item.type == 'region' }}">
<view <view
class="picker" class="picker"
bindtap="showAreaFun" bindtap="{{item.disabled || disable ? '' : 'showAreaFun'}}"
data-target="{{ item }}" data-target="{{ item }}"
> >
<image class="arrow" src="/components/images/arrow-x.png" /> <image class="arrow" wx:if="{{!(item.disabled || disable)}}" src="/components/images/arrow-x.png" />
<view class="text-hide" wx:if="{{ formData[item.labelName] }}">{{ formData[item.labelName] }}</view> <view class="text-hide" wx:if="{{ formData[item.labelName] }}">{{ formData[item.labelName] }}</view>
<text class="placeholder" wx:else>请选择</text> <text class="placeholder" wx:else>请选择</text>
</view> </view>
...@@ -111,6 +111,7 @@ ...@@ -111,6 +111,7 @@
<picker <picker
wx:if="{{ item.type == 'pickerTime' }}" wx:if="{{ item.type == 'pickerTime' }}"
mode="date" mode="date"
disabled="{{item.disabled || disable}}"
fields="{{ item.fields || 'day' }}" fields="{{ item.fields || 'day' }}"
value="{{ formData[name] || formData[item.name] }}" value="{{ formData[name] || formData[item.name] }}"
start="{{ item.start || '' }}" start="{{ item.start || '' }}"
......
...@@ -43,11 +43,15 @@ Component({ ...@@ -43,11 +43,15 @@ Component({
type: Boolean, type: Boolean,
value: false, value: false,
observer: function (newVal) { observer: function (newVal) {
console.log('isEdit changed:', newVal);
const { secondObj } = this.data; const { secondObj } = this.data;
if (secondObj && secondObj.customerFormList) { if (secondObj && secondObj.customerFormList) {
secondObj.customerFormList.forEach((item) => { secondObj.customerFormList.forEach((item) => {
if (item.type && item.type !== 'title') { if (item.type && item.type !== 'title') {
item.disabled = !newVal; item.disabled = !newVal;
if (item.type === 'brand') {
item.haveArrow = newVal;
}
} }
}); });
this.setData({ secondObj }); this.setData({ secondObj });
......
...@@ -39,6 +39,24 @@ Component({ ...@@ -39,6 +39,24 @@ Component({
formRule: constantData.formRule, formRule: constantData.formRule,
}, },
lifetimes: {
attached() {
const newVal = this.data.isEdit;
const { secondObj } = this.data;
if (secondObj && secondObj.customerFormList) {
secondObj.customerFormList.forEach((item) => {
if (item.type && item.type !== 'title') {
item.disabled = !newVal;
if (item.type === 'pickerTime' || item.type === 'region' || item.type === 'brand') {
item.haveArrow = newVal;
}
}
});
this.setData({ secondObj });
}
}
},
methods: { methods: {
getPageData() { getPageData() {
let thisPage = wx.p.router.getPageInstance(); let thisPage = wx.p.router.getPageInstance();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!