取名小程序开发
This commit is contained in:
61
NamingAssistant/utils/adService.js
Normal file
61
NamingAssistant/utils/adService.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const config = require("../config/index");
|
||||
|
||||
function showRewardedVideoAd() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!config.adUnitId) {
|
||||
console.warn("adUnitId 未配置,跳过广告流程(仅限开发环境)");
|
||||
resolve("ad_skipped");
|
||||
return;
|
||||
}
|
||||
if (typeof tt === "undefined" || !tt.createRewardedVideoAd) {
|
||||
reject(new Error("rewarded_ad_unavailable"));
|
||||
return;
|
||||
}
|
||||
const ad = tt.createRewardedVideoAd({
|
||||
adUnitId: config.adUnitId
|
||||
});
|
||||
|
||||
const cleanup = () => {
|
||||
if (!ad) {
|
||||
return;
|
||||
}
|
||||
if (ad.offLoad) {
|
||||
ad.offLoad();
|
||||
}
|
||||
if (ad.offError) {
|
||||
ad.offError();
|
||||
}
|
||||
if (ad.offClose) {
|
||||
ad.offClose();
|
||||
}
|
||||
};
|
||||
|
||||
ad.onError((error) => {
|
||||
cleanup();
|
||||
reject(error);
|
||||
});
|
||||
|
||||
ad.onClose((result = {}) => {
|
||||
cleanup();
|
||||
if (result.isEnded) {
|
||||
resolve();
|
||||
} else {
|
||||
const err = new Error("ad_not_completed");
|
||||
err.code = "ad_not_completed";
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
ad
|
||||
.load()
|
||||
.then(() => ad.show())
|
||||
.catch((error) => {
|
||||
cleanup();
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
showRewardedVideoAd
|
||||
};
|
||||
21
NamingAssistant/utils/date.js
Normal file
21
NamingAssistant/utils/date.js
Normal file
@@ -0,0 +1,21 @@
|
||||
function pad(value) {
|
||||
return value < 10 ? `0${value}` : `${value}`;
|
||||
}
|
||||
|
||||
function formatDate(date) {
|
||||
const year = date.getFullYear();
|
||||
const month = pad(date.getMonth() + 1);
|
||||
const day = pad(date.getDate());
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
function formatTime(date) {
|
||||
const hours = pad(date.getHours());
|
||||
const minutes = pad(date.getMinutes());
|
||||
return `${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatDate,
|
||||
formatTime
|
||||
};
|
||||
Reference in New Issue
Block a user