Order.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace app\api\controller\v1\taxiUser;
  3. use app\api\controller\ApiController;
  4. use app\api\controller\v1\Wechat;
  5. use app\common\model\Taxi;
  6. use app\http\IResponse;
  7. use think\App;
  8. use app\api\service\JWTAuth as IAuth;
  9. use think\Db;
  10. class Order extends ApiController
  11. {
  12. protected $model;
  13. public function __construct(App $app = null, IAuth $auth)
  14. {
  15. parent::__construct($app, $auth);
  16. $this->model = new \app\common\model\TaxiOrder();
  17. $this->taxiUserModel = new \app\api\model\taxi\User();
  18. }
  19. /**
  20. * 接单大厅订单列表
  21. * @return mixed|\think\response\Json
  22. * @throws \Lettered\Support\Exceptions\FailedException
  23. */
  24. public function index()
  25. {
  26. // 1. 传入用户位置
  27. $param = $this->request->param();
  28. // 数据校验
  29. $valid = $this->validate($param, [
  30. 'lng' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  31. 'lat' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  32. 'page' => 'require',
  33. ], [
  34. 'lng.require' => '缺少经度参数',
  35. 'lng.regex' => '经度参数有误',
  36. 'lat.require' => '缺少维度参数',
  37. 'lat.regex' => '维度参数有误'
  38. ]);
  39. // 错误
  40. if (true !== $valid) {
  41. return IResponse::failure($valid);
  42. }
  43. $limit = isset($param['pageSize']) ? $param['pageSize'] : 20;
  44. $taxiUser = $this->auth->guard('taxi_user')->user();
  45. if (!$taxiUser) {
  46. return IResponse::failure('用户不存在,或已被冻结');
  47. }
  48. $categoryIds = Taxi::where(['taxi_user_id' => $taxiUser['id']])->column('category_id');
  49. $categoryIds = array_unique($categoryIds);
  50. // 经纬度升序
  51. $lists = $this->model
  52. ->fieldRaw("* , TRUNCATE(( 6371 * acos (
  53. cos ( radians(" . $param['lat'] . ") )
  54. * cos( radians( lat ) )
  55. * cos( radians( lng ) - radians(" . $param['lng'] . ") )
  56. + sin ( radians(" . $param['lat'] . ") )
  57. * sin( radians( lat ) )
  58. )
  59. ), 2) AS distance")
  60. ->having('distance < 10')
  61. ->where(['status' => 2])
  62. ->where('lat', '>', 0)
  63. ->where('lng', '>', 0)
  64. ->where(function ($query) use ($categoryIds) {
  65. if ($categoryIds) {
  66. $query->whereIn('category_id', $categoryIds);
  67. }
  68. })
  69. ->order(['distance' => 'ASC'])
  70. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  71. ->select();
  72. $order = isset($lists[0])? $lists[0] : [];
  73. $lastOrderTime = isset($order['created_at'])? $order['created_at']['val'] : time();
  74. $hasNewOrder = $order && $lastOrderTime != $taxiUser->last_order_time? true : false;
  75. $taxiUser->last_order_time = $hasNewOrder? $lastOrderTime : $taxiUser->last_order_time;
  76. $taxiUser->save();
  77. $count = $lists->count();
  78. return IResponse::success(['list' => $lists, 'total' => $count, 'hasNewOrder'=> $hasNewOrder], '获取成功');
  79. }
  80. /**
  81. * 我的订单
  82. * @return mixed
  83. * @throws \Lettered\Support\Exceptions\FailedException
  84. */
  85. public function myOrder()
  86. {
  87. // 1. 传入用户位置
  88. $param = $this->request->param();
  89. $limit = isset($param['pageSize']) ? $param['pageSize'] : 20;
  90. $taxiUser = $this->auth->guard('taxi_user')->user();
  91. if (!$taxiUser) {
  92. return IResponse::failure('用户不存在,或已被冻结');
  93. }
  94. // 经纬度升序
  95. $lists = $this->model->with(['paylog', 'user', 'taxi', 'taxiUser'])
  96. ->where(['taxi_uid' => $taxiUser['id']])
  97. ->order(['created_at' => 'desc'])
  98. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  99. ->select();
  100. return IResponse::success($lists, '获取成功');
  101. }
  102. /**
  103. * 等待服务订单
  104. * @return mixed|\think\response\Json
  105. * @throws \Lettered\Support\Exceptions\FailedException
  106. */
  107. public function waitOrder()
  108. {
  109. $taxiUser = $this->auth->guard('taxi_user')->user();
  110. if (!$taxiUser) {
  111. return IResponse::failure('用户不存在,或已被冻结');
  112. }
  113. $info = model('common/TaxiOrder')->with(['paylog', 'user', 'taxi', 'taxiUser'])
  114. ->where(['taxi_uid' => $taxiUser['id']])
  115. ->whereIn('status', [2, 3])
  116. ->order('created_at', 'desc')
  117. ->find();
  118. if ($info) {
  119. return IResponse::success(!is_null($info) ? $info : [], '获取成功');
  120. } else {
  121. return IResponse::failure('获取失败');
  122. }
  123. }
  124. /**
  125. * 接单
  126. * @return mixed
  127. * @throws \Lettered\Support\Exceptions\FailedException
  128. */
  129. public function receive(App $app = null)
  130. {
  131. $param = $this->request->param();
  132. $id = isset($param['id']) ? $param['id'] : 0;
  133. $taxiUser = $this->auth->guard('taxi_user')->user();
  134. if (!$taxiUser) {
  135. return IResponse::failure('用户不存在,或已被冻结');
  136. }
  137. $info = model('common/TaxiOrder')
  138. ->where(['id' => $id, 'status' => 2])
  139. ->where('deleted_at', '<=', 0)
  140. ->order('created_at', 'desc')
  141. ->find();
  142. if (empty($info)) {
  143. return IResponse::failure('订单不存在,或已处理');
  144. }
  145. if (empty($info['category_id'])) {
  146. return IResponse::failure('车型参数错误或不匹配');
  147. }
  148. $taxi = Taxi::where(['taxi_user_id' => $taxiUser['id'], 'category_id' => $info['category_id']])
  149. ->where('status', '>', 0)
  150. ->order('status', 'asc')
  151. ->order('created_at', 'desc')
  152. ->find();
  153. if (empty($taxi)) {
  154. return IResponse::failure('车型参数错误或您的车型不匹配');
  155. }
  156. if ($taxi['status'] != 1) {
  157. return IResponse::failure('您的车辆当前不可再接单,请先完成订单或联系客服');
  158. }
  159. // 写入数据
  160. Db::startTrans();
  161. $info['status'] = 3;
  162. $info['served'] = 1;
  163. $info['taxi_id'] = $taxi['id'];
  164. $info['taxi_uid'] = $taxiUser['id'];
  165. $info['settle_price'] = $info['price'];
  166. if (!$info->save()) {
  167. Db::rollback();
  168. return IResponse::failure('接单失败');
  169. }
  170. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $taxi['id']])->update(['status' => 2])) {
  171. Db::rollback();
  172. return IResponse::failure('接单失败');
  173. }
  174. push_socket_data('motor',[
  175. 'id' => $info['id'],
  176. 'msg' => '有新的摩的订单等待处理,点击前往!'
  177. ]);
  178. Db::commit();
  179. // 模板消息
  180. $tplTitle = "人人接 - 摩的服务";
  181. $thing7 = '电话请保持通畅,师傅正在赶往路上,请稍候';
  182. $user = model('common/Users')->where(['id'=> $info['user_id']])->find();
  183. if($user && $user['open_id']){
  184. $this->tpl1['touser'] = $user['open_id'];
  185. $this->tpl1['data'] = [
  186. 'character_string1' => [
  187. 'value' => $info['order_no'],
  188. ],
  189. 'thing3' => [
  190. 'value' => $tplTitle,
  191. ],
  192. 'amount4' => [
  193. 'value' => $info['price'],
  194. ],
  195. 'date2' => [
  196. 'value' => date("Y/m/d H:i:s"),
  197. ],
  198. 'thing7' => [
  199. 'value' => $thing7,
  200. ]
  201. ];
  202. }
  203. $wechat = new Wechat($app,$this->auth);
  204. $wechat->subscribe_message->send($this->tpl1);
  205. return IResponse::success('接单成功,请尽快前往客户所在地接驾');
  206. }
  207. /**
  208. * 已送达
  209. * @return mixed
  210. * @throws \Lettered\Support\Exceptions\FailedException
  211. */
  212. public function delivered()
  213. {
  214. $param = $this->request->param();
  215. $id = isset($param['id']) ? $param['id'] : 0;
  216. $taxiUser = $this->auth->guard('taxi_user')->user();
  217. if (!$taxiUser) {
  218. return IResponse::failure('用户不存在,或已被冻结');
  219. }
  220. if($taxiUser['user_id']<=0){
  221. return IResponse::failure('账户未绑定用户');
  222. }
  223. $info = model('common/TaxiOrder')
  224. ->where(['taxi_uid' => $taxiUser['id'], 'id' => $id, 'status' => 3])
  225. ->order('created_at', 'desc')
  226. ->find();
  227. if (empty($info)) {
  228. return IResponse::failure('订单不存在,或已处理');
  229. }
  230. if ($info['taxi_uid'] <= 0 || $info['taxi_id'] <= 0) {
  231. return IResponse::failure('参数错误');
  232. }
  233. // 写入数据
  234. Db::startTrans();
  235. $info['status'] = 4;
  236. if (!$info->save()) {
  237. Db::rollback();
  238. return IResponse::failure('确认失败');
  239. }
  240. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $info['taxi_id']])->update(['status' => 1])) {
  241. Db::rollback();
  242. return IResponse::failure('确认失败');
  243. }
  244. // 到账
  245. if($info['settle_price']){
  246. if(!model('common/Users')->changePartnership(
  247. $taxiUser['user_id'],
  248. $info['settle_price'],
  249. "订单完成,金额到账【" . $info['settle_price'] . "】",
  250. 30,
  251. true
  252. )){
  253. Db::rollback();
  254. return IResponse::failure('确认失败');
  255. }
  256. }
  257. // 订单结算给摩的代理
  258. model('\app\api\model\taxi\Award')->send($info);
  259. Db::commit();
  260. return IResponse::success('确认成功');
  261. }
  262. }