取名小程序
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
const namingStore = require("../../store/namingStore");
|
||||
const namingStore = require("../../store/namingStore");
|
||||
const messages = require("../../constants/messages");
|
||||
const { validateSurname, generateName } = require("../../services/namingService");
|
||||
const { showRewardedVideoAd } = require("../../utils/adService");
|
||||
const config = require("../../config/index");
|
||||
|
||||
const LOADING_MESSAGES = [
|
||||
"AI 正在推演八字,请稍候...",
|
||||
"结合天干地支,为您筛选契合佳名...",
|
||||
"耐心等待,吉名正在汇聚灵感..."
|
||||
];
|
||||
|
||||
function showToast(title) {
|
||||
if (typeof tt === "undefined" || !tt.showToast) {
|
||||
console.warn("Toast:", title);
|
||||
@@ -25,7 +31,9 @@ Page({
|
||||
birthTime: "",
|
||||
nameLength: "double",
|
||||
isSubmitting: false,
|
||||
quotaRemaining: null
|
||||
quotaRemaining: null,
|
||||
isLoading: false,
|
||||
loadingMessage: ""
|
||||
},
|
||||
onLoad() {
|
||||
const { form, quota } = namingStore.getState();
|
||||
@@ -53,11 +61,13 @@ Page({
|
||||
}
|
||||
validateSurname(surname)
|
||||
.then((response) => {
|
||||
if (!response || response.isValid === false) {
|
||||
showToast(messages.INVALID_SURNAME);
|
||||
const isValid = response ? response.isValid !== false : true;
|
||||
const message = response && response.message ? response.message : messages.INVALID_SURNAME;
|
||||
if (!isValid) {
|
||||
showToast(message);
|
||||
this.setData({
|
||||
surname: "",
|
||||
surnameError: messages.INVALID_SURNAME
|
||||
surnameError: message
|
||||
});
|
||||
namingStore.setForm({ surname: "" });
|
||||
} else {
|
||||
@@ -132,6 +142,7 @@ Page({
|
||||
} else {
|
||||
showToast("广告加载失败,请稍后重试");
|
||||
}
|
||||
this.hideLoadingOverlay();
|
||||
this.setData({ isSubmitting: false });
|
||||
});
|
||||
},
|
||||
@@ -143,7 +154,8 @@ Page({
|
||||
birthTime: this.data.birthTime || "",
|
||||
nameLength: this.data.nameLength
|
||||
};
|
||||
generateName(payload)
|
||||
this.showLoadingOverlay();
|
||||
return generateName(payload)
|
||||
.then((response) => {
|
||||
const results = response && Array.isArray(response.results) ? response.results : [];
|
||||
const analysis = response && response.analysis ? response.analysis : null;
|
||||
@@ -177,11 +189,35 @@ Page({
|
||||
tt.navigateTo({ url: "/pages/result/index" });
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
showToast(messages.GENERATION_FAILED);
|
||||
.catch((error) => {
|
||||
const message = error && error.data && error.data.message;
|
||||
if (message === "CONTENT_RISK") {
|
||||
showToast(messages.CONTENT_RISK);
|
||||
} else if (message === "INVALID_SURNAME") {
|
||||
showToast(messages.INVALID_SURNAME);
|
||||
} else {
|
||||
showToast(messages.GENERATION_FAILED);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
this.hideLoadingOverlay();
|
||||
this.setData({ isSubmitting: false });
|
||||
});
|
||||
},
|
||||
showLoadingOverlay() {
|
||||
const hint = LOADING_MESSAGES[Math.floor(Math.random() * LOADING_MESSAGES.length)];
|
||||
this.setData({
|
||||
isLoading: true,
|
||||
loadingMessage: hint
|
||||
});
|
||||
},
|
||||
hideLoadingOverlay() {
|
||||
if (!this.data.isLoading) {
|
||||
return;
|
||||
}
|
||||
this.setData({
|
||||
isLoading: false,
|
||||
loadingMessage: ""
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user