index.vue 9.99 KB
Newer Older
程智春 committed
1
<!--
程默 committed
2
 第三方url
程智春 committed
3 4 5 6
-->
<template>
  <div>
    <web-view
7
      :src="useLink"
程智春 committed
8 9 10 11 12 13 14 15
      @message="getMessage"
      @error="handleError"
      @load="handleLoad"
    ></web-view>
  </div>
</template>

<script>
16 17
let forUrlAddKey = require("mayi-front-tools/forUrlAddKey").default;
let getUrlKey = require("mayi-front-tools/getUrlKey").default;
18
let getUrlAllKey = require("mayi-front-tools/getUrlAllKey").default;
19
let removeUrlKey = require("mayi-front-tools/removeUrlKey").default;
张卓 committed
20
import { serialize, getQueryVariable, DFSImg } from "@/utils/index";
21 22
import login from "@/api/login";
import indexApi from "@/api/index";
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
function forUrlAddKey2(url, params) {
  let urlHost = url.slice(0, url.indexOf('?'));
  let urlParams = null;
  let url2 = url;
  let url3 = '';
  if(url2.includes('#/')){
      url2 = url.split('#/')[0];
      url3 = url.split('#/')[1];
  }
  urlParams = getUrlAllKey(url2)
  if (params && params.length) {
      params.forEach(res => {
          delete urlParams[res]
      })
  }
  let datas = [];
  for (let key in urlParams) {
      datas.push(key + '=' + urlParams[key])
  }
  if (datas.length) {
      return urlHost + '?' + datas.join('&') + (url3?`#/${url3}`:'')
  } else {
      return urlHost 
  }
}
李嘉林 committed
48 49
const app = getApp();
const { log } = app;
程智春 committed
50
export default {
程默 committed
51 52 53
  data() {
    return {
      link: "",
54
      useLink: "",
程默 committed
55 56 57
      shareShopName: "", //分享名称
      shareLogoUrl: "", //分享图片url
      newWindowHref: "", //分享页面url
李嘉林 committed
58
      shopId: this.$store.state.mixid,
程默 committed
59 60 61
    };
  },
  async onLoad(options) {
62
    console.log(options,'--------------------options')
李嘉林 committed
63
    log.info("--------------------options",options)
程默 committed
64
    this.link='';
程默 committed
65 66 67 68 69 70 71 72
    /**
     * scene=sso 第三方需要登录的页面
     * login=1   jwt需要登录(临时方案)
     */
    if (options.scene) {
      //扫码
      let data = await this.getParams(options.scene);
      if (data.sso) {
张卓 committed
73 74 75 76
        let link = data.link;
        if(getUrlKey(decodeURIComponent(link), "getMixId") == '1') {//第三方跳转加mixid
          link = forUrlAddKey(decodeURIComponent(link),{mixid: this.$store.state.mixid})
        }
程默 committed
77 78
        if (!wx.getStorageSync("sessionid")) {
          wx.redirectTo({
李嘉林 committed
79
            url: `../login/main?scene=sso&isEncode=1&back=${link}`,
程默 committed
80 81
          });
        } else {
李嘉林 committed
82
          this.getSsoBcakUrl(decodeURIComponent(link).replace(/\/\?(.+?)#\//, '/?token=${token}&scene=sso&toAppid=qOAhsauSaagQlxYdGbJL#/'));
程智春 committed
83
        }
程默 committed
84 85
      }
    } else if (options.link) {
张卓 committed
86 87 88 89
      let link = options.link;
      if(getUrlKey(decodeURIComponent(link), "getMixId") == '1') {//第三方跳转加mixid
        link = forUrlAddKey(decodeURIComponent(link),{mixid: this.$store.state.mixid})
      }
李嘉林 committed
90 91 92 93
      if(getUrlKey(decodeURIComponent(link), "scene") == 'dynamicForm') {
        link = decodeURIComponent(link)
      }
      console.log(link,'--------------------------57')
张卓 committed
94
      if (getUrlKey(decodeURIComponent(link), "scene") == "sso") {
程默 committed
95
        if (!wx.getStorageSync("sessionid")) {
李嘉林 committed
96 97 98 99 100 101 102 103
          // appraisalTwo:道可二级详情页面,之前进入道可项目所有页面都需登录,现在进入二级可不登录
          if (!link.includes("appraisalTwo")) {
            wx.redirectTo({
              url: `../login/main?scene=sso&isEncode=1&back=${link}`,
            });
          } else {
            this.setLink(decodeURIComponent(link));
          }
程默 committed
104
        } else {
李嘉林 committed
105
          this.getSsoBcakUrl(decodeURIComponent(link).replace(/\/\?(.+?)#\//, '/?token=${token}&scene=sso&toAppid=qOAhsauSaagQlxYdGbJL#/'));
106
        }
张卓 committed
107
      } else if (getUrlKey(decodeURIComponent(link), "login") != "1") {
张卓 committed
108 109 110
        //判断是否需要带token
        if(decodeURIComponent(link).indexOf('setToken={token}') > -1) {
          if (wx.getStorageSync("sessionid")) {
张卓 committed
111
            this.getSsoBcakUrl(decodeURIComponent(link).replace(/\/\?(.+?)#\//, '/?token=${token}&scene=sso&toAppid=qOAhsauSaagQlxYdGbJL#/'))
张卓 committed
112
          } else {
李嘉林 committed
113
            let url = `/pages/login/main?scene=sso&isEncode=1&back=${link}`;
张卓 committed
114
            wx.navigateTo({
张卓 committed
115
              url
张卓 committed
116 117 118 119 120
            });
          }
        }else {
          this.setLink(decodeURIComponent(link));
        }
程默 committed
121 122
      } else {
        if (wx.getStorageSync("sessionid")) {
程默 committed
123
          this.setLink(
张卓 committed
124
            forUrlAddKey(decodeURIComponent(link), {
程默 committed
125 126 127 128
              token: wx.getStorageSync("sessionid"),
              jwt: 1,
            })
          );
程默 committed
129
        } else {
李嘉林 committed
130 131 132 133 134 135
          let url = "";
          if(link.includes('formDesignDetail')){
            url = `/pages/login/main?scene=sso&back=${encodeURIComponent(link)}`
          } else {
            url = `/pages/login/main?scene=sso&back=${link}`
          }
程默 committed
136 137 138
          wx.navigateTo({
            url,
          });
张卓 committed
139
        }
程默 committed
140 141 142
      }
    }
  },
143 144 145 146 147 148
  onShow() {
    console.log(this.link,'------------154')
    setTimeout(() => {
      console.log(this.link,'------------154---2')
    }, 1000);
  },
程默 committed
149
  async onShareAppMessage(res) {
150
    console.log(res,'----------------------onShareAppMessage---------res')
程默 committed
151 152 153 154 155 156 157 158
    let url = "";
    console.log(this.newWindowHref + "this.newWindowHref");
    //分享路径
    if (this.newWindowHref) {
      url = this.newWindowHref;
    } else {
      url = res.webViewUrl;
    }
李嘉林 committed
159 160
    // forUrlAddKey2(url, ["token"])
    console.log(url,'++++++++++++++++++',decodeURIComponent(url).replace(/\/\?(.+?)#\//, '/?token=${token}&scene=sso&toAppid=qOAhsauSaagQlxYdGbJL#/'),777777777)
张卓 committed
161
    if(url.indexOf('setToken={token}') == -1) {
李嘉林 committed
162
      url = decodeURIComponent(url).replace(/\/\?(.+?)#\//, '/?token=${token}&scene=sso&toAppid=qOAhsauSaagQlxYdGbJL#/');
张卓 committed
163
    }
李嘉林 committed
164
    console.log(url,'-------------164')
程默 committed
165 166
    //分享页面去掉登录态
    if (url.indexOf("sessionid") > -1) {
李嘉林 committed
167 168 169 170 171 172 173
      if (url.includes('taokor')) {
        if (url.includes('&sessionid')) {
          url = url.split('&sessionid')[0];
        }
      } else {
        url = url.split("?")[0] + "?mixid=" + this.shopId;
      }
张卓 committed
174 175
    }else if(url.indexOf('getMixId') > -1) {//分享链接需要带mixid
      url  = forUrlAddKey(url,{mixid: this.shopId})
程默 committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    }
    this.shareUrl = url;
    console.log(this.shareUrl, "index12");
    console.log(
      `/pages/index/main?link=${encodeURIComponent(this.shareUrl)}`,
      "测试直播分享相关"
    );
    return {
      title: this.shareShopName, // 默认是小程序的名称
      path: `/pages/wxArticle/main?link=${encodeURIComponent(this.shareUrl)}`, // 默认是当前页面
      imageUrl: this.shareLogoUrl,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == "shareAppMessage:ok") {
          console.log(res, "分享成功");
张卓 committed
191
        }
程默 committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == "shareAppMessage:fail cancel") {
          // 用户取消转发
          console.log(res, "分享失败");
        } else if (res.errMsg == "shareAppMessage:fail") {
          // 转发失败,其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调(转发成不成功都会执行)
      },
    };
  },
李嘉林 committed
207 208
  watch:{
    link() {
209 210 211 212
      console.log(this.link,this.link.includes('$%7Btoken%7D'),wx.getStorageSync("sessionid"),'-------watch----link')
      if (this.link.includes('$%7Btoken%7D') && wx.getStorageSync("sessionid")) {
        this.getSsoBcakUrl(decodeURIComponent(this.link));
      }
李嘉林 committed
213
      if ((!this.link.includes('sessionid')) && wx.getStorageSync("sessionid")) {
李嘉林 committed
214
        this.link +=`${this.link.includes('?') && !this.link.includes('#/') || (this.link.split('?').length - 1 > 1) ?'&':'?'}sessionid=${wx.getStorageSync("sessionid")}`
李嘉林 committed
215
      }
216
      this.useLink = this.link;
李嘉林 committed
217
      console.log(this.useLink,'--this.useLink')
218
    },
李嘉林 committed
219
  },
程默 committed
220
  methods: {
程默 committed
221 222
    setLink(data) {
      this.link = data;
223
      console.log(this.useLink,89999999)
程默 committed
224
    },
程默 committed
225
    getSsoBcakUrl(link) {
226
      console.log(link,'--------------------------------------------163-----link')
程默 committed
227 228 229 230 231 232 233
      let op = {
        callbackUrl: link,
        email: "",
        fromAppid: "VsEOuDJqBnGNyXZJWCQS",
        // mobilephone: '15821335747',hgFTLLhgnBmeOyCkcbyv
        // toAppid: 'hgFTLLhgnBmeOyCkcbyv',
        toAppid: getUrlKey(link, "toAppid"), //道可
234
        wxAvatarUrl: this.$store.state.userInfo?this.$store.state.userInfo.avatarUrl:'',
程默 committed
235 236 237
      };
      login.getSsoBcakUrl(op).then((res) => {
        if (res.data.code == "200") {
程默 committed
238
          console.log('14888888',res)
程默 committed
239
          this.setLink(res.data.data);
240
          
程默 committed
241 242
        } else {
          wx.showToast({ title: res.data.msg, icon: "none" });
李嘉林 committed
243 244 245 246 247 248 249 250 251
          if(res.data.msg == "登录人信息错误") {
            // 跳转登录页
            let backUrl = link.split("?")[0];
            let ossLink = link.split("?")[1] +'?'+ link.split("?")[2];
            let url = `/pages/login/main?scene=sso&isEncode=1&back=${backUrl}&ossLink=${encodeURIComponent(ossLink)}`;
            wx.navigateTo({
              url
            });
          }
张卓 committed
252
        }
程默 committed
253
      });
张卓 committed
254
    },
程默 committed
255 256 257 258 259 260 261 262
    getParams(options) {
      return new Promise((resolve, reject) => {
        indexApi
          .getShareSceneRecord(options)
          .then((res) => {
            if (res.data.code == "200") {
              let scene = JSON.parse(res.data.data.scene);
              resolve(scene);
263
            }
程默 committed
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
          })
          .catch((err) => {});
      });
    },
    //加载成功
    handleLoad() {
      console.log("网页加载成功");
    },
    //右上角分享时触发
    getMessage(res) {
      let len = res.target.data.length - 1;
      console.log(res, "h5消息", res.target.data[len]);
      this.shareShopName = res.target.data[len].shareShopName || "";
      if (res.target.data[len].shareLogoUrl) {
        this.shareLogoUrl = DFSImg(res.target.data[len].shareLogoUrl, 500, 400);
      }
      this.newWindowHref = res.target.data[len].shareImgUrl; // + '&fromVisitorOpenId=' + wx.getStorageSync("openid")
      console.log(
        "分享信息:",
        this.shareLogoUrl,
        this.shareShopName,
        1111111,
        res
      );
    },
    handleError(data) {
      wx.showLoading({
        title: "网页加载失败 请右上角刷新",
      });
    },
  },
程智春 committed
295 296
};
</script>