Order.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. namespace addons\ddrive\controller;
  3. use addons\ddrive\extend\Common;
  4. use addons\ddrive\library\Check;
  5. use addons\ddrive\library\Order as Lib;
  6. use addons\ddrive\model\Apply;
  7. use addons\epay\library\Service;
  8. use app\common\controller\Api;
  9. use app\common\model\Config;
  10. use Exception;
  11. use think\addons\Controller;
  12. use think\Db;
  13. use think\Lang;
  14. use think\Log;
  15. use Yansongda\Pay\Pay;
  16. use addons\ddrive\model\User;
  17. use addons\ddrive\model\UserVerified;
  18. use addons\ddrive\model\DdriveOrderComment;
  19. /**
  20. * 订单接口
  21. */
  22. class Order extends Api
  23. {
  24. protected $noNeedLogin = ['notifyx', 'takingList', 'order_info', 'time_out', 'order_taking', 'order_refresh', 'order_eliminate', 'create'];
  25. protected $noNeedRight = ['*'];
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \addons\ddrive\model\Order;
  30. Lang::load(APP_PATH . 'admin/lang/zh-cn/ddrive/order.php');
  31. }
  32. /**
  33. * 用户端-查询用户订单
  34. *
  35. * @return void
  36. */
  37. public function index()
  38. {
  39. $model = $this->model;
  40. $pageSize = $this->request->param('pageSize', 10);
  41. $map = [];
  42. // 用户身份
  43. if ($this->request->request('type') == 'driver') {
  44. $map['driver_id'] = $this->auth->id;
  45. } else {
  46. $map['user_id'] = $this->auth->id;
  47. }
  48. // 订单状态
  49. if ($this->request->has('status') && $this->request->request('status') != 'all') {
  50. $map['status'] = ['in', $this->request->param('status')];
  51. }
  52. $list = $model->where($map)->order('createtime desc')->paginate($pageSize);
  53. $this->success("", $list);
  54. }
  55. /**司机端-订单列表
  56. * order_info
  57. * @des
  58. */
  59. public function order_info()
  60. {
  61. $model = $this->model;
  62. $pageSize = $this->request->param('pageSize', 10);
  63. $map = [];
  64. if (!$this->auth->id) {
  65. $this->success("", ['date' => []]);
  66. }
  67. // 订单类型
  68. if ($this->request->has('order_type')) {
  69. $map['driver_id'] = $this->auth->id;
  70. if ($this->request->request('order_type') == 1) {
  71. $map['status'] = ['in', [1, 2, 4]];//进行中
  72. $list_start = $model->where($map)->order('createtime desc')->select();
  73. $map['status'] = ['in', [5]];//已接单
  74. $list_receiving = $model->where($map)->order('createtime desc')->select();
  75. $list = array_merge($list_start, $list_receiving);
  76. $this->success("查询成功", ['data' => $list]);
  77. } elseif ($this->request->request('order_type') == 2) {
  78. $map['status'] = ['in', [3, 99]];
  79. $list = $model->where($map)->order('createtime desc')->paginate($pageSize);
  80. $this->success("查询成功", $list);
  81. }
  82. }
  83. }
  84. /**用户端-订单检测
  85. * order_info
  86. * @des
  87. */
  88. public function order_taking()
  89. {
  90. if (!$this->auth->id) {
  91. $this->success("查询成功", ['data' => []]);
  92. }
  93. $model = $this->model;
  94. // 订单类型
  95. $map['user_id'] = $this->auth->id;
  96. $map['status'] = ['in', [0, 1, 2, 3, 4, 5]];
  97. $list_start = $model->where($map)->order('createtime desc')->select();
  98. $list_hy_start = (new \addons\ddrive\model\Hyorder())::with('shaddress')->where($map)->order('createtime desc')->select();
  99. $this->success("查询成功", ['data' => $list_start,'hy'=>$list_hy_start]);
  100. }
  101. /**继续呼叫
  102. * again
  103. * @des
  104. */
  105. public function again()
  106. {
  107. $orderId = $this->request->param('order_id');
  108. $info = $this->model->get($orderId);
  109. if ($info['status'] == '-2') {
  110. $res = $this->model->where('id', $info['id'])->update(['status' => '0', 'createtime' => time()]);
  111. if ($res) {
  112. $this->success("继续呼叫成功", ['data' => $info['id']]);
  113. }
  114. $this->error('继续呼叫失败');
  115. }
  116. $this->error('继续呼叫失败');
  117. }
  118. /**
  119. * 订单超时
  120. *
  121. * @return boolean
  122. */
  123. public function time_out()
  124. {
  125. $time = time() - 300;
  126. $order_id = $this->model->where('createtime', '<', $time)->where('status', '=', 0)->where('type', '=', 1)->column('id');
  127. if ($order_id) {
  128. $this->model->whereIn('id', $order_id)->update(['status' => '-2']);
  129. }
  130. }
  131. /**
  132. * 待接单列表
  133. *
  134. * @return void
  135. */
  136. public function takingList()
  137. {
  138. $model = $this->model;
  139. $pageSize = $this->request->param('pageSize', 10);
  140. $city = $this->request->param('city', '');
  141. if ($city) {
  142. $list = $model->where('status', 0)->where('start_address', 'like', '%' . $city . '%')->order('createtime desc')->paginate($pageSize);
  143. } else {
  144. $list = $model->where('status', 0)->order('createtime desc')->paginate($pageSize);
  145. }
  146. $this->success("", $list);
  147. }
  148. /**
  149. * 创建订单
  150. *
  151. * @return void
  152. */
  153. public function create()
  154. {
  155. $user = $this->auth->getUser();
  156. // 联系电话
  157. $mobile = $user['mobile'];
  158. if ($this->request->post('mobile')) {
  159. $mobile = $this->request->post('mobile');
  160. }
  161. $type = 1;
  162. $appointment_time = 0;
  163. $order_pay = $this->model->where('user_id', $this->auth->id)->where('status', 3)->find();
  164. if ($order_pay) {
  165. $this->error('您有订单待支付');
  166. }
  167. //预约时间
  168. if ($this->request->post('appointment_time')) {
  169. $type = 2;
  170. $appointment_time = $this->request->post('appointment_time');
  171. }
  172. // 下单数据
  173. $data = [
  174. 'mobile' => $mobile,
  175. 'start' => $this->request->post('start'),
  176. 'start_city' => $this->request->post('start_city'),
  177. 'start_address' => $this->request->post('start_address'),
  178. 'start_latitude' => $this->request->post('start_latitude'),
  179. 'start_longitude' => $this->request->post('start_longitude'),
  180. 'end' => $this->request->post('end'),
  181. 'end_city' => $this->request->post('end_city'),
  182. 'end_address' => $this->request->post('end_address'),
  183. 'end_latitude' => $this->request->post('end_latitude'),
  184. 'end_longitude' => $this->request->post('end_longitude'),
  185. 'distance' => $this->request->post('distance'),
  186. 'duration' => $this->request->post('duration'),
  187. 'estimated_price' => Lib::getPrice($this->request->post('distance'), date('H', time())),
  188. 'user_id' => $this->auth->id,
  189. 'reachtime' => 0,
  190. 'type' => $type,
  191. 'appointment_time' => $appointment_time,
  192. ];
  193. $rule = [
  194. ['start', 'require', '请填写出发地'],
  195. ['end', 'require', '请填写目的地'],
  196. ];
  197. (new Check())->checkParam($rule);
  198. $model = $this->model;
  199. $res = $model->data($data)->save();
  200. if ($res) {
  201. $sms_config = get_addon_config('alisms');
  202. $ddr_config = get_addon_config('ddrive');
  203. $mobiles = explode("\r\n", $ddr_config['noticeMobile']);
  204. \app\common\library\Sms::notice($ddr_config['noticeMobile'], ['type'=>'代驾'], $sms_config['template']['notice']);
  205. $this->success('订单创建成功', ['order_id' => $model->id]);
  206. } else {
  207. $this->error('订单创建失败');
  208. }
  209. }
  210. /**
  211. * 订单详情
  212. *
  213. * @return void
  214. */
  215. public function info()
  216. {
  217. $orderId = $this->request->param('order_id');
  218. $info = $this->model->get($orderId);
  219. $info['score'] = (new DdriveOrderComment())->where('order_id', $info['id'])->value('score');
  220. $info['user'] = $info->user;
  221. if ($info->driver) {
  222. $info['driver'] = $info->driver;
  223. // 司机照片
  224. $apply = Apply::where('user_id', $info['driver']['id'])->find();
  225. $info['driver']['avatar'] = url("/", "", "", true) . '..' . $apply['image'];
  226. $info['driver']['driving_age'] = $apply['driving_age'];
  227. // 总里程
  228. $total = $this->model->where('driver_id', $info['driver']['id'])->sum('distance');
  229. $info['driver']['total_distance'] = $total ? ceil($total / 1000) : 0;
  230. // 接单量
  231. $info['driver']['total_order'] = $this->model->where('driver_id', $info['driver']['id'])->count();
  232. $score_sum = (new DdriveOrderComment())->where('driver_id', $info['driver']['id'])->sum('score');
  233. $score_count = (new DdriveOrderComment())->where('driver_id', $info['driver']['id'])->count();
  234. if ($score_sum) {
  235. $info['driver']['score'] = round($score_sum / $score_count);
  236. } else {
  237. $info['driver']['score'] = 5;
  238. }
  239. }
  240. // 订单结束后查询到达时间
  241. if ($info['status'] > 2) {
  242. $info['end_time'] = Db::name('ddrive_order_location')->where('order_id', $info['id'])->where('type', 2)->value('createtime');
  243. $info['end_datetime'] = date('Y-m-d H:i:s', $info['end_time']);
  244. }
  245. $this->success('', $info);
  246. }
  247. /**
  248. * 取消订单
  249. *
  250. * @return boolean
  251. */
  252. public function cancel()
  253. {
  254. $orderId = $this->request->param('order_id');
  255. $cancel_type = $this->request->param('cancel_type');
  256. $res = $this->model->where('id', $orderId)->update(['status' => -1, 'cancel_type' => $cancel_type]);
  257. if ($res) {
  258. $this->success('订单取消成功');
  259. } else {
  260. $this->error('订单取消失败');
  261. }
  262. }
  263. /**
  264. * 根据距离估算价格
  265. *
  266. * @return void
  267. */
  268. public function amount()
  269. {
  270. $distance = $this->request->request('distance');
  271. $price = Lib::getPrice($distance);
  272. $this->success('', $price);
  273. }
  274. /**
  275. * 接单
  276. *
  277. * @return void
  278. */
  279. public function taking()
  280. {
  281. //欠缴平台服务费上限
  282. $restrict_service_fee = get_addon_config('ddrive')['restrict_service_fee'];
  283. $platform_service_fee = (new User())->where('id', $this->auth->id)->value('platform_service_fee');
  284. $mobile = (new User())->where('id', $this->auth->id)->value('mobile');
  285. $user_verified = (new UserVerified())->where('user_id', $this->auth->id)->find();
  286. if ($user_verified) {
  287. if ($user_verified['real_verified'] != 1) {
  288. $this->error('请完成实名认证');
  289. }
  290. if ($user_verified['driver_verified'] != 1) {
  291. $this->error('请完成驾照认证');
  292. }
  293. }
  294. if (!$mobile) {
  295. $this->error('请绑定手机号');
  296. }
  297. if ($restrict_service_fee <= $platform_service_fee) {
  298. $this->error('请缴纳平台服务费后接单');
  299. }
  300. $orderId = $this->request->param('order_id');
  301. // 判断订单是否被接单
  302. $order = $this->model->where('id', $orderId)->find();
  303. if (!$order) {
  304. $this->error('订单不存在');
  305. }
  306. if ($order['status'] != 0) {
  307. $this->error('手慢了,订单已被抢');
  308. }
  309. if ($order['type'] == 1) {
  310. $driver_order = $this->model->where('driver_id', $this->auth->id)->whereIn('status', [1, 2, 4, 5])->whereNotIn('type', [2])->find();
  311. if ($driver_order) {
  312. $this->error('存在未完成订单,禁止重复接单');
  313. }
  314. $status = 1;
  315. } else {
  316. $status = 5;
  317. }
  318. // 接单
  319. $res = $this->model->where('id', $orderId)->update(['status' => $status, 'driver_id' => $this->auth->id]);
  320. if ($res) {
  321. $this->success('接单成功');
  322. } else {
  323. $this->error('接单失败');
  324. }
  325. }
  326. /**
  327. * 到达出发地
  328. *
  329. * @return void
  330. */
  331. public function reach()
  332. {
  333. $orderId = $this->request->param('order_id');
  334. $res = $this->model->where('id', $orderId)->update(['status' => 4, 'reachtime' => time()]);
  335. if ($res) {
  336. $this->success('操作成功');
  337. } else {
  338. $this->error('操作失败');
  339. }
  340. }
  341. /**预约单开始出发
  342. * set_out
  343. * @des
  344. */
  345. public function set_out()
  346. {
  347. $orderId = $this->request->param('order_id');
  348. $res = $this->model->where('id', $orderId)->update(['status' => 1]);
  349. if ($res) {
  350. $this->success('操作成功');
  351. } else {
  352. $this->error('操作失败');
  353. }
  354. }
  355. /**线下结算
  356. * offline
  357. * @des
  358. */
  359. public function offline()
  360. {
  361. $orderId = $this->request->param('order_id');
  362. $res = $this->model->where('id', $orderId)->update(['status' => 99, 'complete_time' => time()]);
  363. if ($res) {
  364. $order = $this->model->where('id', $orderId)->find();
  365. // 查询订单上次位置
  366. $location = Db::name('ddrive_order_location')->where('order_id', $orderId)->where('type', 2)->order('id desc')->find();
  367. $price = Lib::getPrice($location['distance'], date('H', $order['createtime']));
  368. // 计算时间
  369. $duration = $location['createtime'] - $order['createtime'];
  370. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  371. $user_platform_service_fee = (new User())->where('id', $this->auth->id)->value('platform_service_fee');
  372. //累加服务费
  373. $user_res = (new User())->where('id', $this->auth->id)->update(['platform_service_fee' => $user_platform_service_fee + number_format(($price * ($platform_service_fee / 100)), 2)]);
  374. // 修改订单信息
  375. $data = [
  376. 'status' => 3,
  377. 'price' => $price,
  378. 'distance' => $location['distance'],
  379. 'duration' => $duration,
  380. 'platform_service_fee' => number_format(($price * ($platform_service_fee / 100)), 2),
  381. ];
  382. if ($user_res) {
  383. $this->success('操作成功');
  384. } else {
  385. $this->model->where('id', $orderId)->update($data);
  386. $this->error('操作失败');
  387. }
  388. } else {
  389. $this->error('操作失败');
  390. }
  391. }
  392. /**
  393. * 立即出发,记录出发位置
  394. *
  395. * @return void
  396. */
  397. public function start()
  398. {
  399. $orderId = $this->request->param('order_id');
  400. $latitude = $this->request->param('latitude');
  401. $longitude = $this->request->param('longitude');
  402. // 判断订单是否被接单
  403. $order = $this->model->where('id', $orderId)->find();
  404. if (!$order) {
  405. $this->error('订单不存在');
  406. }
  407. if ($order['status'] != 4) {
  408. $this->error('无需重复操作');
  409. }
  410. // 接单
  411. $res = $this->model->where('id', $orderId)->update(['status' => 2]);
  412. // 记录起始位置
  413. $data = [
  414. 'order_id' => $orderId,
  415. 'latitude' => $latitude,
  416. 'longitude' => $longitude,
  417. 'distance' => 0, // 行驶路程为0
  418. 'type' => 1, // 位置类型,1为起始位置,2为当前位置
  419. 'createtime' => time(),
  420. ];
  421. Db::name('ddrive_order_location')->insert($data);
  422. if ($res) {
  423. $distance = Lib::getDistance($latitude, $longitude, $latitude, $longitude);
  424. $price = Lib::getPrice($distance, date('H', $order['createtime']), $order['createtime'], $order['reachtime']);
  425. $this->success("操作成功", ['price' => $price, 'distance' => $distance]);
  426. } else {
  427. $this->error('操作失败');
  428. }
  429. }
  430. /**
  431. * 更新订单位置信息,记录订单路程
  432. *
  433. * @return void
  434. */
  435. public function updateLocation()
  436. {
  437. $orderId = $this->request->param('order_id');
  438. $latitude = $this->request->param('latitude');
  439. $longitude = $this->request->param('longitude');
  440. // 查询订单上次位置
  441. $last = Db::name('ddrive_order_location')->where('order_id', $orderId)->where('type', 2)->order('id desc')->find();
  442. if (!$last) {
  443. $start = Db::name('ddrive_order_location')->where('order_id', $orderId)->where('type', 1)->order('id desc')->find();
  444. $distance = Lib::getDistance($latitude, $longitude, $start['latitude'], $start['longitude']);
  445. // 首次更新位置
  446. $data = [
  447. 'order_id' => $orderId,
  448. 'latitude' => $latitude,
  449. 'longitude' => $longitude,
  450. 'distance' => $distance, // 行驶路程为0
  451. 'type' => 2, // 位置类型,1为起始位置,2为当前位置
  452. 'createtime' => time(),
  453. ];
  454. $res = Db::name('ddrive_order_location')->insert($data);
  455. } else {
  456. // 之后更新位置要根据上次位置算出行驶路程
  457. $thisdistance = Lib::getDistance($latitude, $longitude, $last['latitude'], $last['longitude']);
  458. $distance = $thisdistance + $last['distance'];
  459. // 更新总路程和当前位置
  460. $update = [
  461. 'distance' => $distance,
  462. 'latitude' => $latitude,
  463. 'longitude' => $longitude,
  464. 'createtime' => time(),
  465. ];
  466. $res = Db::name('ddrive_order_location')->where('order_id', $orderId)->update($update);
  467. }
  468. // 计算价格返回
  469. $order = $this->model->where('id', $orderId)->field('createtime,reachtime')->find();
  470. $price = Lib::getPrice($distance, date('H', $order['createtime']), $order['createtime'], $order['reachtime']);
  471. $this->success("", ['price' => $price, 'distance' => $distance]);
  472. }
  473. /**
  474. * 结束订单,记录出发位置
  475. *
  476. * @return void
  477. */
  478. public function done()
  479. {
  480. $orderId = $this->request->param('order_id');
  481. // 判断订单是否结束
  482. $order = $this->model->where('id', $orderId)->find();
  483. if (!$order) {
  484. $this->error('订单不存在');
  485. }
  486. // 查询订单上次位置
  487. $location = Db::name('ddrive_order_location')->where('order_id', $orderId)->where('type', 2)->order('id desc')->find();
  488. $price = Lib::getPrice($location['distance'], date('H', $order['createtime']));
  489. // 计算时间
  490. $duration = $location['createtime'] - $order['createtime'];
  491. // 修改订单信息
  492. $data = [
  493. 'status' => 3,
  494. 'price' => $price,
  495. 'distance' => $location['distance'],
  496. 'duration' => $duration,
  497. ];
  498. $res = $this->model->where('id', $orderId)->update($data);
  499. if ($res) {
  500. $this->success('操作成功');
  501. } else {
  502. $this->error('操作失败');
  503. }
  504. }
  505. /**
  506. * 订单支付
  507. *
  508. * @return void
  509. */
  510. public function pay()
  511. {
  512. $site = Config::get('site');
  513. $orderId = $this->request->param('order_id');
  514. // 判断订单是否结束
  515. $order = $this->model->where('id', $orderId)->find();
  516. if (!$order) {
  517. $this->error('订单不存在');
  518. }
  519. $method = $this->request->param('method', 'app');
  520. // 用户信息
  521. $user = $this->auth->getUser();
  522. //订单标题
  523. $title = $site['title'] . ' - 订单费用';
  524. // 订单编号
  525. $out_trade_no = $order['id'] . '-' . time();
  526. // openid
  527. $openid = Db::name('ddrive_user_token')->where('user_id', $user['id'])->value('openid');
  528. //回调链接
  529. $notifyurl = $this->request->root(true) . '/addons/ddrive/order/notifyx/paytype/wechat';
  530. $params = [
  531. 'type' => 'mini',
  532. 'orderid' => $out_trade_no,
  533. 'title' => $title,
  534. 'amount' => $order['price'],
  535. 'method' => $method,
  536. 'openid' => $openid,
  537. 'notifyurl' => $notifyurl,
  538. ];
  539. try {
  540. return Service::submitOrder($params);
  541. } catch (\Throwable $th) {
  542. $this->error('获取支付信息失败');
  543. }
  544. }
  545. /**
  546. * 司机端订单支付
  547. *
  548. * @return void
  549. */
  550. public function driver_pay()
  551. {
  552. $site = Config::get('site');
  553. $orderId = $this->request->param('order_id');
  554. $id = $this->request->param('coupon_id');
  555. $code = $this->request->param('code');
  556. $type = $this->request->param('type', 'user_wechat'); // driver_wechat 司机端 user_wechat 用户端 mini_wechat 小程序
  557. if ($type == 'mini_wechat' && !$code) {
  558. $this->error('网络错误请稍后再试');
  559. }
  560. // 判断订单是否结束
  561. $order = $this->model->where('id', $orderId)->find();
  562. if (!$order) {
  563. $this->error('订单不存在');
  564. }
  565. if ($id) {
  566. $coupon_list = Db::name('user_coupon')
  567. ->where('id', $id)
  568. ->field('limit_price,coupon_price,coupon_status')
  569. ->find();
  570. if (!$coupon_list) {
  571. $this->error('该优惠券不存在');
  572. }
  573. if ($coupon_list['coupon_status'] == 2) {
  574. $this->error('该优惠券已过期');
  575. }
  576. if ($order['price'] < $coupon_list['limit_price']) {
  577. $this->error('该订单没达到最低抵扣额');
  578. }
  579. $order_money = $order['price'] - $coupon_list['limit_price'];
  580. } else {
  581. $order_money = $order['price'];
  582. }
  583. $method = $this->request->param('method', 'app');
  584. // 用户信息
  585. $user = $this->auth->getUser();
  586. //订单标题
  587. $title = $site['title'] . ' - 订单费用';
  588. // 订单编号
  589. $out_trade_no = $order['id'] . '-' . time();
  590. // openid
  591. $openid = '';
  592. if ($type == 'mini_wechat') {
  593. $openid = Db::name('ddrive_user_token')->where('user_id', $user['id'])->value('mini_openid');
  594. }
  595. if (!$openid && $type == 'mini_wechat') {
  596. $info = (new \addons\ddrive\library\Common())->getOpenid($code);
  597. if (isset($info['openid'])) {
  598. $openid = $info['openid'];
  599. } else {
  600. return json_encode(['code' => 0, 'msg' => '失败']);
  601. }
  602. }
  603. //回调链接
  604. if ($type == 'alipay') {
  605. $notifyurl = $this->request->root(true) . '/addons/ddrive/order/notifyx/paytype/alipay';
  606. } else {
  607. $notifyurl = $this->request->root(true) . '/addons/ddrive/order/notifyx/paytype/' . $type;
  608. }
  609. $params = [
  610. 'type' => $type,
  611. 'orderid' => $out_trade_no,
  612. 'title' => $title,
  613. 'amount' => $order_money,
  614. 'method' => $method,
  615. 'openid' => $openid,
  616. 'notifyurl' => $notifyurl,
  617. ];
  618. try {
  619. if ($id) {
  620. $upcoupon = [];
  621. $upcoupon['order_id'] = $orderId;
  622. $upcoupon['usage_time'] = time();
  623. $upcoupon['coupon_status'] = 1;
  624. Db::name('user_coupon')->where('id', $id)->update($upcoupon);
  625. }
  626. $pay = json_encode(Service::submitOrder($params));
  627. if ($type == 'mini_wechat') {
  628. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode($pay, true)]);
  629. } elseif ($type == 'alipay') {
  630. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode($pay, true)]);
  631. } else {
  632. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode(json_decode($pay, true), true)]);
  633. }
  634. } catch (\Throwable $th) {
  635. return json_encode(['code' => 0, 'msg' => '失败:'.($th->raw['return_msg']?$th->raw['return_msg']:"")]);
  636. }
  637. }
  638. /**
  639. * 支付成功
  640. *
  641. * @return void
  642. */
  643. public function notifyx()
  644. {
  645. Log::record('支付回调');
  646. $paytype = $this->request->param('paytype');
  647. $pay = \addons\epay\library\Service::checkNotify($paytype);
  648. if (!$pay) {
  649. Log::record('签名错误');
  650. echo '签名错误';
  651. return;
  652. }
  653. $data = $pay->verify();
  654. try {
  655. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  656. $out_trade_no = $data['out_trade_no'];
  657. Log::record('订单编号:' . $out_trade_no);
  658. $order_id = explode('-', $out_trade_no)[0];
  659. $order = $this->model->where('id', $order_id)->find();
  660. if ($order['status'] != 99) {
  661. $this->model->where('id', $order_id)->setField('status', 99);
  662. $this->model->where('id', $order_id)->setField('complete_time', time());
  663. //平台服务费
  664. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'] / 100;
  665. $fee = round($platform_service_fee * $order['price'], 2);
  666. $this->model->where('id', $order_id)->setField('platform_service_fee', $fee);
  667. // 增加司机余额
  668. Db::name('user')->where('id', $order['driver_id'])->setInc('money', $order['price']);
  669. Db::name('details')->insert([
  670. 'user_id' => $order['driver_id'],
  671. 'fluctuate_type' => 1,
  672. 'msg' => '代驾收入',
  673. 'amount' => $order['price'],
  674. 'assets_type' => 2,
  675. 'source_type' => 2,
  676. 'createtime' => time(),
  677. 'form_id' => $order_id,
  678. ]);
  679. // 增加会员积分
  680. $pointLib = new \addons\ddrive\library\Point;
  681. $pointLib->orderDone($order);
  682. }
  683. } catch (Exception $e) {
  684. Log::record($e->getMessage());
  685. }
  686. echo $pay->success();
  687. }
  688. /**
  689. * 订单评价
  690. *
  691. * @return void
  692. */
  693. public function comment()
  694. {
  695. $orderId = $this->request->param('order_id');
  696. // 判断订单是否存在
  697. $order = $this->model->where('id', $orderId)->find();
  698. if (!$order) {
  699. $this->error('订单不存在');
  700. }
  701. $comment = Db::name('ddrive_order_comment')->where('order_id', $orderId)->find();
  702. if ($comment) {
  703. $this->error('已评价');
  704. }
  705. $score = $this->request->param('score', 5);
  706. $data = [
  707. 'user_id' => $this->auth->id,
  708. 'order_id' => $orderId,
  709. 'score' => $score,
  710. 'driver_id' => $order['driver_id'],
  711. 'createtime' => time(),
  712. ];
  713. $res = Db::name('ddrive_order_comment')->insert($data);
  714. if ($res) {
  715. $this->model->where('id', $orderId)->setField('comment', 1);
  716. $this->success('评价成功');
  717. } else {
  718. $this->error('评价失败');
  719. }
  720. }
  721. /**更新司机端首页订单信息
  722. * order_refresh
  723. * @des
  724. */
  725. public function order_refresh()
  726. {
  727. $orderId = $this->request->param('order_id');
  728. $city = $this->request->param('city');
  729. if (!$orderId) {
  730. $info = $this->model->where('start_address', 'like', '%' . $city . '%')->order('createtime desc')->where('status', '0')->select();
  731. $this->success('', $info);
  732. }
  733. // 判断订单是否存在
  734. $createtime = $this->model->where('id', $orderId)->value('createtime');
  735. if (!$createtime) {
  736. $this->success('', []);
  737. }
  738. $info = $this->model->where('createtime', '>', $createtime)->where('start_address', 'like', '%' . $city . '%')->where('status', '0')->select();
  739. if ($info) {
  740. $this->success('', $info);
  741. }
  742. $this->success('', []);
  743. }
  744. /**剔除司机端首页订单信息
  745. * order_refresh
  746. * @des
  747. */
  748. public function order_eliminate()
  749. {
  750. $city = $this->request->param('city');
  751. $info = $this->model->where('createtime', '>', time() - 303)->where('start_address', 'like', '%' . $city . '%')->where('status', '<>', '0')->column('id');
  752. if ($info) {
  753. $this->success('', $info);
  754. }
  755. $this->success('', []);
  756. }
  757. }