index.vue 19.4 KB
Newer Older
程默 committed
1
<template>
程默 committed
2
  <div>
程默 committed
3 4 5 6
    <!-- <div>{{withoutScene?"初始化完成":"初始化中"}}</div> -->
    <div style="width:100%;height:90vh;display:flex;justify-content: center;align-items: center;">
      <van-loading size="40px" type="spinner"></van-loading>
    </div>
7
    <web-view v-if="withoutScene" :src="pageUrl" @message="getMessage" @error="handleError" @load="handleLoad"></web-view>
程默 committed
8 9 10 11
  </div>
</template>

<script>
程默 committed
12
import spokesman from "@/api/spokesman.js";
chenhui committed
13
import shop from "@/api/shop.js";
14
import indexApi from "@/api/index.js";
程默 committed
15
import { serialize, getQueryVariable, DFSImg } from "@/utils/index";
张卓 committed
16
import login from "@/api/login";
程默 committed
17
export default {
程默 committed
18
  data() {
程默 committed
19
    return {
张卓 committed
20
      code: '',
程智春 committed
21
      shopId: this.$store.state.mixid,
程默 committed
22
      options: {},
程默 committed
23
      session_key: "",
程默 committed
24
      baseUrl: process.env.BASE_URL,
程默 committed
25
      link: this.baseUrl,
程默 committed
26
      page: "/",
程默 committed
27
      params: "?mixid=" + this.shopId+'&native=1',
程默 committed
28

程默 committed
29
      userInfo: {
程默 committed
30 31
        nickName: "mpvue",
        avatarUrl: "http://mpvue.com/assets/logo.png"
程默 committed
32
      },
程默 committed
33
      shareUrl: "",
程默 committed
34

程默 committed
35 36
      //分享
      shopConfig: null,
程默 committed
37 38 39
      spokesmanGroupId: "", // 代言人的集团id
      spokesmanShopId: "", // 代言人的门店id
      spokesmanRelId: "", //代言人的id
程智春 committed
40
      userId : '',
程默 committed
41
      hasInvitationStatus: 0, // 邀请资格:0-不能够邀请下级,1-能够邀请下级
程默 committed
42
      newHref: "",
程默 committed
43 44
      shareShopName: "",
      shareLogoUrl: "",
程默 committed
45
      shareOrderSn: "",
程默 committed
46
      shareTid: "",
程智春 committed
47
      newWindowHref : '',
程默 committed
48
      withoutScene: false,
程默 committed
49 50 51 52
      sharePageList: [
        "goods/",
        "receivingGift",
        "article/",
程默 committed
53
        "/personalCenter/spokesmanCenter"
程默 committed
54
      ] //分享白名单
程默 committed
55 56
    };
  },
57 58
  computed: {
    pageUrl() {
张卓 committed
59
      return this.link+this.page+this.params;
60 61
    }
  },
62
  watch: {
程默 committed
63
    pageUrl(n,o) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      //埋点未登录时进入文章
      // this.params+='&articleId=111'
      console.log(wx.getStorageSync("sessionid"),3333333,this.pageUrl)
      if(!wx.getStorageSync("sessionid")&&this.pageUrl.indexOf('articleId')!=-1) {
        // wx.setStorageSync('articleId',true)
        var vars = this.pageUrl.split("&");
        console.log(vars,2222)
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            console.log(pair,1111)
            if (pair[0] == 'articleId') {
              wx.setStorageSync(this.$store.state.mixid+'articleId',pair[1])
            }
        }
      }
    }
  },
程默 committed
81
  onLoad(options) {
程默 committed
82
    
83
    this.options = options;
程默 committed
84
    Object.assign(this.$data, this.$options.data());
程默 committed
85
    this.withoutScene = true;
张卓 committed
86 87 88 89 90
    //获取openId
     wx.login({
      success: res => {
        this.code = res.code;
        login.miniLogin({ code: this.code}).then(res1 => {
91
            console.log(res1,555555)
张卓 committed
92 93 94 95 96 97
            if (res1.data.code == 200) {
                wx.setStorage({
                    key: "openid",
                    data: res1.data.data.openid
                });
            }
98 99
            this.params += '&open_id='+wx.getStorageSync("openid");
            console.log(this.code,77777,this.params)
张卓 committed
100 101 102 103
        })
      },
    }) 
    
程默 committed
104
    //检测登录态
程默 committed
105
    this.checkLogin();
程默 committed
106
    this.options = options;
程默 committed
107 108
    if (options.share) {
      //来自分享
程默 committed
109
      this.link = decodeURIComponent(options.share);
程默 committed
110 111 112 113 114
      this.spokesmanGroupId =
        getQueryVariable(this.link, "spokesmanGroupId") || "";
      this.spokesmanShopId =
        getQueryVariable(this.link, "spokesmanShopId") || "";
      this.spokesmanRelId = getQueryVariable(this.link, "spokesmanRelId") || "";
程智春 committed
115
      this.userId = getQueryVariable(this.link, "userId") || "";
程默 committed
116
      if (this.spokesmanGroupId != "") {
程默 committed
117
        //上级分销员信息
程默 committed
118 119 120
        this.$store.commit("setSpokesman", {
          spokesmanGroupId: this.spokesmanGroupId,
          spokesmanShopId: this.spokesmanShopId,
程默 committed
121
          spokesmanRelId: this.spokesmanRelId
程默 committed
122 123
        });
      }
程默 committed
124 125
      this.page = "";
      this.params = "";
程默 committed
126
    } else if (options.from && options.from == "h5login") {
程默 committed
127 128
      //来自h5登录
      let params = JSON.parse(options.params);
程默 committed
129 130
      wx.setStorage({
        key: "sessionid",
程默 committed
131
        data: params.sessionid
程默 committed
132
      });
程默 committed
133
    } else if (options.from && (options.from == "home" || options.from == "livedToIndex")) {
李嘉林 committed
134 135
      //来自直播详情
      this.page = decodeURIComponent(options.backpath);
李嘉林 committed
136
      if(this.page.includes("?")){
程默 committed
137
        this.params="&mixid=" + this.shopId+'&native=1';
李嘉林 committed
138
      }
程默 committed
139 140
      options.params && (this.params += "&" + serialize(decodeURIComponent(options.params)));
        console.log(this.page,this.params)
李嘉林 committed
141 142 143
    } else if (options.from && options.from == "livedBackShop") {
      //直播页back到商城
      this.page = decodeURIComponent(options.backpath);
程智春 committed
144
      this.params += "&" + serialize(options.params);
程智春 committed
145 146 147 148
    } else if(options.from && options.from == "liveList"){
      //从主播端跳到商品
      this.page = decodeURIComponent(options.backpath);
      this.params += "&" + serialize(options.params);
李嘉林 committed
149 150 151 152 153 154 155 156
    } else if (options.from && options.from == "livedToGoodsInfo") {
      //从直播详情商品列表到商城商品详情
      this.page = decodeURIComponent(options.backpath);
      this.params += "&" + serialize(options.params);
    } else if (options.from && options.from == "livedToOrderConfirm") {
      //从直播详情商品列表到商城确认订单
      this.page = decodeURIComponent(options.backpath);
      this.params += "&" + serialize(options.params);
程默 committed
157
    } else if (options.from && options.from == "login") {
程默 committed
158
      //来自小程序登录页面
程默 committed
159
      this.page = decodeURIComponent(options.backpath);
程默 committed
160
      this.params += "&" + decodeURIComponent(options.params);
程默 committed
161
    } else if (options.from && options.from == "logout") {
程默 committed
162
      //来自用户登出
程默 committed
163 164 165 166 167 168
      wx.removeStorage({
        key: "sessionid"
      });
      wx.removeStorage({
        key: "openid"
      });
程默 committed
169
    } else if (options.from && options.from == "address") {
程默 committed
170
      // 来自小程序地址页面
程默 committed
171
      this.link = decodeURIComponent(options.backpath);
程默 committed
172
      console.log("adress", options.backpath, this.link);
程默 committed
173 174
      this.page = "";
      this.params = "";
程默 committed
175
    } else if (options.from == "wxPay") {
程默 committed
176 177
      //来自支付页面
      this.page = options.backpath;
程默 committed
178
      this.params += "&" + serialize(options.params);
程默 committed
179
    } else if (options.scene) {
张卓 committed
180 181
      console.log(options)
      console.log(222222)
182
      if(options.scene.indexOf('_') == -1){
183
        this.withoutScene = false;
184
        //新二维码扫码进入
张卓 committed
185
        wx.nextTick(() => {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
          indexApi.getShareSceneRecord(options.scene).then(res=>{
            this.withoutScene = true;
            if(res.data.code == '200'){
              this.page = res.data.data.pageUri;
              let paramsObj = JSON.parse(res.data.data.scene);
              wx.setStorageSync('attractingCustomerChannelId',paramsObj.attractingCustomerChannelId)
              console.log(res.data.data)
              console.log(wx.getStorageSync("openid"))
              console.log(333333)
              if(wx.getStorageSync("openid")) {
                indexApi.addRecord(
                  {
                    attractingCustomersChannelId: paramsObj.attractingCustomerChannelId,
                    miniOpenId: wx.getStorageSync("openid"),
                    registerFlag: 1,
                    newRegisterFlag: 0,
                  }
                ).then(ele=>{
                  //清空引客渠道id
                  wx.setStorageSync('attractingCustomerChannelId','')
                })
              }
              let params = '';
              for(let i in paramsObj){
                params += "&"+i+'='+paramsObj[i]
              }
              this.params += params
213
            }
214 215
          }).catch((err)=>{
            this.withoutScene = true;
张卓 committed
216 217 218
          })    
        });
          
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      }else {
        //来自扫码
        // options 中的scene需要使用decodeURIComponent才能获取到生成二维码时传入的scene
        var scene = decodeURIComponent(options.scene); //参数二维码传递过来的参数
        let querys = scene.split("_");
        if (querys) {
          if (querys[0] == "pid") {
            //商品详情 pid_123
            this.page = `/goods/${querys[1]}`;
            this.params += this.paramsHander(querys,3)+`&discountMargin=${querys[querys.length-1]}`
            // if(querys.length > 3){
            //   if(querys[5]){
            //     this.params += `&spokesmanGroupId=${querys[2]}&spokesmanShopId=${querys[3]}&spokesmanRelId=${querys[4]}&userId=${querys[5]}`
            //   }else{
            //     this.params += `&spokesmanGroupId=${querys[2]}&spokesmanShopId=${querys[3]}&spokesmanRelId=${querys[4]}`
            //   }
              
            // }else if(querys.length == 3){
            //   this.params += `&userId=${querys[2]}`
            // }
程智春 committed
239

程智春 committed
240
            
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
          } else if (querys[0] == "activty") {
            //活动页面activty_turntable_123
            this.params += `&pageInfo=${scene}`;
          } else if (querys[0] == "spokesman") {
            //分销员 小程序码
            this.page = `/personalCenter/spokesmanCenter`;
            this.params += this.paramsHander(querys,2)
            // if(querys.length > 2){
            //   if(querys[4]){
            //     this.params += `&spokesmanGroupId=${querys[1]}&spokesmanShopId=${querys[2]}&spokesmanRelId=${querys[3]}&userId=${querys[4]}`
            //   }else{
            //     this.params += `&spokesmanGroupId=${querys[1]}&spokesmanShopId=${querys[2]}&spokesmanRelId=${querys[3]}`
            //   }
              
            // }else if(querys.length == 2){
            //   this.params += `&userId=${querys[1]}`
            // }
程智春 committed
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          }else if(querys[0] == "home"){
            this.page = `/`;
            this.params += this.paramsHander(querys,2)
            // if(querys.length > 2){
            //   if(querys[4]){
            //     this.params += `&spokesmanGroupId=${querys[1]}&spokesmanShopId=${querys[2]}&spokesmanRelId=${querys[3]}&userId=${querys[4]}`
            //   }else{
            //     this.params += `&spokesmanGroupId=${querys[1]}&spokesmanShopId=${querys[2]}&spokesmanRelId=${querys[3]}`
            //   }
              
            // }else if(querys.length == 2){
            //   this.params += `&userId=${querys[1]}`
            // }
          }else if(querys[0] == "coupon"){
            this.page = `/personalCenter/coupon/getCoupon`;
            this.params+=`&couponId=${querys[1]}`
          }else if(querys[0] == "a"){
            this.page = "/activty/receivingGift"
            this.params += `&orderId=${querys[1]}`
            this.params += this.paramsHander(querys,3)
            // if(querys.length > 3){
            //   if(querys[5]){
            //     this.params += `&orderSn=${querys[1]}&spokesmanGroupId=${querys[2]}&spokesmanShopId=${querys[3]}&spokesmanRelId=${querys[4]}&userId=${querys[5]}`
            //   }else{
            //     this.params += `&orderSn=${querys[1]}&spokesmanGroupId=${querys[2]}&spokesmanShopId=${querys[3]}&spokesmanRelId=${querys[4]}`
            //   }
              
            // }else if(querys.length == 3){
            //   this.params += `&orderSn=${querys[1]}&userId=${querys[2]}`
            // }
          }else if (querys[0] == 'redeemCode') {//兑换码
            this.page = `/personalCenter/CDkey/exchange`;
          }else if(querys[0] == 'card'){
            this.page = `/giftCards/linkReceive`;
            this.params+=`&id=${querys[1]}`
            this.params += this.paramsHander(querys,3)
          }else if(querys[0] == 'giftView'){
            this.page = `/giftCoupon/preview`;
            this.params+=`&id=${querys[1]}`
          }else if(querys[0] == 'form'){//问卷调查
            this.page = `/smartForm`;
            this.params+=`&formId=${querys[1]}`;
            this.params += this.paramsHander(querys,3)
          }
程默 committed
303
        }
304 305 306
        console.log(querys, "scene");
        console.log(this.page)
        console.log(this.params)
程默 committed
307
      }
308 309
      
      
程默 committed
310
      // var query = options.query.dentistId; // 参数二维码传递过来的场景参数
程默 committed
311
    }
程默 committed
312
    this.init();
程默 committed
313
  },
程默 committed
314 315 316 317 318 319 320 321 322 323 324 325 326
  onShow(){
    let ss=wx.getStorageSync("sessionid");
    if (ss && !this.params.includes("sessionid")) {
       this.params +=`&sessionid=${ss}`
    }
    console.log(this.getTabBar,'312')
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: this.index
      })
    }
  },
程默 committed
327
  methods: {
程默 committed
328
    init() {
chenhui committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
      // 获取小程序开启的订阅消息配置
      shop.getWxMiniSubscribeMessageConfig().then(res => {
        if (res.data.code == 200) {
          const data = res.data.data;
          console.log("wx mini subscribe message data: ", data);
          if (data != null) {
            const subscribeMessageObj = {};
            for (var key in data) {
              if(data[key] && data[key].template_id) {
                subscribeMessageObj[key] = data[key].template_id;
              }
            }

            this.$store.commit("setSubscribeMessageObj", subscribeMessageObj);
          }
        }
      });
程默 committed
346
    },
程默 committed
347 348 349
    checkLogin() {
      let getOpenid = wx.getStorageSync("openid");
      let getSessionid = wx.getStorageSync("sessionid");
程默 committed
350 351

      console.log('244',getOpenid,getSessionid);
程默 committed
352
      if (getOpenid == "" || getSessionid == "") {
程默 committed
353
        // this.params += "&logOut=true";
程默 committed
354
      }
程默 committed
355
    },
程默 committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    getLocationHandle() {
      wx.getLocation({
        type: "wgs84",
        success: res => {
          var latitude = res.latitude;
          var longitude = res.longitude;
          var speed = res.speed;
          var accuracy = res.accuracy;
          console.log("~~~~~~~~~1~~~~~~~", res);
        },
        fail: res => {
          console.log("++++++++++2++++++", res);
        }
      });
    },
程默 committed
371 372 373 374 375 376 377 378
    async getSpokesman() {
      await spokesman.getSpokesmanidByShare().then(res => {
        if (res.data.code == 200) {
          if (res.data.data.hasInvitationStatus == 1) {
            this.hasInvitationStatus = 1;
            this.spokesmanGroupId = res.data.data.groupId;
            this.spokesmanShopId = res.data.data.shopId;
            this.spokesmanRelId = res.data.data.spokesmanId;
程默 committed
379 380 381 382 383 384

            this.newHref = `&spokesmanGroupId=${
              this.spokesmanGroupId
            }&spokesmanShopId=${this.spokesmanShopId}&spokesmanRelId=${
              this.spokesmanRelId
            }`;
程默 committed
385 386
          }else{
            this.newHref="";
程默 committed
387
          }
程默 committed
388 389 390 391
          console.log(this.newHref, "this.newHrefthis.newHrefthis.newHref");
        }
      });
    },
程默 committed
392 393 394 395
    bindViewTap() {
      const url = "../logs/main";
      if (mpvuePlatform === "wx") {
        mpvue.switchTab({ url });
程默 committed
396
      } else {
程默 committed
397
        mpvue.navigateTo({ url });
程默 committed
398 399
      }
    },
程默 committed
400
    //右上角分享时触发
程默 committed
401
    getMessage(res) {
程默 committed
402 403 404
      let len = res.target.data.length - 1;
      console.log(res, "h5消息", res.target.data[len]);
      this.shareShopName = res.target.data[len].shareShopName || "";
程默 committed
405
      this.shareLogoUrl = DFSImg(res.target.data[len].shareLogoUrl, 500, 400);
程智春 committed
406
      
程默 committed
407
      //分享内容中有订单信息为送礼活动
程默 committed
408
      this.shareOrderSn = res.target.data[len].orderSn;
程默 committed
409
      this.shareTid = res.target.data[len].shareTid;
张卓 committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
      this.newWindowHref = res.target.data[len].shareImgUrl;// + '&fromVisitorOpenId=' + wx.getStorageSync("openid")
      console.log("分享信息:", this.shareLogoUrl, this.shareShopName,1111111,this.newWindowHref);
      //文章详情分享埋点
      if(this.newWindowHref.indexOf('articleId')!=-1) {
        var articleId = ''
        var vars = this.newWindowHref.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            console.log(pair,2222)
            if (pair[0] == 'articleId') {
              articleId = pair[1];
            }
        }
        indexApi.forward({articleId: articleId,}).then(res=>{
        
        })
      }
程默 committed
427
    },
428 429 430 431
    //加载成功
    handleLoad(){
      console.log('网页加载成功');
    },
程默 committed
432
    handleError(data){
433 434 435
      wx.showLoading({
        title: "网页加载失败 请右上角刷新"
      });
程默 committed
436 437 438 439 440 441
      this.$mpAnalytics.recordError({
      data,
      extendsInfo: {
        mixid: this.$store.state.mixid || null
      }
    });
442
    },
程默 committed
443 444 445 446 447 448 449
    checkPageUrl(val) {
      for (let i = 0; i < this.sharePageList.length; i++) {
        if (val.indexOf(this.sharePageList[i]) != -1) {
          return true;
        }
      }
      return false;
程智春 committed
450 451 452 453 454 455 456
    },
    //增加params参数
    paramsHander(querys,midNum){

      let params = ''
      if(querys.length > midNum){
        if(querys[midNum + 2]){
程智春 committed
457
          params += `&spokesmanGroupId=${querys[midNum - 1]}&spokesmanShopId=${querys[midNum]}&spokesmanRelId=${querys[midNum+1]}&userId=${querys[midNum+2]}`
程智春 committed
458 459 460 461 462 463 464 465 466
        }else{
          params += `&spokesmanGroupId=${querys[midNum - 1]}&spokesmanShopId=${querys[midNum]}&spokesmanRelId=${querys[midNum+1]}`
        }
        
      }else if(querys.length == midNum){
        params += `&userId=${querys[midNum - 1]}`
      }

      return params;
程默 committed
467 468
    }
  },
程默 committed
469
  onUnload() {
程默 committed
470
    console.log("卸载----------------", this.options);
程默 committed
471
  },
程默 committed
472 473
  async onShareAppMessage(res) {
    await this.getSpokesman();
程智春 committed
474
    let url = ''
程智春 committed
475
    console.log(this.newWindowHref + 'this.newWindowHref')
1  
程默 committed
476
    //分享路径
程智春 committed
477 478
    if(this.newWindowHref){
      url = this.newWindowHref;
程智春 committed
479 480 481
    }else{
      url = res.webViewUrl;
    }
程默 committed
482 483 484 485
    //分享页面去掉登录态
    if (url.indexOf("sessionid") > -1) {
      url = url.split("?")[0] + "?mixid=" + this.shopId;
    }
程默 committed
486 487 488 489 490 491
    //分销员推广页面 默认分享分销员中心页面
    if (url.indexOf("/personalCenter/spokesmanCenter/mine/popularize") > -1) {
      url = `${this.baseUrl}/personalCenter/spokesmanCenter?mixid=${
        this.shopId
      }`;
    }
程智春 committed
492

1  
程默 committed
493
    console.log('shareUrl---------------' + url)
程默 committed
494
    //增加分销员信息
程默 committed
495 496
    if(this.hasInvitationStatus == 1 && url.indexOf('spokesmanGroupId')==-1){
      this.shareUrl = url+ this.newHref
程智春 committed
497
    }else{
程默 committed
498
      this.shareUrl = url ;
程智春 committed
499 500
    }
    
501

程默 committed
502
    console.log(this.shareUrl, "index12");
程智春 committed
503
    console.log(`/pages/index/main?share=${encodeURIComponent(this.shareUrl)}`, '测试直播分享相关')
程默 committed
504
    return {
程默 committed
505
      title: this.shareShopName, // 默认是小程序的名称
程默 committed
506
      path: `/pages/index/main?share=${encodeURIComponent(this.shareUrl)}`, // 默认是当前页面
程默 committed
507
      imageUrl: this.shareLogoUrl,
程默 committed
508 509 510
      success: function(res) {
        // 转发成功之后的回调
        if (res.errMsg == "shareAppMessage:ok") {
程默 committed
511
          console.log(res, "分享成功");
程默 committed
512 513 514 515 516 517
        }
      },
      fail: function() {
        // 转发失败之后的回调
        if (res.errMsg == "shareAppMessage:fail cancel") {
          // 用户取消转发
程默 committed
518
          console.log(res, "分享失败");
程默 committed
519 520 521 522 523 524 525 526
        } else if (res.errMsg == "shareAppMessage:fail") {
          // 转发失败,其中 detail message 为详细失败信息
        }
      },
      complete: function() {
        // 转发结束之后的回调(转发成不成功都会执行)
      }
    };
程默 committed
527
  }
程默 committed
528
};
程默 committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
</script>

<style scoped>
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.form-control {
  display: block;
  padding: 0 12px;
  margin-bottom: 5px;
  border: 1px solid #ccc;
}
程默 committed
555 556 557 558
.all {
  width: 7.5rem;
  height: 1rem;
  background-color: blue;
程默 committed
559
}
程默 committed
560 561 562 563
.all:after {
  display: block;
  content: "";
  clear: both;
程默 committed
564
}
程默 committed
565 566 567 568 569
.left {
  float: left;
  width: 3rem;
  height: 1rem;
  background-color: red;
程默 committed
570 571
}

程默 committed
572 573 574 575 576
.right {
  float: left;
  width: 4.5rem;
  height: 1rem;
  background-color: green;
程默 committed
577 578
}
</style>