<template>
  <view class="container">
    <image class="logo" :src="userInfo.avatarUrl"></image>
    <view class="nickname"> {{ userInfo.nickName }}</view>
    <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
      使用微信登录
    </button>
  </view>
</template>

<script>
import request from "../../components/request.js";
import  genTestUserSig  from "../../components/debug/GenerateTestUserSig.js";
import TUIlogin from "../../components/TUILogin.js";
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
// const  genTestUserSig  = require('../../components/debug/GenerateTestUserSig.js');
export default {
  inject: ['$global'],
  data() {
    return {
      userInfo: {},
      info: {},
      userSig: "",
      lastPage: "",
      myuserSig: "",
    };
  },
  onLoad() {
    uni.getUserInfo({
      provider: "weixin",
      success: (res) => {
        this.userInfo = res.userInfo;
      },
    });
    uni.getStorage({
      key: "lastPage",
      success: (res) => {
        this.lastPage = "/"+res.data;
        console.log(this.lastPage);
      },
      fail: () => {
        this.lastPage = "/pages/Home/Home";
      },
    })
  },
  
  methods: {
    // 获取手机号
    async getPhoneNumber(e) {
      uni.showLoading({
        title: "登录中...",
        mask: true,
      });
        const res = await request({
          url: "user/loginWithPhoneNumber",
          method: "POST",
          data: {
            code: e.detail.code,
          },
          userInfo: false,
        });
        this.info = res;
        console.log("登录信息", this.info);
        const sdkAppID = Number(this.info.data.chatInfo.appId);
            const userID ="administrator";
            this.userSig = genTestUserSig({
              SDKAPPID : sdkAppID,
              SECRETKEY:this.info.data.chatInfo.appKey,
              userID: userID,
            })
            this.myuserSig = genTestUserSig({
              SDKAPPID : sdkAppID,
              SECRETKEY:this.info.data.chatInfo.appKey,
              userID: String(res.data.info.id),
            })
            console.log("userSig",this.myuserSig.userSig);
            uni.setStorageSync("myuserSig", this.myuserSig)
            uni.setStorageSync("chatInfo", this.info.data.chatInfo)
            uni.setStorageSync("userSig", this.userSig)
            uni.setStorageSync("userinfo", this.info.data.info);
            counter.$patch({ myitem:this.info.data.info })
        if (this.info.code === 200) {
          if (this.info.data.newAccount) {
            uni.reLaunch({
              url: "/pages/UserInformation/UserInformation",
            });
            TUIlogin(this.info.data.chatInfo.appId, this.info.data.info.id,this.myuserSig.userSig)
            uni.hideLoading();
          } else {
            uni.setStorageSync("userinfo", this.info.data.info);
            TUIlogin(this.info.data.chatInfo.appId, this.info.data.info.id,this.myuserSig.userSig)
            uni.hideLoading();
            //跳转原来页面否则首页
            console.log("跳的地址", this.lastPage);
            uni.reLaunch({
            url: this.lastPage, 
          });
          }
        } else {
          uni.showToast({
            title: "登录失败",
            icon: "none",
          });
        }
    },
  },
};
</script>

<style>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to bottom, #11cb2a6e, #2574fc6d);
}

.logo {
  width: 200rpx;
  height: 200rpx;
  border-radius: 50%;
}
.nickname {
  width: 300rpx;
  height: 100rpx;
  color: rgb(255, 255, 255);
  border-radius: 0px;
  padding: 20rpx 40rpx;
  font-size: 30rpx;
  text-align: center;
  line-height: 100rpx;
}
.login-btn {
  background-color: hsla(0, 0%, 100%, 0);
  border: 1px solid #00ff0000;
  color: #ffffff;
}

.tips {
  color: #666;
  font-size: 24rpx;
  margin-top: 40rpx;
}
</style>
