ModelAttr.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\model\shopro\traits;
  3. trait ModelAttr
  4. {
  5. /**
  6. * 默认类型列表,子类重写
  7. *
  8. * @return array
  9. */
  10. public function typeList()
  11. {
  12. return [];
  13. }
  14. /**
  15. * 默认状态列表,子类可重写
  16. *
  17. * @return array
  18. */
  19. public function statusList()
  20. {
  21. return [
  22. 'normal' => '正常',
  23. 'hidden' => '隐藏',
  24. 'enable' => '启用中',
  25. 'disabled' => '已禁用',
  26. 'up' => '上架',
  27. 'down' => '下架',
  28. ];
  29. }
  30. /**
  31. * (查询范围)正常
  32. */
  33. public function scopeNormal($query)
  34. {
  35. return $query->where('status', 'normal');
  36. }
  37. /**
  38. * (查询范围)启用
  39. */
  40. public function scopeEnable($query)
  41. {
  42. return $query->where('status', 'enable');
  43. }
  44. /**
  45. * (查询范围)禁用
  46. */
  47. public function scopeDisabled($query)
  48. {
  49. return $query->where('status', 'disabled');
  50. }
  51. /**
  52. * (查询范围)隐藏 hidden 和框架底层 hidden 冲突了
  53. */
  54. public function scopeStatusHidden($query)
  55. {
  56. return $query->where('status', 'hidden');
  57. }
  58. /**
  59. * (查询范围)上架
  60. */
  61. public function scopeUp($query)
  62. {
  63. return $query->where('status', 'up');
  64. }
  65. /**
  66. * (查询范围)下架
  67. */
  68. public function scopeDown($query)
  69. {
  70. return $query->where('status', 'down');
  71. }
  72. // /**
  73. // * 创建时间格式化
  74. // *
  75. // * @param string $value
  76. // * @param array $data
  77. // * @return string
  78. // */
  79. // public function getCreatetimeAttr($value, $data)
  80. // {
  81. // return $this->attrTimeFormat($value, $data, 'createtime');
  82. // }
  83. // /**
  84. // * 更新时间格式化
  85. // *
  86. // * @param string $value
  87. // * @param array $data
  88. // * @return string
  89. // */
  90. // public function getUpdatetimeAttr($value, $data)
  91. // {
  92. // return $this->attrTimeFormat($value, $data, 'updatetime');
  93. // }
  94. /**
  95. * 更新时间格式化
  96. *
  97. * @param string $value
  98. * @param array $data
  99. * @return string
  100. */
  101. public function getDeletetimeAttr($value, $data)
  102. {
  103. return $this->attrTimeFormat($value, $data, 'deletetime');
  104. }
  105. /**
  106. * 通用类型获取器
  107. *
  108. * @param string $value
  109. * @param array $data
  110. * @return string
  111. */
  112. public function getTypeTextAttr($value, $data)
  113. {
  114. $value = $value ?: ($data['type'] ?? null);
  115. $list = $this->typeList();
  116. return isset($list[$value]) ? $list[$value] : '';
  117. }
  118. /**
  119. * 通用状态获取器
  120. *
  121. * @param string $value
  122. * @param array $data
  123. * @return string
  124. */
  125. public function getStatusTextAttr($value, $data)
  126. {
  127. $value = $value ?: ($data['status'] ?? '');
  128. $list = $this->statusList();
  129. return isset($list[$value]) ? $list[$value] : '';
  130. }
  131. /**
  132. * 时间格式化
  133. *
  134. * @param mix $value
  135. * @param array $data
  136. * @param string $field
  137. * @return string
  138. */
  139. protected function attrTimeFormat($value, $data, $field)
  140. {
  141. $value = $value ?: ($data[$field] ?? null);
  142. return $value ? (is_string($value) ? $value : date('Y-m-d H:i:s', $value)) : null;
  143. }
  144. /**
  145. * 获取器格式化 json
  146. *
  147. * @param mix $value
  148. * @param array $data
  149. * @param string $field
  150. * @param bool $return_array
  151. * @return array|null
  152. */
  153. protected function attrFormatJson($value, $data, $field, $return_array = false)
  154. {
  155. $value = $value ?: ($data[$field] ?? null);
  156. $value = $value ? json_decode($value, true) : ($return_array ? [] : $value);
  157. return $value === false ? $data[$field] : $value;
  158. }
  159. /**
  160. * 获取器格式化 , 隔开的数据
  161. *
  162. * @param mix $value
  163. * @param array $data
  164. * @param string $field
  165. * @param bool $return_array
  166. * @return array|null
  167. */
  168. protected function attrFormatComma($value, $data, $field, $return_array = false)
  169. {
  170. $value = $value ?: ($data[$field] ?? null);
  171. $value = $value ? explode(',', $value) : ($return_array ? [] : $value);
  172. return $value === false ? $data[$field] : $value;
  173. }
  174. /**
  175. * 将时间格式化为时间戳
  176. *
  177. * @param [type] $time
  178. * @return void
  179. */
  180. protected function attrFormatUnix($time)
  181. {
  182. return $time ? (!is_numeric($time) ? strtotime($time) : $time) : null;
  183. }
  184. }