<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";
// const  genTestUserSig  = require('../../components/debug/GenerateTestUserSig.js');
export default {
  data() {
    return {
      userInfo: {},
      info: {},
    };
  },
  onLoad() {
    uni.getUserInfo({
      provider: "weixin",
      success: (res) => {
        this.userInfo = res.userInfo;
      },
    });
  },
  methods: {
    // 获取手机号
    async getPhoneNumber(e) {
      uni.showLoading({
        title: "登录中...",
        mask: true,
      });
      try {
        const res = await request({
          url: "/user/loginWithPhoneNumber",
          method: "POST",
          data: {
            code: e.detail.code,
          },
          userInfo: false,
        });
        console.log("登录结果:", res);
        this.info = res;
        if (this.info.code === 200) {
          if (this.info.data.newAccount) {
            const sdkAppID = Number(this.info.data.chatInfo.appId);
            const userID ="administrator";
            const {userSig} = genTestUserSig({
              SDKAPPID : sdkAppID,
              SECRETKEY:this.info.data.chatInfo.appKey,
              userID: userID,
            })
            uni.setStorageSync("userSig", userSig)
            uni.setStorageSync("userinfo", this.info.data.info);
            uni.reLaunch({
              url: "/pages/UserInformation/UserInformation",
            });
          } else {
            uni.setStorageSync("userinfo", this.info.data.info);
            uni.hideLoading();
            // `
            uni.navigateBack({
              delta: 1, // 返回层级（默认值为 1）
            });
            // `································································登录成功后跳转回原页面 或 首页
            // const fromPage = decodeURIComponent(options.from || "");
            // if (fromPage) {
            //   uni.redirectTo({
            //     url: fromPage,
            //   });
            // } else {
            //   uni.switchTab({ url: "/pages/index/index" }); // 默认首页
            // }
            // `·································································
          }
        } else {
          uni.showToast({
            title: "登录失败",
            icon: "none",
          });
        }
      } catch (err) {
        console.error("登录错误:", err);
        uni.showToast({
          title: "请检查网络连接",
          icon: "none",
        });
      } finally {
        uni.hideLoading();
      }
    },
  },
};
</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>
