refactor: 统一API请求路径并优化请求逻辑

将多个页面中的API请求路径统一为相对路径,并在`utils/login.js`中自动补全为完整URL。优化了请求方法,默认为POST,同时支持GET请求。简化了代码结构,提高了代码的可维护性和一致性。
This commit is contained in:
XiaoMo 2025-04-10 19:01:02 +08:00
parent 764df18ad0
commit 1af84dc1ee
11 changed files with 235 additions and 247 deletions

View File

@ -1,66 +1,52 @@
// pages/duiquan/duiquan.js var t = require("../../utils/login.js");
Page({ Page({
/**
* 页面的初始数据
*/
data: { data: {
pic:"https://jm-static.v0750.com/image/dui.jpg" pic: "https://jm-static.v0750.com/image/dui.jpg",
tips: "温馨提示:\n1、请在30分钟内完成兑换超时作废\n2、请在兑换完成后在本页面点击\"确认兑换\"\n3、兑换成功后将在3个工作日内发放奖品",
}, },
onLoad() {
/** t.request('/miniprogram/coupons', {}, !0).then((res) => {
* 生命周期函数--监听页面加载 if (res.code == 200) {
*/ this.setData({
onLoad(options) { tips: res.data.tips,
pic: res.data.pic
}, })
}
/** })
* 生命周期函数--监听页面初次渲染完成 },
*/
onReady() { onSubmitToGroup(e) {
const code = e.detail.value.code
}, if (!code) {
wx.showToast({
/** title: '请输入券码',
* 生命周期函数--监听页面显示 icon: 'none'
*/ })
onShow() { return
}
},
wx.showLoading({
/** title: '兑换中...'
* 生命周期函数--监听页面隐藏 })
*/ t.request('/miniprogram/coupons/importDouyin', { code: code }, !0).then((res) => {
onHide() { wx.hideLoading()
if (res.code == 200) {
}, wx.showToast({
title: '兑换成功',
/** icon: 'success'
* 生命周期函数--监听页面卸载 });
*/ setTimeout(() => {
onUnload() { wx.navigateTo({
url: '/pages/washCodeList/washCodeList'
}, })
})
/** } else {
* 页面相关事件处理函数--监听用户下拉动作 wx.showToast({
*/ title: res.message || '兑换失败',
onPullDownRefresh() { icon: 'none'
})
}, }
})
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
} }
}) })

View File

@ -2,23 +2,13 @@
<image src="{{pic}}" mode="widthFix" class="pic"></image> <image src="{{pic}}" mode="widthFix" class="pic"></image>
<view class="content"> <view class="content">
<view class="title">请输入券码</view> <view class="title">请输入券码</view>
<form bind:submitToGroup="onSubmitToGroup"> <form bindsubmit="onSubmitToGroup">
<input class="input" auto-focus placeholder="支持抖音/美团/聚美券码"/> <input class="input" name="code" auto-focus placeholder="支持抖音/美团/聚美券码"/>
<button class="button" form-type="submit" type="primary">立即兑换</button> <button class="button" form-type="submit" type="primary">立即兑换</button>
</form> </form>
<text class="tips"> <text class="tips">
温馨提示: {{tips}}
1、卡券一经兑换不作退换
2、请在有效期内、在对应网点使用
3、请按网点内安全指引操作洗车机
4、洗车场地湿滑请小心滑倒
5、不要在洗车场内奔跑、玩耍
6、禁止攀爬
7、照看好老人、小孩
8、喷枪水压极大禁止使用喷枪对着人体喷水
9、驶出洗车场时注意四周行人及车辆
10、请保管好您的财物请勿离开洗车区域.
</text> </text>
</view> </view>
</view> </view>

View File

@ -38,7 +38,7 @@ Page({
e.globalData.latitude = res.latitude; e.globalData.latitude = res.latitude;
e.globalData.longitude = res.longitude; e.globalData.longitude = res.longitude;
// 获取网点列表 // 获取网点列表
t.request('https://t-jm.v0750.com/miniprogram/branch/lists', { t.request('/miniprogram/branch/lists', {
latitude: res.latitude || '', latitude: res.latitude || '',
longitude: res.longitude || '' longitude: res.longitude || ''
}, !0).then(function (res) { }, !0).then(function (res) {
@ -49,7 +49,7 @@ Page({
}, },
fail: function () { fail: function () {
// 即使获取位置失败也加载网点列表,只是不传入经纬度参数 // 即使获取位置失败也加载网点列表,只是不传入经纬度参数
t.request('https://t-jm.v0750.com/miniprogram/branch/lists', {}, !0).then(function(res) { t.request('/miniprogram/branch/lists', {}, !0).then(function (res) {
a.setData({ a.setData({
wangdianList: res.data || [] wangdianList: res.data || []
}); });
@ -65,14 +65,11 @@ Page({
}); });
} }
}); });
},
getData: function () {
var a = this;
// 获取首页数据 // 获取首页数据
t.request(e.globalData.config.home, {}, !0).then(function (e) { t.request('/miniprogram/index/home', { method: 'GET' }, !0).then(function (e) {
a.setData({ a.setData({
banner: e.data.banner, banner: e.data.banner,
user: e.data.user,
tmplIds: e.data.tmplIds, tmplIds: e.data.tmplIds,
joinUs: e.data.joinUs, joinUs: e.data.joinUs,
notice: e.data.notice, notice: e.data.notice,
@ -90,6 +87,15 @@ Page({
}); });
}); });
}, },
getData: function () {
var a = this;
// 获取首页数据
t.request('/miniprogram/index/balance', {}, !0).then(function (e) {
a.setData({
user: e.data || [],
});
});
},
load: function (e) { load: function (e) {
console.log(e); console.log(e);
var t = e.currentTarget.dataset.index, a = this.data.swiperWidth * e.detail.height / e.detail.width; var t = e.currentTarget.dataset.index, a = this.data.swiperWidth * e.detail.height / e.detail.width;
@ -100,7 +106,7 @@ Page({
onShow: function () { onShow: function () {
this.setData({ this.setData({
isScan: !!e.globalData.devicecode || this.data.isScan isScan: !!e.globalData.devicecode || this.data.isScan
}), e.globalData.config && e.globalData.config.home && (this.getData(), this.isneedUpload = !1); }), (this.getData(), this.isneedUpload = !1);
}, },
toPage: function (t) { toPage: function (t) {
console.info(t); console.info(t);
@ -127,7 +133,7 @@ Page({
success: function (i) { success: function (i) {
console.log(i), wx.showLoading({ console.log(i), wx.showLoading({
title: "" title: ""
}), e.globalData.qrcode = i.result, t.request("https://t-jm.v0750.com/miniprogram/identify", i, !0).then(function (t) { }), e.globalData.qrcode = i.result, t.request("/miniprogram/identify", i, !0).then(function (t) {
console.log("data", t), wx.hideLoading(), e.globalData.devicecode = t.data.devicecode || e.globalData.devicecode, console.log("data", t), wx.hideLoading(), e.globalData.devicecode = t.data.devicecode || e.globalData.devicecode,
a.setData({ a.setData({
isScan: !!t.data.devicecode isScan: !!t.data.devicecode
@ -199,7 +205,7 @@ Page({
wx.requestSubscribeMessage({ wx.requestSubscribeMessage({
tmplIds: this.data.tmplIds, tmplIds: this.data.tmplIds,
success: function (e) { success: function (e) {
console.log("requestSubscribeMessage", e), t.request("https://t-jm.v0750.com/miniprogram/index/newtmpl", e, !0).then(function (e) { }).catch(function (e) { }); console.log("requestSubscribeMessage", e), t.request("/miniprogram/index/newtmpl", e, !0).then(function (e) { }).catch(function (e) { });
}, },
fail: function (e) { fail: function (e) {
console.log("requestSubscribeMessagefail", e); console.log("requestSubscribeMessagefail", e);

View File

@ -28,7 +28,7 @@ Page({
var n = Object.assign(this.userInfo, t, { var n = Object.assign(this.userInfo, t, {
code: a code: a
}); });
o.request(e.globalData.config.authorization, n, !0).then(function(o) { o.request('/miniprogram/index/authorization', n, !0).then(function(o) {
if (wx.hideLoading(), console.log("toAuthorization", "app.globalData.needJumpUrl", e.globalData.needJumpUrl, o), if (wx.hideLoading(), console.log("toAuthorization", "app.globalData.needJumpUrl", e.globalData.needJumpUrl, o),
200 != o.code) return wx.showModal({ 200 != o.code) return wx.showModal({
title: "提示", title: "提示",

View File

@ -82,7 +82,7 @@ Page({
content: "请输入手机号码~" content: "请输入手机号码~"
}), !1; }), !1;
var a = this; var a = this;
o.request(t.globalData.config.verify.sms, { o.request('/miniprogram/verify/sms', {
mobile: a.data.info.mobile, mobile: a.data.info.mobile,
type: a.data.type type: a.data.type
}, !0).then(function (t) { }, !0).then(function (t) {

View File

@ -21,7 +21,7 @@ Page({
var n = this; var n = this;
wx.showLoading({ wx.showLoading({
title: "" title: ""
}), e.request(t.globalData.config.user.log, {}, !0).then(function(t) { }), e.request('/miniprogram/user', {}, !0).then(function(t) {
if (wx.hideLoading(), 200 != t.code) return wx.showModal({ if (wx.hideLoading(), 200 != t.code) return wx.showModal({
title: "提示", title: "提示",
content: t.message content: t.message
@ -71,7 +71,7 @@ Page({
success: function(uploadRes) { success: function(uploadRes) {
var data = JSON.parse(uploadRes.data); var data = JSON.parse(uploadRes.data);
if (data.code == 200) { if (data.code == 200) {
e.request('https://t-jm.v0750.com/miniprogram/user/update', { e.request('/miniprogram/user/update', {
avatar: data.data.url avatar: data.data.url
}, true).then(function(res) { }, true).then(function(res) {
if (res.code == 200) { if (res.code == 200) {
@ -101,7 +101,8 @@ Page({
placeholderText: '请输入新昵称', placeholderText: '请输入新昵称',
success: function(res) { success: function(res) {
if (res.confirm && res.content) { if (res.confirm && res.content) {
e.request('https://t-jm.v0750.com/miniprogram/user/update', { // 发送请求到后端,更新用户昵称
e.request('/miniprogram/user/update', {
nickname: res.content nickname: res.content
}, true).then(function(res) { }, true).then(function(res) {
if (res.code == 200) { if (res.code == 200) {

View File

@ -44,7 +44,7 @@ Page({
loading: !0 loading: !0
}), wx.showLoading({ }), wx.showLoading({
title: "" title: ""
}), e.request(t.globalData.config.recharge.init, { }), e.request('/miniprogram/recharge', {
page: this.data.page.next_page page: this.data.page.next_page
}, !0).then(function (t) { }, !0).then(function (t) {
if (wx.hideLoading(), a.setData({ if (wx.hideLoading(), a.setData({
@ -68,7 +68,7 @@ Page({
var i = a.currentTarget.dataset.id; var i = a.currentTarget.dataset.id;
wx.showLoading({ wx.showLoading({
title: "" title: ""
}), e.request(t.globalData.config.recharge.create, { }), e.request('/miniprogram/recharge/create', {
id: i id: i
}, !0).then(function (t) { }, !0).then(function (t) {
if (wx.hideLoading(), 200 != t.code) return wx.showModal({ if (wx.hideLoading(), 200 != t.code) return wx.showModal({
@ -125,7 +125,7 @@ Page({
title: "" title: ""
}); });
var i = this; var i = this;
e.request(t.globalData.config.recharge.create, { e.request('/miniprogram/recharge/create', {
id: i.data.selectItemID id: i.data.selectItemID
}, !0).then(function (t) { }, !0).then(function (t) {
if (wx.hideLoading(), i.isSubmit = !1, 200 != t.code) return wx.showModal({ if (wx.hideLoading(), i.isSubmit = !1, 200 != t.code) return wx.showModal({

View File

@ -1,4 +1,6 @@
// pages/wangdian/wangdian.js // pages/wangdian/wangdian.js
var t = require("../../utils/login.js");
Page({ Page({
/** /**
@ -98,39 +100,31 @@ Page({
}, },
// 获取网点详情信息 // 获取网点详情信息
getBranchDetail() { getBranchDetail() {
wx.request({ t.request('/miniprogram/branch/detail', {
url: 'https://t-jm.v0750.com/miniprogram/branch/detail',
data: {
number: this.data.number, number: this.data.number,
latitude: wx.getStorageSync('latitude'), latitude: wx.getStorageSync('latitude'),
longitude: wx.getStorageSync('longitude'), longitude: wx.getStorageSync('longitude'),
},
method: 'GET', method: 'GET',
success: (res) => { }, !0).then((res) => {
if (res.data && res.data.code == 200) { if (res.code == 200) {
this.setData({ this.setData({
banner: res.data.data.banner || [], banner: res.data.banner || [],
info: res.data.data.info || {} info: res.data.info || {},
}); });
} }
} })
});
}, },
// 获取机器列表 // 获取机器列表
getMachineList() { getMachineList() {
wx.request({
url: 'https://t-jm.v0750.com/miniprogram/branch/machine',
data: {
number: this.data.number,
},
method: 'POST',
success: (res) => {
if (res.data && res.data.code == 200) {
this.setData({
jiqilist: res.data.data || []
});
t.request('/miniprogram/branch/machine', {
number: this.data.number,
}).then((res) => {
if (res.code == 200) {
this.setData({
jiqilist: res.data || [],
});
// 计算机器数量, 与空闲机器数量 free machine // 计算机器数量, 与空闲机器数量 free machine
let machineCount = this.data.jiqilist.length; let machineCount = this.data.jiqilist.length;
let freeMachineCount = 0; let freeMachineCount = 0;
@ -144,9 +138,7 @@ Page({
machineCount, machineCount,
freeMachineCount freeMachineCount
}); });
} }
} })
});
}, },
}) })

View File

@ -20,7 +20,7 @@ Page({
var o = this; var o = this;
wx.showLoading({ wx.showLoading({
title: "" title: ""
}), i.request(e.globalData.config.carwashcoupon.details, { }), i.request('/miniprogram/carwashcoupon/details', {
id: this.data.id id: this.data.id
}, !0).then(function (e) { }, !0).then(function (e) {
if (wx.hideLoading(), 200 != e.code) return wx.showModal({ if (wx.hideLoading(), 200 != e.code) return wx.showModal({
@ -40,7 +40,7 @@ Page({
received: function () { received: function () {
if (this.isSubmit) return !1; if (this.isSubmit) return !1;
var t = this; var t = this;
this.isSubmit = !0, wx.showLoading(), console.log("received"), i.request(e.globalData.config.carwashcoupon.receive, { this.isSubmit = !0, wx.showLoading(), console.log("received"), i.request('/miniprogram/carwashcoupon/receive', {
id: t.data.id, id: t.data.id,
token: t.data.info.token token: t.data.info.token
}, !0).then(function (e) { }, !0).then(function (e) {

View File

@ -50,17 +50,17 @@ Page({
if (this.isloading) return !1; if (this.isloading) return !1;
wx.showLoading(), this.setData({ wx.showLoading(), this.setData({
loading: !0 loading: !0
}), this.isloading = !0, e.request(t.globalData.config.carwashcoupon.lists, {}, !0).then(function(t) { }), this.isloading = !0, e.request('/miniprogram/coupons/lists', { devicecode: t.globalData.devicecode }, !0).then(function (t) {
if (o.isloading = !1, o.setData({ if (o.isloading = !1, o.setData({
loading: !1 loading: !1
}), wx.hideLoading(), 200 != t.code) return wx.showModal({ }), wx.hideLoading(), 200 != t.code) return wx.showModal({
title: "提示", title: "提示",
content: t.message content: t.message
}), !1; }), !1;
t.data.list.map(function(t) { t.data.data.map(function (t) {
t.starttime = a.changeTimeyear(new Date(1e3 * t.starttime)), t.deadline = a.changeTimeyear(new Date(1e3 * t.deadline)); t.starttime = a.changeTimeyear(new Date(1e3 * t.starttime)), t.deadline = a.changeTimeyear(new Date(1e3 * t.deadline));
}), o.setData({ }), o.setData({
list: t.data.list, list: t.data.data,
user: t.data.user, user: t.data.user,
showLoding: !1 showLoding: !1
}); });
@ -77,7 +77,7 @@ Page({
success: function (i) { success: function (i) {
wx.showLoading({ wx.showLoading({
title: "" title: ""
}), t.globalData.qrcode = i.result, e.request("https://t-jm.v0750.com/miniprogram/identify", i, !0).then(function(e) { }), t.globalData.qrcode = i.result, e.request("/miniprogram/identify", i, !0).then(function (e) {
if (wx.hideLoading(), 200 != e.code) return wx.showModal({ if (wx.hideLoading(), 200 != e.code) return wx.showModal({
title: "提示", title: "提示",
content: e.message content: e.message
@ -112,7 +112,7 @@ Page({
success: function (i) { success: function (i) {
i.confirm && (a.isSubmit = !0, wx.showLoading({ i.confirm && (a.isSubmit = !0, wx.showLoading({
title: "正在启动" title: "正在启动"
}), e.request(t.globalData.config.machine.carwashcodestartup, { }), e.request('/miniprogram/machine/carwashcodestartup', {
washcode: a.data.selectID washcode: a.data.selectID
}, !0).then(function (t) { }, !0).then(function (t) {
a.isSubmit = !1, wx.hideLoading(), 200 == t.code && wx.redirectTo({ a.isSubmit = !1, wx.hideLoading(), 200 == t.code && wx.redirectTo({

View File

@ -12,6 +12,19 @@ function t() {
function n(a) { function n(a) {
var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}; var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
// a 是否 https, 如果不是自动补全
if (!/^https/.test(a)) {
a = 'https://t-jm.v0750.com' + a;
}
// method 默认为 GET
let method = "POST";
if (t.method) {
method = t.method;
delete t.method;
}
return wx.showLoading(), new Promise(function (n, c) { return wx.showLoading(), new Promise(function (n, c) {
console.log("requestdevicecode", o.globalData.devicecode); console.log("requestdevicecode", o.globalData.devicecode);
var i = Object.assign({}, { var i = Object.assign({}, {
@ -26,7 +39,7 @@ function n(a) {
Cookie: "_miucms_session=" + wx.getStorageSync("_miucms_session"), Cookie: "_miucms_session=" + wx.getStorageSync("_miucms_session"),
token: wx.getStorageSync("session") || "" token: wx.getStorageSync("session") || ""
}, },
method: "POST", method: method,
success: function (o) { success: function (o) {
var t = o.data; var t = o.data;
if ("object" != e(t) && null != t) { if ("object" != e(t) && null != t) {