<template>
  <view class="container">
    <u-image src="@/static/logo.png" mode="widthFix"></u-image>
    <view class="login-btn-wrap">
      <u-button shape="circle" type="primary" :loading="loading" @tap="login"
        >微信用户一键登录</u-button
      >
      <u-button
        shape="circle"
        :customStyle="{ 'margin-top': '20rpx' }"
        @tap="$Router.push('/pages/login/phoneLogin')"
        >手机号登录/注册</u-button
      >
    </view>
    <view>
      我已阅读并同意
      <text>《用户协议》</text>
      <text>《隐私政策》</text>
    </view>
  </view>
</template>

<script>
import { getWxLoginCode } from '@/api/login.js';

export default {
  data() {
    return {
      loading: false,
      form: {
        client_id: 'chelvc_client',
        client_secret: 'qWBe6jD%GCuPPTkP',
        grant_type: 'wechat',
        token: '',
      },
    };
  },
  methods: {
    async login() {
      this.loading = true;
      this.form.token = await getWxLoginCode();
      this.$store
        .dispatch('LoginByWxCode', this.form)
        .then(() => {
          this.loading = false;
          uni.$u.toast('登录成功');
          this.$store
            .dispatch('GetUserInfo')
            .then(() => {
              this.$Router.pushTab('/pages/tabbar/home/index');
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.loading = false;
          uni.$u.toast('登录失败');
        });
    },
  },
};
</script>

<style scoped>
.container {
  padding: 0 20rpx;
  box-sizing: border-box;
}
.login-btn-wrap {
  margin-top: 40rpx;
}
</style>
