Order.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\api\controller\v1\taxiUser;
  3. use app\api\controller\ApiController;
  4. use app\common\model\Taxi;
  5. use app\common\model\TaxiServiceCategory;
  6. use app\common\model\Users;
  7. use app\http\IResponse;
  8. use EasyWeChat\Factory;
  9. use Lettered\Support\Upload;
  10. use think\App;
  11. use app\api\service\JWTAuth as IAuth;
  12. use think\Db;
  13. use think\Exception;
  14. class Order extends ApiController
  15. {
  16. protected $model;
  17. public function __construct(App $app = null, IAuth $auth)
  18. {
  19. parent::__construct($app, $auth);
  20. $this->model = new \app\common\model\TaxiOrder();
  21. $this->taxiUserModel = new \app\api\model\taxi\User();
  22. }
  23. /**
  24. * 接单大厅订单列表
  25. * @return mixed|\think\response\Json
  26. * @throws \Lettered\Support\Exceptions\FailedException
  27. */
  28. public function index()
  29. {
  30. // 1. 传入用户位置
  31. $param = $this->request->param();
  32. // 数据校验
  33. $valid = $this->validate($param, [
  34. 'lng' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  35. 'lat' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  36. 'page' => 'require',
  37. ],[
  38. 'lng.require' => '缺少经度参数',
  39. 'lng.regex' => '经度参数有误',
  40. 'lat.require' => '缺少维度参数',
  41. 'lat.regex' => '维度参数有误'
  42. ]);
  43. // 错误
  44. if (true !== $valid){
  45. return IResponse::failure($valid);
  46. }
  47. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  48. $taxiUser = $this->auth->guard('taxi_user')->user();
  49. if(!$taxiUser){
  50. return IResponse::failure('用户不存在,或已被冻结');
  51. }
  52. $categoryIds = Taxi::where(['taxi_user_id'=> $taxiUser['id']])->column('category_id');
  53. $categoryIds = array_unique($categoryIds);
  54. // 经纬度升序
  55. $lists = $this->model
  56. ->fieldRaw("* , TRUNCATE(( 6371 * acos (
  57. cos ( radians(".$param['lat'].") )
  58. * cos( radians( latitude ) )
  59. * cos( radians( longitude ) - radians(".$param['lng'].") )
  60. + sin ( radians(".$param['lat'].") )
  61. * sin( radians( latitude ) )
  62. )
  63. ), 2) AS distance")
  64. ->having('distance < 10')
  65. ->where(['status' => 2])
  66. ->where(function($query) use($categoryIds){
  67. if($categoryIds){
  68. $query->whereIn('category_id', $categoryIds);
  69. }
  70. })
  71. ->order(['distance' => 'ASC'])
  72. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  73. ->select();
  74. return IResponse::success($lists,'获取成功');
  75. }
  76. /**
  77. * 我的订单
  78. * @return mixed
  79. * @throws \Lettered\Support\Exceptions\FailedException
  80. */
  81. public function myOrder()
  82. {
  83. // 1. 传入用户位置
  84. $param = $this->request->param();
  85. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  86. $taxiUser = $this->auth->guard('taxi_user')->user();
  87. if(!$taxiUser){
  88. return IResponse::failure('用户不存在,或已被冻结');
  89. }
  90. // 经纬度升序
  91. $lists = $this->model->with(['paylog','user','taxi','taxiUser'])
  92. ->where(['taxi_uid' => $taxiUser['id']])
  93. ->order(['created_at' => 'desc'])
  94. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  95. ->select();
  96. return IResponse::success($lists,'获取成功');
  97. }
  98. /**
  99. * 等待服务订单
  100. * @return mixed|\think\response\Json
  101. * @throws \Lettered\Support\Exceptions\FailedException
  102. */
  103. public function waitOrder()
  104. {
  105. $taxiUser = $this->auth->guard('taxi_user')->user();
  106. if(!$taxiUser){
  107. return IResponse::failure('用户不存在,或已被冻结');
  108. }
  109. $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser'])
  110. ->where(['taxi_uid' => $taxiUser['id']])
  111. ->whereIn('status',[2,3])
  112. ->order('created_at','desc')
  113. ->find();
  114. if($info){
  115. return IResponse::success(!is_null($info)? $info : [],'获取成功');
  116. }else{
  117. return IResponse::failure('获取失败');
  118. }
  119. }
  120. /**
  121. * 接单
  122. * @return mixed
  123. * @throws \Lettered\Support\Exceptions\FailedException
  124. */
  125. public function receive()
  126. {
  127. $param = $this->request->param();
  128. $id = isset($param['id'])? $param['id'] : 0;
  129. $taxiUser = $this->auth->guard('taxi_user')->user();
  130. if(!$taxiUser){
  131. return IResponse::failure('用户不存在,或已被冻结');
  132. }
  133. $info = model('common/TaxiOrder')
  134. ->where(['taxi_uid' => $taxiUser['id'],'id'=> $id, 'status'=>3])
  135. ->order('created_at','desc')
  136. ->find();
  137. if(empty($info)){
  138. return IResponse::failure('订单不存在,或已处理');
  139. }
  140. }
  141. /**
  142. * 已送达
  143. * @return mixed
  144. * @throws \Lettered\Support\Exceptions\FailedException
  145. */
  146. public function delivered()
  147. {
  148. $param = $this->request->param();
  149. $id = isset($param['id'])? $param['id'] : 0;
  150. $taxiUser = $this->auth->guard('taxi_user')->user();
  151. if(!$taxiUser){
  152. return IResponse::failure('用户不存在,或已被冻结');
  153. }
  154. $info = model('common/TaxiOrder')
  155. ->where(['taxi_uid' => $taxiUser['id'],'id'=> $id, 'status'=>3])
  156. ->order('created_at','desc')
  157. ->find();
  158. if(empty($info)){
  159. return IResponse::failure('订单不存在,或已处理');
  160. }
  161. }
  162. }