Taxi.php 18 KB

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