Address.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\model\DataUserAddress;
  5. use hg\apidoc\annotation\Title;
  6. use think\admin\extend\CodeExtend;
  7. /**
  8. * @Title("用户收货地址管理")
  9. */
  10. class Address extends Auth
  11. {
  12. /**
  13. * @Title("添加收货地址")
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. */
  18. public function set()
  19. {
  20. $data = $this->_vali([
  21. 'uuid.value' => $this->uuid,
  22. 'type.default' => 0,
  23. 'code.default' => '',
  24. 'idcode.default' => '', // 身份证号码
  25. 'idimg1.default' => '', // 身份证正面
  26. 'idimg2.default' => '', // 身份证反面
  27. 'type.in:0,1' => '地址状态不在范围!',
  28. 'name.require' => '收货姓名不能为空!',
  29. 'phone.mobile' => '收货手机格式错误!',
  30. 'phone.require' => '收货手机不能为空!',
  31. 'province.require' => '地址省份不能为空!',
  32. 'city.require' => '地址城市不能为空!',
  33. 'area.require' => '地址区域不能为空!',
  34. 'address.require' => '详情地址不能为空!',
  35. 'deleted.value' => 0,
  36. ]);
  37. if (empty($data['code'])) {
  38. unset($data['code']);
  39. $count = DataUserAddress::mk()->where($data)->count();
  40. if ($count > 0) $this->error('抱歉,该地址已经存在!');
  41. $data['code'] = CodeExtend::uniqidDate(20, 'A');
  42. if (DataUserAddress::mk()->insert($data) === false) {
  43. $this->error('添加地址失败!');
  44. }
  45. } else {
  46. $map = ['uuid' => $this->uuid, 'code' => $data['code']];
  47. $addr = DataUserAddress::mk()->where($map)->find();
  48. if (empty($addr)) $this->error('修改地址不存在!');
  49. DataUserAddress::mk()->where($map)->update($data);
  50. }
  51. // 去除其它默认选项
  52. if (isset($data['type']) && $data['type'] > 0) {
  53. $map = [['uuid', '=', $this->uuid], ['code', '<>', $data['code']]];
  54. DataUserAddress::mk()->where($map)->update(['type' => 0]);
  55. }
  56. $this->success('地址保存成功!', $this->getAddress($data['code']));
  57. }
  58. /**
  59. * 获取收货地址
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function get()
  65. {
  66. $query = DataUserAddress::mQuery()->withoutField('deleted');
  67. $query->equal('code')->where(['uuid' => $this->uuid, 'deleted' => 0]);
  68. $result = $query->order('type desc,id desc')->page(false, false, false, 15);
  69. $this->success('获取地址数据!', $result);
  70. }
  71. /**
  72. * 修改地址状态
  73. */
  74. public function state()
  75. {
  76. $data = $this->_vali([
  77. 'uuid.value' => $this->uuid,
  78. 'type.in:0,1' => '地址状态不在范围!',
  79. 'type.require' => '地址状态不能为空!',
  80. 'code.require' => '地址编号不能为空!',
  81. ]);
  82. // 检查地址是否存在
  83. $map = ['uuid' => $data['uuid'], 'code' => $data['code']];
  84. if (DataUserAddress::mk()->where($map)->count() < 1) {
  85. $this->error('修改的地址不存在!');
  86. }
  87. // 更新默认地址状态
  88. $data['type'] = intval($data['type']);
  89. DataUserAddress::mk()->where($map)->update(['type' => $data['type']]);
  90. // 去除其它默认选项
  91. if ($data['type'] > 0) {
  92. $map = [['uuid', '=', $this->uuid], ['code', '<>', $data['code']]];
  93. DataUserAddress::mk()->where($map)->update(['type' => 0]);
  94. }
  95. $this->success('默认设置成功!', $this->getAddress($data['code']));
  96. }
  97. /**
  98. * 删除收货地址
  99. */
  100. public function remove()
  101. {
  102. $map = $this->_vali([
  103. 'uuid.value' => $this->uuid,
  104. 'code.require' => '地址不能为空!',
  105. ]);
  106. $item = DataUserAddress::mk()->where($map)->findOrEmpty();
  107. if ($item->isEmpty()) $this->error('需要删除的地址不存在!');
  108. if ($item->save(['deleted' => 1]) !== false) {
  109. $this->success('删除地址成功!');
  110. } else {
  111. $this->error('删除地址失败!');
  112. }
  113. }
  114. /**
  115. * 获取指定的地址
  116. * @param string $code
  117. * @return null|array
  118. */
  119. private function getAddress(string $code): array
  120. {
  121. $map = ['code' => $code, 'uuid' => $this->uuid, 'deleted' => 0];
  122. return DataUserAddress::mk()->withoutField('deleted')->where($map)->findOrEmpty()->toArray();
  123. }
  124. }