* @description 掌飞购物相关函数,获取商店物品信息。 * @param {object} shopInfo - 商店信息对象 * @param {object} overage - 用户余额对象 * @returns {[array, number, str]} - 返回购买的物品数组、总数和单位信息的数组
(shopInfo, overage)
| 372 | * @returns {[array, number, str]} - 返回购买的物品数组、总数和单位信息的数组 |
| 373 | */ |
| 374 | function getShopItems(shopInfo, overage) { |
| 375 | // 创建一个包含道具信息和空白数据列表的初始对象 |
| 376 | const info = {"Id": shopInfo.iId, "data": []}; |
| 377 | |
| 378 | // 根据用户会员状态获取道具折扣,判断道具类型并获取相应的购买数值 |
| 379 | const iMemeberRebate = $.isVip ? parseInt(shopInfo.iMemeberRebate) / 100 : 1; |
| 380 | const shopType = shopInfo.szItems[0].ItemNum !== ""; // 表示购买的道具是按数量购买 |
| 381 | const values = shopType ? shopInfo.szItems[0].ItemNum : shopInfo.szItems[0].ItemAvailPeriod; |
| 382 | |
| 383 | // 将物品数值转换成数组 |
| 384 | const numArray = values.split(',').filter(item => item !== '').map((item) => { |
| 385 | if (item === "-1") return 999; |
| 386 | return shopType ? parseInt(item) : parseInt(item) / 24; |
| 387 | }); |
| 388 | |
| 389 | // 根据价格排序道具数据 |
| 390 | const sortedData = shopInfo.szPrices.map((price, index) => ({ |
| 391 | price: parseInt(price.SuperMoneyPrice) * iMemeberRebate, |
| 392 | count: numArray[index], idx: index |
| 393 | })).sort((a, b) => b.count - a.count); |
| 394 | |
| 395 | // 将排序后的数据存入 info 对象 |
| 396 | sortedData.forEach(({ price, count, idx }) => { |
| 397 | info.data.push({ count, price, idx }); |
| 398 | }); |
| 399 | |
| 400 | // 初始化购买总数、物品数组和投入金额 |
| 401 | let totalCount = 0; |
| 402 | let purchasedItemsList = []; |
| 403 | let remMoney = $.lastDayOfMonth ? overage.money + overage.coupons : overage.coupons;y = overage.coupons; |
| 404 | |
| 405 | // 定义商品数据、最便宜商品序列(最便宜商品序列一定是列表最后一个) |
| 406 | const itemData = info.data; |
| 407 | const cheapestItemIndex = itemData.length - 1; |
| 408 | |
| 409 | for (let m = 0; m < itemData.length; m++) { |
| 410 | // 每次循环开始前把元素添加次数重置为0 |
| 411 | let pushCounts = 0; |
| 412 | |
| 413 | // 判断是否购买永久道具且传入的金额足够购买永久道具 |
| 414 | if (itemData[m].count === 999 && remMoney > itemData[m].price) { |
| 415 | purchasedItemsList.push({"count": 999, "id": info.Id, "idx": itemData[m].idx}); |
| 416 | totalCount = itemData[m].count; |
| 417 | info.unit = "永久"; |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | // 计算最大可购买的物品数量并更新总数和剩余金钱 |
| 422 | const maxPurchasableItems = Math.floor(remMoney / itemData[m].price); // 这是一个计算出的整数,表示根据当前余额和道具价格,最多可以购买的道具数量。 |
| 423 | thisTimeCost = maxPurchasableItems * itemData[m].price; // 这是一个累加的变量,用于跟踪本轮循环购买道具的总花费。 |
| 424 | totalCount += maxPurchasableItems * itemData[m].count; // 这是一个累加的变量,用于跟踪购买的总道具数量。 |
| 425 | remMoney -= maxPurchasableItems * itemData[m].price; // 这是当前可用的余额。在每次购买道具后,余额会根据购买的道具数量和价格进行更新,以反映购买后的余额。 |
| 426 | |
| 427 | // 将购买的物品加入数组 |
| 428 | for (let n = 0; n < maxPurchasableItems; n++) { |
| 429 | purchasedItemsList.push({ |
| 430 | "count": itemData[m].count, |
| 431 | "id": info.Id, |