shawenguan 1 год назад
Родитель
Сommit
d196b7f088
1 измененных файлов с 59 добавлено и 2 удалено
  1. 59 2
      Scripts/imoutai/imoutaiHelper.js

+ 59 - 2
Scripts/imoutai/imoutaiHelper.js

@@ -571,8 +571,8 @@ function getMTRequestId() {
     return `${new Date().getTime()}${getRandomNumber(11111, 99999)}`;
 }
 
-function checkShopItemIdIndex(shopObj, itemId){
-    const items = shopObj.items;
+function checkShopItemIdIndex(skuData, itemId){
+    const items = skuData.items;
     let retIndex = -1;
     for(let i=0; i < items.length; i++){
         if(items[i].itemId == itemId){
@@ -583,6 +583,57 @@ function checkShopItemIdIndex(shopObj, itemId){
     return retIndex;
 }
 
+// 按库存最大来筛选排序
+function fliterShopByInventory(shops){
+    let tmpList = [];
+    for (let i = 0; i < shops.length; i++) {
+        const skuData = shops[i];
+        const shopId = shops[i].shopId;
+        const items = shops[i].items;
+        const existIdx = checkShopItemIdIndex(skuData, itemId);
+        if(existIdx > -1){
+            tmpList.push([items[existIdx].inventory, skuData]);
+        }
+    }
+    tmpList.sort((a,b)=>{
+        if(a[0] == b[0]){
+            return 0;
+        }
+        return a[0] > b[0] ? -1 : 1;
+    });
+    let retList = [];
+    for(let i = 0; i < tmpList.length; i++){
+        retList.push(tmpList[i][1]);
+    }
+    return retList;
+}
+
+// 按距离最近来筛选排序
+function fliterShopByDistance(itemId, shops, lat, lng){
+    let tmpList = [];
+    for (let i = 0; i < shops.length; i++) {
+        const skuData = shops[i];
+        const shopId = shops[i].shopId;
+        const items = shops[i].items;
+        if(checkShopItemIdIndex(skuData, itemId) > -1){
+            const shopInfo = G_SHOP_SRC_MAP[shopId];
+            const distance = Math.sqrt(Math.pow(parseFloat(lat) - shopInfo.lat,2) + Math.pow(parseFloat(lng) - shopInfo.lng,2));
+            tmpList.push([distance, skuData]);
+        }
+    }
+    tmpList.sort((a,b)=>{
+        if(a[0] == b[0]){
+            return 0;
+        }
+        return a[0] > b[0] ? 1 : -1;
+    });
+    let retList = [];
+    for(let i = 0; i < tmpList.length; i++){
+        retList.push(tmpList[i][1]);
+    }
+    return retList;
+}
+
 async function getShopItem(sessionId, itemId, token, deviceId, province, city, district, zeroTimeStamp) {
     return new Promise((resolve, _reject) => {
         try {
@@ -616,6 +667,7 @@ async function getShopItem(sessionId, itemId, token, deviceId, province, city, d
                 lk.log(`获取[${province + city}-${district}]门店库存`);
             }
             // lk.log(url);
+            let fliterRule = 1;
             lk.get(options, async (error, _response, data) => {
                 let ret;
                 try {
@@ -629,6 +681,11 @@ async function getShopItem(sessionId, itemId, token, deviceId, province, city, d
                                 const rspData = jsonRetObj.data || {};
                                 const shops = rspData.shops || [];
                                 // lk.log(`data=${JSON.stringify(shopIds)}`);
+                                if(fliterRule == 0){
+                                    shops = fliterShopByDistance(itemId, shops, iMT_Location.lat, iMT_Location.lng);
+                                }else if(fliterRule == 1){
+                                    shops = fliterShopByInventory(itemId, shops);
+                                }
                                 for (let i = 0; i < shops.length; i++) {
                                     const skuData = shops[i];
                                     const shopId = shops[i].shopId;