Hyorder.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/2/22
  6. * Time: 15:42
  7. */
  8. namespace addons\ddrive\controller;
  9. use addons\ddrive\library\Common;
  10. use addons\ddrive\model\DdriveHyOrderComment;
  11. use addons\ddrive\model\UserVerified;
  12. use addons\ddrive\model\DriverRefund;
  13. use addons\epay\library\Service;
  14. use app\admin\model\DdriveSfOrderComment;
  15. use app\common\controller\Api;
  16. use addons\ddrive\library\Hyorder as Hy;
  17. use addons\ddrive\library\Check;
  18. use think\Db;
  19. use think\Lang;
  20. use think\Log;
  21. class Hyorder extends Api
  22. {
  23. protected $noNeedLogin = ['notifyx','amount'];
  24. protected $noNeedRight = ['*'];
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = new \addons\ddrive\model\Hyorder;
  29. Lang::load(APP_PATH . 'admin/lang/zh-cn/ddrive/hyorder.php');
  30. }
  31. /**
  32. * 创建订单
  33. *
  34. * @return void
  35. */
  36. public function create()
  37. {
  38. $params = $this->request->param();
  39. $start_address = str_replace('&quot;', '"', $params['start_address']);
  40. $start_address = json_decode($start_address, true);
  41. $sh_address = str_replace('&quot;', '"', $params['sh_address']);
  42. $sh_address = json_decode($sh_address, true);
  43. if (empty($sh_address) || empty($start_address)) {
  44. $this->error('请选择地址');
  45. }
  46. $address = [];
  47. foreach ($sh_address as $k => $v) {
  48. $address[$k]['mobile'] = $v['tel'];
  49. $address[$k]['end'] = $v['address']['name'];
  50. $address[$k]['end_city'] = $v['address']['city'];
  51. $address[$k]['end_address'] = $v['address']['address'];
  52. $address[$k]['end_lat'] = $v['address']['latitude'];
  53. $address[$k]['end_lng'] = $v['address']['longitude'];
  54. $address[$k]['floor'] = $v['floor'];
  55. $address[$k]['house_number'] = $v['code'];
  56. }
  57. if (empty($address)) {
  58. $this->error('请选择收获地址');
  59. }
  60. $rule = [
  61. ['people_num', 'require', '请选中跟车人数'],
  62. ['car_id', 'require', '请选择运输车辆'],
  63. ];
  64. (new Check())->checkParam($rule);
  65. $carInfo = (new \addons\ddrive\model\Freight())->where('id', $params['car_id'])->field('start_price,start_mileage,section1_price,section1_min_mileage,section1_max_mileage,section2_price,section2_mileage')->find();
  66. if (!$carInfo) {
  67. $this->error('请先选择车辆');
  68. }
  69. // 计算距离
  70. $distance = Hy::GetDistance($start_address['latitude'], $start_address['longitude'], $address[0]['end_lat'], $address[0]['end_lng']); //计算两点间距离
  71. foreach ($address as $k => $v) {
  72. if (isset($address[$k + 1])) {
  73. $distance2 = Hy::GetDistance($v['end_lat'], $v['end_lng'], $address[$k + 1]['end_lat'], $address[$k + 1]['end_lng']); //计算两点间距离
  74. $distance = $distance + $distance2;
  75. }
  76. }
  77. $is_transport = '0';
  78. $order_price = Hy::getPrice($distance, $carInfo);
  79. $demand = $params['demand'] ? explode(',', $params['demand']) : $params['demand'];
  80. if (is_array($demand)) {
  81. if (in_array('2', $demand)) {
  82. $order_price = $order_price + round($order_price * 0.4, 1);
  83. }
  84. if (in_array('1', $demand)) {
  85. $is_transport = 1;
  86. }
  87. }
  88. if($params['pay_method'] == 2 &&$params['coupon_id']){
  89. $this->error('暂不支持货到付款');
  90. }
  91. if ($params['coupon_id']) {
  92. $coupon_list = Db::name('user_coupon')
  93. ->where('id', $params['coupon_id'])
  94. ->field('limit_price,coupon_price,coupon_status')
  95. ->find();
  96. if (!$coupon_list) {
  97. $this->error('该优惠券不存在');
  98. }
  99. if ($coupon_list['coupon_status'] == 2) {
  100. $this->error('该优惠券已过期');
  101. }
  102. if ($order_price < $coupon_list['limit_price']) {
  103. $this->error('该订单没达到最低抵扣额');
  104. }
  105. $discount_price = $order_price - $coupon_list['coupon_price'];
  106. } else {
  107. $discount_price = $order_price;
  108. }
  109. $user = $this->auth->getUser();
  110. Db::startTrans();
  111. try {
  112. $data = [
  113. 'user_id' => $this->auth->id,
  114. 'out_trade_no' => Hy::createOrderSn(),
  115. 'mobile' => $params['mobile'] ? $params['mobile'] : $user['mobile'],
  116. 'start_mobile' => '',
  117. 'car_id' => $params['car_id'],
  118. 'type' => $params['appointment_time'] ? 2 : 1,
  119. 'appointment_time' => $params['appointment_time'] ? strtotime($params['appointment_time']) : 0,
  120. 'people_num' => $params['people_num'],
  121. 'demand' => $params['demand'],
  122. 'order_price' => $order_price,
  123. 'discount_price' => $discount_price,
  124. 'is_transport' => $is_transport,
  125. 'pay_method' => $params['pay_method'],
  126. 'remark' => $params['remark'],
  127. 'start' => $start_address['name'],
  128. 'start_city' => $start_address['city'],
  129. 'start_address' => $start_address['address'],
  130. 'start_lat' => $start_address['latitude'],
  131. 'start_lng' => $start_address['longitude'],
  132. 'status' => $params['pay_method'] == 1 ? 7 : 0,
  133. 'createtime' => time(),
  134. 'distance' => $distance,
  135. ];
  136. $order_id = Db::name('ddrive_hy_order')->insertGetId($data);
  137. foreach ($address as $k => $v) {
  138. $address[$k]['order_id'] = $order_id;
  139. $address[$k]['createtime'] = time();
  140. }
  141. Db::name('ddrive_hy_address')->insertAll($address);
  142. Db::commit();
  143. } catch (\Exception $e) {
  144. Db::rollback();
  145. $this->error('预约失败');
  146. }
  147. $sms_config = get_addon_config('alisms');
  148. $ddr_config = get_addon_config('ddrive');
  149. $mobiles = explode("\r\n", $ddr_config['noticeMobile']);
  150. \app\common\library\Sms::notice($ddr_config['noticeMobile'], ['type'=>'货运'], $sms_config['template']['notice']);
  151. $this->success('预约成功', ['order_id' => $order_id]);
  152. }
  153. /**
  154. * 根据距离估算价格
  155. *
  156. * @return void
  157. */
  158. public function amount()
  159. {
  160. $start_lat = $this->request->param('start_lat');
  161. $start_lng = $this->request->param('start_lng');
  162. $car_id = $this->request->param('car_id');
  163. $demand = $this->request->param('demand');
  164. $coupond_id = $this->request->param('coupond_id');
  165. $carInfo = (new \addons\ddrive\model\Freight())->where('id', $car_id)->field('start_price,start_mileage,section1_price,section1_min_mileage,section1_max_mileage,section2_price,section2_mileage')->find();
  166. if (!$carInfo) {
  167. $this->error('请先选择车辆');
  168. }
  169. $end_address = str_replace('&quot;', '"', $this->request->param('end_address'));
  170. $end_address = json_decode($end_address, true);
  171. if (empty($end_address)) {
  172. $this->error('请选择收获地址');
  173. }
  174. $end_new_address = [];
  175. $address_num = 0;
  176. foreach ($end_address as $k => $v) {
  177. $end_new_address[] = $v['address'];
  178. $address_num += 1;
  179. }
  180. if (empty($end_new_address)) {
  181. $this->error('请选择收获地址');
  182. }
  183. $distance = Hy::GetDistance($start_lat, $start_lng, $end_new_address[0]['latitude'], $end_new_address[0]['longitude']); //计算两点间距离
  184. foreach ($end_new_address as $k => $v) {
  185. if (isset($end_new_address[$k + 1])) {
  186. $distance2 = Hy::GetDistance($v['latitude'], $v['longitude'], $end_new_address[$k + 1]['latitude'], $end_new_address[$k + 1]['longitude']); //计算两点间距离
  187. $distance = $distance + $distance2;
  188. }
  189. }
  190. $price = Hy::getPrice($distance, $carInfo);
  191. if (isset($demand)) {
  192. if ($demand == 1) {
  193. $price = $price + round($price * 0.4, 1);
  194. }
  195. }
  196. if ($coupond_id) {
  197. $coupon_list = Db::name('user_coupon')
  198. ->where('id', $coupond_id)
  199. ->field('limit_price,coupon_price,coupon_status')
  200. ->find();
  201. if (!$coupon_list) {
  202. $this->error('该优惠券不存在');
  203. }
  204. if ($coupon_list['coupon_status'] == 2) {
  205. $this->error('该优惠券已过期');
  206. }
  207. if ($price < $coupon_list['limit_price']) {
  208. $this->error('该订单没达到最低抵扣额');
  209. }
  210. $price = $price - $coupon_list['coupon_price'];
  211. } else {
  212. $price = $price;
  213. }
  214. $this->success('', ['distance_price' => $price, 'distance' => $distance]);
  215. }
  216. /**
  217. * 订单详情
  218. *
  219. * @return void
  220. */
  221. public function info()
  222. {
  223. $order_id = $this->request->param('order_id');
  224. $info = $this->model::with(['shaddress'=>function($query){
  225. $query->field('id, order_id, mobile,end,end_city,end_address,end_lat as end_latitude,end_lng as end_longitude,floor,house_number,createtime');
  226. }])
  227. ->where('id', $order_id)
  228. ->find();
  229. if (!$info) {
  230. $this->error('该订单不存在');
  231. }
  232. $score = (new DdriveHyOrderComment())->where('order_id', $info['id'])->value('score');
  233. $info['score'] = $score ? $score : 0;
  234. $info['car_name'] = (new \addons\ddrive\model\Freight())->where('id', $info['car_id'])->value('car_name');
  235. if ($info['demand']) {
  236. $info['demand_text'] = Hy::getDemand($info['demand']);
  237. } else {
  238. $info['demand_text'] = '';
  239. }
  240. if ($info->driver) {
  241. $info['driver'] = $info->driver;
  242. $score_sum = (new DdriveHyOrderComment())->where('driver_id', $info['driver']['id'])->sum('score');
  243. $score_count = (new DdriveHyOrderComment())->where('driver_id', $info['driver']['id'])->count();
  244. if ($score_sum) {
  245. $info['driver']['score'] = round($score_sum / $score_count);
  246. } else {
  247. $info['driver']['score'] = 5;
  248. }
  249. }
  250. $info['start_latitude'] = $info['start_lat'];
  251. $info['start_longitude'] = $info['start_lng'];
  252. //$info['time'] = $info['createtime'];
  253. $info['createtime'] = $info['createtime'];
  254. $this->success('', $info);
  255. }
  256. /**
  257. * 待接单列表
  258. *
  259. * @return void
  260. */
  261. public function taskingList()
  262. {
  263. $pageSize = $this->request->param('pageSize', 10);
  264. $city = $this->request->param('city', '');
  265. if ($city) {
  266. $list = (new \addons\ddrive\model\Hyorder())::with('shaddress')
  267. ->field('id,user_id,type,appointment_time,status,people_num,demand,order_price,
  268. discount_price,remark,start,start_city,start_address,start_lat,is_transport,start_lng,distance')
  269. ->where('status', '0')
  270. ->where('start_city', 'like', '%' . $city . '%')
  271. ->order('createtime desc')
  272. ->paginate($pageSize);
  273. } else {
  274. $list = (new \addons\ddrive\model\Hyorder())::with('shaddress')
  275. ->field('id,user_id,type,appointment_time,status,people_num,demand,order_price,
  276. discount_price,platform_service_fee,remark,start,start_city,start_address,is_transport,start_lat,start_lng')
  277. ->where('status', '0')
  278. ->order('createtime desc')
  279. ->paginate($pageSize);
  280. }
  281. foreach ($list as $k => $v) {
  282. if($v['demand']){
  283. $list[$k]['demand_text'] = Hy::getDemand($v['demand']);
  284. }else{
  285. $list[$k]['demand_text'] = '';
  286. }
  287. }
  288. $this->success("", $list);
  289. }
  290. /**
  291. * 接单
  292. *
  293. * @return void
  294. */
  295. public function tasking()
  296. {
  297. //欠缴平台服务费上限
  298. $restrict_service_fee = get_addon_config('ddrive')['restrict_service_fee'];
  299. $platform_service_fee = (new \addons\ddrive\model\User())->where('id', $this->auth->id)->value('platform_service_fee');
  300. $mobile = (new \addons\ddrive\model\User())->where('id', $this->auth->id)->value('mobile');
  301. $user_verified = (new UserVerified())->where('user_id', $this->auth->id)->find();
  302. if ($user_verified) {
  303. if ($user_verified['real_verified'] != 1) {
  304. $this->error('请完成实名认证');
  305. }
  306. if ($user_verified['driver_verified'] != 1) {
  307. $this->error('请完成驾照认证');
  308. }
  309. //if ($user_verified['card_verified'] != 1) {
  310. //$this->error('请完成车主认证');
  311. //}
  312. }
  313. if (!$mobile) {
  314. $this->error('请绑定手机号');
  315. }
  316. if ($restrict_service_fee <= $platform_service_fee) {
  317. $this->error('请缴纳平台服务费后接单');
  318. }
  319. $orderId = $this->request->param('order_id');
  320. // 判断订单是否被接单
  321. $order = $this->model->where('id', $orderId)->find();
  322. if (!$order) {
  323. $this->error('订单不存在');
  324. }
  325. if ($order['status'] != 0) {
  326. $this->error('手慢了,订单已被抢');
  327. }
  328. if ($order['type'] == 1) {
  329. $driver_order = $this->model->where('cargo_driver_id', $this->auth->id)->whereIn('status', [1, 2, 4, 5])->whereNotIn('type', [2])->find();
  330. if ($driver_order) {
  331. $this->error('存在未完成订单,禁止重复接单');
  332. }
  333. $status = 1;
  334. } else {
  335. $status = 5;
  336. }
  337. // 接单
  338. $res = $this->model->where('id', $orderId)->update(['status' => $status, 'cargo_driver_id' => $this->auth->id]);
  339. if ($res) {
  340. $this->success('接单成功');
  341. } else {
  342. $this->error('接单失败');
  343. }
  344. }
  345. /**
  346. * 到达出发地
  347. *
  348. * @return void
  349. */
  350. public function reach()
  351. {
  352. $orderId = $this->request->param('order_id');
  353. // 判断订单是否被接单
  354. $order = $this->model->where('id', $orderId)->find();
  355. if (!$order) {
  356. $this->error('订单不存在');
  357. }
  358. if($order['status'] !=1){
  359. $this->error('无需重复操作');
  360. }
  361. $res = $this->model->where('id', $orderId)->update(['status' => 4]);
  362. if ($res) {
  363. $this->success('操作成功');
  364. } else {
  365. $this->error('操作失败');
  366. }
  367. }
  368. /**
  369. * 开始出发
  370. *
  371. * @return void
  372. */
  373. public function start()
  374. {
  375. $orderId = $this->request->param('order_id');
  376. // 判断订单是否被接单
  377. $order = $this->model->where('id', $orderId)->find();
  378. if (!$order) {
  379. $this->error('订单不存在');
  380. }
  381. if ($order['status'] != 4) {
  382. $this->error('无需重复操作');
  383. }
  384. // 接单
  385. $res = $this->model->where('id', $orderId)->update(['status' => 2]);
  386. if ($res) {
  387. $this->success('操作成功');
  388. } else {
  389. $this->success('操作失败');
  390. }
  391. }
  392. /**预约单开始出发
  393. * set_out
  394. * @des
  395. */
  396. public function set_out()
  397. {
  398. $orderId = $this->request->param('order_id');
  399. $order = $this->model->where('id', $orderId)->find();
  400. if (!$order) {
  401. $this->error('订单不存在');
  402. }
  403. if($order['status'] !=5){
  404. $this->error('无需重复操作');
  405. }
  406. $res = $this->model->where('id', $orderId)->update(['status' => 1]);
  407. if ($res) {
  408. $this->success('操作成功');
  409. } else {
  410. $this->error('操作失败');
  411. }
  412. }
  413. /**
  414. * 确认货物已到达
  415. *
  416. * @return void
  417. */
  418. public function done()
  419. {
  420. $orderId = $this->request->param('order_id');
  421. // 判断订单是否进行中
  422. $order = $this->model->where('id', $orderId)->find();
  423. if (!$order) {
  424. $this->error('订单不存在');
  425. }
  426. if($order['status'] !=2){
  427. $this->error('无需重复操作');
  428. }
  429. // 接单
  430. if ($order['pay_method'] == 2) {
  431. $res = $this->model->where('id', $orderId)->update(['status' => 3]);
  432. } else {
  433. //平台服务费
  434. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  435. $fee = number_format(($order['discount_price'] * ($platform_service_fee / 100)), 2);
  436. $money = round($order['order_price'] - $fee,2);
  437. // 增加司机余额
  438. Db::name('user')->where('id', $order['cargo_driver_id'])->setInc('money', $money);
  439. Db::name('details')->insert([
  440. 'user_id' => $order['cargo_driver_id'],
  441. 'fluctuate_type' => 1,
  442. 'msg' => '货运收入',
  443. 'amount' => $money,
  444. 'assets_type' => 2,
  445. 'source_type' => 2,
  446. 'createtime' => time(),
  447. 'form_id' => $orderId,
  448. ]);
  449. $res = $this->model->where('id', $orderId)->update(['status' => 99,'complete_time'=>time()]);
  450. }
  451. if ($res) {
  452. $this->success('操作成功');
  453. } else {
  454. $this->success('操作失败');
  455. }
  456. }
  457. /**
  458. * 货到付款
  459. *
  460. * @return void
  461. */
  462. public function offline()
  463. {
  464. $orderId = $this->request->param('order_id');
  465. // 判断订单是否被接单
  466. $order = $this->model->where('id', $orderId)->find();
  467. if (!$order) {
  468. $this->error('订单不存在');
  469. }
  470. if($order['pay_method'] ==1){
  471. $this->error('该订单无需确认');
  472. }
  473. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  474. $service_fee = number_format(($order['discount_price'] * ($platform_service_fee / 100)), 2);
  475. $this->model->where('id', $orderId)->update(['platform_service_fee' => $service_fee]);
  476. $this->success('成功', ['order_price' => $order['discount_price'], 'service_fee' => $service_fee]);
  477. }
  478. /**
  479. * 确认结算
  480. *
  481. * @return void
  482. */
  483. public function confirm()
  484. {
  485. $orderId = $this->request->param('order_id');
  486. // 判断订单是否进行中
  487. $order = $this->model->where('id', $orderId)->find();
  488. if (!$order) {
  489. $this->error('订单不存在');
  490. }
  491. if ($order['status'] != 3) {
  492. $this->error('无需重复操作');
  493. }
  494. Db::startTrans();
  495. try {
  496. switch ($order['pay_method']){
  497. case '1':
  498. break;
  499. case '2':
  500. // 修改订单状态
  501. $this->model->where('id', $orderId)->update(['status' => 99, 'complete_time' => time()]);
  502. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  503. $user_platform_service_fee = (new \addons\ddrive\model\User())->where('id', $this->auth->id)->value('platform_service_fee');
  504. //累加服务费
  505. (new \addons\ddrive\model\User())->where('id', $this->auth->id)->update(['platform_service_fee' => $user_platform_service_fee + round(($order['discount_price'] * ($platform_service_fee / 100)), 2)]);
  506. break;
  507. }
  508. Db::commit();
  509. } catch (\Exception $e) {
  510. Db::rollback();
  511. $this->error($e->getMessage());
  512. }
  513. $this->success('操作成功');
  514. }
  515. /**
  516. * 订单评价
  517. *
  518. * @return void
  519. */
  520. public function comment()
  521. {
  522. $orderId = $this->request->param('order_id');
  523. // 判断订单是否存在
  524. $order = $this->model->where('id', $orderId)->find();
  525. if (!$order) {
  526. $this->error('订单不存在');
  527. }
  528. $comment = Db::name('ddrive_hy_order_comment')->where('order_id', $orderId)->find();
  529. if ($comment) {
  530. $this->error('已评价');
  531. }
  532. $score = $this->request->param('score', 5);
  533. $data = [
  534. 'user_id' => $this->auth->id,
  535. 'order_id' => $orderId,
  536. 'score' => $score,
  537. 'driver_id' => $order['cargo_driver_id'],
  538. 'createtime' => time(),
  539. ];
  540. $res = Db::name('ddrive_hy_order_comment')->insert($data);
  541. if ($res) {
  542. $this->model->where('id', $orderId)->setField('comment_status', 1);
  543. $this->success('评价成功');
  544. } else {
  545. $this->error('评价失败');
  546. }
  547. }
  548. /**
  549. * 取消订单
  550. *
  551. * @return boolean
  552. */
  553. public function cancel()
  554. {
  555. $orderId = $this->request->param('order_id');
  556. $cancel_type = $this->request->param('cancel_type');
  557. $order = $this->model->where('id', $orderId)->find();
  558. if (!$order) {
  559. $this->error('订单不存在');
  560. }
  561. if (!in_array($order['status'], [0, 1, 4, 5])) {
  562. $this->error('订单不在取消状态');
  563. }
  564. if ($order['user_id'] != $this->auth->id) {
  565. $this->error('网络异常,请稍后重试!');
  566. }
  567. $data = [
  568. 'user_id' => $this->auth->id,
  569. 'order_id' => $order['id'],
  570. 'account' => $order['pay_type_text'],
  571. 'order_number' => $order['out_trade_no'],
  572. 'number' => $order['pay_number'],
  573. 'pay_type' => $order['pay_type'],
  574. 'check_status' => '0',
  575. 'apply_money' => $order['discount_price'],
  576. 'apply_time' => time(),
  577. ];
  578. Db::startTrans();
  579. try {
  580. $id = 0;
  581. if($order['pay_method'] == 1){
  582. $id = Db::name('ddrive_refund')->insertGetId($data);
  583. }
  584. $this->model->where('id', $orderId)->update(['status' => -1, 'cancel_type' => $cancel_type]);
  585. Db::commit();
  586. } catch (\Exception $e) {
  587. Db::rollback();
  588. $this->error('订单取消失败');
  589. }
  590. $this->success('订单取消成功', ['refund_id' => $id]);
  591. }
  592. /**司机端-订单列表
  593. * order_info
  594. * @des
  595. */
  596. public function order_list()
  597. {
  598. $model = $this->model;
  599. $pageSize = $this->request->param('pageSize', 10);
  600. $map = [];
  601. if (!$this->auth->id) {
  602. $this->success("", ['date' => []]);
  603. }
  604. // 订单类型
  605. if ($this->request->has('type')) {
  606. $map['cargo_driver_id'] = $this->auth->id;
  607. if ($this->request->request('type') == 1) {
  608. $map['status'] = ['in', [1, 2, 4,5]];//已接单
  609. } else {
  610. $map['status'] = ['in', [3,99]];
  611. }
  612. $list = $model::with('shaddress')
  613. ->field('id,user_id,type,appointment_time,status,people_num,demand,order_price,
  614. discount_price,remark,start,start_city,is_transport,start_address,start_lat,start_lng,distance,platform_service_fee')
  615. ->where($map)
  616. ->order('createtime desc')
  617. ->paginate($pageSize);
  618. foreach ($list as $k => $v) {
  619. if($v['demand']){
  620. $list[$k]['demand_text'] = Hy::getDemand($v['demand']);
  621. }else{
  622. $list[$k]['demand_text'] = '';
  623. }
  624. }
  625. $this->success("查询成功", $list);
  626. }
  627. }
  628. /**
  629. * 用户端-用户订单
  630. *
  631. * @return void
  632. */
  633. public function index()
  634. {
  635. $model = $this->model;
  636. $pageSize = $this->request->param('pageSize', 10);
  637. $map = [];
  638. // 用户身份
  639. $map['user_id'] = $this->auth->id;
  640. $map['status'] = ['<>', '7'];
  641. // 订单状态
  642. if ($this->request->has('status') && $this->request->request('status') != 'all') {
  643. $map['comment_status'] = 0;
  644. $map['status'] = ['in', $this->request->param('status')];
  645. }
  646. $list = $model::with('shaddress')
  647. ->field('id,user_id,type,appointment_time,status,people_num,demand,order_price,
  648. discount_price,remark,start,start_city,is_transport,start_address,start_lat,start_lng,distance,comment_status')
  649. ->where($map)
  650. ->order('createtime desc')
  651. ->paginate($pageSize);
  652. foreach ($list as $k => $v) {
  653. if($v['demand']){
  654. $list[$k]['demand_text'] = Hy::getDemand($v['demand']);
  655. }else{
  656. $list[$k]['demand_text'] = '';
  657. }
  658. }
  659. $this->success("", $list);
  660. }
  661. /**用户端-订单检测
  662. * order_info
  663. * @des
  664. */
  665. public function order_taking()
  666. {
  667. if (!$this->auth->id) {
  668. $this->success("查询成功", ['data' => []]);
  669. }
  670. $model = $this->model;
  671. // 订单类型
  672. $map['user_id'] = $this->auth->id;
  673. $map['status'] = ['in', [0, 1, 2, 3, 4, 5]];
  674. $list_start = $model::with('shaddress')->where($map)->order('createtime desc')->select();
  675. $this->success("查询成功", ['data' => $list_start]);
  676. }
  677. /**
  678. * 订单支付
  679. *
  680. * @return void
  681. */
  682. public function driver_pay()
  683. {
  684. $orderId = $this->request->param('order_id');
  685. $id = $this->request->param('coupon_id');
  686. $code = $this->request->param('code');
  687. $type = $this->request->param('type', 'user_wechat'); // driver_wechat 司机端 user_wechat 用户端 mini_wechat 小程序
  688. if ($type == 'mini_wechat' && !$code) {
  689. $this->error('网络错误请稍后再试');
  690. }
  691. // 判断订单是否结束
  692. $order = $this->model->where('id', $orderId)->find();
  693. if (!$order) {
  694. $this->error('订单不存在');
  695. }
  696. // 是否存在优惠后金额
  697. if (!empty($order['discount_price'])){
  698. $order_money = $order['discount_price'];
  699. }else{
  700. $order_money = $order['order_price'];
  701. }
  702. $method = $this->request->param('method', 'app');
  703. // 用户信息
  704. $user = $this->auth->getUser();
  705. //订单标题
  706. $title = '订单费用';
  707. // 订单编号
  708. $out_trade_no = $order['out_trade_no'];
  709. // openid
  710. $openid = '';
  711. if ($type == 'mini_wechat') {
  712. $openid = Db::name('ddrive_user_token')->where('user_id', $user['id'])->value('mini_openid');
  713. }
  714. if (!$openid && $type == 'mini_wechat') {
  715. $info = (new Common())->getOpenid($code);
  716. if (isset($info['openid'])) {
  717. $openid = $info['openid'];
  718. } else {
  719. return json_encode(['code' => 0, 'msg' => '失败']);
  720. }
  721. }
  722. //回调链接
  723. if ($type == 'alipay') {
  724. $notifyurl = $this->request->root(true) . '/addons/ddrive/hyorder/notifyx/paytype/alipay';
  725. } else {
  726. $notifyurl = $this->request->root(true) . '/addons/ddrive/hyorder/notifyx/paytype/' . $type;
  727. }
  728. $params = [
  729. 'type' => $type,
  730. 'orderid' => $out_trade_no,
  731. 'title' => $title,
  732. 'amount' => $order_money,
  733. 'method' => $method,
  734. 'openid' => $openid,
  735. 'notifyurl' => $notifyurl,
  736. ];
  737. try {
  738. if ($id) {
  739. $upcoupon = [];
  740. $upcoupon['order_id'] = $orderId;
  741. $upcoupon['usage_time'] = time();
  742. $upcoupon['coupon_status'] = 1;
  743. Db::name('user_coupon')->where('id', $id)->update($upcoupon);
  744. }
  745. $pay = json_encode(Service::submitOrder($params));
  746. if ($type == 'mini_wechat') {
  747. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode($pay, true)]);
  748. } elseif ($type == 'alipay') {
  749. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode($pay, true)]);
  750. } else {
  751. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode(json_decode($pay, true), true)]);
  752. }
  753. } catch (\Throwable $th) {
  754. return json_encode(['code' => 0, 'msg' => '失败']);
  755. }
  756. }
  757. /**
  758. * 支付成功
  759. *
  760. * @return void
  761. */
  762. public function notifyx()
  763. {
  764. Log::record('支付回调');
  765. $paytype = $this->request->param('paytype');
  766. $pay = \addons\epay\library\Service::checkNotify($paytype);
  767. if (!$pay) {
  768. Log::record('签名错误');
  769. echo '签名错误';
  770. return;
  771. }
  772. $data = $pay->verify();
  773. try {
  774. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  775. $number = $paytype == 'alipay' ? $data['trade_no'] : $data['transaction_id'];
  776. $out_trade_no = $data['out_trade_no'];
  777. Log::record('订单编号:' . $out_trade_no);
  778. $order = $this->model->where('out_trade_no', $out_trade_no)->find();
  779. if ($order['status'] == 7) {
  780. //平台服务费
  781. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  782. $fee = number_format(($payamount * ($platform_service_fee / 100)), 2);
  783. $up_order = [];
  784. $up_order['platform_service_fee'] = $fee;
  785. $up_order['pay_time'] = time();
  786. $up_order['status'] = 0;
  787. $up_order['pay_number'] = $number;
  788. $up_order['pay_type'] = $paytype == 'alipay' ? 1 : 2;
  789. $this->model->where('out_trade_no', $out_trade_no)->update($up_order);
  790. }
  791. } catch (\Exception $e) {
  792. Log::record($e->getMessage());
  793. }
  794. echo $pay->success();
  795. }
  796. /**退款详情
  797. * refund_info
  798. * @des
  799. */
  800. public function refund_info()
  801. {
  802. $id = request()->param('id');
  803. if (!$id) {
  804. $this->error('网络异常,请稍后重试!');
  805. }
  806. $where['id'] = $id;
  807. $info = (new DriverRefund())->getInfo($where, 'id,user_id,apply_money,money,check_status,account,FROM_UNIXTIME(apply_time) as apply_time,FROM_UNIXTIME(confirm_time) as confirm_time,FROM_UNIXTIME(success_time) as success_time', 'find');
  808. if (!$info) {
  809. $this->error('网络异常,请稍后重试!');
  810. }
  811. if ($info['user_id'] != $this->auth->id) {
  812. $this->error('网络异常,请稍后重试!');
  813. }
  814. $info['predict_time'] = date("Y-m-d H:i:s", strtotime("+1 day", strtotime($info['success_time'])));
  815. $this->success('查询成功', $info);
  816. }
  817. /**更新司机端首页订单信息
  818. * order_refresh
  819. * @des
  820. */
  821. public function order_refresh()
  822. {
  823. $orderId = $this->request->param('order_id');
  824. $city = $this->request->param('city');
  825. if (!$orderId) {
  826. $info = $this->model::with('shaddress')->where('start_address', 'like', '%' . $city . '%')->order('createtime desc')->where('status', '0')->select();
  827. $this->success('', $info);
  828. }
  829. // 判断订单是否存在
  830. $createtime = $this->model->where('id', $orderId)->value('createtime');
  831. if (!$createtime) {
  832. $this->success('', []);
  833. }
  834. $info = $this->model::with('shaddress')->where('createtime', '>', $createtime)->where('start_address', 'like', '%' . $city . '%')->where('status', '0')->select();
  835. if ($info) {
  836. $this->success('', $info);
  837. }
  838. $this->success('', []);
  839. }
  840. /**剔除司机端首页订单信息
  841. * order_refresh
  842. * @des
  843. */
  844. public function order_eliminate()
  845. {
  846. $city = $this->request->param('city');
  847. $info = $this->model::with('shaddress')->where('start_address', 'like', '%' . $city . '%')->where('status', '<>', '0')->column('id');
  848. if ($info) {
  849. $this->success('', $info);
  850. }
  851. $this->success('', []);
  852. }
  853. }