86 lines
2.3 KiB
PHP
86 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use app\common\model\traits\TimestampInteger;
|
|
use support\think\Model;
|
|
|
|
/**
|
|
* MallAddress
|
|
*/
|
|
class MallAddress extends Model
|
|
{
|
|
use TimestampInteger;
|
|
|
|
// 表名
|
|
protected $name = 'mall_address';
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = true;
|
|
|
|
// 追加属性
|
|
protected $append = [
|
|
'region_text',
|
|
];
|
|
|
|
|
|
public function getregionAttr($value): array
|
|
{
|
|
if ($value === '' || $value === null) return [];
|
|
if (!is_array($value)) {
|
|
return explode(',', $value);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
public function setregionAttr($value): string
|
|
{
|
|
return is_array($value) ? implode(',', $value) : $value;
|
|
}
|
|
|
|
public function getregionTextAttr($value, $row): string
|
|
{
|
|
if ($row['region'] === '' || $row['region'] === null) return '';
|
|
$region = $row['region'];
|
|
$ids = $region;
|
|
if (!is_array($ids)) {
|
|
$ids = explode(',', (string) $ids);
|
|
}
|
|
$ids = array_values(array_filter(array_map('trim', $ids), static function ($s) {
|
|
return $s !== '';
|
|
}));
|
|
if (empty($ids)) {
|
|
return '';
|
|
}
|
|
$cityNames = \support\think\Db::name('area')->whereIn('id', $ids)->column('name');
|
|
return $cityNames ? implode(',', $cityNames) : '';
|
|
}
|
|
|
|
public function playxUserAsset(): \think\model\relation\BelongsTo
|
|
{
|
|
return $this->belongsTo(\app\common\model\MallUserAsset::class, 'playx_user_asset_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 实物订单收货快照(写入 mall_order.receiver_*,与 mall_address 当前内容一致)
|
|
*
|
|
* @return array{receiver_name: string, receiver_phone: string, receiver_address: string}
|
|
*/
|
|
public static function snapshotForPhysicalOrder(self $addr): array
|
|
{
|
|
$regionText = $addr->region_text ?? '';
|
|
$parts = array_filter([
|
|
trim($regionText),
|
|
trim($addr->detail_address ?? ''),
|
|
], static function ($s) {
|
|
return $s !== '';
|
|
});
|
|
$receiverAddress = implode(' ', $parts);
|
|
|
|
return [
|
|
'receiver_name' => trim($addr->receiver_name ?? ''),
|
|
'receiver_phone' => trim($addr->phone ?? ''),
|
|
'receiver_address' => $receiverAddress,
|
|
];
|
|
}
|
|
} |