<template>
  <v-header scroll headerBackgroundColor="transparent" immersion backgroundColor="transparent" title=" ">
    <image slot="background" mode="aspectFill" src="/static/images/login-background.png" class="absolute login-background screen_all"></image>
    <view class="login-container flex jCenter">
      <view class="login-title">欢迎使用沣胜商城</view>
      <view class="login-info">点击按钮一键登录</view>
      <button @click="login" class="login-button center clear-button">立即登录</button>
      <view class="row center login-agreement">
        <view>登录即视为同意</view>
        <view class="login-agreement-label" @click="routerPrivacy">《沣盛用户协议》</view>
      </view>
    </view>
  </v-header>
</template>

<script>
import vHeader from '@/components/v-header/main.vue';
import params from '@/mixins/params';
import globalMixins from '@/mixins/global';
export default {
  name: "login",
  components:{
    vHeader
  },
  mixins:[globalMixins,params],
  methods:{
    routerPrivacy(){
      return this.$router.navigateTo({
        name:'agreement',
        params:{
          type:'privacy'
        }
      })
    },

    getUserProfilePromise(){
      return new Promise(function (resolve,reject) {
        return  uni.getUserProfile({
          lang: 'zh_CN',
          desc: '用于登录服务',
          success:resolve,
          fail:reject
        });
      });
    },

    loginPromise(){

      return new Promise( (resolve, reject)=> {
        return  wx.login({
          success:resolve,
          fail:reject
        })
      });

    },

    getUserInfo(){
      return Promise.all([this.loginPromise(),this.getUserProfilePromise()]);
    },

    login(){

      if (this.look.status) return;
      // 上锁
      this.look.close();

      this.getUserInfo().then(([res,data])=>{

        let userInfo = {};
        try {
          userInfo = JSON.parse(data.rawData);
        } catch (e) {

        }
        this.$request({
          url:'user/wxMiniProgramLogin',
          data:{
            code: res.code,
            nickname: userInfo.nickName,
            avatar: userInfo.avatarUrl
          },
          loading:'登录中',
          next:({status})=>{
            this.look[status?'close':'open']();
          },
          failMessage:true,
          message:2
        }).then((data)=>{
          if (data.isSuccess) {
            this.$store.commit('setUserInfo',data.data);
            this.$store.dispatch('updateUserInfoPromise',false);
            return this.loginSuccessCallBack();
          }
        });

      }).catch(()=>{
        this.look.open();
      });

    },
    loginSuccessCallBack(){

      if (this.$params.router) {

        return this.$router.autoRouter(this.$params.router).then(()=>{

        }).catch(()=>{
          this.$router.switchTab('index');
        });

      } else {
        return this.$router.navigateBack().then(()=>{

        }).catch(()=>{
          this.$router.switchTab('index');
        });
      }

    },
  },



  created(){
    this.look ={
      status:false,
      close:function (){
        if (!this.status) {
          this.status = true;
        }
      },
      open:function (){
        if (this.status) {
          this.status = false;
        }
      }
    }
  }
}
</script>

<style scoped lang="scss" src="./style.scss">

</style>
