Taxi.php 19 KB

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