Address.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model\store;
  13. use cores\BaseModel;
  14. use app\common\model\Region as RegionModel;
  15. /**
  16. * 商家地址模型
  17. * Class Address
  18. * @package app\common\model\store
  19. */
  20. class Address extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'store_address';
  24. // 定义主键
  25. protected $pk = 'address_id';
  26. /**
  27. * 追加字段
  28. * @var array
  29. */
  30. protected $append = ['cascader', 'region', 'full_address'];
  31. /**
  32. * 省市区ID集
  33. * @param $value
  34. * @param $data
  35. * @return array
  36. */
  37. public function getCascaderAttr($value, $data): array
  38. {
  39. return [$data['province_id'], $data['city_id'], $data['region_id']];
  40. }
  41. /**
  42. * 省市区名称集
  43. * @param $value
  44. * @param $data
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getRegionAttr($value, $data): array
  51. {
  52. return $this->getRegionNames($data);
  53. }
  54. /**
  55. * 获取完整地址
  56. * @param $value
  57. * @param $data
  58. * @return string
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function getFullAddressAttr($value, $data): string
  64. {
  65. $rgionNames = $this->getRegionNames($data);
  66. return "{$rgionNames['province']}{$rgionNames['city']}{$rgionNames['region']}{$data['detail']}";
  67. }
  68. /**
  69. * 获取省市区名称
  70. * @param array $data
  71. * @return array|mixed
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. private function getRegionNames(array $data)
  77. {
  78. static $dataset = [];
  79. $id = $data[$this->getPk()];
  80. if (!isset($dataset[$id])) {
  81. $dataset[$id] = [
  82. 'province' => RegionModel::getNameById($data['province_id']),
  83. 'city' => RegionModel::getNameById($data['city_id']),
  84. 'region' => RegionModel::getNameById($data['region_id']),
  85. ];
  86. }
  87. return $dataset[$id];
  88. }
  89. /**
  90. * 详情记录
  91. * @param int $addressId
  92. * @return static|array|null
  93. */
  94. public static function detail(int $addressId)
  95. {
  96. return self::get($addressId);
  97. }
  98. }