Files
shoot-miniprograms/src/apis.js

458 lines
12 KiB
JavaScript
Raw Normal View History

2025-08-06 11:56:28 +08:00
let BASE_URL = "https://api.shelingxingqiu.com/api/shoot"; // 默认正式版
try {
const accountInfo = uni.getAccountInfoSync();
const envVersion = accountInfo.miniProgram.envVersion;
switch (envVersion) {
case "develop": // 开发版
2025-08-15 16:37:10 +08:00
// BASE_URL = "http://192.168.1.242:8000/api/shoot";
BASE_URL = "https://apitest.shelingxingqiu.com/api/shoot";
2025-08-06 11:56:28 +08:00
break;
case "trial": // 体验版
2025-08-09 12:16:36 +08:00
BASE_URL = "https://apitest.shelingxingqiu.com/api/shoot";
2025-08-06 11:56:28 +08:00
break;
case "release": // 正式版
BASE_URL = "https://api.shelingxingqiu.com/api/shoot";
break;
default:
// 保持默认值
break;
}
} catch (e) {
console.error("获取环境信息失败,使用默认正式环境", e);
}
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);
2025-08-04 16:28:34 +08:00
else if (message) {
2025-07-03 21:13:55 +08:00
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-08-01 15:25:51 +08:00
battleId,
2025-07-14 13:39:10 +08:00
mode: battleStats.mode, // 1.几V几 2.大乱斗
gameMode: battleStats.gameMode, // 1.约战 2.排位
2025-08-15 11:23:23 +08:00
teamSize: battleStats.teamSize,
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-08-15 11:23:23 +08:00
data.mvps = [];
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;
}
2025-08-15 11:23:23 +08:00
if (playerBattleStats.mvp) {
data.mvps.push(playerBattleStats);
}
2025-06-09 01:17:18 +08:00
roundRecords.forEach((round) => {
data.roundsData[round.roundNumber] = {
...data.roundsData[round.roundNumber],
[round.playerId]: round.arrowHistory,
};
});
2025-06-08 20:59:41 +08:00
});
2025-08-15 11:23:23 +08:00
data.mvps.sort((a, b) => b.totalRings - a.totalRings);
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) => {
2025-07-29 11:12:51 +08:00
const reuslt = await request("GET", `/user/order/list?page=${page}`);
2025-07-01 00:25:17 +08:00
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 || {};
};
2025-08-04 16:28:34 +08:00
export const getPointBookConfigAPI = async () => {
return request("GET", "/user/score/sheet/option");
};
2025-08-04 17:54:59 +08:00
export const savePointBookAPI = async (
bowType,
distance,
targetType,
groups,
arrows,
2025-08-05 11:51:09 +08:00
data = []
2025-08-04 17:54:59 +08:00
) => {
return request("POST", "/user/score/sheet/report", {
bowType,
distance,
targetType,
groups,
arrows,
2025-08-05 11:51:09 +08:00
group_data: data.map((item) =>
item.map((i) => ({
...i,
ring: i.ring === "M" ? -1 : i.ring === "X" ? 0 : Number(i.ring),
}))
),
2025-08-04 17:54:59 +08:00
});
};
2025-08-05 11:51:09 +08:00
export const getPointBookListAPI = async (
page = 1,
bowType,
distance,
targetType
) => {
let url = `/user/score/sheet/list?pageNum=${page}&pageSize=10`;
if (bowType) url += `&bowType=${bowType}`;
if (distance) url += `&distance=${distance}`;
if (targetType) url += `&targetType=${targetType}`;
const result = await request("GET", url);
return result.list || [];
2025-08-04 16:28:34 +08:00
};
2025-08-04 17:54:59 +08:00
export const getPointBookDetailAPI = async (id) => {
return request("GET", `/user/score/sheet/detail?id=${id}`);
};
2025-08-06 18:36:30 +08:00
2025-08-13 17:11:30 +08:00
export const getPointBookDataAPI = async () => {
2025-08-06 18:36:30 +08:00
return request("GET", "/user/score/sheet/statistics");
};
2025-08-13 17:11:30 +08:00
export const getPractiseDataAPI = async () => {
return request("GET", "/user/practice/statistics");
};
export const getBattleDataAPI = async () => {
return request("GET", "/user/fight/statistics");
};
2025-08-14 15:24:12 +08:00
export const chooseTeamAPI = async (number, group) => {
return request("POST", "/user/room/group", { number, group });
};