Order.php 10 KB

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