Address.php 3.6 KB

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