Address.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. //$row['address'] = $row->address_full;
  39. });
  40. }
  41. public function getCityAttr($value, $data)
  42. {
  43. $areainfo = $this->getAreainfoAttr($value, $data);
  44. return $areainfo['city'] ? $areainfo['city']['name'] : '';
  45. }
  46. public function getAreaAttr($value, $data)
  47. {
  48. $areainfo = $this->getAreainfoAttr($value, $data);
  49. return $areainfo['area'] ? $areainfo['area']['name'] : '';
  50. }
  51. public function getProvinceAttr($value, $data)
  52. {
  53. $areainfo = $this->getAreainfoAttr($value, $data);
  54. return $areainfo['province'] ? $areainfo['province']['name'] : '';
  55. }
  56. public function getAddressBaseAttr($value, $data)
  57. {
  58. $areainfo = $this->getAreainfoAttr($value, $data);
  59. $province = $areainfo['province'] ? $areainfo['province']['name'] : '';
  60. $city = $areainfo['city'] ? $areainfo['city']['name'] : '';
  61. $area = $areainfo['area'] ? $areainfo['area']['name'] : '';
  62. $value = $data['address'];
  63. $value = preg_replace("/^{$province}{$city}{$area}/", "", $value);
  64. return $value;
  65. }
  66. public function getAddressFullAttr($value, $data)
  67. {
  68. $areainfo = $this->getAreainfoAttr($value, $data);
  69. $province = $areainfo['province'] ? $areainfo['province']['name'] : '';
  70. $city = $areainfo['city'] ? $areainfo['city']['name'] : '';
  71. $area = $areainfo['area'] ? $areainfo['area']['name'] : '';
  72. $value = $data['address'];
  73. $value = preg_replace("/^{$province}{$city}{$area}/", "", $value);
  74. $value = $province . $city . $area . $value;
  75. return $value;
  76. }
  77. /**
  78. * 获取城市和地区信息
  79. * @param $value
  80. * @param $data
  81. * @return array
  82. */
  83. public function getAreainfoAttr($value, $data)
  84. {
  85. $result = [
  86. 'province' => null,
  87. 'city' => null,
  88. 'area' => null,
  89. ];
  90. $areaList = Area::where('id', 'in', [$data['province_id'], $data['city_id'], $data['area_id']])->select();
  91. foreach ($areaList as $index => $item) {
  92. $levelName = ($item['level'] == 1 ? 'province' : ($item['level'] == 2 ? 'city' : 'area'));
  93. $result[$levelName] = $item;
  94. }
  95. return $result;
  96. }
  97. /**
  98. * 获取会员地址列表
  99. * @param $user_id
  100. * @return false|\PDOStatement|string|\think\Collection
  101. */
  102. public static function getAddressList($user_id)
  103. {
  104. $addressList = self::where('user_id', $user_id)->where('status', StatusEnum::ENABLED)->order('used_nums desc,id desc')->select();
  105. return $addressList;
  106. }
  107. }