feat(dmepa): 全局统一规范DMEPA命名并优化游客与品酒师身份显示
这个提交包含在:
@@ -9,11 +9,52 @@ use app\admin\model\shopro\commission\Agent as AgentModel;
|
||||
*/
|
||||
class IdentityService
|
||||
{
|
||||
public static function build($agent)
|
||||
public static function hasPurchased($userId, $user = null)
|
||||
{
|
||||
$userId = intval($userId);
|
||||
if ($userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 如果传入了 user 对象且 total_consume > 0,快速命中
|
||||
if ($user) {
|
||||
$consume = floatval(is_array($user) ? ($user['total_consume'] ?? 0) : ($user->total_consume ?? 0));
|
||||
if ($consume > 0) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// 若未传入 user 对象,轻量获取 user 表的 total_consume 尝试命中
|
||||
try {
|
||||
$userConsume = \think\Db::name('user')->where('id', $userId)->value('total_consume');
|
||||
if (floatval($userConsume) > 0) {
|
||||
return true;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 容错继续
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 查询有效已支付订单(排除全额退款完成的订单)
|
||||
try {
|
||||
$hasOrder = \think\Db::name('shopro_order')
|
||||
->where('user_id', $userId)
|
||||
->whereIn('status', ['paid', 'completed'])
|
||||
->where('apply_refund_status', '<>', 2)
|
||||
->whereNull('deletetime')
|
||||
->count() > 0;
|
||||
return $hasOrder;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function build($agent, $user = null)
|
||||
{
|
||||
$empty = [
|
||||
'identities' => [],
|
||||
'identity_text' => '',
|
||||
'identities' => ['guest'],
|
||||
'identity_text' => '游客',
|
||||
'is_guest' => true,
|
||||
'has_purchased' => false,
|
||||
'is_partner' => false,
|
||||
'is_advanced' => false,
|
||||
'maker_level_name' => '',
|
||||
@@ -23,24 +64,33 @@ class IdentityService
|
||||
if (!$agent) {
|
||||
return $empty;
|
||||
}
|
||||
$userId = intval(is_array($agent) ? ($agent['user_id'] ?? 0) : ($agent->user_id ?? 0));
|
||||
$level = intval(is_array($agent) ? ($agent['level'] ?? 0) : ($agent->level ?? 0));
|
||||
$isVenue = intval(is_array($agent) ? ($agent['is_venue'] ?? 0) : ($agent->is_venue ?? 0));
|
||||
$isCounty = intval(is_array($agent) ? ($agent['is_county'] ?? 0) : ($agent->is_county ?? 0));
|
||||
$isPartner = intval(is_array($agent) ? ($agent['is_partner'] ?? 0) : ($agent->is_partner ?? 0));
|
||||
|
||||
$roleNames = PartnerConfig::roleNames();
|
||||
$tasterName = $roleNames['taster'];
|
||||
$makerBaseName = $roleNames['maker'];
|
||||
$venueName = $roleNames['venue'];
|
||||
$countyName = $roleNames['county'];
|
||||
$partnerName = $roleNames['partner'];
|
||||
$tasterName = $roleNames['taster'] ?? '品酒师';
|
||||
$makerBaseName = $roleNames['maker'] ?? '创客';
|
||||
$venueName = $roleNames['venue'] ?? '酒道馆';
|
||||
$countyName = $roleNames['county'] ?? '区县';
|
||||
$partnerName = $roleNames['partner'] ?? '合伙股东';
|
||||
|
||||
$isAdvanced = ($level >= 2) || ($isVenue == 1) || ($isCounty == 1) || ($isPartner == 1);
|
||||
$hasPurchased = $userId > 0 ? self::hasPurchased($userId, $user ?: (is_array($agent) ? ($agent['user'] ?? null) : ($agent->user ?? null))) : false;
|
||||
$isGuest = ($level == 1 && !$hasPurchased && !$isAdvanced);
|
||||
|
||||
$identities = [];
|
||||
$parts = [];
|
||||
$makerLevelName = '';
|
||||
|
||||
if ($level == 1) {
|
||||
$identities[] = 'taster';
|
||||
if ($hasPurchased) {
|
||||
$identities[] = 'taster';
|
||||
} elseif (!$isAdvanced) {
|
||||
$identities[] = 'guest';
|
||||
}
|
||||
} elseif ($level >= 2) {
|
||||
$identities[] = 'maker';
|
||||
$makerLevelName = $makerBaseName . 'V' . ($level - 1);
|
||||
@@ -59,16 +109,16 @@ class IdentityService
|
||||
$parts[] = $partnerName;
|
||||
}
|
||||
|
||||
// 若无进阶角色身份且等级为1,则身份为基础品酒师/自定义基础名称
|
||||
// 若无进阶角色身份且等级为1,根据是否买单显示品酒师或游客
|
||||
if (empty($parts) && $level == 1) {
|
||||
$parts[] = $tasterName;
|
||||
$parts[] = $hasPurchased ? $tasterName : '游客';
|
||||
}
|
||||
|
||||
$isAdvanced = ($level >= 2) || ($isVenue == 1) || ($isCounty == 1) || ($isPartner == 1);
|
||||
|
||||
return [
|
||||
'identities' => $identities,
|
||||
'identity_text' => implode(' · ', $parts),
|
||||
'identity_text' => implode(' · ', $parts) ?: ($isGuest ? '游客' : ''),
|
||||
'is_guest' => $isGuest,
|
||||
'has_purchased' => $hasPurchased,
|
||||
'is_partner' => (bool) $isPartner,
|
||||
'is_advanced' => $isAdvanced,
|
||||
'maker_level_name' => $makerLevelName,
|
||||
@@ -85,7 +135,7 @@ class IdentityService
|
||||
public static function formatMadow($value)
|
||||
{
|
||||
$floatVal = (float) $value;
|
||||
// 当存在小于 1 豆且大于 0 的微量马豆时,自适应保留有效小数(最多4位),避免直接抹零显示为 0 豆
|
||||
// 当存在小于 1 豆且大于 0 的微量dmepa时,自适应保留有效小数(最多4位),避免直接抹零显示为 0 豆
|
||||
if ($floatVal > 0 && $floatVal < 1) {
|
||||
return rtrim(rtrim(number_format($floatVal, 4, '.', ''), '0'), '.');
|
||||
}
|
||||
@@ -107,12 +157,12 @@ class IdentityService
|
||||
return $trimmed !== '' ? $trimmed : '0';
|
||||
}
|
||||
|
||||
public static function attachAgent($agent, $rate = null)
|
||||
public static function attachAgent($agent, $rate = null, $user = null)
|
||||
{
|
||||
if (!$agent) {
|
||||
return null;
|
||||
}
|
||||
$identity = self::build($agent);
|
||||
$identity = self::build($agent, $user);
|
||||
$madow = is_array($agent) ? ($agent['madow'] ?? 0) : ($agent->madow ?? 0);
|
||||
$released = is_array($agent) ? ($agent['voucher_released'] ?? 0) : ($agent->voucher_released ?? 0);
|
||||
$used = is_array($agent) ? ($agent['voucher_used'] ?? 0) : ($agent->voucher_used ?? 0);
|
||||
@@ -185,14 +235,16 @@ class IdentityService
|
||||
]);
|
||||
}
|
||||
|
||||
public static function attachProfile($agent, $rate = null)
|
||||
public static function attachProfile($agent, $rate = null, $user = null)
|
||||
{
|
||||
if (!$agent) {
|
||||
$baseRate = PartnerConfig::get('partner_madow_base_rate', '0.00000075');
|
||||
$roleNames = PartnerConfig::roleNames();
|
||||
return [
|
||||
'identity_text' => '',
|
||||
'identities' => [],
|
||||
'identity_text' => '游客',
|
||||
'identities' => ['guest'],
|
||||
'is_guest' => true,
|
||||
'has_purchased' => false,
|
||||
'is_partner' => false,
|
||||
'is_advanced' => false,
|
||||
'maker_level_name' => '',
|
||||
@@ -215,11 +267,13 @@ class IdentityService
|
||||
'partner_mode' => PartnerConfig::isPartnerMode(),
|
||||
];
|
||||
}
|
||||
$full = self::attachAgent($agent, $rate);
|
||||
$full = self::attachAgent($agent, $rate, $user);
|
||||
$roleNames = $full['role_names'] ?? PartnerConfig::roleNames();
|
||||
return [
|
||||
'identity_text' => $full['identity_text'],
|
||||
'identities' => $full['identities'],
|
||||
'is_guest' => $full['is_guest'] ?? false,
|
||||
'has_purchased' => $full['has_purchased'] ?? false,
|
||||
'is_partner' => $full['is_partner'],
|
||||
'is_advanced' => $full['is_advanced'] ?? false,
|
||||
'maker_level_name' => $full['maker_level_name'],
|
||||
|
||||
在新工单中引用
屏蔽一个用户