<template></template>

<script lang="ts" setup>
import { ref, vueVersion } from "../../TUIKit/adapter-vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { loginChat } from "../../loginChat";
const inputValue: any = ref("");
const userSigs = ref();
onLoad(() => {
  const paramsToFetch = [
    "type",
    "token",
    "id",
    "userSig",
    "loginType",
    "companyUserId",
    "postId",
    "recruitUserId",
    "userId",
    "link",
    "Identity",
    "shopUserId",
    "houseId",
    "refId",
    "demandId",
    "userLoginType",
    "userType",
    "agentType",
  ];
  // 批量获取参数并存储到storage
  const params: any = {};
  paramsToFetch.forEach((key) => {
    const value = getQueryString(key);
    if (value !== undefined && value !== "undefined") {
      uni.setStorageSync(key, value);
      params[key] = value;
    } else {
      uni.removeStorageSync(key);
    }
  });
  console.log("params", params);
  // 单独处理userSig
  userSigs.value = params.userSig || "";
  inputValue.value = params;
  handleLoginInfo();
});

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

const handleLoginInfo = () => {
  const loginInfo = {
    SDKAppID: uni.$chat_SDKAppID,
    userID: inputValue.value.id,
    userSig: inputValue.value.userSig,
    useUploadPlugin: true,
    framework: `vue${vueVersion}`,
  };
  login(loginInfo);
};

const login = (loginInfo: any) => {
  loginChat(loginInfo).catch(() => {
    uni.showToast({
      title: TUITranslateService.t("Login.登录失败"),
      icon: "none",
    });
  });
};
</script>

<style lang="scss" scoped>
@import "../../styles/login";

.icon {
  display: inline;
}

.btn {
  background: none;
  border: none;
}

.icon-unselected {
  display: inline-block;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
}

.selected-icon {
  width: 14px;
  height: 14px;
}

.icon-default {
  margin: 7px 6px 0 0;
}

.login-input-uniapp {
  border: 1px solid #ccc;
  border-radius: 4px;
  outline: none;
  height: 40px;
  padding: 0 0 0 14px;
}

.logo-back-png {
  width: 4.61rem;
  height: 3.23rem;
}

.private-content-link {
  display: inline-block;
}
</style>
