Address.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. class Address extends Model
  6. {
  7. use SoftDelete;
  8. // 表名
  9. protected $name = 'shop_address';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = 'deletetime';
  16. // 追加属性
  17. protected $append = [];
  18. public static function init()
  19. {
  20. parent::init();
  21. self::beforeWrite(function ($row) {
  22. $changed = $row->getChangedData();
  23. if (isset($changed['isdefault']) && $changed['isdefault']) {
  24. $info = \app\admin\model\shop\Address::where('isdefault', 1)->where('user_id', $row['user_id'])->find();
  25. if ($info && (!isset($row['id']) || $info['id'] != $row['id'])) {
  26. $info->isdefault = 0;
  27. $info->save();
  28. }
  29. }
  30. });
  31. }
  32. public function getStatusList()
  33. {
  34. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  35. }
  36. public function getStatusTextAttr($value, $data)
  37. {
  38. $value = $value ?: ($data['status'] ?? '');
  39. $list = $this->getStatusList();
  40. return $list[$value] ?? '';
  41. }
  42. public function User()
  43. {
  44. return $this->hasOne('\\app\\common\\model\\User', 'id', 'user_id', [], 'LEFT');
  45. }
  46. public function Province()
  47. {
  48. return $this->hasOne('Area', 'id', 'province_id', [], 'LEFT');
  49. }
  50. public function City()
  51. {
  52. return $this->hasOne('Area', 'id', 'city_id', [], 'LEFT');
  53. }
  54. public function Area()
  55. {
  56. return $this->hasOne('Area', 'id', 'area_id', [], 'LEFT');
  57. }
  58. }