UserAddress.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\model;
  20. use app\common\service\RegionService;
  21. use think\model\concern\SoftDelete;
  22. /**
  23. * 用户地址模型
  24. * Class UserLevel
  25. * @package app\common\model
  26. */
  27. class UserAddress extends BaseModel
  28. {
  29. use SoftDelete;
  30. protected $deleteTime = 'delete_time';
  31. protected $append = ['province', 'city', 'district'];
  32. public function getProvinceAttr($value, $data)
  33. {
  34. return RegionService::getAddress($data['province_id']);
  35. }
  36. public function getCityAttr($value, $data)
  37. {
  38. return RegionService::getAddress($data['city_id']);
  39. }
  40. public function getDistrictAttr($value, $data)
  41. {
  42. return RegionService::getAddress($data['district_id']);
  43. }
  44. /**
  45. * @notes 获取默认收货地址
  46. * @param $user_id
  47. * @return array
  48. * @throws @\think\db\exception\DataNotFoundException
  49. * @throws @\think\db\exception\DbException
  50. * @throws @\think\db\exception\ModelNotFoundException
  51. * @author 张无忌
  52. * @date 2021/8/3 16:39
  53. */
  54. public static function getDefaultAddress($user_id)
  55. {
  56. $model = new self;
  57. $result = $model->where(['user_id' => $user_id])
  58. ->where('is_default', 1)
  59. ->order('is_default desc')
  60. ->find();
  61. if (!$result) {
  62. return [];
  63. }
  64. return $result;
  65. }
  66. /**
  67. * @notes 根据ID获取地址
  68. * @param $user_id
  69. * @param $id
  70. * @return array
  71. * @throws @\think\db\exception\DataNotFoundException
  72. * @throws @\think\db\exception\DbException
  73. * @throws @\think\db\exception\ModelNotFoundException
  74. * @author 张无忌
  75. * @date 2021/8/3 16:47
  76. */
  77. public static function getAddressById($user_id, $id)
  78. {
  79. $model = new self;
  80. $result = $model->where(['user_id'=>$user_id])
  81. ->where('id', '=', intval($id))
  82. ->find();
  83. if (!$result) {
  84. return [];
  85. }
  86. return $result;
  87. }
  88. /**
  89. * @notes 获取一条收货地址
  90. * @param $user_id
  91. * @param $id
  92. * @return array
  93. * @author 张无忌
  94. * @date 2021/8/3 16:42
  95. */
  96. public static function getOneAddress($user_id, $id=0)
  97. {
  98. if ($id > 0) {
  99. return self::getAddressById($user_id, $id);
  100. }
  101. return self::getDefaultAddress($user_id);
  102. }
  103. }