Address.php 5.2 KB

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