2025-06-14 22:45:16 +08:00
|
|
|
const BASE_URL = "https://api.shelingxingqiu.com/api/shoot";
|
2025-05-10 22:25:06 +08:00
|
|
|
|
2025-05-31 17:59:36 +08:00
|
|
|
function request(method, url, data = {}) {
|
2025-05-29 23:45:44 +08:00
|
|
|
const token = uni.getStorageSync("token");
|
2025-07-22 00:01:29 +08:00
|
|
|
const header = {};
|
|
|
|
|
if (token) header.Authorization = `Bearer ${token || ""}`;
|
2025-05-10 22:25:06 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.request({
|
2025-05-31 17:59:36 +08:00
|
|
|
url: `${BASE_URL}${url}`,
|
|
|
|
|
method,
|
2025-07-22 00:01:29 +08:00
|
|
|
header,
|
2025-05-31 17:59:36 +08:00
|
|
|
data,
|
2025-07-28 15:05:19 +08:00
|
|
|
timeout: 10000,
|
2025-05-10 22:25:06 +08:00
|
|
|
success: (res) => {
|
2025-07-03 21:13:55 +08:00
|
|
|
if (res.data) {
|
|
|
|
|
const { code, data, message } = res.data;
|
|
|
|
|
if (code === 0) resolve(data);
|
|
|
|
|
else {
|
|
|
|
|
if (message.indexOf("登录身份已失效") !== -1) {
|
|
|
|
|
uni.removeStorageSync("token");
|
|
|
|
|
}
|
2025-07-25 10:00:18 +08:00
|
|
|
if (message === "ROOM_FULL") {
|
|
|
|
|
resolve({ full: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (message === "ERROR_ROOM_GAME_START") {
|
|
|
|
|
resolve({ started: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-03 21:13:55 +08:00
|
|
|
if (url.indexOf("/user/room") !== -1 && method === "GET") {
|
|
|
|
|
resolve({});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (message === "ERROR_BATTLE_GAMING") {
|
|
|
|
|
resolve({});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-26 19:35:31 +08:00
|
|
|
if (message === "BIND_DEVICE") {
|
|
|
|
|
resolve({ binded: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-12 17:12:24 +08:00
|
|
|
if (message === "ERROR_ORDER_UNPAY") {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: "当前有未支付订单",
|
|
|
|
|
icon: "none",
|
|
|
|
|
});
|
|
|
|
|
resolve({});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-03 21:13:55 +08:00
|
|
|
uni.showToast({
|
|
|
|
|
title: message,
|
|
|
|
|
icon: "none",
|
|
|
|
|
});
|
2025-06-22 02:39:03 +08:00
|
|
|
}
|
2025-06-16 00:30:56 +08:00
|
|
|
}
|
2025-05-10 22:25:06 +08:00
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
2025-07-28 15:05:19 +08:00
|
|
|
handleRequestError(err, url);
|
2025-05-10 22:25:06 +08:00
|
|
|
reject(err);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-05-31 17:59:36 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 15:05:19 +08:00
|
|
|
// 统一的错误处理函数
|
|
|
|
|
function handleRequestError(err, url) {
|
2025-07-29 11:04:19 +08:00
|
|
|
console.log("请求失败:", { err, url });
|
|
|
|
|
|
2025-07-28 15:05:19 +08:00
|
|
|
// 根据错误类型显示不同提示
|
|
|
|
|
if (err.errMsg) {
|
2025-07-29 11:04:19 +08:00
|
|
|
if (err.errMsg.includes("timeout")) {
|
2025-07-28 15:05:19 +08:00
|
|
|
showCustomToast("请求超时,请稍后重试", "timeout");
|
2025-07-29 11:04:19 +08:00
|
|
|
} else if (err.errMsg.includes("fail")) {
|
2025-07-28 15:05:19 +08:00
|
|
|
// 检查网络状态
|
|
|
|
|
uni.getNetworkType({
|
|
|
|
|
success: (res) => {
|
2025-07-29 11:04:19 +08:00
|
|
|
if (res.networkType === "none") {
|
2025-07-28 15:05:19 +08:00
|
|
|
showCustomToast("网络连接已断开,请检查网络设置", "network");
|
|
|
|
|
} else {
|
|
|
|
|
showCustomToast("服务器连接失败,请稍后重试", "server");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: () => {
|
|
|
|
|
showCustomToast("网络异常,请检查网络连接", "unknown");
|
2025-07-29 11:04:19 +08:00
|
|
|
},
|
2025-07-28 15:05:19 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
showCustomToast("请求失败,请稍后重试", "general");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
showCustomToast("网络异常,请稍后重试", "unknown");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自定义提示函数
|
|
|
|
|
function showCustomToast(message, type) {
|
|
|
|
|
const config = {
|
|
|
|
|
title: message,
|
|
|
|
|
icon: "none",
|
2025-07-29 11:04:19 +08:00
|
|
|
duration: 3000,
|
2025-07-28 15:05:19 +08:00
|
|
|
};
|
2025-07-29 11:04:19 +08:00
|
|
|
|
2025-07-28 15:05:19 +08:00
|
|
|
// 根据错误类型可以添加不同的处理逻辑
|
|
|
|
|
switch (type) {
|
2025-07-29 11:04:19 +08:00
|
|
|
case "timeout":
|
2025-07-28 15:05:19 +08:00
|
|
|
config.duration = 4000; // 超时提示显示更久
|
|
|
|
|
break;
|
2025-07-29 11:04:19 +08:00
|
|
|
case "network":
|
2025-07-28 15:05:19 +08:00
|
|
|
config.duration = 5000; // 网络问题提示显示更久
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-29 11:04:19 +08:00
|
|
|
|
2025-07-28 15:05:19 +08:00
|
|
|
uni.showToast(config);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 17:59:36 +08:00
|
|
|
// 获取全局配置
|
|
|
|
|
export const getAppConfig = () => {
|
|
|
|
|
return request("GET", "/index/appConfig");
|
2025-05-10 22:25:06 +08:00
|
|
|
};
|
2025-05-15 12:42:15 +08:00
|
|
|
|
2025-07-13 13:28:21 +08:00
|
|
|
export const getHomeData = (seasonId) => {
|
|
|
|
|
return request("GET", `/user/myHome?seasonId=${seasonId}`);
|
2025-05-15 12:42:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getProvinceData = () => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("GET", "/index/provinces/list");
|
2025-05-26 16:28:13 +08:00
|
|
|
};
|
|
|
|
|
|
2025-06-27 10:33:53 +08:00
|
|
|
export const loginAPI = async (nickName, avatarData, code) => {
|
2025-06-05 17:43:22 +08:00
|
|
|
const result = await request("POST", "/index/code", {
|
2025-05-31 17:59:36 +08:00
|
|
|
appName: "shoot",
|
|
|
|
|
appId: "wxa8f5989dcd45cc23",
|
|
|
|
|
nickName,
|
2025-06-27 10:33:53 +08:00
|
|
|
avatarData,
|
2025-05-31 17:59:36 +08:00
|
|
|
code,
|
2025-05-26 16:28:13 +08:00
|
|
|
});
|
2025-06-05 17:43:22 +08:00
|
|
|
uni.setStorageSync("token", result.token);
|
|
|
|
|
return result;
|
2025-05-26 16:28:13 +08:00
|
|
|
};
|
2025-05-29 23:45:44 +08:00
|
|
|
|
|
|
|
|
export const bindDeviceAPI = (device) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/device/bindDevice", {
|
|
|
|
|
device,
|
2025-05-30 16:14:17 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const unbindDeviceAPI = (deviceId) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/device/unbindDevice", {
|
|
|
|
|
deviceId,
|
2025-05-29 23:45:44 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-31 14:17:56 +08:00
|
|
|
export const getMyDevicesAPI = () => {
|
2025-05-31 17:59:36 +08:00
|
|
|
// "/user/device/getBinding?deviceId=9ZF9oVXs"
|
2025-05-31 18:39:41 +08:00
|
|
|
return request("GET", "/user/device/getBindings");
|
2025-05-29 23:45:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createPractiseAPI = (arrows) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/practice/create", {
|
|
|
|
|
arrows,
|
2025-05-30 16:14:17 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-21 12:59:59 +08:00
|
|
|
export const getPractiseAPI = async (id) => {
|
|
|
|
|
const result = await request("GET", `/user/practice/get?id=${id}`);
|
|
|
|
|
const data = { ...(result.UserPracticeRound || {}) };
|
|
|
|
|
if (data.arrows) data.arrows = JSON.parse(data.arrows);
|
|
|
|
|
return data;
|
2025-06-15 15:53:57 +08:00
|
|
|
};
|
|
|
|
|
|
2025-05-30 16:14:17 +08:00
|
|
|
export const createRoomAPI = (gameType, teamSize) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/createroom", {
|
|
|
|
|
gameType,
|
|
|
|
|
teamSize,
|
2025-05-29 23:45:44 +08:00
|
|
|
});
|
2025-05-30 16:14:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getRoomAPI = (number) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("GET", `/user/room?number=${number}`);
|
2025-05-30 16:14:17 +08:00
|
|
|
};
|
|
|
|
|
|
2025-06-11 23:57:03 +08:00
|
|
|
export const joinRoomAPI = (number) => {
|
|
|
|
|
return request("POST", `/user/room/join`, { number });
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-30 16:14:17 +08:00
|
|
|
export const destroyRoomAPI = (roomNumber) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/room/destroyRoom", {
|
|
|
|
|
roomNumber,
|
2025-05-30 16:14:17 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const exitRoomAPI = (number) => {
|
2025-05-31 17:59:36 +08:00
|
|
|
return request("POST", "/user/room/exitRoom", {
|
|
|
|
|
number,
|
2025-05-30 16:14:17 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const startRoomAPI = (number) => {
|
2025-06-13 14:05:30 +08:00
|
|
|
return request("POST", "/user/room/start", { number });
|
2025-05-30 16:14:17 +08:00
|
|
|
};
|
2025-05-31 15:03:14 +08:00
|
|
|
|
2025-06-18 12:32:08 +08:00
|
|
|
export const getPractiseResultListAPI = async (page = 1, page_size = 15) => {
|
|
|
|
|
const reuslt = await request(
|
2025-05-31 17:59:36 +08:00
|
|
|
"GET",
|
|
|
|
|
`/user/practice/list?page=${page}&page_size=${page_size}`
|
|
|
|
|
);
|
2025-06-18 12:32:08 +08:00
|
|
|
return reuslt.list;
|
2025-05-31 17:59:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const matchGameAPI = (match, gameType, teamSize) => {
|
|
|
|
|
return request("POST", "/user/game/match", {
|
|
|
|
|
match,
|
|
|
|
|
gameType,
|
|
|
|
|
teamSize,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const readyGameAPI = (battleId) => {
|
|
|
|
|
return request("POST", "/user/game/prepare", {
|
|
|
|
|
battleId,
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-06-06 00:34:54 +08:00
|
|
|
|
2025-06-08 20:59:41 +08:00
|
|
|
export const getGameAPI = async (battleId) => {
|
|
|
|
|
const result = await request("POST", "/user/battle/detail", {
|
2025-06-06 00:34:54 +08:00
|
|
|
id: battleId,
|
|
|
|
|
});
|
2025-07-29 11:04:19 +08:00
|
|
|
if (!result.battleStats) return {};
|
2025-07-06 12:36:20 +08:00
|
|
|
const {
|
|
|
|
|
battleStats = {},
|
|
|
|
|
playerStats = {},
|
|
|
|
|
goldenRoundRecords = [],
|
|
|
|
|
} = result;
|
2025-06-08 20:59:41 +08:00
|
|
|
const data = {
|
2025-07-14 13:39:10 +08:00
|
|
|
mode: battleStats.mode, // 1.几V几 2.大乱斗
|
|
|
|
|
gameMode: battleStats.gameMode, // 1.约战 2.排位
|
2025-06-08 20:59:41 +08:00
|
|
|
};
|
2025-07-08 13:23:29 +08:00
|
|
|
if (battleStats && battleStats.mode === 1) {
|
2025-06-09 01:17:18 +08:00
|
|
|
data.winner = battleStats.winner;
|
|
|
|
|
data.roundsData = {};
|
|
|
|
|
data.redPlayers = {};
|
|
|
|
|
data.bluePlayers = {};
|
2025-07-08 13:23:29 +08:00
|
|
|
data.goldenRound =
|
|
|
|
|
goldenRoundRecords && goldenRoundRecords.length
|
|
|
|
|
? goldenRoundRecords[0]
|
|
|
|
|
: null;
|
2025-06-09 01:17:18 +08:00
|
|
|
playerStats.forEach((item) => {
|
|
|
|
|
const { playerBattleStats = {}, roundRecords = [] } = item;
|
|
|
|
|
if (playerBattleStats.team === 0) {
|
|
|
|
|
data.redPlayers[playerBattleStats.playerId] = playerBattleStats;
|
|
|
|
|
}
|
|
|
|
|
if (playerBattleStats.team === 1) {
|
|
|
|
|
data.bluePlayers[playerBattleStats.playerId] = playerBattleStats;
|
|
|
|
|
}
|
|
|
|
|
roundRecords.forEach((round) => {
|
|
|
|
|
data.roundsData[round.roundNumber] = {
|
|
|
|
|
...data.roundsData[round.roundNumber],
|
|
|
|
|
[round.playerId]: round.arrowHistory,
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-06-08 20:59:41 +08:00
|
|
|
});
|
2025-06-09 01:17:18 +08:00
|
|
|
}
|
2025-07-08 13:23:29 +08:00
|
|
|
if (battleStats && battleStats.mode === 2) {
|
2025-06-09 01:17:18 +08:00
|
|
|
data.players = [];
|
|
|
|
|
playerStats.forEach((item) => {
|
|
|
|
|
data.players.push({
|
|
|
|
|
...item.playerBattleStats,
|
|
|
|
|
arrowHistory: item.roundRecords[0].arrowHistory,
|
2025-06-09 12:24:01 +08:00
|
|
|
});
|
2025-06-09 01:17:18 +08:00
|
|
|
});
|
2025-06-09 12:24:01 +08:00
|
|
|
data.players = data.players.sort((a, b) => b.totalScore - a.totalScore);
|
2025-06-09 01:17:18 +08:00
|
|
|
}
|
2025-07-05 20:04:28 +08:00
|
|
|
// console.log("game result:", result);
|
|
|
|
|
// console.log("format data:", data);
|
2025-06-08 20:59:41 +08:00
|
|
|
return data;
|
2025-06-06 00:34:54 +08:00
|
|
|
};
|
2025-06-08 13:55:09 +08:00
|
|
|
|
|
|
|
|
export const simulShootAPI = (device_id, x, y) => {
|
|
|
|
|
const data = {
|
|
|
|
|
device_id,
|
|
|
|
|
};
|
2025-07-05 18:51:27 +08:00
|
|
|
if (x !== undefined && y !== undefined) {
|
2025-06-08 13:55:09 +08:00
|
|
|
data.x = x;
|
|
|
|
|
data.y = y;
|
|
|
|
|
}
|
|
|
|
|
return request("POST", "/index/arrow", data);
|
|
|
|
|
};
|
2025-06-09 12:29:48 +08:00
|
|
|
|
2025-06-11 23:57:03 +08:00
|
|
|
export const getBattleListAPI = async (page, battleType) => {
|
|
|
|
|
const data = [];
|
|
|
|
|
const result = await request("POST", "/user/battle/details/list", {
|
|
|
|
|
page,
|
|
|
|
|
battleType,
|
|
|
|
|
modeType: 0,
|
|
|
|
|
});
|
|
|
|
|
(result.Battles || []).forEach((item) => {
|
|
|
|
|
let name = "";
|
|
|
|
|
if (item.battleStats.mode === 1) {
|
|
|
|
|
name = `${item.playerStats.length / 2}V${item.playerStats.length / 2}`;
|
|
|
|
|
}
|
|
|
|
|
if (item.battleStats.mode === 2) {
|
|
|
|
|
name = `${item.playerStats.length}人大乱斗`;
|
|
|
|
|
}
|
|
|
|
|
data.push({
|
|
|
|
|
name,
|
|
|
|
|
battleId: item.battleStats.battleId,
|
|
|
|
|
mode: item.battleStats.mode,
|
|
|
|
|
createdAt: item.battleStats.createdAt,
|
|
|
|
|
gameEndAt: item.battleStats.gameEndAt,
|
2025-06-18 02:02:09 +08:00
|
|
|
winner: item.battleStats.winner,
|
2025-06-11 23:57:03 +08:00
|
|
|
players: item.playerStats
|
|
|
|
|
.map((p) => p.playerBattleStats)
|
|
|
|
|
.sort((a, b) => b.totalScore - a.totalScore),
|
2025-06-18 02:02:09 +08:00
|
|
|
redPlayers: item.playerStats
|
|
|
|
|
.filter((p) => p.playerBattleStats.team === 0)
|
|
|
|
|
.map((p) => p.playerBattleStats),
|
|
|
|
|
bluePlayers: item.playerStats
|
|
|
|
|
.filter((p) => p.playerBattleStats.team === 1)
|
|
|
|
|
.map((p) => p.playerBattleStats),
|
2025-06-11 23:57:03 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
};
|
2025-06-26 23:41:23 +08:00
|
|
|
|
|
|
|
|
export const getRankListAPI = () => {
|
|
|
|
|
return request("GET", "/index/ranklist");
|
|
|
|
|
};
|
2025-07-01 00:25:17 +08:00
|
|
|
|
|
|
|
|
export const createOrderAPI = (vipId) => {
|
|
|
|
|
return request("POST", "/user/order/create", {
|
|
|
|
|
vipId,
|
|
|
|
|
quanity: 1,
|
|
|
|
|
tradeType: "mini",
|
|
|
|
|
payType: "wxpay",
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const payOrderAPI = (id) => {
|
|
|
|
|
return request("POST", "/user/order/pay", {
|
|
|
|
|
id,
|
|
|
|
|
tradeType: "mini",
|
|
|
|
|
payType: "wxpay",
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getOrderListAPI = async (page) => {
|
|
|
|
|
const reuslt = await request("GET", `/user/order/list?page${page}`);
|
|
|
|
|
return reuslt.items || [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const cancelOrderListAPI = async (id) => {
|
|
|
|
|
return request("POST", "/user/order/cancelOrder", { id });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isGamingAPI = async () => {
|
|
|
|
|
const result = await request("GET", "/user/isGaming");
|
|
|
|
|
return result.gaming || false;
|
|
|
|
|
};
|
2025-07-03 21:13:55 +08:00
|
|
|
|
|
|
|
|
export const getCurrentGameAPI = async () => {
|
2025-07-23 11:18:47 +08:00
|
|
|
uni.$emit("update-header-loading", true);
|
2025-07-03 21:13:55 +08:00
|
|
|
const result = await request("GET", "/user/join/battle");
|
|
|
|
|
return result.currentGame || {};
|
|
|
|
|
};
|