278 lines
9.4 KiB
JavaScript
278 lines
9.4 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const utils_config = require("../../utils/config.js");
|
|
const api_index = require("../../api/index.js");
|
|
class UserInfo extends UTS.UTSType {
|
|
static get$UTSMetadata$() {
|
|
return {
|
|
kind: 2,
|
|
get fields() {
|
|
return {
|
|
id: { type: String, optional: false },
|
|
nickName: { type: String, optional: false },
|
|
avatar: { type: String, optional: false },
|
|
phone: { type: String, optional: false }
|
|
};
|
|
},
|
|
name: "UserInfo"
|
|
};
|
|
}
|
|
constructor(options, metadata = UserInfo.get$UTSMetadata$(), isJSONParse = false) {
|
|
super();
|
|
this.__props__ = UTS.UTSType.initProps(options, metadata, isJSONParse);
|
|
this.id = this.__props__.id;
|
|
this.nickName = this.__props__.nickName;
|
|
this.avatar = this.__props__.avatar;
|
|
this.phone = this.__props__.phone;
|
|
delete this.__props__;
|
|
}
|
|
}
|
|
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
|
|
__name: "index",
|
|
setup(__props) {
|
|
const userInfo = common_vendor.ref(new UserInfo({
|
|
id: "",
|
|
nickName: "",
|
|
avatar: "",
|
|
phone: ""
|
|
}));
|
|
const isLoggedIn = common_vendor.ref(false);
|
|
const bookingCount = common_vendor.ref(0);
|
|
const favoriteCount = common_vendor.ref(0);
|
|
const cacheSize = common_vendor.ref("0KB");
|
|
const checkLoginStatus = () => {
|
|
var _a, _b, _c, _d;
|
|
const token = common_vendor.index.getStorageSync(utils_config.STORAGE_KEYS.TOKEN);
|
|
isLoggedIn.value = token != "";
|
|
if (isLoggedIn.value) {
|
|
const info = common_vendor.index.getStorageSync(utils_config.STORAGE_KEYS.USER_INFO);
|
|
if (info != null) {
|
|
userInfo.value = new UserInfo({
|
|
id: (_a = info["id"]) !== null && _a !== void 0 ? _a : "",
|
|
nickName: (_b = info["nickName"]) !== null && _b !== void 0 ? _b : "",
|
|
avatar: (_c = info["avatar"]) !== null && _c !== void 0 ? _c : "",
|
|
phone: (_d = info["phone"]) !== null && _d !== void 0 ? _d : ""
|
|
});
|
|
}
|
|
}
|
|
};
|
|
const getCacheSize = () => {
|
|
common_vendor.index.getStorageInfo(new UTSJSONObject({
|
|
success: (res) => {
|
|
const size = res.currentSize;
|
|
if (size < 1024) {
|
|
cacheSize.value = `${size}KB`;
|
|
} else {
|
|
cacheSize.value = `${(size / 1024).toFixed(2)}MB`;
|
|
}
|
|
}
|
|
}));
|
|
};
|
|
const fetchStats = () => {
|
|
bookingCount.value = 0;
|
|
const favorites = common_vendor.index.getStorageSync(utils_config.STORAGE_KEYS.FAVORITES);
|
|
favoriteCount.value = favorites ? favorites.length : 0;
|
|
};
|
|
const handleLogin = () => {
|
|
common_vendor.index.getUserProfile(new UTSJSONObject({
|
|
desc: "用于完善用户资料",
|
|
success: (profileRes = null) => {
|
|
common_vendor.index.login(new UTSJSONObject({
|
|
provider: "weixin",
|
|
success: (loginRes) => {
|
|
return common_vendor.__awaiter(this, void 0, void 0, function* () {
|
|
var _a;
|
|
const code = loginRes.code;
|
|
try {
|
|
const res = yield api_index.wechatLogin(code);
|
|
if (res.code === 0 && res.data != null) {
|
|
const data = res.data;
|
|
const token = data["access_token"];
|
|
const userDataObj = data["user"];
|
|
common_vendor.index.setStorageSync(utils_config.STORAGE_KEYS.TOKEN, token);
|
|
userInfo.value = new UserInfo({
|
|
id: String(userDataObj["id"]),
|
|
nickName: profileRes.userInfo.nickName,
|
|
avatar: profileRes.userInfo.avatarUrl,
|
|
phone: (_a = userDataObj["phone"]) !== null && _a !== void 0 ? _a : ""
|
|
});
|
|
common_vendor.index.setStorageSync(utils_config.STORAGE_KEYS.USER_INFO, new UTSJSONObject({
|
|
id: userDataObj["id"],
|
|
nickName: profileRes.userInfo.nickName,
|
|
avatar: profileRes.userInfo.avatarUrl,
|
|
phone: userDataObj["phone"]
|
|
}));
|
|
isLoggedIn.value = true;
|
|
common_vendor.index.showToast({
|
|
title: "登录成功",
|
|
icon: "success"
|
|
});
|
|
fetchStats();
|
|
} else {
|
|
throw new Error(res.message);
|
|
}
|
|
} catch (error) {
|
|
common_vendor.index.__f__("error", "at pages/user/index.uvue:248", "登录失败", error);
|
|
common_vendor.index.showToast({
|
|
title: "登录失败,请重试",
|
|
icon: "none"
|
|
});
|
|
}
|
|
});
|
|
},
|
|
fail: () => {
|
|
common_vendor.index.showToast({
|
|
title: "登录失败",
|
|
icon: "none"
|
|
});
|
|
}
|
|
}));
|
|
},
|
|
fail: () => {
|
|
common_vendor.index.showToast({
|
|
title: "授权失败",
|
|
icon: "none"
|
|
});
|
|
}
|
|
}));
|
|
};
|
|
const handleLogout = () => {
|
|
common_vendor.index.showModal(new UTSJSONObject({
|
|
title: "提示",
|
|
content: "确定要退出登录吗?",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
common_vendor.index.removeStorageSync(utils_config.STORAGE_KEYS.TOKEN);
|
|
common_vendor.index.removeStorageSync(utils_config.STORAGE_KEYS.USER_INFO);
|
|
isLoggedIn.value = false;
|
|
userInfo.value = new UserInfo({
|
|
id: "",
|
|
nickName: "",
|
|
avatar: "",
|
|
phone: ""
|
|
});
|
|
common_vendor.index.showToast({
|
|
title: "已退出登录",
|
|
icon: "success"
|
|
});
|
|
}
|
|
}
|
|
}));
|
|
};
|
|
const goToBookingList = () => {
|
|
if (!isLoggedIn.value) {
|
|
common_vendor.index.showToast({
|
|
title: "请先登录",
|
|
icon: "none"
|
|
});
|
|
return null;
|
|
}
|
|
common_vendor.index.navigateTo({
|
|
url: "/pages/user/booking-list"
|
|
});
|
|
};
|
|
const goToFavorites = () => {
|
|
if (!isLoggedIn.value) {
|
|
common_vendor.index.showToast({
|
|
title: "请先登录",
|
|
icon: "none"
|
|
});
|
|
return null;
|
|
}
|
|
common_vendor.index.navigateTo({
|
|
url: "/pages/user/favorites"
|
|
});
|
|
};
|
|
const goToAbout = () => {
|
|
common_vendor.index.navigateTo({
|
|
url: "/pages/about/index"
|
|
});
|
|
};
|
|
const callService = () => {
|
|
common_vendor.index.makePhoneCall({
|
|
phoneNumber: "400-888-8888",
|
|
fail: () => {
|
|
common_vendor.index.showToast({
|
|
title: "拨打电话失败",
|
|
icon: "none"
|
|
});
|
|
}
|
|
});
|
|
};
|
|
const openFeedback = () => {
|
|
common_vendor.index.showToast({
|
|
title: "功能开发中",
|
|
icon: "none"
|
|
});
|
|
};
|
|
const checkUpdate = () => {
|
|
common_vendor.index.showToast({
|
|
title: "已是最新版本",
|
|
icon: "success"
|
|
});
|
|
};
|
|
const clearCache = () => {
|
|
common_vendor.index.showModal(new UTSJSONObject({
|
|
title: "提示",
|
|
content: "确定要清除缓存吗?",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
common_vendor.index.clearStorage(new UTSJSONObject({
|
|
success: () => {
|
|
cacheSize.value = "0KB";
|
|
common_vendor.index.showToast({
|
|
title: "清除成功",
|
|
icon: "success"
|
|
});
|
|
}
|
|
}));
|
|
}
|
|
}
|
|
}));
|
|
};
|
|
common_vendor.onShow(() => {
|
|
checkLoginStatus();
|
|
getCacheSize();
|
|
});
|
|
return (_ctx, _cache) => {
|
|
"raw js";
|
|
const __returned__ = common_vendor.e({
|
|
a: common_vendor.unref(userInfo).avatar || "/static/images/default-avatar.png",
|
|
b: common_vendor.t(common_vendor.unref(userInfo).nickName || "点击登录"),
|
|
c: common_vendor.unref(userInfo).phone
|
|
}, common_vendor.unref(userInfo).phone ? {
|
|
d: common_vendor.t(common_vendor.unref(userInfo).phone)
|
|
} : {}, {
|
|
e: !common_vendor.unref(isLoggedIn)
|
|
}, !common_vendor.unref(isLoggedIn) ? {
|
|
f: common_vendor.o(handleLogin)
|
|
} : {}, {
|
|
g: common_vendor.unref(bookingCount) > 0
|
|
}, common_vendor.unref(bookingCount) > 0 ? {
|
|
h: common_vendor.t(common_vendor.unref(bookingCount))
|
|
} : {}, {
|
|
i: common_vendor.o(goToBookingList),
|
|
j: common_vendor.unref(favoriteCount) > 0
|
|
}, common_vendor.unref(favoriteCount) > 0 ? {
|
|
k: common_vendor.t(common_vendor.unref(favoriteCount))
|
|
} : {}, {
|
|
l: common_vendor.o(goToFavorites),
|
|
m: common_vendor.o(goToAbout),
|
|
n: common_vendor.o(callService),
|
|
o: common_vendor.o(openFeedback),
|
|
p: common_vendor.o(checkUpdate),
|
|
q: common_vendor.t(common_vendor.unref(cacheSize)),
|
|
r: common_vendor.o(clearCache),
|
|
s: common_vendor.unref(isLoggedIn)
|
|
}, common_vendor.unref(isLoggedIn) ? {
|
|
t: common_vendor.o(handleLogout)
|
|
} : {}, {
|
|
v: common_vendor.sei(common_vendor.gei(_ctx, ""), "view")
|
|
});
|
|
return __returned__;
|
|
};
|
|
}
|
|
});
|
|
wx.createPage(_sfc_main);
|
|
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/user/index.js.map
|