index.vue 7.19 KB
Newer Older
程默 committed
1
<template>
李嘉林 committed
2
  <tabbarPage :index="4" @getMessage="getMessage"></tabbarPage>
程默 committed
3 4 5
</template>

<script>
程默 committed
6
import tabbarPage from "../../components/tabbarPage.vue";
李嘉林 committed
7 8 9 10 11
import indexApi from "@/api/index.js";
import spokesman from "@/api/spokesman.js";
import { concatUrl, DFSImg } from "../../utils/index.js";
const app = getApp();
const { log } = app;
程默 committed
12
export default {
程默 committed
13 14
  components: {
    tabbarPage,
程默 committed
15
  },
李嘉林 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  data() {
    return {
      pageInfo: {},
      pageData: {},
      newHref: "",
      shareShopName: "",
      shareLogoUrl: "",
      shareOrderSn: "",
      shareTid: "",
      newWindowHref: "",
    };
  },
  onLoad(options) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"],
    });

    console.log(this.mpApp.globalData.pageList, "llll");

    this.init();

    if (options.userId || options.spokesmanRelId) {
      wx.setStorage({
        key: "becomeInfo",
        data: JSON.stringify(options),
      });
    }

    if (wx.getStorageSync("sessionid")) {
      // wx.showToast({
      //   title: JSON.stringify(wx.getStorageSync("sessionid")),
      //   icon: 'none',
      //   image: '',
      //   duration: 1500,

      // });
      //获取分销信息
      this.getSpokesmanInit(
        wx.getStorageSync("becomeInfo")
          ? JSON.parse(wx.getStorageSync("becomeInfo"))
          : ""
      );
    }
  },
  async onShareAppMessage(res) {
    log.info("onShareAppMessage", res);
    await this.getSpokesman();
    let url = "";
    log.info(this.newWindowHref + "this.newWindowHref");
    //分享路径
    if (this.newWindowHref) {
      url = this.newWindowHref;
    } else {
      url = res.webViewUrl;
    }
    //分享页面去掉登录态
    if (url.indexOf("sessionid") > -1) {
      // url = url.split("?")[0] + "?mixid=" + this.shopId;
      url = this.delParam(this.newWindowHref, "sessionid");
      url = this.delParam(url, "open_id");
    }
    //分销员推广页面 默认分享分销员中心页面
    if (url.indexOf("/personalCenter/spokesmanCenter/mine/popularize") > -1) {
      url = `${this.baseUrl}/personalCenter/spokesmanCenter?mixid=${this.shopId}`;
    }

    log.info("shareUrl---------------" + url);
    //增加分销员信息
    if (
      this.hasInvitationStatus == 1 &&
      url.indexOf("spokesmanGroupId") == -1
    ) {
      this.shareUrl = url + this.newHref;
    } else {
      this.shareUrl = url;
    }

    log.info("onShareAppMessage-shareUrl", this.shareUrl);
    log.info(
      `/pages/index/main?share=${encodeURIComponent(this.shareUrl)}`,
      "测试直播分享相关"
    );
    return {
      title: this.shareShopName, // 默认是小程序的名称
      path: `/pages/index/main?share=${encodeURIComponent(this.shareUrl)}`, // 默认是当前页面
      imageUrl: this.shareLogoUrl,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == "shareAppMessage:ok") {
          log.info(res, "分享成功");
        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == "shareAppMessage:fail cancel") {
          // 用户取消转发
          log.info(res, "分享失败");
        } else if (res.errMsg == "shareAppMessage:fail") {
          // 转发失败,其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调(转发成不成功都会执行)
      },
    };
  },
  onShareTimeline(res) {
    log.info(res, "-----onShareTimeline");
  },
  methods: {
    init() {
      this.setVisible(this.mpApp.globalData.pageList, (res) => {
        this.pageInfo = res;
        this.pageData = JSON.parse(this.pageInfo.pageData);
      });
    },
    async setVisible(pageList, cb) {
      // 首页显示条件
      let pageInfo = pageList.filter((item) => item.pageCode == 1)[0];
      cb(pageInfo);
    },
    getSpokesmanInit(extConfig) {
      //绑定上下级关系
      app.fenxiaoModel.becomeRelation(extConfig).then((res) => {
141
        if (res && res.data == 'true') {
李嘉林 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
          wx.removeStorage({
            key: "becomeInfo",
          });
        }
      });
    },

    //右上角分享时触发
    getMessage(res) {
      let len = res.target.data.length - 1;
      log.info("右上角分享触发", res, res.target.data[len]);
      console.log("右上角分享触发", res, res.target.data[len]);
      this.shareShopName = res.target.data[len].shareShopName || "";
      this.shareLogoUrl = res.target.data[len].shareLogoUrl
        ? DFSImg(res.target.data[len].shareLogoUrl, 500, 400)
        : "";

      //分享内容中有订单信息为送礼活动
      this.shareOrderSn = res.target.data[len].orderSn;
      this.shareTid = res.target.data[len].shareTid;
      this.newWindowHref = res.target.data[len].shareImgUrl + "&verifyShare=1"; // + '&fromVisitorOpenId=' + wx.getStorageSync("openid")
      log.info(
        "getMessage-分享信息:",
        this.shareLogoUrl,
        this.shareShopName,
        1111111,
        this.newWindowHref
      );
      console.log(
        "getMessage-分享信息:",
        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("=");
          log.info(pair, 2222);
          if (pair[0] == "articleId") {
            articleId = pair[1];
          }
        }
        indexApi.forward({ articleId: articleId }).then((res) => {});
      }
    },
    async getSpokesman() {
      await spokesman.getSpokesmanidByShare().then((res) => {
        log.info("onShareAppMessage-getSpokesmanidByShare", 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;

            this.newHref = `&spokesmanGroupId=${this.spokesmanGroupId}&spokesmanShopId=${this.spokesmanShopId}&spokesmanRelId=${this.spokesmanRelId}`;
          } else {
            this.newHref = "";
          }
          log.info("onShareAppMessage-getSpokesmanidByShare", this.newHref);
        }
      });
    },
    delParam(oldUrl,paramKey) {
      var url = oldUrl;    //页面url
      var urlParam = oldUrl.split("?")[1];   //页面参数
      var beforeUrl = url.substr(0, url.indexOf("?"));   //页面主地址(参数之前地址)
      var nextUrl = "";

      var arr = new Array();
      if (urlParam != "") {
        var urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
        for (var i = 0; i < urlParamArr.length; i++) {
          var paramArr = urlParamArr[i].split("="); //将参数键,值拆开
          //如果键雨要删除的不一致,则加入到参数中
          if (paramArr[0] != paramKey) {
            arr.push(urlParamArr[i]);
          }
        }
      }
      if (arr.length > 0) {
        nextUrl = "?" + arr.join("&");
      }
      url = beforeUrl + nextUrl;
      return url;
    },
  },
程默 committed
233 234
};
</script>