UserAddressController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\UserApi;
  4. use app\common\model\Area;
  5. use app\common\model\UserAddress;
  6. /**
  7. * 用户端地址模块
  8. */
  9. class UserAddressController extends UserApi
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = '*';
  13. /**
  14. * 列表
  15. * @ApiReturnParams (name=id,description=id)
  16. * @ApiReturnParams (name=name,description=姓名)
  17. * @ApiReturnParams (name=mobile,description=手机号码)
  18. * @ApiReturnParams (name=address,description=详细地址)
  19. * @ApiReturnParams (name=city,description=所在城市)
  20. * @ApiReturnParams (name=city_detail,description=所在城市名)
  21. * @ApiReturnParams (name=longitude,description=经度)
  22. * @ApiReturnParams (name=latitude,description=纬度)
  23. * @ApiReturnParams (name=location,description=所在地区)
  24. */
  25. public function list(){
  26. $list=$this->auth
  27. ->getUser()
  28. ->address()
  29. ->order('id','desc')
  30. ->select();
  31. $this->success('',$list);
  32. }
  33. /**
  34. * 新增或修改地址
  35. * @ApiParams (name=id,description="id,修改的时候必须")
  36. * @ApiParams (name=name,description="姓名")
  37. * @ApiParams (name=mobile,description="手机号")
  38. * @ApiParams (name=city,description="区县名字")
  39. * @ApiParams (name=address,description="详细地址")
  40. * @ApiParams (name=is_default,description="1和0,是否默认")
  41. */
  42. public function edit(){
  43. $user=$this->auth->getUser();
  44. $data=input();
  45. $this->validate($data,[
  46. 'name'=>'require|max:5',
  47. 'mobile'=>'require|number',
  48. 'city'=>'require',
  49. 'address'=>'require',
  50. //'location'=>'require',
  51. //'longitude'=>'require|float',
  52. //'latitude'=>'require|float',
  53. 'is_default'=>'require|in:0,1',
  54. ]);
  55. $city=Area::area()->where('name|shortname',$data['city'])->find();
  56. if(!$city){
  57. $this->error('区县不存在');
  58. }
  59. if(!empty($data['id'])){
  60. $address=$user->address()->findOrFail($data['id']);
  61. $address->save($data);
  62. }else{
  63. $user->address()->save($data);
  64. }
  65. $this->success();
  66. }
  67. /**
  68. * 地址详情
  69. * @ApiParams (name=id,description=id)
  70. * @ApiReturnParams (name=name,description=姓名)
  71. * @ApiReturnParams (name=mobile,description=手机号码)
  72. * @ApiReturnParams (name=address,description=详细地址)
  73. * @ApiReturnParams (name=city,description=所在城市)
  74. * @ApiReturnParams (name=city_detail,description=所在城市名)
  75. * @ApiReturnParams (name=longitude,description=经度)
  76. * @ApiReturnParams (name=latitude,description=纬度)
  77. * @ApiReturnParams (name=location,description=所在地区)
  78. *
  79. */
  80. public function detail(){
  81. $user=$this->auth->getUser();
  82. $data=input();
  83. $this->validate($data,[
  84. 'id'=>'require',
  85. ]);
  86. $addr=$user->address()->findOrFail($data['id']);
  87. $this->success('',$addr);
  88. }
  89. /**
  90. * 删除地址
  91. * @ApiParams (name=id,description=id)
  92. */
  93. public function delete(){
  94. $user=$this->auth->getUser();
  95. $data=input();
  96. $this->validate($data,[
  97. 'id'=>'require',
  98. ]);
  99. $user->address()->delete($data['id']);
  100. $this->success();
  101. }
  102. }