代码优化
This commit is contained in:
@@ -73,6 +73,43 @@ class TrafficPoolSource extends Model
|
||||
return $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容历史库:s2_wechat_friend 早期可能不存在 headImgUrl 列
|
||||
* @param string $wechatId
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFriendHeadImgUrl(string $wechatId): string
|
||||
{
|
||||
$wechatId = trim($wechatId);
|
||||
if ($wechatId === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
static $hasHeadImgUrl = null;
|
||||
if ($hasHeadImgUrl === null) {
|
||||
try {
|
||||
$cols = Db::query("SHOW COLUMNS FROM s2_wechat_friend LIKE 'headImgUrl'");
|
||||
$hasHeadImgUrl = !empty($cols);
|
||||
} catch (\Throwable $e) {
|
||||
$hasHeadImgUrl = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasHeadImgUrl) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
$val = Db::table('s2_wechat_friend')
|
||||
->where('wechatId', $wechatId)
|
||||
->value('headImgUrl');
|
||||
return $val ? (string)$val : '';
|
||||
} catch (\Throwable $e) {
|
||||
// 兜底:避免因字段缺失导致接口直接 1054
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取来源类型名称
|
||||
* @return string
|
||||
@@ -224,11 +261,10 @@ class TrafficPoolSource extends Model
|
||||
|
||||
// 尝试获取好友头像
|
||||
if (!empty($source['sourceWechatId'])) {
|
||||
$sourceData['sourceAvatar'] = Db::table('ck_traffic_pool')
|
||||
$avatar = Db::table('ck_traffic_pool')
|
||||
->where('wechatId', $source['sourceWechatId'])
|
||||
->value('avatar') ?: Db::table('s2_wechat_friend')
|
||||
->where('wechatId', $source['sourceWechatId'])
|
||||
->value('headImgUrl') ?: '';
|
||||
->value('avatar');
|
||||
$sourceData['sourceAvatar'] = $avatar ?: self::getFriendHeadImgUrl((string)$source['sourceWechatId']);
|
||||
}
|
||||
} else {
|
||||
$sourceData['chatroomOwners'] = [];
|
||||
@@ -448,11 +484,10 @@ class TrafficPoolSource extends Model
|
||||
$sourceData['chatroomInfo'] = null;
|
||||
// 尝试获取好友头像
|
||||
if (!empty($source['sourceWechatId'])) {
|
||||
$sourceData['sourceAvatar'] = Db::table('ck_traffic_pool')
|
||||
$avatar = Db::table('ck_traffic_pool')
|
||||
->where('wechatId', $source['sourceWechatId'])
|
||||
->value('avatar') ?: Db::table('s2_wechat_friend')
|
||||
->where('wechatId', $source['sourceWechatId'])
|
||||
->value('headImgUrl') ?: '';
|
||||
->value('avatar');
|
||||
$sourceData['sourceAvatar'] = $avatar ?: self::getFriendHeadImgUrl((string)$source['sourceWechatId']);
|
||||
}
|
||||
} else {
|
||||
$sourceData['chatroomOwners'] = [];
|
||||
|
||||
Reference in New Issue
Block a user