Taxi.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. $params = [
  155. 'user_id'=> $user['id'],
  156. 'category_id'=> isset($datas['category_id'])? $datas['category_id'] : 0,
  157. 'order_no'=> get_order_no(),
  158. 'price'=> isset($datas['price'])? $datas['price'] : 0.00,
  159. 'count'=> isset($datas['count'])? $datas['count'] : 0,
  160. 'mobile'=> isset($datas['mobile'])? $datas['mobile'] : '',
  161. 'depart'=> isset($datas['depart'])? $datas['depart'] : '',
  162. 'arrive'=> isset($datas['arrive'])? $datas['arrive'] : '',
  163. 'depart_lat'=> isset($datas['depart_lat'])? $datas['depart_lat'] : 0,
  164. 'depart_lng'=> isset($datas['depart_lng'])? $datas['depart_lng'] : 0,
  165. 'lat'=> isset($datas['lat'])? $datas['lat'] : 0,
  166. 'lng'=> isset($datas['lng'])? $datas['lng'] : 0,
  167. 'km'=> isset($datas['km'])? $datas['km'] : '',
  168. ];
  169. // 验证是否有未支付订单,有则直接更新订单为新订单
  170. $hasOrderId = model('common/TaxiOrder')->where(['user_id'=> $user['id'],'status'=> 1])->order('created_at','desc')->value('id');
  171. if($hasOrderId){
  172. model('common/TaxiOrder')->where(['id'=> $hasOrderId])->update($params);
  173. $order = model('common/TaxiOrder')->where(['id'=> $hasOrderId])->findOrFail();
  174. }else{
  175. $order = model('common/TaxiOrder')::create($params,true);
  176. }
  177. // 订单信息验证处理
  178. if ($order && $order['id']){
  179. // 创建对应支付记录
  180. $trade_no = get_order_no();
  181. $logID = model('common/OrderPaylog')->storeBy([
  182. 'out_trade_no' => $trade_no,
  183. 'total_price' => $params['price'],
  184. 'order_idx' => $order['id'],
  185. 'ascription' => 'motor' // 归属订单
  186. ]);
  187. // 免单优惠 20201201 添加条件 仅限xx 公里以下 不包含 xx 公里
  188. if ($count < sys_config('taxi_order_free_num','store') && $params['km'] < sys_config('taxi_order_free_km','store')){
  189. model('common/OrderPaylog')->updateBy($logID,[
  190. 'pay_price' => $params['price'],
  191. 'is_pay' => 1
  192. ]);
  193. model('common/TaxiOrder')->updateBy($order['id'], [
  194. 'is_free' => 1, // 免单
  195. 'status' => 2
  196. ]);
  197. // 后台推送
  198. // push_socket_data('motor',[
  199. // 'id' => $order['id'],
  200. // 'msg' => '有新的(免费)摩的订单等待处理哟,点击前往!'
  201. // ]);
  202. return $this->ApiJson(0,"本单免费,等待师傅接驾", [
  203. 'type' => 'free',
  204. 'success' => "ok!"
  205. ]);
  206. }
  207. // 返回支付单号
  208. return $this->ApiJson(0,'订单提交成功', [
  209. 'type' => 'wx',
  210. 'success' => "ok!",
  211. 'order_id' => $order['id'],
  212. 'trade_no' => $trade_no
  213. ]);
  214. }
  215. return $this->ApiJson(-1,'发生异常,请骚后重试...');
  216. }
  217. /**
  218. * 订单详情
  219. *
  220. * @author 许祖兴 < zuxing.xu@lettered.cn>
  221. * @date 2020/7/6 11:09
  222. *
  223. * @return \think\response\Json
  224. * @throws \Lettered\Support\Exceptions\FailedException
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. * @throws \think\exception\DbException
  228. */
  229. public function taxiOrder()
  230. {
  231. $param = $this->request->param();
  232. // 数据校验
  233. $valid = $this->validate($param, [
  234. 'order_id' => 'require',
  235. ]);
  236. // 错误
  237. if (true !== $valid) {
  238. return $this->ApiJson(-1, $valid);
  239. }
  240. $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser'])
  241. ->where(['user_id' => $this->auth->user()['id'], 'id'=> $param['order_id']])
  242. ->find();
  243. return $this->ApiJson(0,'获取成功', $info);
  244. }
  245. /**
  246. * 当前进行中订单详情
  247. *
  248. * @author 许祖兴 < zuxing.xu@lettered.cn>
  249. * @date 2020/7/6 11:09
  250. *
  251. * @return \think\response\Json
  252. * @throws \Lettered\Support\Exceptions\FailedException
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\ModelNotFoundException
  255. * @throws \think\exception\DbException
  256. */
  257. public function waitOrder()
  258. {
  259. $info = model('common/TaxiOrder')->with(['paylog','user','taxi','taxiUser'])
  260. ->where(['user_id' => $this->auth->user()['id']])
  261. ->whereIn('status',[2,3])
  262. ->order('created_at','desc')
  263. ->find();
  264. if($info){
  265. if($info['taxi_user']){
  266. $info['taxi_user']['level_name'] = TaxiUsersLevel::where(['level'=>$info['taxi_user']['level']])->value('title');
  267. }
  268. }
  269. return $this->ApiJson(0,'获取成功', !is_null($info)? $info : []);
  270. }
  271. /**
  272. * 订单
  273. *
  274. * @author 许祖兴 < zuxing.xu@lettered.cn>
  275. * @date 2020/7/6 11:09
  276. *
  277. * @return \think\response\Json
  278. * @throws \Lettered\Support\Exceptions\FailedException
  279. * @throws \think\db\exception\DataNotFoundException
  280. * @throws \think\db\exception\ModelNotFoundException
  281. * @throws \think\exception\DbException
  282. */
  283. public function order()
  284. {
  285. $param = $this->request->param();
  286. $limit = 10;
  287. // 数据校验
  288. $valid = $this->validate($param, [
  289. 'page' => 'require',
  290. ]);
  291. // 错误
  292. if (true !== $valid) {
  293. return $this->ApiJson(-1, $valid);
  294. }
  295. $orders = model('common/TaxiOrder')
  296. ->with(['paylog'])
  297. ->where(['user_id' => $this->auth->user()['id']])
  298. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  299. ->order(['id' => 'desc'])
  300. ->select();
  301. return $this->ApiJson(0,'获取成功', $orders);
  302. }
  303. /**
  304. * @desc 取消订单
  305. * @return \think\response\Json
  306. * @throws \think\db\exception\DataNotFoundException
  307. * @throws \think\db\exception\ModelNotFoundException
  308. * @throws \think\exception\DbException
  309. * @author weichuanbao<654745815@qq.com>
  310. * @date 2021/12/2 0002
  311. */
  312. public function cancelOrder()
  313. {
  314. $param = $this->request->param();
  315. // 数据校验
  316. $valid = $this->validate($param, [
  317. 'id' => 'require|number',
  318. ]);
  319. // 错误
  320. if (true !== $valid) {
  321. return $this->ApiJson(-1, $valid);
  322. }
  323. $row = model('common/TaxiOrder')->with(['paylog'])
  324. ->field('id,taxi_uid,taxi_id,status,created_at')
  325. ->whereNotIn('status', [0,5])
  326. ->find($param['id']);
  327. $user = $this->auth->user();
  328. if ($row) {
  329. if (time() - $row['created_at']['val'] < (60 * 10)) {
  330. // return $this->ApiJson(-1, '10分钟内无法取消订单');
  331. }
  332. $taxiUid = isset($row['taxi_uid'])? intval($row['taxi_uid']) : 0;
  333. if ($row['status'] == 2 || $row['status'] == 3) {
  334. $config = Cache::get('system_config');
  335. $total_price = $row->paylog['total_price'];
  336. // 如果平台已指派司机,则扣除部分金额
  337. $cost_price = 0;
  338. $total = $total_price;
  339. $createTime = isset($row['created_at'])? $row['created_at'] : 0;
  340. $cancelTime = isset($config['store']['store_cancel_time'])? $config['store']['store_cancel_time'] : 0;
  341. $taxiUser = model('common/TaxiUser')->where(['id'=> $taxiUid])->find();
  342. $taxiUserId = isset($taxiUser['user_id'])? $taxiUser['user_id'] : 0;
  343. if ($row['status'] == 3 ) {
  344. // 免费取消时间内不扣除
  345. if($cancelTime<=0 || $createTime < (time() - $cancelTime*60)){
  346. $total_price = round($total_price * (1 - $config['store']['store_cancel_order']/100), 2);
  347. $cost_price = $total-$total_price;
  348. }
  349. }
  350. if ($row->paylog['pay_type'] == 'balance') {
  351. model('common/Users')->changeBalance(
  352. $user['id'],
  353. $total_price,
  354. "取消成功,取消金额【" . $total_price . "】",
  355. true
  356. );
  357. // 退款扣除金额直接给司机
  358. if($taxiUserId && $cost_price>0){
  359. model('common/Users')->changePartnership(
  360. $taxiUserId,
  361. $cost_price,
  362. "取消订单,扣除金额到账【" . $cost_price . "】",
  363. 30,
  364. true
  365. );
  366. }
  367. } else if ($row->paylog['pay_type'] == 'property') {
  368. model('common/Users')->changeProperty(
  369. $user['id'],
  370. $total_price,
  371. "取消成功,取消资产【{$total_price}】",
  372. true
  373. );
  374. // 退款扣除金额直接给司机
  375. if($taxiUserId && $cost_price>0){
  376. model('common/Users')->changePartnership(
  377. $taxiUserId,
  378. $cost_price,
  379. "取消订单,扣除金额到账【" . $cost_price . "】",
  380. 30,
  381. true
  382. );
  383. }
  384. } else if ($row->paylog['pay_type'] == 'wechat') {
  385. // 加载配置
  386. $wechat = sys_config('', 'wechat');
  387. $config = [
  388. // 前面的appid什么的也得保留哦
  389. 'app_id' => $wechat['mini_appid'],
  390. 'mch_id' => $wechat['pay_mch_id'],
  391. 'key' => $wechat['pay_secret_key'],
  392. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  393. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  394. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  395. // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify',
  396. // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify',
  397. // 'sandbox' => true
  398. ];
  399. // 创建应用实例
  400. $app = Factory::payment($config);
  401. // Example:
  402. $result = $app->refund->byOutTradeNumber($row->paylog['out_trade_no'], get_order_no(), $row->paylog['total_price'] * 100, $total_price * 100, [
  403. // 可在此处传入其他参数,详细参数见微信支付文档
  404. 'refund_desc' => '用户申请退款',
  405. ]);
  406. app()->log(json_encode($result));
  407. if ($result['return_code'] == 'SUCCESS') {
  408. if ($result['result_code'] != 'SUCCESS') {
  409. $this->ApiJson(-1, '失败');
  410. }
  411. }
  412. else {
  413. // 退款扣除金额直接给司机
  414. if($taxiUserId && $cost_price>0){
  415. model('common/Users')->changePartnership(
  416. $taxiUserId,
  417. $cost_price,
  418. "取消订单,扣除金额到账【" . $cost_price . "】",
  419. 30,
  420. true
  421. );
  422. }
  423. $this->ApiJson(-1, '成功');
  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. }