140 lines
3.9 KiB
JavaScript
140 lines
3.9 KiB
JavaScript
(function (global) {
|
|
var DEFAULT_REQUIRED_DAILY_FREE_ARTICLES = 2;
|
|
|
|
function isTruthy(value) {
|
|
return (
|
|
value === true ||
|
|
value === 1 ||
|
|
value === '1' ||
|
|
String(value).toLowerCase() === 'true'
|
|
);
|
|
}
|
|
|
|
function firstDefined(source, keys) {
|
|
if (!source || typeof source !== 'object') return undefined;
|
|
|
|
for (var i = 0; i < keys.length; i += 1) {
|
|
var key = keys[i];
|
|
if (
|
|
Object.prototype.hasOwnProperty.call(source, key) &&
|
|
source[key] !== undefined &&
|
|
source[key] !== null
|
|
) {
|
|
return source[key];
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
function toNumber(value, fallback) {
|
|
var numberValue = Number(value);
|
|
return Number.isFinite(numberValue) ? numberValue : fallback;
|
|
}
|
|
|
|
function buildQuotaReason(todayCount, requiredCount) {
|
|
return (
|
|
'今天需先发布' +
|
|
requiredCount +
|
|
'篇审核通过的免费文章后,才能发布付费文章(当前' +
|
|
todayCount +
|
|
'篇)'
|
|
);
|
|
}
|
|
|
|
function parseSummary(userData, options) {
|
|
var data = userData && typeof userData === 'object' ? userData : {};
|
|
var config = options || {};
|
|
var status = String(
|
|
firstDefined(data, ['expertStatus', 'expert_status']) || ''
|
|
).toLowerCase();
|
|
var isIntern = /实习|intern|trial/.test(status);
|
|
var isRegularByStatus = /正式|regular|formal/.test(status);
|
|
var isRegularExpert =
|
|
isTruthy(
|
|
firstDefined(data, [
|
|
'is_regular_expert',
|
|
'isRegularExpert',
|
|
'regularExpert',
|
|
])
|
|
) || isRegularByStatus;
|
|
var hasExpertIdentity =
|
|
isTruthy(firstDefined(data, ['is_expert', 'isExpert', 'expert'])) ||
|
|
isTruthy(
|
|
firstDefined(data, [
|
|
'is_regular_expert',
|
|
'isRegularExpert',
|
|
'regularExpert',
|
|
])
|
|
) ||
|
|
isIntern ||
|
|
isRegularByStatus;
|
|
var requiredCount = toNumber(
|
|
firstDefined(data, [
|
|
'requiredDailyApprovedFreeArticleCount',
|
|
'required_daily_approved_free_article_count',
|
|
]),
|
|
config.requiredDailyFreeArticleCount || DEFAULT_REQUIRED_DAILY_FREE_ARTICLES
|
|
);
|
|
var todayCount = toNumber(
|
|
firstDefined(data, [
|
|
'todayApprovedFreeArticleCount',
|
|
'today_approved_free_article_count',
|
|
]),
|
|
0
|
|
);
|
|
var quotaExemptRaw = firstDefined(data, [
|
|
'payArticleFreeQuotaExempt',
|
|
'pay_article_free_quota_exempt',
|
|
]);
|
|
var canPublishRaw = firstDefined(data, [
|
|
'canPublishPayArticle',
|
|
'can_publish_pay_article',
|
|
]);
|
|
var backendReason = String(
|
|
firstDefined(data, [
|
|
'payArticlePublishReason',
|
|
'pay_article_publish_reason',
|
|
]) || ''
|
|
);
|
|
var quotaExempt = isTruthy(quotaExemptRaw);
|
|
var hasBackendPaidDecision = canPublishRaw !== undefined;
|
|
var canPaid = false;
|
|
var paidReason = '';
|
|
|
|
if (quotaExempt) {
|
|
canPaid = true;
|
|
} else if (hasBackendPaidDecision) {
|
|
canPaid = isTruthy(canPublishRaw);
|
|
paidReason = canPaid
|
|
? ''
|
|
: backendReason ||
|
|
(!hasExpertIdentity || !isRegularExpert
|
|
? '正式专家才能发布付费文章'
|
|
: buildQuotaReason(todayCount, requiredCount));
|
|
} else if (!hasExpertIdentity || !isRegularExpert) {
|
|
paidReason = '正式专家才能发布付费文章';
|
|
} else {
|
|
canPaid = todayCount >= requiredCount;
|
|
paidReason = canPaid ? '' : buildQuotaReason(todayCount, requiredCount);
|
|
}
|
|
|
|
return {
|
|
hasExpertIdentity: hasExpertIdentity,
|
|
isRegularExpert: hasExpertIdentity && isRegularExpert,
|
|
canFree: hasExpertIdentity,
|
|
canPaid: canPaid,
|
|
quotaExempt: quotaExempt,
|
|
todayFreeArticleCount: todayCount,
|
|
requiredDailyFreeArticleCount: requiredCount,
|
|
paidReason: paidReason,
|
|
reason: paidReason,
|
|
};
|
|
}
|
|
|
|
global.PublishPermission = {
|
|
isTruthy: isTruthy,
|
|
parseSummary: parseSummary,
|
|
};
|
|
})(typeof window !== 'undefined' ? window : globalThis);
|