<script>
import { login } from '@/api/xincheng.api';
import { encryption } from '@/service/encrypt.xc';
import localStorage from '@/service/localStorage.service';
export default {
  data() {
    return {
      tips: '加载中...'
    };
  },
  async created() {
    localStorage.set('vccChannel', 161085);
    const { url, phone } = this.$route.query || '';
    if (!url || !phone) {
      this.tips = '参数错误请重新打开优品商城...';
      return;
    }
    try {
      const { token = '' } = await this.login(phone);
      const jumpUrl = this.getJumpUrl({
        url,
        vccToken: token,
        vccChannel: 161085,
        origin: encodeURIComponent(location.origin)
      });
      if (!token) {
        this.tips = '网络错误请重新打开优品商城...';
        return;
      }
      location.replace(jumpUrl);
    } catch (error) {
      this.tips = '网络错误请重新打开优品商城...';
    }
  },

  methods: {
    async login(phone) {
      const signData = encryption(`phoneNo=${phone}`);
      const [data] = await login({ signType: 'SHA1WithRSA', phoneNo: phone, ...signData });
      return data;
    },
    getJumpUrl({ url, vccToken, vccChannel, origin }) {
      return `${decodeURIComponent(
        url
      )}?vccToken=${vccToken}&vccChannel=${vccChannel}&fromHost=${origin}`;
    }
  },
  render() {
    return <div class="tips">{this.tips}</div>;
  }
};
</script>
<style lang="less" scoped>
.tips {
  font-size: 14px;
  text-align: center;
  margin-top: 240px;
  color: #666;
}
</style>
