"use strict";
const common_vendor = require("../../common/vendor.js");
const store_index = require("../../store/index.js");
const api_index = require("../../api/index.js");
const utils_index = require("../../utils/index.js");
require("../../api/request.js");
require("../../config/index.js");
if (!Array) {
  const _easycom_uni_icons2 = common_vendor.resolveComponent("uni-icons");
  _easycom_uni_icons2();
}
const _easycom_uni_icons = () => "../../uni_modules/uni-icons/components/uni-icons/uni-icons.js";
if (!Math) {
  _easycom_uni_icons();
}
const _sfc_main = {
  __name: "login",
  setup(__props) {
    store_index.useStore();
    common_vendor.onLoad(() => {
    });
    const avatarUrl = common_vendor.ref(null);
    const nickname = common_vendor.ref("");
    const res_form = common_vendor.ref(null);
    common_vendor.watch(nickname, (v1, v2) => {
      console.log("watch", v1);
    });
    function onChooseAvatar(e) {
      avatarUrl.value = e.detail.avatarUrl;
    }
    function getPhoneNumber(e) {
      getAccess_token({ code: e.detail.code });
    }
    async function getAccess_token(params = {}) {
      const data = {
        username: params.code,
        //手机号按钮获取的code
        type: "wechat_mini_app",
        identity: "user",
        password: "111",
        avatarUrl: avatarUrl.value,
        nickName: nickname.value
      };
      const res = await api_index.API.getToken(data);
      if (res.code === 200) {
        common_vendor.index.setStorageSync("avatarUrl", avatarUrl.value);
        res_form.value = res.data;
        res_form.value.access_token = "Bearer " + res.data.access_token;
        let userId = res.data.userId;
        common_vendor.index.setStorageSync("access_token", res_form.value.access_token);
        wx_login(userId);
      }
    }
    async function wx_login(userId) {
      let res = await new Promise((resolve) => {
        common_vendor.index.login({
          provider: "weixin",
          //使用微信登录
          success: function(loginRes) {
            resolve(loginRes);
            console.log(loginRes, "微信登录返回信息");
          }
        });
      });
      let res2 = await api_index.API.wx_login({ code: res.code }, userId);
      if (res2.code === 0) {
        common_vendor.index.setStorageSync("userInfo", JSON.stringify(res_form.value));
        utils_index.custom.uploadImage(avatarUrl.value, async (file) => {
          avatarUrl.value = file.data.file.url;
          common_vendor.index.setStorageSync("avatarUrl", file.data.file.url);
          await updateFunc();
          common_vendor.index.switchTab({
            url: "/pages/user/index"
          });
        });
      } else {
        common_vendor.index.clearStorage();
      }
    }
    async function updateFunc() {
      let user_info = JSON.parse(common_vendor.index.getStorageSync("userInfo"));
      user_info.avatar = avatarUrl.value;
      user_info.id = user_info.userId;
      return await api_index.API.userinfoUpdae(user_info);
    }
    function nameInput(e) {
      nickname.value = e.detail.value;
    }
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: avatarUrl.value
      }, avatarUrl.value ? {
        b: avatarUrl.value
      } : {
        c: common_vendor.p({
          type: "person-filled",
          color: "white",
          size: "60"
        })
      }, {
        d: common_vendor.o(onChooseAvatar),
        e: common_vendor.o(nameInput),
        f: nickname.value,
        g: common_vendor.o(($event) => nickname.value = $event.detail.value),
        h: common_vendor.o(getPhoneNumber)
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e4e4508d"], ["__file", "F:/项目2023/视力表/vision-record/pages/login/login.vue"]]);
wx.createPage(MiniProgramPage);
