Taxi.php 18 KB

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