AttireBack.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class AttireBack extends Model
  6. {
  7. // 表名
  8. protected $name = 'attire_back';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'type_text',
  18. 'duetime_text',
  19. 'is_use_text',
  20. 'is_using_text'
  21. ];
  22. protected static function init()
  23. {
  24. self::beforeInsert(function ($row) {
  25. $attireInfo = Db::name('attire')->where('id',$row['attire_id'])->find();
  26. $row["attire_name"] = $attireInfo["title"];
  27. $row["price"] = $attireInfo["price"];
  28. $row["file_image"] = $attireInfo["file_image"];
  29. $row["android_image"] = $attireInfo["android_image"];
  30. $row["gif_image"] = $attireInfo["gif_image"];
  31. $row["limit_day"] = $attireInfo["limit_day"];
  32. $row["type"] = $attireInfo["type"];
  33. });
  34. }
  35. public function getTypeList()
  36. {
  37. return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
  38. }
  39. public function getIsUseList()
  40. {
  41. return ['1' => __('Is_use 1'), '0' => __('Is_use 0')];
  42. }
  43. public function getIsUsingList()
  44. {
  45. return ['1' => __('Is_using 1'), '0' => __('Is_using 0')];
  46. }
  47. public function getTypeTextAttr($value, $data)
  48. {
  49. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  50. $list = $this->getTypeList();
  51. return isset($list[$value]) ? $list[$value] : '';
  52. }
  53. public function getDuetimeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['duetime']) ? $data['duetime'] : '');
  56. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  57. }
  58. public function getIsUseTextAttr($value, $data)
  59. {
  60. $value = $value ? $value : (isset($data['is_use']) ? $data['is_use'] : '');
  61. $list = $this->getIsUseList();
  62. return isset($list[$value]) ? $list[$value] : '';
  63. }
  64. public function getIsUsingTextAttr($value, $data)
  65. {
  66. $value = $value ? $value : (isset($data['is_using']) ? $data['is_using'] : '');
  67. $list = $this->getIsUsingList();
  68. return isset($list[$value]) ? $list[$value] : '';
  69. }
  70. protected function setDuetimeAttr($value)
  71. {
  72. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  73. }
  74. public function user()
  75. {
  76. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  77. }
  78. }