mayi.js 13.3 KB
Newer Older
李嘉林 committed
1
import cart from "@/api/cart"
李嘉林 committed
2 3
import shop from "@/api/shop"
import coupon from "@/api/coupon";
李嘉林 committed
4
import classificationApi from "@/api/classification";
程默 committed
5
// 登录白名单 name
6
export const noLoginList = ["index", "media-video", "goods-goodsInfo", "goods-commodityMenu", "goodsSearch-goodsSearch", "contentSearch-contentSearch", "chooseStores", "activty-receivingGift", "activty", "login-register", "login-phoneLogin", "login-authInformation", "login-forgetPassWord", "login-accountLogin", "login-wxRegister", "pay-payList", "comment-evaluateList", "goods-commonProblemList", "article-articlePage", "article-articleList", "goods-posters", "liveBroadcast", "liveBroadcast-list", "personalCenter-coupon-getCoupon", "buyerShow-showDetail", "goods-addGoodsList", "personalCenter-CDkey-exchange", "giftCards-linkReceive", "giftCoupon-preview", "smartForm", "shopCart-shareShopCart", "groupBuying-beInvite", "brandTopics", "personalCenter-spokesmanCenter", "personalCenter-spokesmanCenter-mine-spokesmanHomePage", "liveBroadcast-lived", "personalCenter-spokesmanCenter-mine-getBusinessCard", "changeAdr"];
程默 committed
7
// path
程默 committed
8 9 10 11
export const noLoginListPath = noLoginList.map(item => {
    item = item.replace(/-/g, '/')
    item = '/' + item
    item == "/index" && (item = "/")
程默 committed
12 13
    return item
})
程默 committed
14
// 有底部栏页面
程默 committed
15
export const containFooterPage = ["/", "/goods/commodityMenu", "/shopCart/shoppingCart", "/personalCenter"]
程默 committed
16
// 主题色
程默 committed
17
export let themeColor = {
程默 committed
18 19 20 21
    "--main-color": "#ffffff",
    "--minor-color": "#ffffff"
}

程默 committed
22 23
// 获取link 中页面地址(link兼容 有object或者string)
export function getUrlofLink(data) {
24 25 26 27 28 29 30

    if(typeof data == 'object' && data.link){
        return data.link.split('?')[0]
    }
    if(typeof data == 'string'){
        return data.split('?')[0]
    }
程默 committed
31 32 33
}
export function getUrlofQuery(data) {
    return data.link.split('?')[1] || ''
程默 committed
34
}
程默 committed
35 36
// 全局跳转  data:link数据    option:是否为底部栏触发
export function $themeToLink(data, option) {
程默 committed
37 38 39 40 41
   
    // console.log(currentPage,'currentPage',getUrlofLink(data))
    // if(getUrlofLink(data).includes(currentPage)) return

    // 
李嘉林 committed
42
    let app = getApp();
程默 committed
43
    let type = data.type;
程默 committed
44 45
    // 登录拦截和底部导航栏拦截(非功能项)
    if (type != 2) {
程默 committed
46
        // 登录拦截
程默 committed
47
        let parseLink = getUrlofLink(data);
程默 committed
48 49 50 51
        let parseQuery = getUrlofQuery(data);

        let noLogin = noLoginListPath.includes(parseLink) || parseLink.substr(0, 7) == "/goods/" || parseLink.includes('/brandTopics');
        if (!noLogin && !wx.getStorageSync("sessionid")) {
程默 committed
52
            let url = `/pages/login/main?back=${parseLink}`;
程默 committed
53
            wx.navigateTo({ url });
程默 committed
54 55
            return
        }
程默 committed
56
        //判断是否市跳转到tabbar页面 替换首页位置 
程默 committed
57
        let isTabbarIndex = checkTabbarPage(parseLink);
程默 committed
58
        if (isTabbarIndex >= 0) {
程默 committed
59
            // 无底部栏h5页面
程默 committed
60 61 62 63 64 65 66 67 68 69 70 71 72
            // if (!containFooterPage.includes(parseLink)) {
            //     toPage(getUrlofLink(data), getUrlofQuery(data))
            //     return
            // }


            isTabbarIndex = isTabbarIndex == 0 ? checkTabbarPage('/') : isTabbarIndex;
            let url=parseLink == '/' ? `/pages/home/main` : `/pages/tabBar${isTabbarIndex}/main`;
            let routes = getCurrentPages();
            let currentPage = routes[routes.length - 1].route;
            if (currentPage==url) return

            let query=parseQuery?`?${parseQuery}`:'';
程默 committed
73
            wx.reLaunch({
程默 committed
74
                url:url+query
程默 committed
75 76 77
            })
            return
        }
程默 committed
78
    }
程默 committed
79 80


程默 committed
81
    // 历史数据
程默 committed
82 83
    if (type == undefined || type == 'undefined') {
        if (data.length > 0) {
程默 committed
84
            toPage(getUrlofLink(data), getUrlofQuery(data))
程默 committed
85 86 87 88 89 90 91 92 93
        }
        return
    }
    // -----------------
    if (type == 0) {//无链接
        return
    } else if (type == 1) {//h5页面
        //兼容
        if (data.link.substring(0, 1) == "/") {
程默 committed
94
            toPage(getUrlofLink(data), getUrlofQuery(data))
程默 committed
95 96
        } else {
            if (data.link.startsWith('http') || data.link.startsWith('https')) {
程默 committed
97
                wx.navigateTo({ url: '/pages/wxArticle/main?link=' + encodeURIComponent(data.link) });
程默 committed
98 99 100
            }
        }
    } else if (type == 1.1) {//小程序页面  可能增加登录态拦截
程默 committed
101
        wx.navigateTo({ url: data.link });
程默 committed
102 103 104 105
    } else if (type == 1.2) { //第三方链接
        if (data.link == 'outerChain:businessCenter') {
            // 进入商家中心外链
            let url = `${config.GUIDE_URL}/login?mixid=${this.$nuxt.$route.query.mixid}&back=${encodeURIComponent(window.location.href)}`;
程默 committed
106
            wx.navigateTo({ url: '/pages/wxArticle/main?link=' + encodeURIComponent(url) });
程默 committed
107 108 109
            return
        }
        if (data.link.substring(0, 1) == "/") {
程默 committed
110
            toPage(getUrlofLink(data), getUrlofQuery(data))
程默 committed
111
        } else {
程默 committed
112
            wx.navigateTo({ url: '/pages/wxArticle/main?link=' + encodeURIComponent(data.link) });
程默 committed
113 114 115 116
        }
    } else if (type == 2) {//功能
        let featureType = data.feature.type;
        if (featureType == 'themeCouponClick' || featureType == 0) {//优惠券
程默 committed
117
            // this.$themeCouponClick(data.feature.value.couponCode, data.feature.value);
程默 committed
118
        } else if (featureType == 1) {//分享
程默 committed
119
            console.log('是分享了。。。。');
李嘉林 committed
120
            app.shareInit({ itemList: data.feature.value });
程默 committed
121 122 123 124
        } else if (featureType == "themeMemberCardClick" || featureType == 2) { //会员卡
            // this.$themeMemberCardClick(data.feature.value);
        }
    } else if (type == 3) { //小程序跳转到小程序
程默 committed
125 126 127
        wx.navigateTo({
            url: `/pages/toMini/main?weappPage=${encodeURIComponent(data.link)}&weappAppId=${data.name}`,
        })
程默 committed
128 129 130
    } else {
        //向后兼容
        if (data.length > 0) {
程默 committed
131
            toPage(getUrlofLink(data), getUrlofQuery(data))
程默 committed
132 133 134
        }
    }
}
李嘉林 committed
135 136 137
export function $themeAddToCard(item, callback) {
    let errorMsg = "";
    if (item.productNature == 5) {
程默 committed
138
        errorMsg = "电子卡券不可加入购物车";
李嘉林 committed
139 140
    }
    if (item.productNature == 3) {
程默 committed
141
        errorMsg = "会员卡不可加入购物车";
李嘉林 committed
142 143
    }
    if (item.productNature == 2) {
程默 committed
144
        errorMsg = "虚拟商品不可加入购物车";
李嘉林 committed
145 146 147
    }
    if (errorMsg) {
        wx.showToast({
程默 committed
148
            title: errorMsg,
李嘉林 committed
149 150 151 152
            icon: 'none',
        });
    }
    cart
程默 committed
153 154 155 156 157 158 159 160 161
        .addCart([
            { productGoodsId: item.minProductGoodsId, qty: item.saleQty }
        ])
        .then(res => {
            if (res.data.code == 200) {
                wx.showToast({ title: "加入购物车成功", icon: "none" });
            }
            callback();
        });
李嘉林 committed
162
}
程默 committed
163

李嘉林 committed
164
function toPage(backPath, backParams = "") {
李嘉林 committed
165
    console.log(backPath, backParams,'--------------------------159');
程默 committed
166
    wx.navigateTo({
程默 committed
167
        url: `../index/main?from=themeLink&backpath=${encodeURIComponent(backPath)}&params=${encodeURIComponent(backParams)}`
程默 committed
168 169 170
    });
}

程默 committed
171 172 173 174 175 176
// check 链接是否为底部导航栏页面 返回索引
export function checkTabbarPage(link) {
    let app = getApp()
    if (!app.globalData.footerVal) return -1
    let footerVal = app.globalData.footerVal.componentData.list;
    let isFooter = footerVal.findIndex(item => {
程默 committed
177
        return getUrlofLink(item.link) == link.split('?')[0]
程默 committed
178 179 180 181 182 183
    })
    return isFooter
}

// 当前底部栏选中项
export function setTabBarActive(data) {
程默 committed
184
    // console.log(data,'sssssssssss',this.$mp.page.getTabBar(),data)
程默 committed
185 186 187 188 189 190 191 192
    if (data < 0 || !this.$mp.page) return
    if (typeof this.$mp.page.getTabBar === 'function' &&
        this.$mp.page.getTabBar()) {
        this.$mp.page.getTabBar().setData({
            selected: data
        })
    }
}
程默 committed
193 194 195 196 197


// 遍历所有组件有显示条件的id  是否包含地区导航组件
export function checkShowConditionIds(list) {
    let allCondition = [];
程默 committed
198
    let isAreaNavigation = false;
程默 committed
199
    list.forEach(element => {
程默 committed
200 201 202 203
        // 有地区导航组件
        if (element.componentCode == 'area-navigation') {
            isAreaNavigation = true
        }
程默 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        pollCondition(element)
    });
    function pollCondition(object) {
        if (Object.prototype.toString.call(object) === '[object Object]') {
            if (object.condition != '' && object.condition != undefined) {
                allCondition.push(object.condition);
            }
            for (const key in object) {
                if (object.hasOwnProperty(key)) {
                    const element = object[key];
                    if (Object.prototype.toString.call(element) === '[object Object]') {
                        if (element.condition != '' && element.condition != undefined) {
                            allCondition.push(element.condition);
                        }
                        pollCondition(element)
                    } else if (Object.prototype.toString.call(element) === '[object Array]' && element.length > 0) {
                        element.forEach((item) => {
                            pollCondition(item)
                        })
                    }
                }
            }
        }
    }
    return {
程默 committed
229
        allCondition: [...new Set(allCondition)],
程默 committed
230 231
        isAreaNavigation
    }
李嘉林 committed
232
}
程默 committed
233 234 235 236 237 238 239 240 241
export function $themeArticleLike(item, callback) {
    let query = { articleId: item.id };
    classificationApi.addLike(query).then(res => {
        if (res.data.code == "200") {
            if (res.data.data == 'true') {
                callback("true");
            } else {
                callback("false");
            }
李嘉林 committed
242 243
        } else {
        }
程默 committed
244
    });
李嘉林 committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}
export function getCoupon(code, item) {
    console.log("--------------------239");
    let usingIntegrals = (item.consumeIntegral - 0).toFixed(0);
    let couponCode = code;
    console.log(item, "------------------------242");
    if (item.receiveCostType == 1) {
        wx.showModal({
            confirmColor: "#CA173B",
            content: `是否消耗${usingIntegrals}积分领取`
            })
            .then(() => {
            useCoupon(2, couponCode, usingIntegrals);
            })
            .catch(() => {
            // on cancel
        });
        return;
    } else {
        useCoupon(1, couponCode, usingIntegrals);
    }
};
function useCoupon(val, couponCode, usingIntegrals) {
  coupon.receive_coupon(couponCode).then(res => {
    console.log(res, "cou137");
    if (res.data.data == "true") {
      if (val == 1) {
        wx.showToast({ title: "领取成功", icon: "success" });
      } else {
        let tit = "积分-" + usingIntegrals;
        wx.showToast({ title: tit, icon: "success" });
      }
      // item.receive=false;
    } else {
      wx.showToast({title:res.data.msg ,icon :"none"});
      // item.receive=true;
    }
  });
};
李嘉林 committed
284
export function themeMemberCardClick(item) {
李嘉林 committed
285 286 287 288 289 290 291 292 293 294 295 296
    let query = {
        portalCardId: item.id
    };
    shop.getV3CardList(query).then(res => {
        console.log(res,'--------------------282')
        if (res.data.code == 200) {
            let cardData = "";
            res.data.data.forEach(innerItem => {
            if (innerItem.id == item.id) {
                cardData = innerItem;
            }
        });
297 298
        let cardId = cardData.id;
        let userInfo = wx.getStorageSync("userInfo") ? JSON.parse(wx.getStorageSync("userInfo")) : "";
李嘉林 committed
299 300 301 302 303
        if (cardData.receiveWay == 0) {
            // 无门槛
        shop.drawV3Card(cardId).then(res => {
          if (res.data.code == 200) {
            wx.showToast({
李嘉林 committed
304 305 306 307
                content: `开通【${cardData.title}】成功`,
                showCancel: false,
                confirmColor: '',
                success: res => {
李嘉林 committed
308 309 310 311
                    let backParams = `&cardId=${cardData.id}`;
                    if (userInfo.mobilephone) {
                      backParams += `&userTel=${userInfo.mobilephone}`;
                    }
李嘉林 committed
312 313 314
                    toPage("/personalCenter/membershipCard", backParams);
                }
            })
李嘉林 committed
315 316
          } else {
              if (res.data.msg == "不能重复领卡") {
李嘉林 committed
317
                  console.log("---------------------------to-----1")
李嘉林 committed
318 319 320 321
                  let backParams = `&cardId=${cardData.id}`;
                  if (userInfo.mobilephone) {
                    backParams += `&userTel=${userInfo.mobilephone}`;
                  }
李嘉林 committed
322 323 324 325
                  toPage("/personalCenter/membershipCard",backParams);
                } else {
                    wx.showToast({ title: res.data.msg, icon: "none" });
                }
李嘉林 committed
326 327
            }
        });
李嘉林 committed
328
    } else if (cardData.receiveWay == 1) {
李嘉林 committed
329 330
        // 自动领取
        console.log("自动领取");
李嘉林 committed
331
    } else if (cardData.receiveWay == 2) {
李嘉林 committed
332 333
        // 付费购买
        if (cardData.receiveStatus == 0) {
李嘉林 committed
334 335 336
            console.log("---------------------------to-----2")
            let backParams = `&cardId=${cardData.id}`;
            toPage("/pay/payCard", backParams);
李嘉林 committed
337
        } else {
李嘉林 committed
338
            console.log("---------------------------to-----3")
李嘉林 committed
339 340 341 342
            let backParams = `&cardId=${cardData.id}`;
            if (userInfo.mobilephone) {
              backParams += `&userTel=${userInfo.mobilephone}`;
            }
李嘉林 committed
343
            toPage("/personalCenter/membershipCard", backParams);
李嘉林 committed
344 345 346 347 348
        }
      } else {
        console.error("无效的领取方式");
      }
    } else {
349 350 351 352
        wx.showToast({title:res.data.msg,icon:"none"});
        if (res.data.code == 300) {
            console.log('----------334')
            //   未登录
李嘉林 committed
353 354 355 356
            setTimeout(() => {
                let url = `/pages/login/main?back=${encodeURIComponent('/')}`;
                wx.navigateTo({ url: url });
            }, 200);
357
        }
李嘉林 committed
358 359 360
    }
  });
};