login.vue 2.5 KB
<template>
  <div class="containerbox"></div>
</template>

<script>
import Vue from "vue";
import { getUrlKey } from "../../utils/utils.js";
const murl = "http://tangyuan.h.brofirst.cn";
const now_url = sessionStorage.getItem("now_url");
console.log('页面路径', now_url)

export default {
  //生命周期函数
  created() {
    const code = this.GetUrlParame("code"); // 截取code
    console.log('9999', code)

    if (!code) {
      let urlk = window.location.href
      console.log(urlk, 34893488493)
      let that = this;
      var url = "/api/user/authorize";
      let param = {
        redirect_uri: urlk
      };

      that.$axios
        .post(url, param)
        .then(function (res) {
          console.log(res);
          window.location.href = res.data.wechat_url
        })
        .catch(function (error) {
          console.log(error);
        });
    } else {
      let param = {
        platform: 'wechat',
        code: code
      }
      console.log(param);
      this.$axios
        .get("/api/user/third?code=" + code) //如果有code,说明用户点击了授权  将获取到的code传递给后台
        .then(res => {
          // 返回状态和UId
          console.log(res, '返回的数据')
          if (res.data) {
            sessionStorage.setItem("token", res.data.token);
          }
          console.log(now_url, 99999)

          if (now_url) {

            window.location.replace(murl + now_url);
          } else {
            window.location.replace(murl);

          }
        });
    }
  },
  data() {
    return {};
  },
  methods: {
    // 截取code
    GetUrlParame(parameName) {
      /// 获取地址栏指定参数的值
      /// <param name="parameName">参数名</param>
      // 获取url中跟在问号后面的部分
      var parames = window.location.search;
      // 检测参数是否存在
      if (parames.indexOf(parameName) > -1) {
        var parameValue = "";
        parameValue = parames.substring(
          parames.indexOf(parameName),
          parames.length
        );
        // 检测后面是否还有参数
        if (parameValue.indexOf("&") > -1) {
          // 去除后面多余的参数, 得到最终 parameName=parameValue 形式的值
          parameValue = parameValue.substring(0, parameValue.indexOf("&"));
          // 去掉参数名, 得到最终纯值字符串
          parameValue = parameValue.replace(parameName + "=", "");
          return parameValue;
        }
        return "";
      }
    }
  }
};
</script>

<style></style>