Sforder.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. <?php
  2. namespace addons\ddrive\controller;
  3. use addons\ddrive\extend\Common;
  4. use addons\ddrive\library\Check;
  5. use addons\ddrive\model\UserVerified;
  6. use addons\epay\library\Service;
  7. use app\admin\model\Coupon;
  8. use app\admin\model\UserCoupon;
  9. use app\common\controller\Api;
  10. use think\Db;
  11. use addons\ddrive\library\Sforder as Lib;
  12. use think\Exception;
  13. use think\Log;
  14. use app\common\model\Config;
  15. class Sforder extends Api
  16. {
  17. protected $noNeedLogin = ['order_index', 'takingList', 'order_info', 'time_out', 'order_refresh', 'order_eliminate','recommend_route','index_order'];
  18. protected $noNeedRight = ['*'];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \addons\ddrive\model\Sforder();
  23. }
  24. /**
  25. * 订单列表
  26. *
  27. * @return void
  28. */
  29. public function order_list()
  30. {
  31. //$where = "(user_id = '" . $this->auth->id . "' OR other_user_id = '" . $this->auth->id . ",') AND pid = 0";
  32. $field = 'id,order_type,start_time,pid,order_money,start_city,start_address,end_city,end_address,status,tel,start_name,end_name,people_num,more_seats';
  33. //**用户预约单
  34. $sf_user_order = $this->model->where('other_user_id', $this->auth->id)->where('order_type', 1)->whereIn('status', [2, 3, 4])->field($field)->order('createtime desc')->select();
  35. //**司机发布预约单
  36. $sf_driver_order = $this->model->where('user_id', $this->auth->id)->where('order_type', 2)->whereIn('status', [1, 3, 4])->field($field)->order('createtime desc')->select();
  37. $new_driver_order = [];
  38. foreach ($sf_driver_order as $k => $v) {
  39. $child_order = $this->model->where('pid', $v['id'])->find();
  40. if ($child_order) {
  41. $new_driver_order[] = $child_order;
  42. }
  43. }
  44. $sf_order = array_merge($sf_user_order, $new_driver_order);
  45. //查询子单
  46. foreach (collection($sf_order)->toArray() as $k => $v) {
  47. $sf_order[$k]['new_tel'] = substr($v['tel'], 7, 4);
  48. $sf_order[$k]['tel'] = $v['tel'];
  49. $sf_order[$k]['more_seats'] = $v['more_seats'] ? $v['more_seats'] : 0;
  50. $sf_order[$k]['week'] = Lib::getWeek(date("w", $v['start_time']));
  51. $sf_order[$k]['start_time'] = date('m-d H:i', $v['start_time']);
  52. $sf_order[$k]['statusText'] = Lib::getStatus($v['status']);
  53. }
  54. $this->success('成功', $sf_order ? $sf_order : []);
  55. }
  56. /**
  57. * 预约订单(司机发布预约单)
  58. *
  59. * @return void
  60. */
  61. public function reserve_order()
  62. {
  63. $order_id = $this->request->param('order_id');
  64. $people_num = $this->request->param('people_num', 1);
  65. $tel = $this->request->param('tel', 1);
  66. $remark = $this->request->param('remark');
  67. $sf_order = $this->model->where('id', $order_id)->find();
  68. if (!$sf_order) {
  69. $this->error('该订单不存在');
  70. }
  71. if (floor($people_num) != $people_num) {
  72. $this->error('请填写正确人数');
  73. }
  74. if ($sf_order['more_seats'] < $people_num) {
  75. $this->error('当前余座已不足', '');
  76. }
  77. if ($sf_order['status'] != 1) {
  78. $this->error('该订单当前无法预约');
  79. }
  80. $user = $this->auth->getUser();
  81. // 联系电话
  82. if ($tel) {
  83. $mobile = $tel;
  84. } else {
  85. $mobile = $user['mobile'];
  86. }
  87. $data = [
  88. 'user_id' => $sf_order['user_id'],
  89. 'order_type' => $sf_order['order_type'],
  90. 'start_address' => $sf_order['start_address'],
  91. 'end_address' => $sf_order['end_address'],
  92. 'route' => $sf_order['route'],
  93. 'start_time' => $sf_order['start_time'],
  94. 'car_type' => $sf_order['car_type'],
  95. 'more_seats' => $sf_order['more_seats'] - $people_num,
  96. 'car_price' => $sf_order['car_price'],
  97. 'remark' => $remark,
  98. 'start_city' => $sf_order['start_city'],
  99. 'end_city' => $sf_order['end_city'],
  100. 'start_name' => $sf_order['start_name'],
  101. 'end_name' => $sf_order['end_name'],
  102. 'pid' => $sf_order['id'],
  103. 'status' => 3,
  104. 'start_latitude' => $sf_order['start_latitude'],
  105. 'start_longitude' => $sf_order['start_longitude'],
  106. 'end_latitude' => $sf_order['end_latitude'],
  107. 'end_longitude' => $sf_order['end_longitude'],
  108. 'tel' => $mobile,
  109. 'people_num' => $people_num,
  110. 'other_user_id' => $this->auth->id,
  111. ];
  112. try {
  113. $this->model->data($data)->save();
  114. $updata = [];
  115. $updata['more_seats'] = $sf_order['more_seats'] - $people_num;
  116. $updata['updatetime'] = time();
  117. $updata['order_money'] = $sf_order['order_money'] + ($people_num * $sf_order['car_price']);
  118. if ($sf_order['more_seats'] - $people_num == 0) {
  119. $updata['status'] = 3;
  120. }
  121. $this->model->where('id', $order_id)->update($updata);
  122. Db::commit();
  123. } catch (\Exception $e) {
  124. Db::rollback();
  125. $this->error('预约失败');
  126. }
  127. $sms_config = get_addon_config('alisms');
  128. $ddr_config = get_addon_config('ddrive');
  129. $mobiles = explode("\r\n", $ddr_config['noticeMobile']);
  130. \app\common\library\Sms::notice($ddr_config['noticeMobile'], ['type'=>'顺风车'], $sms_config['template']['notice']);
  131. $this->success('预约成功', ['order_id' => $this->model->id]);
  132. }
  133. /**
  134. * 发布预约订单
  135. *
  136. * @return void
  137. */
  138. public function ddriver_create()
  139. {
  140. $user = $this->auth->getUser();
  141. // 联系电话
  142. $mobile = $user['mobile'];
  143. if (!$mobile) {
  144. $this->error('请先去绑定手机号');
  145. }
  146. $start_address = $this->request->param('start_address');
  147. $end_address = $this->request->param('end_address');
  148. $start_name = $this->request->param('start_name');
  149. $end_name = $this->request->param('end_name');
  150. $start_latitude = $this->request->param('start_latitude', '');
  151. $start_longitude = $this->request->param('start_longitude', '');
  152. $end_latitude = $this->request->param('end_latitude', '');
  153. $end_longitude = $this->request->param('end_longitude', '');
  154. $order_type = $this->request->param('order_type', 2);
  155. $route = $this->request->param('route');
  156. $start_time = $this->request->param('start_time');
  157. $car_type = $this->request->param('car_type');
  158. $more_seats = $this->request->param('more_seats', 0);
  159. $car_price = $this->request->param('car_price');
  160. $remark = $this->request->param('remark');
  161. $start_city = $this->request->param('start_city');
  162. $end_city = $this->request->param('end_city');
  163. $people_num = $this->request->param('people_num', 0);
  164. $tel = $this->request->param('tel');
  165. if ($tel) {
  166. $mobile = $tel;
  167. } else {
  168. $mobile = $mobile;
  169. }
  170. if (!in_array($order_type, [1, 2])) {
  171. $this->error('公共参数错误');
  172. }
  173. $rule = [
  174. ['start_address', 'require', '请填写详细地址'],
  175. ['end_address', 'require', '请填写目的地详细地址'],
  176. ['start_name', 'require', '请填写出发地'],
  177. ['end_name', 'require', '请填写目的地'],
  178. ['start_time', 'require', '请选择出发时间'],
  179. ['start_city', 'require', '请选择出发城市'],
  180. ['end_city', 'require', '请选择目的地城市'],
  181. ];
  182. if ($order_type == 1) {
  183. $new_rule = [
  184. ['people_num', 'require', '请填写预约人数'],
  185. ];
  186. } else {
  187. $new_rule = [
  188. ['car_type', 'require', '请填写车型'],
  189. ['more_seats', 'require', '请填写余座'],
  190. ['car_price', 'require', '请填写座位价格'],
  191. ['route', 'require', '请输入路线'],
  192. ];
  193. }
  194. $rule = array_merge($rule, $new_rule);
  195. (new Check())->checkParam($rule);
  196. $user_verified = (new UserVerified())->where('user_id', $this->auth->id)->find();
  197. if ($user_verified) {
  198. if ($user_verified['real_verified'] != 1) {
  199. $this->error('请完成实名认证');
  200. }
  201. }
  202. if ($order_type == 2) {
  203. if (!$route || !$car_type || !$more_seats) {
  204. $this->error('司机端参数错误');
  205. }
  206. if ($user_verified['driver_verified'] != 1) {
  207. $this->error('请完成驾照认证');
  208. }
  209. if ($user_verified['card_verified'] != 1) {
  210. $this->error('请完成车辆认证');
  211. }
  212. }
  213. $data = [
  214. 'user_id' => $user['id'],
  215. 'order_type' => $order_type,
  216. 'start_address' => $start_address,
  217. 'start_name' => $start_name,
  218. 'end_address' => $end_address,
  219. 'end_name' => $end_name,
  220. 'start_latitude' => $start_latitude,
  221. 'start_longitude' => $start_longitude,
  222. 'end_latitude' => $end_latitude,
  223. 'end_longitude' => $end_longitude,
  224. 'route' => $route,
  225. 'start_time' => $start_time,
  226. 'car_type' => $car_type,
  227. 'more_seats' => $more_seats,
  228. 'car_price' => $car_price,
  229. 'remark' => $remark,
  230. 'start_city' => $start_city,
  231. 'end_city' => $end_city,
  232. 'pid' => 0,
  233. 'status' => 1,
  234. 'tel' => $mobile,
  235. 'people_num' => $people_num,
  236. ];
  237. $model = $this->model;
  238. $res = $model->data($data)->save();
  239. if ($res) {
  240. $this->success('订单创建成功', ['order_id' => $model->id]);
  241. } else {
  242. $this->error('订单创建失败');
  243. }
  244. }
  245. /**
  246. * 首页(司机端)
  247. *
  248. * @return void
  249. */
  250. public function order_index()
  251. {
  252. $order_type = $this->request->param('order_type', 2);
  253. $start_city = $this->request->param('start_city', '');
  254. $end_city = $this->request->param('end_city', '');
  255. $page = $this->request->param('page');
  256. if (!in_array($order_type, [1, 2])) {
  257. $this->error('公共参数错误');
  258. }
  259. // if (!$start_city) {
  260. // $this->error('请选择乘坐城市');
  261. // }
  262. $where = [];
  263. $where['SF.status'] = '1';
  264. //查询预约订单
  265. $sf_order = Db::name('ddrive_sf_order')->alias('SF')
  266. ->join('real_verified RV', 'RV.user_id = SF.user_id', 'LEFT')
  267. ->where('SF.start_address', 'like', '%' . $start_city . '%')
  268. ->where('SF.end_address', 'like', '%' . $end_city . '%')
  269. ->where('SF.order_type', $order_type)
  270. ->where($where)
  271. ->field('SF.*,RV.truename')
  272. ->order('SF.createtime desc')
  273. ->limit('10')
  274. ->page($page)
  275. ->select();
  276. if (!$sf_order) {
  277. $this->success('暂无预约单', []);
  278. }
  279. if ($order_type == 1) {
  280. foreach ($sf_order as $k => $v) {
  281. $order[] = [
  282. 'id' => $v['id'],
  283. 'start_city' => $v['start_city'],
  284. 'end_city' => $v['end_city'],
  285. 'start_address' => $v['start_address'],
  286. 'end_address' => $v['end_address'],
  287. 'start_name' => $v['start_name'],
  288. 'end_name' => $v['end_name'],
  289. 'people_num' => $v['people_num'],
  290. 'start_time' => date('m-d H:i', $v['start_time']),
  291. 'statusText' => Lib::getStatus($v['status']),
  292. ];
  293. }
  294. } else {
  295. foreach ($sf_order as $k => $v) {
  296. //司机评分计算
  297. $score_sum = Db::name('ddrive_sf_order_comment')->where('driver_id', $v['user_id'])->sum('score');
  298. $score_count = Db::name('ddrive_sf_order_comment')->where('driver_id', $v['user_id'])->count();
  299. if ($score_sum) {
  300. $score = round($score_sum / $score_count);
  301. } else {
  302. $score = 5;
  303. }
  304. $order[] = [
  305. 'id' => $v['id'],
  306. 'driver_name' => mb_substr($v['truename'], 0, 1) . '师傅',
  307. 'car_type' => $v['car_type'],
  308. 'route' => $v['route'],
  309. 'remark' => $v['remark'] ? $v['remark'] : '顺路上下,预定后电话确认一下。',
  310. 'start_city' => $v['start_city'],
  311. 'end_city' => $v['end_city'],
  312. 'start_address' => $v['start_address'],
  313. 'start_name' => $v['start_name'],
  314. 'end_name' => $v['end_name'],
  315. 'end_address' => $v['end_address'],
  316. 'more_seats' => $v['more_seats'],
  317. 'car_price' => $v['car_price'],
  318. 'score' => $score,
  319. 'start_time' => date('m-d H:i', $v['start_time']),
  320. 'statusText' => Lib::getStatus($v['status']),
  321. ];
  322. }
  323. }
  324. $this->success('成功', $order);
  325. }
  326. /**
  327. * 司机接单(用户预约单)
  328. *
  329. * @return void
  330. */
  331. public function ddrive_taking()
  332. {
  333. $order_id = $this->request->param('order_id');
  334. $sf_order = $this->model->where('id', $order_id)->find();
  335. if (!$sf_order) {
  336. $this->error('该订单不存在');
  337. }
  338. $user_verified = (new UserVerified())->where('user_id', $this->auth->id)->find();
  339. if ($user_verified) {
  340. if ($user_verified['real_verified'] != 1) {
  341. $this->error('请完成实名认证');
  342. }
  343. if ($user_verified['driver_verified'] != 1) {
  344. $this->error('请完成驾照认证');
  345. }
  346. if ($user_verified['card_verified'] != 1) {
  347. $this->error('请完成车辆认证');
  348. }
  349. }
  350. if ($sf_order['status'] != 1) {
  351. $this->error('该订单在接单状态');
  352. }
  353. $ret = $this->model->where('id', $order_id)->update(['status' => 2, 'other_user_id' => $this->auth->id]);
  354. if ($ret) {
  355. $this->success('接单成功', '');
  356. } else {
  357. $this->error('接单失败');
  358. }
  359. }
  360. /**
  361. * 订单详情
  362. *
  363. * @return void
  364. */
  365. public function info()
  366. {
  367. $order_id = $this->request->param('order_id');
  368. $sf_order = Db::name('ddrive_sf_order')->alias('SF')
  369. ->where('SF.id', $order_id)
  370. ->find();
  371. //**判断订单是否存在
  372. if (!$sf_order) {
  373. $this->error('该订单不存在');
  374. }
  375. if ($sf_order['order_type'] == 1) {
  376. $sf_orderinfo = Db::name('ddrive_sf_order')->alias('SF')
  377. ->join('user UV', 'UV.id = SF.other_user_id', 'LEFT')
  378. ->join('real_verified RV', 'RV.user_id = SF.other_user_id', 'LEFT')
  379. ->join('ddrive_sf_order_comment OM', 'OM.order_id = SF.id', 'LEFT')
  380. ->where('SF.id', $order_id)
  381. ->field('SF.*,UV.mobile,RV.truename,OM.score')
  382. ->find();
  383. $chil_order = [];
  384. $info = [
  385. 'id' => $sf_orderinfo['id'],
  386. 'truename' => mb_substr($sf_orderinfo['truename'], 0, 1) . '师傅',
  387. 'order_type' => $sf_orderinfo['order_type'],
  388. 'sf_type' => 1,
  389. 'start_city' => $sf_orderinfo['start_city'],
  390. 'end_city' => $sf_orderinfo['end_city'],
  391. 'start_address' => $sf_orderinfo['start_address'],
  392. 'end_address' => $sf_orderinfo['end_address'],
  393. 'start_name' => $sf_orderinfo['start_name'],
  394. 'end_name' => $sf_orderinfo['end_name'],
  395. 'route' => $sf_orderinfo['route'] ? $sf_orderinfo['route'] : '',
  396. 'car_type' => $sf_orderinfo['car_type'] ? $sf_orderinfo['car_type'] : '',
  397. 'car_price' => $sf_orderinfo['car_price'] ? $sf_orderinfo['car_price'] : 0,
  398. 'people_num' => $sf_orderinfo['people_num'],
  399. 'newtel' => substr($sf_orderinfo['tel'], 7, 4),
  400. 'tel' => $sf_orderinfo['tel'],
  401. 'driver_tel' => $sf_orderinfo['mobile'],
  402. 'score' => $sf_orderinfo['score'] ? $sf_orderinfo['score'] : 0,
  403. 'start_time' => date('m-d H:i', $sf_orderinfo['start_time']),
  404. 'order_money' => $sf_orderinfo['order_money'] ? $sf_orderinfo['order_money'] : '',
  405. 'status' => $sf_orderinfo['status'],
  406. 'pay_status' => $sf_orderinfo['pay_status'],
  407. 'more_seats' => $sf_orderinfo['more_seats'] ? $sf_orderinfo['more_seats'] : 0,
  408. 'platform_service_fee' => $sf_orderinfo['platform_service_fee'],
  409. 'pay_type' => $sf_orderinfo['pay_type'],
  410. 'remark' => $sf_orderinfo['remark'],
  411. 'statusText' => Lib::getStatus($sf_orderinfo['status']),
  412. 'chil_order' => $chil_order,
  413. ];
  414. }
  415. if ($sf_order['order_type'] == 2) {
  416. $sf_orderinfo = Db::name('ddrive_sf_order')->alias('SF')
  417. ->join('user UV', 'UV.id = SF.user_id', 'LEFT')
  418. ->join('real_verified RV', 'RV.user_id = SF.user_id', 'LEFT')
  419. ->join('ddrive_sf_order_comment OM', 'OM.order_id = SF.id', 'LEFT')
  420. ->where('SF.id', $order_id)
  421. ->field('SF.*,UV.mobile,RV.truename,OM.score')
  422. ->find();
  423. //**子订单
  424. $chil_order = $this->model
  425. ->where('order_type', $sf_order['order_type'])
  426. ->where('pid', $order_id)
  427. ->field('id,tel,people_num,car_price,start_address,pay_status,start_name,status,order_money')
  428. ->select();
  429. foreach ($chil_order as $k => $v) {
  430. $chil_order[$k]['statusText'] = Lib::getStatus($v['status']);
  431. }
  432. $info = [
  433. 'id' => $sf_orderinfo['id'],
  434. 'truename' => mb_substr($sf_orderinfo['truename'], 0, 1) . '师傅',
  435. 'order_type' => $sf_orderinfo['order_type'],
  436. 'sf_type' => 1,
  437. 'start_city' => $sf_orderinfo['start_city'],
  438. 'end_city' => $sf_orderinfo['end_city'],
  439. 'start_address' => $sf_orderinfo['start_address'],
  440. 'end_address' => $sf_orderinfo['end_address'],
  441. 'start_name' => $sf_orderinfo['start_name'],
  442. 'end_name' => $sf_orderinfo['end_name'],
  443. 'route' => $sf_orderinfo['route'] ? $sf_orderinfo['route'] : '',
  444. 'car_type' => $sf_orderinfo['car_type'] ? $sf_orderinfo['car_type'] : '',
  445. 'car_price' => $sf_orderinfo['car_price'] ? $sf_orderinfo['car_price'] : 0,
  446. 'people_num' => $sf_orderinfo['people_num'],
  447. 'newtel' => substr($sf_orderinfo['tel'], 7, 4),
  448. 'tel' => $sf_orderinfo['tel'],
  449. 'driver_tel' => $sf_orderinfo['mobile'],
  450. 'score' => $sf_orderinfo['score'] ? $sf_orderinfo['score'] : 0,
  451. 'start_time' => date('m-d H:i', $sf_orderinfo['start_time']),
  452. 'order_money' => $sf_orderinfo['order_money'] ? $sf_orderinfo['order_money'] : '',
  453. 'status' => $sf_orderinfo['status'],
  454. 'pay_status' => $sf_orderinfo['pay_status'],
  455. 'more_seats' => $sf_orderinfo['more_seats'] ? $sf_orderinfo['more_seats'] : 0,
  456. 'platform_service_fee' => $sf_orderinfo['platform_service_fee'],
  457. 'pay_type' => $sf_orderinfo['pay_type'],
  458. 'remark' => $sf_orderinfo['remark'],
  459. 'statusText' => Lib::getStatus($sf_orderinfo['status']),
  460. 'chil_order' => $chil_order,
  461. ];
  462. }
  463. $this->success('成功', $info);
  464. }
  465. /**
  466. * 确认订单(用户预约单)
  467. *
  468. * @return void
  469. */
  470. public function confirm()
  471. {
  472. $order_id = $this->request->param('order_id');
  473. $order_money = $this->request->param('order_money');
  474. if (!$order_id || !$order_money) {
  475. $this->error('参数错误');
  476. }
  477. $sf_order = $this->model->where('id', $order_id)->find();
  478. if (!$sf_order) {
  479. $this->error('该订单不存在');
  480. }
  481. if ($sf_order['status'] != 2) {
  482. $this->error('该订单不在确认状态');
  483. }
  484. $car_price = bcdiv($order_money, $sf_order['people_num'], 2);
  485. $ret = $this->model->where('id', $order_id)->update(['status' => 3, 'order_money' => $order_money, 'car_price' => $car_price]);
  486. if ($ret) {
  487. $this->success('确认接单');
  488. } else {
  489. $this->error('失败');
  490. }
  491. }
  492. /**
  493. * 取消订单
  494. *
  495. * @return void
  496. */
  497. public function cancel()
  498. {
  499. $order_id = $this->request->param('order_id');
  500. $cancel_type = $this->request->param('cancel_type');
  501. $sf_order = $this->model->where('id', $order_id)->find();
  502. if (!$sf_order) {
  503. $this->error('该订单不存在');
  504. }
  505. if (!in_array($sf_order['status'], [1, 2, 3])) {
  506. $this->error('该订单无法取消');
  507. }
  508. if ($sf_order['status'] == -1) {
  509. $this->error('该订单已取消');
  510. }
  511. try {
  512. if ($sf_order['order_type'] == 1) {
  513. if ($sf_order['other_user_id'] == $this->auth->id && !$cancel_type) {//司机取消
  514. $data = [
  515. 'status' => 1,
  516. 'updatetime' => time(),
  517. 'other_user_id' => 0,
  518. ];
  519. } else {//用户取消
  520. $data = [
  521. 'status' => -1,
  522. 'cancel_time' => time(),
  523. 'updatetime' => time(),
  524. 'cancel_type' => $cancel_type,
  525. ];
  526. }
  527. } else {
  528. if ($sf_order['other_user_id'] == $this->auth->id && $cancel_type) {//用户取消
  529. $data = [
  530. 'status' => -1,
  531. 'cancel_time' => time(),
  532. 'updatetime' => time(),
  533. 'cancel_type' => $cancel_type,
  534. ];
  535. Db::name('ddrive_sf_order')->where('id', $sf_order['pid'])->setInc('more_seats', $sf_order['people_num']);
  536. } else {//司机取消
  537. //检测是否有客户预约
  538. $chile_order = Db::name('ddrive_sf_order')->where('pid', $order_id)->where('status', '<>', '-1')->column('id');
  539. if ($chile_order) {
  540. $data = [
  541. 'updatetime' => time(),
  542. ];
  543. $this->model->where('id', $order_id)->update(['more_seats' => 0]);
  544. } else {
  545. $data = [
  546. 'status' => -1,
  547. 'cancel_time' => time(),
  548. 'updatetime' => time(),
  549. ];
  550. }
  551. }
  552. }
  553. $this->model->where('id', $order_id)->update($data);
  554. Db::commit();
  555. } catch (\Exception $e) {
  556. Db::rollback();
  557. $this->error('取消失败');
  558. }
  559. $this->success('取消成功');
  560. }
  561. /**
  562. * 司机开始出发
  563. *
  564. * @return void
  565. */
  566. public function set_out()
  567. {
  568. $order_id = $this->request->param('order_id');
  569. $sf_order = $this->model->where('id', $order_id)->find();
  570. if (!$sf_order) {
  571. $this->error('该订单不存在');
  572. }
  573. if ($sf_order['status'] == -1) {
  574. $this->error('该订单已取消');
  575. }
  576. if ($sf_order['status'] == -2) {
  577. $this->error('该订单已超时');
  578. }
  579. if ($sf_order['status'] == 4) {
  580. $this->error('该订单已出发');
  581. }
  582. if ($sf_order['status'] == 5) {
  583. $this->error('该订单已完成');
  584. }
  585. if ($sf_order['order_type'] == 2) {
  586. $chil_order_id = Db::name('ddrive_sf_order')
  587. ->where('order_type', $sf_order['order_type'])
  588. ->where('pid', $order_id)
  589. ->column('id');
  590. array_push($chil_order_id, $order_id);
  591. $ret = $this->model->whereIn('id', $chil_order_id)->update(['status' => 4, 'updatetime' => time()]);
  592. } else {
  593. $ret = $this->model->where('id', $order_id)->update(['status' => 4, 'updatetime' => time()]);
  594. }
  595. if ($ret) {
  596. $this->success('出发成功');
  597. } else {
  598. $this->error('失败');
  599. }
  600. }
  601. /**
  602. * 线下结算
  603. *
  604. * @return void
  605. */
  606. public function offline_settlement()
  607. {
  608. $order_id = $this->request->param('order_id'); //司机端发布 传子单id
  609. $sf_order = $this->model->where('id', $order_id)->find();
  610. if (!$sf_order) {
  611. $this->error('该订单不存在');
  612. }
  613. if ($sf_order['pay_status'] == 1) {
  614. $this->error('该订单已结算');
  615. }
  616. if ($sf_order['status'] != 4) {
  617. $this->error('该订单不在结算状态');
  618. }
  619. //服务费率
  620. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'];
  621. if ($sf_order['order_type'] == 1) {
  622. $platform_service_fee = number_format(($sf_order['order_money'] * ($platform_service_fee / 100)), 2);
  623. } else {
  624. $platform_service_fee = number_format((($sf_order['car_price'] * $sf_order['people_num']) * ($platform_service_fee / 100)), 2);
  625. }
  626. $data = [
  627. 'pay_type' => 3,
  628. 'pay_time' => time(),
  629. 'pay_status' => 1,
  630. 'platform_service_fee' => $platform_service_fee,
  631. ];
  632. $this->model->where('id', $order_id)->update($data);
  633. try {
  634. if ($sf_order['order_type'] == 2) {//司机发布预约单,存在子单
  635. $chil_order_id = Db::name('ddrive_sf_order')->where('pid', $sf_order['pid'])->where('pay_status', 0)->column('id');
  636. if (!$chil_order_id) { // 无子单未支付
  637. $updata = [
  638. 'pay_type' => 3,
  639. 'pay_time' => time(),
  640. 'pay_status' => 1,
  641. ];
  642. $this->model->where('id', $sf_order['pid'])->update($updata);
  643. }
  644. $user_order_platform_service_fee = Db::name('ddrive_sf_order')->where('id', $sf_order['pid'])->value('platform_service_fee');
  645. //累加服务费
  646. Db::name('ddrive_sf_order')->where('id', $sf_order['pid'])->update(['platform_service_fee' => $user_order_platform_service_fee + $platform_service_fee]);
  647. }
  648. //累加服务费
  649. Db::name('user')->where('id', $sf_order['user_id'])->setInc('platform_service_fee', $platform_service_fee);
  650. Db::commit();
  651. } catch (\Exception $e) {
  652. Db::rollback();
  653. $this->error('线下结算失败');
  654. }
  655. $this->success('线下结算成功');
  656. }
  657. /**
  658. * 行程结束
  659. *
  660. * @return void
  661. */
  662. public function order_complete()
  663. {
  664. $order_id = $this->request->param('order_id');
  665. $sf_order = $this->model->where('id', $order_id)->find();
  666. if (!$sf_order) {
  667. $this->error('该订单不存在');
  668. }
  669. if ($sf_order['pay_status'] != 1 && $sf_order['order_type'] == 1) {
  670. $this->error('乘客还未支付');
  671. }
  672. if ($sf_order['status'] == 5) {
  673. $this->error('该行程已完成');
  674. }
  675. $data = [
  676. 'status' => 5,
  677. 'updatetime' => time(),
  678. 'complete_time' => time(),
  679. ];
  680. try {
  681. if ($sf_order['order_type'] == 2) {//司机发布预约单,存在子单
  682. $chil_order_id = Db::name('ddrive_sf_order')->where('pid', $sf_order['id'])->where('pay_status', 0)->column('id');
  683. if ($chil_order_id) { // 有未支付订单
  684. exception('乘客还未支付');
  685. }
  686. $this->model->where('pid', $sf_order['id'])->update($data);
  687. }
  688. $this->model->where('id', $order_id)->update($data);
  689. Db::commit();
  690. } catch (\Exception $e) {
  691. Db::rollback();
  692. $this->error($e->getMessage());
  693. }
  694. $this->success('结束成功');
  695. }
  696. /**
  697. * 顺风车发布
  698. *
  699. * @return void
  700. */
  701. public function release_order()
  702. {
  703. $page = $this->request->param('page', 1);
  704. $where = "(user_id = '" . $this->auth->id . "' OR other_user_id = '" . $this->auth->id . ",') AND pid = 0 ";
  705. $field = 'id,start_time,start_city,start_address,end_city,end_address,more_seats,start_name,end_name,status';
  706. $sf_order = $this->model->where($where)->where('order_type', 2)->field($field)->limit(10)->page($page)->order('createtime desc')->select();
  707. foreach ($sf_order as $k => $v) {
  708. $sf_order[$k]['week'] = Lib::getWeek(date("w", $v['start_time']));
  709. $sf_order[$k]['start_time'] = date('m-d H:i', $v['start_time']);
  710. $sf_order[$k]['statusText'] = Lib::getStatus($v['status']);
  711. }
  712. $this->success('成功', $sf_order ? $sf_order : []);
  713. }
  714. /**
  715. * 预约人次(弃用)
  716. *
  717. * @return void
  718. */
  719. public function release_people()
  720. {
  721. $order_id = $this->request->param('order_id');
  722. $release_people = $this->model->where('pid', $order_id)->field('id,tel,people_num,car_price,start_address')->select();
  723. if (!$release_people) {
  724. $this->error('该订单不存在');
  725. }
  726. $this->success('成功', $release_people);
  727. }
  728. /**
  729. * 我的订单
  730. *
  731. * @return void
  732. */
  733. public function user_order()
  734. {
  735. $type = $this->request->param('type');
  736. $page = $this->request->param('page', 1);
  737. if ($type == 1) {
  738. $where = "(user_id = '" . $this->auth->id . "' AND order_type = 1) OR (other_user_id = '" . $this->auth->id . "' AND order_type = 2 AND pid != 0)";
  739. $status = ['-2', '-1', '1', '2', '3', '4', '5'];
  740. } else {
  741. $where = "(other_user_id = '" . $this->auth->id . "' AND order_type = 1) OR (user_id = '" . $this->auth->id . "' AND order_type = 2 AND pid = 0)";
  742. $status = ['-2', '-1', '5'];
  743. }
  744. $field = 'id,order_type,start_time,order_money,start_city,start_address,end_city,end_address,status,platform_service_fee,pay_type,start_name,end_name,more_seats,people_num';
  745. $user_order = $this->model->where($where)->whereIn('status', $status)->field($field)->limit(10)->page($page)->order('createtime desc')->select();
  746. foreach ($user_order as $k => $v) {
  747. $user_order[$k]['week'] = Lib::getWeek(date("w", $v['start_time']));
  748. $user_order[$k]['start_time'] = date('m-d H:i', $v['start_time']);
  749. $user_order[$k]['platform_service_fee'] = $v['platform_service_fee'];
  750. $user_order[$k]['statusText'] = Lib::getStatus($v['status']);
  751. }
  752. $this->success('成功', $user_order);
  753. }
  754. /**
  755. * 推荐路线
  756. *
  757. * @return void
  758. */
  759. public function recommend_route()
  760. {
  761. $route = [
  762. '杭州市' . '->' . '郑州市',
  763. '杭州市' . '->' . '绍兴市',
  764. '杭州市' . '->' . '温州市',
  765. '杭州市' . '->' . '丽水市',
  766. '杭州市' . '->' . '北京市',
  767. '杭州市' . '->' . '上海市',
  768. '杭州市' . '->' . '武汉市',
  769. '杭州市' . '->' . '苏州市',
  770. ];
  771. $this->success('成功', $route);
  772. }
  773. /**
  774. * 首页状态(用户端订单)
  775. *
  776. * @return void
  777. */
  778. public function index_order()
  779. {
  780. if($this->auth->id){
  781. $where = "(user_id = '" . $this->auth->id . "' AND order_type = 1) OR (other_user_id = '" . $this->auth->id . "' AND order_type = 2 AND pid != 0)";
  782. $index_order = $this->model->where($where)->whereIn('status', [1, 2, 3, 4])->field('id,start_city,end_city,status,start_time,order_type')->select();
  783. foreach ($index_order as $k => $v) {
  784. $index_order[$k]['start_time'] = date('m-d H:i');
  785. $index_order[$k]['statusText'] = Lib::getStatus($v['status']);
  786. }
  787. }else{
  788. $index_order = [];
  789. }
  790. $this->success('成功', $index_order ? $index_order : []);
  791. }
  792. /**
  793. * 订单评价
  794. *
  795. * @return void
  796. */
  797. public function comment()
  798. {
  799. $orderId = $this->request->param('order_id');
  800. // 判断订单是否存在
  801. $order = $this->model->where('id', $orderId)->find();
  802. if (!$order) {
  803. $this->error('订单不存在');
  804. }
  805. if ($order['order_type'] == 1) {
  806. $driver_id = $order['other_user_id'];
  807. } else {
  808. $driver_id = $order['user_id'];
  809. }
  810. $comment = Db::name('ddrive_sf_order_comment')->where('order_id', $orderId)->find();
  811. if ($comment) {
  812. $this->error('已评价');
  813. }
  814. $score = $this->request->param('score', 5);
  815. $data = [
  816. 'user_id' => $this->auth->id,
  817. 'order_id' => $orderId,
  818. 'score' => $score,
  819. 'driver_id' => $driver_id,
  820. 'createtime' => time(),
  821. ];
  822. $res = Db::name('ddrive_sf_order_comment')->insert($data);
  823. if ($res) {
  824. $this->model->where('id', $orderId)->setField('assess', 1);
  825. $this->success('评价成功');
  826. } else {
  827. $this->error('评价失败');
  828. }
  829. }
  830. /**
  831. * 司机端订单支付
  832. *
  833. * @return void
  834. */
  835. public function driver_pay()
  836. {
  837. $site = Config::get('site');
  838. $orderId = $this->request->param('order_id');
  839. $id = $this->request->param('coupon_id');
  840. $code = $this->request->param('code');
  841. $type = $this->request->param('type', 'user_wechat'); // driver_wechat 司机端 user_wechat 用户端 mini_wechat 小程序
  842. // 判断订单是否结束
  843. $order = $this->model->where('id', $orderId)->find();
  844. if (!$order) {
  845. $this->error('订单不存在');
  846. }
  847. if ($id) {
  848. $coupon_list = Db::name('user_coupon')
  849. ->where('id', $id)
  850. ->field('limit_price,coupon_price,coupon_status')
  851. ->find();
  852. if (!$coupon_list) {
  853. $this->error('该优惠券不存在');
  854. }
  855. if ($coupon_list['coupon_status'] == 2) {
  856. $this->error('该优惠券已过期');
  857. }
  858. if ($order['order_money'] < $coupon_list['limit_price']) {
  859. $this->error('该订单没达到最低抵扣额');
  860. }
  861. $order_money = $order['order_money'] - $coupon_list['coupon_price'];
  862. } else {
  863. $order_money = $order['order_money'];
  864. }
  865. $method = $this->request->param('method', 'app');
  866. // 用户信息
  867. $user = $this->auth->getUser();
  868. //订单标题
  869. $title = $site['title'] . ' - 订单费用';
  870. // 订单编号
  871. $out_trade_no = $order['id'] . '-' . time();
  872. // openid
  873. $openid = '';
  874. if ($type == 'mini_wechat') {
  875. $openid = Db::name('ddrive_user_token')->where('user_id', $user['id'])->value('mini_openid');
  876. }
  877. if (!$openid && $type == 'mini_wechat') {
  878. $info = (new \addons\ddrive\library\Common())->getOpenid($code);
  879. if (isset($info['openid'])) {
  880. $openid = $info['openid'];
  881. } else {
  882. return json_encode(['code' => 0, 'msg' => '失败']);
  883. }
  884. }
  885. //回调链接
  886. $notifyurl = $this->request->root(true) . '/addons/ddrive/order/notifyx/paytype/wechat';
  887. $params = [
  888. 'type' => $type,
  889. 'orderid' => $out_trade_no,
  890. 'title' => $title,
  891. 'amount' => $order_money,
  892. 'method' => $method,
  893. 'openid' => $openid,
  894. 'notifyurl' => $notifyurl,
  895. ];
  896. try {
  897. if ($id) {
  898. $upcoupon = [];
  899. $upcoupon['order_id'] = $orderId;
  900. $upcoupon['usage_time'] = time();
  901. $upcoupon['coupon_status'] = 1;
  902. Db::name('user_coupon')->where('id', $id)->update($upcoupon);
  903. }
  904. $pay = json_encode(Service::submitOrder($params));
  905. if ($type == 'mini_wechat') {
  906. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode($pay, true)]);
  907. } else {
  908. return json_encode(['code' => 1, 'msg' => '成功', 'data' => json_decode(json_decode($pay, true), true)]);
  909. }
  910. } catch (\Throwable $th) {
  911. return json_encode(['code' => 0, 'msg' => '失败']);
  912. }
  913. }
  914. /**
  915. * 支付成功
  916. *
  917. * @return void
  918. */
  919. public function notifyx()
  920. {
  921. Log::record('支付回调');
  922. $paytype = $this->request->param('paytype');
  923. $pay = \addons\epay\library\Service::checkNotify($paytype);
  924. if (!$pay) {
  925. Log::record('签名错误');
  926. echo '签名错误';
  927. return;
  928. }
  929. $data = $pay->verify();
  930. try {
  931. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  932. $pay_type = $paytype == 'alipay' ? 2 : 1;
  933. $out_trade_no = $data['out_trade_no'];
  934. Log::record('订单编号:' . $out_trade_no);
  935. $order_id = explode('-', $out_trade_no)[0];
  936. $order = $this->model->where('id', $order_id)->find();
  937. if ($order['pay_status'] != 1) {
  938. //平台服务费
  939. $platform_service_fee = get_addon_config('ddrive')['platform_service_fee'] / 100;
  940. $fee = round($platform_service_fee * $order['price'], 2);
  941. $update = [];
  942. $update['pay_status'] = 1;
  943. $update['pay_time'] = time();
  944. $update['pay_type'] = $pay_type;
  945. $update['status'] = 5;
  946. $update['platform_service_fee'] = $fee;
  947. $this->model->where('id', $order_id)->update($update);
  948. // 增加司机余额
  949. Db::name('user')->where('id', $order['driver_id'])->setInc('money', $order['price']);
  950. Db::name('details')->insert([
  951. 'user_id' => $order['driver_id'],
  952. 'fluctuate_type' => 1,
  953. 'msg' => '顺风车收入',
  954. 'amount' => $order['price'],
  955. 'assets_type' => 2,
  956. 'source_type' => 2,
  957. 'createtime' => time(),
  958. 'form_id' => $order_id,
  959. ]);
  960. }
  961. } catch (Exception $e) {
  962. Log::record($e->getMessage());
  963. }
  964. echo $pay->success();
  965. }
  966. /**更新司机端首页订单信息
  967. * order_refresh
  968. * @des
  969. */
  970. public function order_refresh()
  971. {
  972. $orderId = $this->request->param('order_id');
  973. $city = $this->request->param('city');
  974. $order_type = $this->request->param('order_type', 1);
  975. if (!$orderId) {
  976. $info = $this->model->where('start_city', 'like', '%' . $city . '%')->where('order_type', $order_type)->order('createtime desc')->where('status', '1')->select();
  977. $this->success('', $info);
  978. }
  979. // 判断订单是否存在
  980. $createtime = $this->model->where('id', $orderId)->value('createtime');
  981. if (!$createtime) {
  982. $this->success('', []);
  983. }
  984. $info = $this->model->where('createtime', '>', $createtime)->where('start_city', 'like', '%' . $city . '%')->where('order_type', $order_type)->where('status', '1')->select();
  985. foreach ($info as $k => $v) {
  986. $info[$k]['start_time'] = date('m-d H:i', $v['start_time']);
  987. $info[$k]['statusText'] = Lib::getStatus($v['status']);
  988. }
  989. if ($info) {
  990. $this->success('', $info);
  991. }
  992. $this->success('', []);
  993. }
  994. /**剔除司机端首页订单信息
  995. * order_refresh
  996. * @des
  997. */
  998. public function order_eliminate()
  999. {
  1000. $city = $this->request->param('city');
  1001. $order_type = $this->request->param('order_type', 1);
  1002. $info = $this->model->where('start_city', 'like', '%' . $city . '%')->where('status', '<>', '1')->where('order_type', $order_type)->column('id');
  1003. if ($info) {
  1004. $this->success('', $info);
  1005. }
  1006. $this->success('', []);
  1007. }
  1008. }