AddressService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\member;
  12. use app\model\member\MemberAddress;
  13. use core\base\BaseApiService;
  14. /**
  15. * 会员收货地址服务层
  16. * Class AddressService
  17. * @package app\service\admin\address
  18. */
  19. class AddressService extends BaseApiService
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new MemberAddress();
  25. }
  26. /**
  27. * 获取会员收货地址列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getList(array $where = [])
  32. {
  33. $field = 'id,member_id,name,mobile,address,address_name,full_address,is_default,type';
  34. $order = 'is_default desc, id desc';
  35. $list = $this->model->where([ ['member_id', '=', $this->member_id ] ])->withSearch(["type"], $where)->field($field)->order($order)->select()->toArray();
  36. return $list;
  37. }
  38. /**
  39. * 获取会员收货地址信息
  40. * @param int $id
  41. * @return array
  42. */
  43. public function getInfo(int $id)
  44. {
  45. $field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,address_name,full_address,lng,lat,is_default,type';
  46. $info = $this->model->field($field)->where([ ['id', '=', $id], ['member_id', '=', $this->member_id ] ])->findOrEmpty()->toArray();
  47. return $info;
  48. }
  49. /**
  50. * 添加会员收货地址
  51. * @param array $data
  52. * @return mixed
  53. */
  54. public function add(array $data)
  55. {
  56. if ($data['is_default']) {
  57. $this->model->where([ ['member_id', '=', $this->member_id ], ['type', '=', $data['type']] ])->update(['is_default' => 0]);
  58. }
  59. $data['member_id'] = $this->member_id;
  60. $res = $this->model->create($data);
  61. return $res->id;
  62. }
  63. /**
  64. * 会员收货地址编辑
  65. * @param int $id
  66. * @param array $data
  67. * @return bool
  68. */
  69. public function edit(int $id, array $data)
  70. {
  71. if ($data['is_default']) {
  72. $this->model->where([ ['member_id', '=', $this->member_id ], ['type', '=', $data['type']] ])->update(['is_default' => 0]);
  73. }
  74. $this->model->where([ ['id', '=', $id], ['member_id', '=', $this->member_id ] ])->update($data);
  75. return true;
  76. }
  77. /**
  78. * 删除会员收货地址
  79. * @param int $id
  80. * @return bool
  81. */
  82. public function del(int $id)
  83. {
  84. $model = $this->model->where([ ['id', '=', $id], ['member_id', '=', $this->member_id ] ])->find();
  85. $res = $model->delete();
  86. return $res;
  87. }
  88. }