Vip.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\admin\model\vip;
  3. use think\Exception;
  4. use think\Model;
  5. class Vip extends Model
  6. {
  7. // 表名
  8. protected $name = 'vip';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'status_text'
  18. ];
  19. public static function init()
  20. {
  21. self::beforeWrite(function ($row) {
  22. $exist = self::where(function ($query) use ($row) {
  23. $query->where('level', $row['level']);
  24. if (isset($row['id'])) {
  25. $query->where('id', '<>', $row['id']);
  26. }
  27. })->find();
  28. if ($exist) {
  29. throw new Exception("已经存在同等级VIP");
  30. }
  31. $pricedata = (array)json_decode($row['pricedata'], true);
  32. if (!$pricedata) {
  33. throw new Exception("价格配置不能为空");
  34. }
  35. });
  36. }
  37. public function getStatusList()
  38. {
  39. return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'pulloff' => __('Pulloff')];
  40. }
  41. public function getStatusTextAttr($value, $data)
  42. {
  43. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  44. $list = $this->getStatusList();
  45. return isset($list[$value]) ? $list[$value] : '';
  46. }
  47. }