<script>
export default {
  onLaunch: function () {
    this.handleUrl(location.href);
    //判断客户端类别
    if (/MicroMessenger/.test(window.navigator.userAgent)) {
      uni.setStorage({
        key: 'userAgent',
        data: 'wxAgent'
      });
      console.log('微信客户端');
    } else if (/AlipayClient/.test(window.navigator.userAgent)) {
      uni.setStorage({
        key: 'userAgent',
        data: 'aliAgent'
      });
      console.log('支付宝客户端');
    } else {
      uni.setStorage({
        key: 'userAgent',
        data: 'otherAgent'
      });
      console.log('其他浏览器');
    }
    // 判断从什么入口进入 车位锁等页面无需调用
    const backUrl = location.href,
      openidPage = this.config.onlyWxLogin, // 只需要穿openid的页面集合
      needValidPage = openidPage.filter((item) => backUrl.indexOf(item) > -1); // 是否为需要验证的页面集合
    const { openId } = this.vuex_wxinfo;
    console.log('openId', openId);
    console.log('needValidPage', needValidPage);
    if (openId && needValidPage.length === 0) {
      this.getUserInfo(openId);
    }
    // 获取参数
    const paramsValidPage = openidPage.filter((item) => backUrl.indexOf(item) > -1);
    if (paramsValidPage.length === 0) {
      this.getFreeTime();
    }
  },
  onShow: function () {
    console.log('App Show');
  },
  onHide: function () {
    // console.log('App Hide')
  },
  methods: {
    /**
     * @description:
     * @param {*} openId
     * @return {*}
     */
    async getUserInfo(openId) {
      try {
        const queryParams = {
          loginType: 1,
          openid: openId,
          nickName: this?.vuex_wxinfo?.nickname,
          avatar: this?.vuex_wxinfo?.headImgUrl
        };
        const userInfo = await this.$u.api.userLoginApi.openidLoginApi({ ...queryParams });
        const { accessToken, needVerify } = userInfo.data;
        this.$u.vuex('vuex_token', accessToken);
        this.$u.vuex('vuex_hasLogin', true);
        if (needVerify) {
          localStorage.setItem('backUrl', this.backUrl);
          this.$u.route({
            url: 'pages/center/phoneLogin/phoneLogin',
            type: 'reLaunch'
          });
        }
      } catch (error) {
        this.$refs.uToast.show({
          title: '获取用户信息失败!',
          type: 'error'
        });
      }
    },
    /**
     * @description: 获取参数免费时长
     * @return {*}
     */
    async getFreeTime() {
      try {
        const { msg } = await this.$u.api.getParamsApi({ key: 'park.lock.freetime' });
        this.$u.vuex('free_time', msg);
      } catch (error) {
        console.log(error);
      }
    },
    /**
     * @description: 首次加载url加入时间戳，防止页面未进行刷新
     * @param {*} url
     * @return {*}
     */
    handleUrl(url) {
      // 判断时间戳是否存在
      if (url.indexOf('_t') < 0) {
        // 判断?是否存在  存在直接拼接后面   不存在需要拼接?
        location.href = url.indexOf('?') > -1 ? `${url}&_t=${Date.now()}` : `${url}?_t=${Date.now()}`;
      }
    }
  }
};
</script>

<style lang="scss">
@import '/static/quill/quill.bubble.scss';
@import '/static/quill/quill.snow.scss';
@import '/static/quill/quill.core.scss';
@import './static/css/iconfont.css';
@import 'uview-ui/index.scss';
/*每个页面公共css */
</style>
