Address.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\model;
  3. use app\common\Enum\StatusEnum;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. * 模型
  8. */
  9. class Address extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $name = 'shop_user_address';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. // 追加属性
  21. protected $append = [
  22. 'city',
  23. 'area',
  24. 'province',
  25. ];
  26. public static function init()
  27. {
  28. parent::init();
  29. self::beforeWrite(function ($row) {
  30. $changed = $row->getChangedData();
  31. if (isset($changed['is_default']) && $changed['is_default']) {
  32. $info = self::where('is_default', 1)->where('user_id', $row['user_id'])->find();
  33. if ($info && (!isset($row['id']) || $info['id'] != $row['id'])) {
  34. $info->is_default = 0;
  35. $info->save();
  36. }
  37. }
  38. //新增时 第一个地址 默认地址
  39. if (empty($row['id'])) {
  40. $count = self::where('user_id', $row['user_id'])->count();
  41. if ($count == 0) {
  42. $row->is_default = 1;
  43. }
  44. } else {
  45. // 编辑时,如果只有一个地址,强制设为默认
  46. $count = self::where('user_id', $row['user_id'])->count();
  47. if ($count == 1) {
  48. $row->is_default = 1;
  49. }
  50. }
  51. });
  52. }
  53. public function getCityAttr($value, $data)
  54. {
  55. $areainfo = $this->getAreainfoAttr($value, $data);
  56. return $areainfo['city'] ? $areainfo['city']['name'] : '';
  57. }
  58. public function getAreaAttr($value, $data)
  59. {
  60. $areainfo = $this->getAreainfoAttr($value, $data);
  61. return $areainfo['area'] ? $areainfo['area']['name'] : '';
  62. }
  63. public function getProvinceAttr($value, $data)
  64. {
  65. $areainfo = $this->getAreainfoAttr($value, $data);
  66. return $areainfo['province'] ? $areainfo['province']['name'] : '';
  67. }
  68. public function getAddressBaseAttr($value, $data)
  69. {
  70. $areainfo = $this->getAreainfoAttr($value, $data);
  71. $province = $areainfo['province'] ? $areainfo['province']['name'] : '';
  72. $city = $areainfo['city'] ? $areainfo['city']['name'] : '';
  73. $area = $areainfo['area'] ? $areainfo['area']['name'] : '';
  74. $value = $data['address'];
  75. $value = preg_replace("/^{$province}{$city}{$area}/", "", $value);
  76. return $value;
  77. }
  78. public function getAddressFullAttr($value, $data)
  79. {
  80. $areainfo = $this->getAreainfoAttr($value, $data);
  81. $province = $areainfo['province'] ? $areainfo['province']['name'] : '';
  82. $city = $areainfo['city'] ? $areainfo['city']['name'] : '';
  83. $area = $areainfo['area'] ? $areainfo['area']['name'] : '';
  84. $value = $data['address'];
  85. $value = preg_replace("/^{$province}{$city}{$area}/", "", $value);
  86. $value = $province . $city . $area . $value;
  87. return $value;
  88. }
  89. /**
  90. * 获取城市和地区信息
  91. * @param $value
  92. * @param $data
  93. * @return array
  94. */
  95. public function getAreainfoAttr($value, $data)
  96. {
  97. $result = [
  98. 'province' => null,
  99. 'city' => null,
  100. 'area' => null,
  101. ];
  102. $areaList = Area::where('id', 'in', [$data['province_id'], $data['city_id'], $data['area_id']])->select();
  103. foreach ($areaList as $index => $item) {
  104. $levelName = ($item['level'] == 1 ? 'province' : ($item['level'] == 2 ? 'city' : 'area'));
  105. $result[$levelName] = $item;
  106. }
  107. return $result;
  108. }
  109. /**
  110. * 获取会员地址列表
  111. * @param $user_id
  112. * @return false|\PDOStatement|string|\think\Collection
  113. */
  114. public static function getAddressList($user_id)
  115. {
  116. $addressList = self::where('user_id', $user_id)->where('status', StatusEnum::ENABLED)->order('used_nums desc,id desc')->select();
  117. return $addressList;
  118. }
  119. }