Address.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. /**
  5. * 首页接口
  6. */
  7. class Address extends Api
  8. {
  9. protected $noNeedLogin = [''];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 获取地址列表
  13. * @return void
  14. * @throws \think\exception\DbException
  15. */
  16. public function addressList()
  17. {
  18. if (!$this->request->isPost()) {
  19. $this->error('请求方式异常');
  20. }
  21. $uid = $this->auth->id;
  22. $page = input('page/d',1);
  23. $limit = 10;
  24. $list = model('app\api\model\service\Address')
  25. ->where('user_id', $uid)
  26. ->field('id,user_id,province,city,district,area,address,lng,lat,state,name,mobile,sex')
  27. ->order('state desc,id desc')
  28. ->page($page)
  29. ->limit($limit)
  30. ->select();
  31. $this->success('返回成功', $list);
  32. }
  33. /**
  34. * 地址操作
  35. * @return void
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public function handleAddress()
  41. {
  42. $uid = $this->auth->id;
  43. if (!$this->request->isPost()) {
  44. $this->error('请求方式异常');
  45. }
  46. $post = input('post.','','trim,strip_tags');
  47. if($post['type'] == 'add')
  48. {
  49. $post['user_id'] = $uid;
  50. $address = new \app\api\model\service\Address($post);
  51. $address->allowField(true)->save();
  52. if($post['state'] == 1)
  53. {
  54. model('app\api\model\service\Address')->where(['state'=>1,'id'=>['<>',$address->id],'user_id'=>$uid])->update(['state'=>0]);
  55. }
  56. $this->success('地址添加成功');
  57. }elseif($post['type'] == 'edit')
  58. {
  59. $info = model('app\api\model\service\Address')->where(['id'=>$post['id'],'user_id'=>$uid])->find();
  60. !$info && $this->error('地址信息异常');
  61. $address = new \app\api\model\service\Address();
  62. $address->allowField(true)->save($post,['id'=>$post['id']]);
  63. if($info['state'] == 0 && $post['state'] == 1)
  64. {
  65. model('app\api\model\service\Address')->where(['state'=>1,'id'=>['<>',$post['id']],'user_id'=>$uid])->update(['state'=>0]);
  66. }
  67. $this->success('信息已更新');
  68. }elseif ($post['type'] == 'del')
  69. {
  70. $info = model('app\api\model\service\Address')->where(['id'=>$post['id'],'user_id'=>$uid])->find();
  71. !$info && $this->error('地址信息异常');
  72. \app\api\model\service\Address::destroy($post['id']);
  73. $this->success('地址信息已删除');
  74. }
  75. $this->error('接口异常');
  76. }
  77. /**
  78. * 地址详情
  79. * @return void
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function info()
  85. {
  86. if (!$this->request->isPost()) {
  87. $this->error('请求方式异常');
  88. }
  89. $id = input('id/d','');
  90. $info = model('app\api\model\service\Address')->where(['id'=>$id])->find();
  91. !$info && $this->error('地址信息异常');
  92. $this->success('信息返回成功',$info);
  93. }
  94. }