Taxi.php 18 KB

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