Exchange.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Exchange extends Model
  5. {
  6. // 表名
  7. protected $name = 'shop_exchange';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. protected static $config = [];
  15. // 追加属性
  16. protected $append = [
  17. 'type_text',
  18. 'status_text',
  19. 'url'
  20. ];
  21. public function getUrlAttr($value, $data)
  22. {
  23. return $this->buildUrl($value, $data);
  24. }
  25. public function getFullurlAttr($value, $data)
  26. {
  27. return $this->buildUrl($value, $data, true);
  28. }
  29. private function buildUrl($value, $data, $domain = false)
  30. {
  31. $vars = [
  32. ':id' => $data['id']
  33. ];
  34. $suffix = static::$config['moduleurlsuffix']['exchange'] ?? static::$config['urlsuffix'];
  35. return addon_url('shop/exchange/show', $vars, $suffix, $domain);
  36. }
  37. protected static function init()
  38. {
  39. $config = get_addon_config('shop');
  40. self::$config = $config;
  41. self::afterInsert(function ($row) {
  42. $pk = $row->getPk();
  43. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  44. });
  45. }
  46. public function getTypeList()
  47. {
  48. return ['virtual' => __('Type virtual'), 'reality' => __('Type reality')];
  49. }
  50. public function getStatusList()
  51. {
  52. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  53. }
  54. public function getTypeTextAttr($value, $data)
  55. {
  56. $value = $value ?: ($data['type'] ?? '');
  57. $list = $this->getTypeList();
  58. return $list[$value] ?? '';
  59. }
  60. public function getStatusTextAttr($value, $data)
  61. {
  62. $value = $value ?: ($data['status'] ?? '');
  63. $list = $this->getStatusList();
  64. return $list[$value] ?? '';
  65. }
  66. public function getImageAttr($value)
  67. {
  68. return cdnurl($value, true);
  69. }
  70. //获取列表
  71. public static function tableList($param)
  72. {
  73. $pageNum = 15;
  74. if (!empty($param['num'])) {
  75. $pageNum = $param['num'];
  76. }
  77. $orderby = 'createtime';
  78. $orderway = 'desc';
  79. if (!empty($param['orderby']) && in_array($param['orderby'], ['createtime', 'score', 'sales', 'weigh'])) {
  80. $orderby = $param['orderby'];
  81. }
  82. if (!empty($param['orderway']) && in_array($param['orderway'], ['asc', 'desc'])) {
  83. $orderway = $param['orderway'];
  84. }
  85. return self::where(function ($query) use ($param) {
  86. $query->where('stocks', '>', 0)->where('status', 'normal');
  87. if (!empty($param['type'])) {
  88. $query->where('type', $param['type']);
  89. }
  90. if (isset($param['keyword']) && $param['keyword'] != '') {
  91. $query->where('title', 'like', '%' . $param['keyword'] . '%');
  92. }
  93. })->order("$orderby $orderway")->paginate($pageNum);
  94. }
  95. }