"use strict";
const common_vendor = require("../../common/vendor.js");
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const $api = common_vendor.inject("$api");
    const $app = common_vendor.inject("$app");
    const $config = common_vendor.inject("$config");
    const config = common_vendor.ref({ logo: [] });
    common_vendor.onLoad(async () => {
      await searchConfig();
      await search();
    });
    const searchConfig = async () => {
      let res = await $api(`config`, "GET", {});
      if (res.errcode == 0) {
        config.value = res.data[0] || {};
        common_vendor.index.setStorage({
          key: "config",
          data: res.data[0] || {}
        });
      }
    };
    const search = async () => {
      common_vendor.index.getStorage({
        key: "openid",
        success: async function(res) {
          var _a;
          const user = await $api("token/openid", "POST", {
            openid: res.data
          });
          if (user.errcode == "0" && ((_a = user.data) == null ? void 0 : _a._id)) {
            common_vendor.index.setStorage({
              key: "user",
              data: user.data
            });
          } else {
            common_vendor.index.removeStorage({
              key: "user",
              success: function(res2) {
                console.log("success");
              }
            });
          }
          common_vendor.index.reLaunch({
            url: `/pages/home/index`
          });
        },
        fail: function(err) {
          common_vendor.index.login({
            success: async function(res) {
              var _a;
              if (res.code) {
                common_vendor.index.reLaunch({
                  url: `/pages/home/index`
                });
                const aee = await $app("/wechat/api/login/app", "GET", {
                  js_code: res.code,
                  config: $config.wx_projectkey
                });
                if (aee.errcode == "0") {
                  common_vendor.index.setStorage({
                    key: "openid",
                    data: aee.data.openid
                  });
                  const user = await $api("token/openid", "POST", {
                    openid: aee.data.openid
                  });
                  if (user.errcode == "0" && ((_a = user.data) == null ? void 0 : _a._id)) {
                    common_vendor.index.setStorage({
                      key: "user",
                      data: user.data
                    });
                  } else {
                    common_vendor.index.removeStorage({
                      key: "user",
                      success: function(res2) {
                        console.log("success");
                      }
                    });
                  }
                  common_vendor.index.reLaunch({
                    url: `/pages/home/index`
                  });
                } else {
                  common_vendor.index.showToast({
                    title: aee.errmsg,
                    icon: "none"
                  });
                }
              } else {
                common_vendor.index.showToast({
                  title: res.errMsg,
                  icon: "none"
                });
              }
            }
          });
        }
      });
    };
    return (_ctx, _cache) => {
      var _a, _b;
      return {
        a: ((_b = (_a = config.value) == null ? void 0 : _a.logo[0]) == null ? void 0 : _b.url) || "/static/logo.jpg"
      };
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1cf27b2a"], ["__file", "D:/project/学吧/learn_applet/pages/index/index.vue"]]);
wx.createPage(MiniProgramPage);
