Taxi.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\api\model\taxi\MotorRecord;
  5. use app\api\service\SmsCode;
  6. use app\common\model\TaxiUsersLevel;
  7. use app\common\validate\IDMustBePositiveInt;
  8. use app\http\IResponse;
  9. use EasyWeChat\Factory;
  10. use think\Db;
  11. use think\facade\Cache;
  12. class Taxi extends ApiController
  13. {
  14. /**
  15. * 获取车辆
  16. *
  17. * @url GET /taxi/nearby?page=1
  18. * @author 许祖兴 < zuxing.xu@lettered.cn>
  19. * @date 2020/7/3 16:29
  20. *
  21. * @return \think\response\Json
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function getNearby()
  27. {
  28. $param = $this->request->param();
  29. $param['category_id'] = $this->request->param('category_id', 0);
  30. $limit = 10;
  31. $map['status'] = 1;
  32. if ($param['category_id']) {
  33. $map['category_id'] = $param['category_id'];
  34. }
  35. //
  36. $taxi = model('common/Taxi')->with(['user'])
  37. ->where($map)
  38. ->select();
  39. return $this->ApiJson(0,'获取成功', $taxi);
  40. }
  41. /**
  42. * 用车信息
  43. *
  44. * @url GET /taxi/:id
  45. * @author 许祖兴 < zuxing.xu@lettered.cn>
  46. * @date 2020/7/1 10:37
  47. *
  48. * @param int $id 用车ID
  49. * @return \think\response\Json
  50. * @throws \Lettered\Support\Exceptions\EvidentException
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. public function getTaxiByID($id)
  56. {
  57. (new IDMustBePositiveInt())->valid();
  58. $taxi = model('common/Taxi')->with(['user'])->hidden(['user.id_card'])
  59. ->find($id);
  60. if (!$taxi){
  61. return $this->ApiJson(-1, '不存在用车编号');
  62. }
  63. return $this->ApiJson(0, '获取用车信息成功', $taxi);
  64. }
  65. /**
  66. * 用车
  67. */
  68. public function callTaxi()
  69. {
  70. $params = $this->request->param();
  71. // 参数校验
  72. $valid = $this->validate($params, [
  73. 'taxi_id|关联车辆' => 'require',
  74. 'mobile|联系方式' => 'require',
  75. 'count|乘车人数' => 'require',
  76. 'depart|起始位置' => 'require',
  77. 'arrive|送达位置' => 'require',
  78. 'km|公里数' => 'require',
  79. 'price|价格' => 'require',
  80. ]);
  81. // 错误返回
  82. if(true !== $valid){
  83. return $this->ApiJson(-1, $valid);
  84. };
  85. $user = $this->auth->user();
  86. // 统计订单
  87. $count = model('common/TaxiOrder')->where('status','>=','2')->where(['is_free' => '1','user_id' => $user['id']])->count('id');
  88. // 创建订单
  89. $params['order_no'] = get_order_no();
  90. $params['user_id'] = $user['id'];
  91. $order = model('common/TaxiOrder')::create($params,true);
  92. //
  93. if ($order){
  94. // 创建对应支付记录
  95. $trade_no = get_order_no();
  96. $logID = model('common/OrderPaylog')->storeBy([
  97. 'out_trade_no' => $trade_no,
  98. 'total_price' => $params['price'],
  99. 'order_idx' => $order['id'],
  100. 'ascription' => 'motor' // 归属订单
  101. ]);
  102. // 免单优惠 20201201 添加条件 仅限xx 公里以下 不包含 xx 公里
  103. if ($count < sys_config('taxi_order_free_num','store') && $params['km'] < sys_config('taxi_order_free_km','store')){
  104. model('common/OrderPaylog')->updateBy($logID,[
  105. 'pay_price' => $params['price'],
  106. 'is_pay' => 1
  107. ]);
  108. model('common/TaxiOrder')->updateBy($order['id'], [
  109. 'is_free' => 1, // 免单
  110. 'status' => 2
  111. ]);
  112. // 后台推送
  113. // push_socket_data('motor',[
  114. // 'id' => $order['id'],
  115. // 'msg' => '有新的(免费)摩的订单等待处理哟,点击前往!'
  116. // ]);
  117. return $this->ApiJson(0,"本单免费,等待师傅接驾", [
  118. 'type' => 'free',
  119. 'success' => "ok!"
  120. ]);
  121. }
  122. // 返回支付单号
  123. return $this->ApiJson(0,'订单提交成功', [
  124. 'type' => 'wx',
  125. 'success' => "ok!",
  126. 'trade_no' => $trade_no
  127. ]);
  128. }
  129. return $this->ApiJson(-1,'发生异常,请骚后重试...');
  130. }
  131. /**
  132. * 用车,新的地图下单方式
  133. */
  134. public function newCallTaxi()
  135. {
  136. $datas = $this->request->param();
  137. // 参数校验
  138. $valid = $this->validate($datas, [
  139. 'mobile|联系方式' => 'require',
  140. 'count|乘车人数' => 'require',
  141. 'depart|起始位置' => 'require',
  142. 'arrive|送达位置' => 'require',
  143. 'km|公里数' => 'require',
  144. 'price|价格' => 'require',
  145. ]);
  146. // 错误返回
  147. if(true !== $valid){
  148. return $this->ApiJson(-1, $valid);
  149. };
  150. $user = $this->auth->user();
  151. // 统计订单
  152. $count = model('common/TaxiOrder')->where('status','>=','2')->where(['is_free' => '1','user_id' => $user['id']])->count('id');
  153. // 创建订单
  154. $driver = sys_config('', 'driver');
  155. $divide = isset($driver['platform_divide'])? $driver['platform_divide'] : 0;
  156. $params = [
  157. 'user_id'=> $user['id'],
  158. 'category_id'=> isset($datas['category_id'])? $datas['category_id'] : 0,
  159. 'order_no'=> get_order_no(),
  160. 'price'=> isset($datas['price'])? $datas['price'] : 0.00,
  161. 'settle_price' => $divide>0 && $divide<=100? round($datas['price']*(100-$divide)/100, 2) : $datas['price'],
  162. 'count'=> isset($datas['count'])? $datas['count'] : 0,
  163. 'mobile'=> isset($datas['mobile'])? $datas['mobile'] : '',
  164. 'depart'=> isset($datas['depart'])? $datas['depart'] : '',
  165. 'arrive'=> isset($datas['arrive'])? $datas['arrive'] : '',
  166. 'depart_lat'=> isset($datas['depart_lat'])? $datas['depart_lat'] : 0,
  167. 'depart_lng'=> isset($datas['depart_lng'])? $datas['depart_lng'] : 0,
  168. 'lat'=> isset($datas['lat'])? $datas['lat'] : 0,
  169. 'lng'=> isset($datas['lng'])? $datas['lng'] : 0,
  170. 'km'=> isset($datas['km'])? $datas['km'] : '',
  171. ];
  172. // 验证是否有未支付订单,有则直接更新订单为新订单
  173. $hasOrderId = model('common/TaxiOrder')->where(['user_id'=> $user['id'],'status'=> 1])->order('created_at','desc')->value('id');
  174. if($hasOrderId){
  175. model('common/TaxiOrder')->where(['id'=> $hasOrderId])->update($params);
  176. $order = model('common/TaxiOrder')->where(['id'=> $hasOrderId])->findOrFail();
  177. }else{
  178. $order = model('common/TaxiOrder')::create($params,true);
  179. }
  180. // 订单信息验证处理
  181. if ($order && $order['id']){
  182. // 创建对应支付记录
  183. $trade_no = get_order_no();
  184. $logID = model('common/OrderPaylog')->storeBy([
  185. 'out_trade_no' => $trade_no,
  186. 'total_price' => $params['price'],
  187. 'order_idx' => $order['id'],
  188. 'ascription' => 'motor' // 归属订单
  189. ]);
  190. // 免单优惠 20201201 添加条件 仅限xx 公里以下 不包含 xx 公里
  191. if ($count < sys_config('taxi_order_free_num','store') && $params['km'] < sys_config('taxi_order_free_km','store')){
  192. model('common/OrderPaylog')->updateBy($logID,[
  193. 'pay_price' => $params['price'],
  194. 'is_pay' => 1
  195. ]);
  196. model('common/TaxiOrder')->updateBy($order['id'], [
  197. 'is_free' => 1, // 免单
  198. 'status' => 2
  199. ]);
  200. // 后台推送
  201. // push_socket_data('motor',[
  202. // 'id' => $order['id'],
  203. // 'msg' => '有新的(免费)摩的订单等待处理哟,点击前往!'
  204. // ]);
  205. return $this->ApiJson(0,"本单免费,等待师傅接驾", [
  206. 'type' => 'free',
  207. 'success' => "ok!"
  208. ]);
  209. }
  210. // 返回支付单号
  211. return $this->ApiJson(0,'订单提交成功', [
  212. 'type' => 'wx',
  213. 'success' => "ok!",
  214. 'order_id' => $order['id'],
  215. 'trade_no' => $trade_no
  216. ]);
  217. }
  218. return $this->ApiJson(-1,'发生异常,请骚后重试...');
  219. }
  220. /**
  221. * 订单详情
  222. *
  223. * @author 许祖兴 < zuxing.xu@lettered.cn>
  224. * @date 2020/7/6 11:09
  225. *
  226. * @return \think\response\Json
  227. * @throws \Lettered\Support\Exceptions\FailedException
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\ModelNotFoundException
  230. * @throws \think\exception\DbException
  231. */
  232. public function taxiOrder()
  233. {
  234. $param = $this->request->param();
  235. // 数据校验
  236. $valid = $this->validate($param, [
  237. 'order_id' => 'require',
  238. ]);
  239. // 错误
  240. if (true !== $valid) {
  241. return $this->ApiJson(-1, $valid);
  242. }
  243. $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser'])
  244. ->where(['user_id' => $this->auth->user()['id'], 'id'=> $param['order_id']])
  245. ->find();
  246. return $this->ApiJson(0,'获取成功', $info);
  247. }
  248. /**
  249. * 当前进行中订单详情
  250. *
  251. * @author 许祖兴 < zuxing.xu@lettered.cn>
  252. * @date 2020/7/6 11:09
  253. *
  254. * @return \think\response\Json
  255. * @throws \Lettered\Support\Exceptions\FailedException
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. * @throws \think\exception\DbException
  259. */
  260. public function waitOrder()
  261. {
  262. $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser'])
  263. ->where(['user_id' => $this->auth->user()['id']])
  264. ->whereIn('status',[2,3])
  265. ->order('created_at','desc')
  266. ->find();
  267. if($info){
  268. if($info['taxi_user']){
  269. $info['taxi_user']['level_name'] = TaxiUsersLevel::where(['level'=>$info['taxi_user']['level']])->value('title');
  270. }
  271. }
  272. return $this->ApiJson(0,'获取成功', !is_null($info)? $info : []);
  273. }
  274. /**
  275. * 订单
  276. *
  277. * @author 许祖兴 < zuxing.xu@lettered.cn>
  278. * @date 2020/7/6 11:09
  279. *
  280. * @return \think\response\Json
  281. * @throws \Lettered\Support\Exceptions\FailedException
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. * @throws \think\exception\DbException
  285. */
  286. public function order()
  287. {
  288. $param = $this->request->param();
  289. $limit = 10;
  290. // 数据校验
  291. $valid = $this->validate($param, [
  292. 'page' => 'require',
  293. ]);
  294. // 错误
  295. if (true !== $valid) {
  296. return $this->ApiJson(-1, $valid);
  297. }
  298. $orders = model('common/TaxiOrder')
  299. ->with(['paylog'])
  300. ->where(['user_id' => $this->auth->user()['id']])
  301. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  302. ->order(['id' => 'desc'])
  303. ->select();
  304. return $this->ApiJson(0,'获取成功', $orders);
  305. }
  306. /**
  307. * @desc 取消订单
  308. * @return \think\response\Json
  309. * @throws \think\db\exception\DataNotFoundException
  310. * @throws \think\db\exception\ModelNotFoundException
  311. * @throws \think\exception\DbException
  312. * @author weichuanbao<654745815@qq.com>
  313. * @date 2021/12/2 0002
  314. */
  315. public function cancelOrder()
  316. {
  317. $param = $this->request->param();
  318. // 数据校验
  319. $valid = $this->validate($param, [
  320. 'id' => 'require|number',
  321. ]);
  322. // 错误
  323. if (true !== $valid) {
  324. return $this->ApiJson(-1, $valid);
  325. }
  326. $row = model('common/TaxiOrder')->with(['paylog'])
  327. ->field('id,taxi_uid,taxi_id,status,created_at')
  328. ->whereNotIn('status', [0,5])
  329. ->find($param['id']);
  330. $user = $this->auth->user();
  331. if ($row) {
  332. if (time() - $row['created_at']['val'] < (60 * 10)) {
  333. // return $this->ApiJson(-1, '10分钟内无法取消订单');
  334. }
  335. $taxiUid = isset($row['taxi_uid'])? intval($row['taxi_uid']) : 0;
  336. if ($row['status'] == 2 || $row['status'] == 3) {
  337. $config = Cache::get('system_config');
  338. $total_price = $row->paylog['total_price'];
  339. // 如果平台已指派司机,则扣除部分金额
  340. $cost_price = 0;
  341. $total = $total_price;
  342. $createTime = isset($row['created_at'])? $row['created_at'] : 0;
  343. $cancelTime = isset($config['store']['store_cancel_time'])? $config['store']['store_cancel_time'] : 0;
  344. $taxiUser = model('common/TaxiUser')->where(['id'=> $taxiUid])->find();
  345. $taxiUserId = isset($taxiUser['user_id'])? $taxiUser['user_id'] : 0;
  346. if ($row['status'] == 3 ) {
  347. // 免费取消时间内不扣除
  348. if($cancelTime<=0 || $createTime < (time() - $cancelTime*60)){
  349. $total_price = round($total_price * (1 - $config['store']['store_cancel_order']/100), 2);
  350. $cost_price = $total-$total_price;
  351. }
  352. }
  353. if ($row->paylog['pay_type'] == 'balance') {
  354. model('common/Users')->changeBalance(
  355. $user['id'],
  356. $total_price,
  357. "取消成功,取消金额【" . $total_price . "】",
  358. true
  359. );
  360. // 退款扣除金额直接给司机
  361. if($taxiUserId && $cost_price>0){
  362. model('common/Users')->changePartnership(
  363. $taxiUserId,
  364. $cost_price,
  365. "取消订单,扣除金额到账【" . $cost_price . "】",
  366. 30,
  367. true
  368. );
  369. }
  370. } else if ($row->paylog['pay_type'] == 'property') {
  371. model('common/Users')->changeProperty(
  372. $user['id'],
  373. $total_price,
  374. "取消成功,取消资产【{$total_price}】",
  375. true
  376. );
  377. // 退款扣除金额直接给司机
  378. if($taxiUserId && $cost_price>0){
  379. model('common/Users')->changePartnership(
  380. $taxiUserId,
  381. $cost_price,
  382. "取消订单,扣除金额到账【" . $cost_price . "】",
  383. 30,
  384. true
  385. );
  386. }
  387. } else if ($row->paylog['pay_type'] == 'wechat') {
  388. // 加载配置
  389. $wechat = sys_config('', 'wechat');
  390. $config = [
  391. // 前面的appid什么的也得保留哦
  392. 'app_id' => $wechat['mini_appid'],
  393. 'mch_id' => $wechat['pay_mch_id'],
  394. 'key' => $wechat['pay_secret_key'],
  395. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  396. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  397. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  398. // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify',
  399. // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify',
  400. // 'sandbox' => true
  401. ];
  402. // 创建应用实例
  403. $app = Factory::payment($config);
  404. // Example:
  405. $result = $app->refund->byOutTradeNumber($row->paylog['out_trade_no'], get_order_no(), $row->paylog['total_price'] * 100, $total_price * 100, [
  406. // 可在此处传入其他参数,详细参数见微信支付文档
  407. 'refund_desc' => '用户申请退款',
  408. ]);
  409. app()->log(json_encode($result));
  410. if ($result['return_code'] == 'SUCCESS') {
  411. if ($result['result_code'] != 'SUCCESS') {
  412. $this->ApiJson(-1, '失败');
  413. }
  414. }
  415. // 退款扣除金额直接给司机
  416. if($taxiUserId && $cost_price>0){
  417. model('common/Users')->changePartnership(
  418. $taxiUserId,
  419. $cost_price,
  420. "取消订单,扣除金额到账【" . $cost_price . "】",
  421. 30,
  422. true
  423. );
  424. }
  425. }
  426. }
  427. // 后台推送
  428. push_socket_data('motor',[
  429. 'id' => $row['id'],
  430. 'msg' => '有用户取消订单,点击前往!'
  431. ]);
  432. Db::startTrans();
  433. $row->status = 5;
  434. $row->settle_price = $cost_price;
  435. $row->remark = $param['result'];
  436. if(!$row->save()){
  437. Db::rollback();
  438. return IResponse::failure('取消失败');
  439. }
  440. if ($row['taxi_id'] && !\app\common\model\Taxi::where(['taxi_user_id' => $taxiUid, 'id' => $row['taxi_id']])->update(['status' => 1])) {
  441. Db::rollback();
  442. return IResponse::failure('取消失败');
  443. }
  444. Db::commit();
  445. return $this->ApiJson(0, '取消成功', $row);
  446. }
  447. return $this->ApiJson(-1, '取消失败');
  448. }
  449. }