123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\admin\model\shopro\traits;
- trait ModelAttr
- {
- /**
- * 默认类型列表,子类重写
- *
- * @return array
- */
- public function typeList()
- {
- return [];
- }
- /**
- * 默认状态列表,子类可重写
- *
- * @return array
- */
- public function statusList()
- {
- return [
- 'normal' => '正常',
- 'hidden' => '隐藏',
- 'enable' => '启用中',
- 'disabled' => '已禁用',
- 'up' => '上架',
- 'down' => '下架',
- ];
- }
- /**
- * (查询范围)正常
- */
- public function scopeNormal($query)
- {
- return $query->where('status', 'normal');
- }
- /**
- * (查询范围)启用
- */
- public function scopeEnable($query)
- {
- return $query->where('status', 'enable');
- }
- /**
- * (查询范围)禁用
- */
- public function scopeDisabled($query)
- {
- return $query->where('status', 'disabled');
- }
- /**
- * (查询范围)隐藏 hidden 和框架底层 hidden 冲突了
- */
- public function scopeStatusHidden($query)
- {
- return $query->where('status', 'hidden');
- }
- /**
- * (查询范围)上架
- */
- public function scopeUp($query)
- {
- return $query->where('status', 'up');
- }
- /**
- * (查询范围)下架
- */
- public function scopeDown($query)
- {
- return $query->where('status', 'down');
- }
- // /**
- // * 创建时间格式化
- // *
- // * @param string $value
- // * @param array $data
- // * @return string
- // */
- // public function getCreatetimeAttr($value, $data)
- // {
- // return $this->attrTimeFormat($value, $data, 'createtime');
- // }
- // /**
- // * 更新时间格式化
- // *
- // * @param string $value
- // * @param array $data
- // * @return string
- // */
- // public function getUpdatetimeAttr($value, $data)
- // {
- // return $this->attrTimeFormat($value, $data, 'updatetime');
- // }
- /**
- * 更新时间格式化
- *
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getDeletetimeAttr($value, $data)
- {
- return $this->attrTimeFormat($value, $data, 'deletetime');
- }
- /**
- * 通用类型获取器
- *
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getTypeTextAttr($value, $data)
- {
- $value = $value ?: ($data['type'] ?? null);
- $list = $this->typeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 通用状态获取器
- *
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ?: ($data['status'] ?? '');
- $list = $this->statusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 时间格式化
- *
- * @param mix $value
- * @param array $data
- * @param string $field
- * @return string
- */
- protected function attrTimeFormat($value, $data, $field)
- {
- $value = $value ?: ($data[$field] ?? null);
- return $value ? (is_string($value) ? $value : date('Y-m-d H:i:s', $value)) : null;
- }
- /**
- * 获取器格式化 json
- *
- * @param mix $value
- * @param array $data
- * @param string $field
- * @param bool $return_array
- * @return array|null
- */
- protected function attrFormatJson($value, $data, $field, $return_array = false)
- {
- $value = $value ?: ($data[$field] ?? null);
- $value = $value ? json_decode($value, true) : ($return_array ? [] : $value);
- return $value === false ? $data[$field] : $value;
- }
- /**
- * 获取器格式化 , 隔开的数据
- *
- * @param mix $value
- * @param array $data
- * @param string $field
- * @param bool $return_array
- * @return array|null
- */
- protected function attrFormatComma($value, $data, $field, $return_array = false)
- {
- $value = $value ?: ($data[$field] ?? null);
- $value = $value ? explode(',', $value) : ($return_array ? [] : $value);
- return $value === false ? $data[$field] : $value;
- }
- /**
- * 将时间格式化为时间戳
- *
- * @param [type] $time
- * @return void
- */
- protected function attrFormatUnix($time)
- {
- return $time ? (!is_numeric($time) ? strtotime($time) : $time) : null;
- }
- }
|