<template>
  <view
      class="login-container"
      :style="{
         background: `url(${getImageUrl(ICON_CFG.login_bg)}) no-repeat `,
         backgroundSize: '100%',
       }"
  >
    <button class="login-btn" open-type="getUserInfo" @getuserinfo="getUserInfo">
      <u-icon name="weixin-fill" color="#fefefe" size="28"></u-icon>
      一键微信登录
    </button>
  </view>
</template>

<script>

import { ICON_CFG } from '@/common/EnumConst';
import { getImageUrl, setStorageObj, syncWxlogin } from '@/util';

export default {
  name: 'login',
  props: {},
  data() {
    return {
      ICON_CFG,
    };
  },
  created() {
  },
  onLoad() {
  },
  methods: {
    getImageUrl,
    // 获取用户信息
    async getUserInfo(e) {
      if (e.mp.detail.userInfo) {
        this.userInfo = e.mp.detail.userInfo;
        this.hasUserInfo = true;
        const {
          success,
          data,
        } = await syncWxlogin();
        if (success) {
          // 将用户信息和session存储到本地
          setStorageObj('userInfo', data.user);
          setStorageObj('token', data.token);
          this.realoadPage();
        }
      } else {
        uni.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
          showCancel: false,
          confirmText: '返回授权',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击了“返回授权”');
            }
          },
        });
      }
    },
    realoadPage() {
      let pages = getCurrentPages();  //获取当前页面
      let beforePage = pages[pages.length - 2];  //获取上一个页面的实例
      //1、调用上一个页面onLoad
      beforePage.onLoad();
      uni.navigateBack({
        delta: 1,
      });
    },
  },
};
</script>
<style lang="scss" src="./index.scss" />;
