Order.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Loader;
  7. /**
  8. * 首页接口
  9. */
  10. class Order extends Api
  11. {
  12. protected $noNeedLogin = ['refundReason','complaintReason','refundInfo','complaintInfo'];
  13. protected $noNeedRight = ['*'];
  14. public function settle()
  15. {
  16. if (!$this->request->isPost()) {
  17. $this->error('请求方式异常');
  18. }
  19. $user = $this->auth->getUserinfo();
  20. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  21. $data = [];
  22. try{
  23. $data['settle'] = \app\api\model\service\Order::settle($post);
  24. $data['money'] = \app\api\model\service\User::getMoney($user['id']);
  25. $data['userInfo'] = \app\api\model\service\UserInfo::where(['user_id'=>$user['id']])->field('is_plus,discount')->find();
  26. $data['skillInfo'] = isset($post['skill_id'])?\app\api\model\service\Skill::getOrderSkill($post['skill_id']):'';
  27. $data['timeInfo'] = isset($post['skill_id'])?\app\api\model\service\SkillTime::getSettleTime($post['skill_id']):\app\api\model\service\Shop::getShopTime();
  28. } catch (Exception $e) {
  29. $this->error('下单结算失败',$e->getMessage());
  30. }
  31. $this->success('信息返回成功',$data);
  32. }
  33. /**
  34. * 计算车费
  35. * @return void
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public function travelPrice()
  41. {
  42. if (!$this->request->isPost()) {
  43. $this->error('请求方式异常');
  44. }
  45. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  46. $goodsConfig = \app\api\model\service\Goods::where(['id'=>$post['goods_id']])->value('is_travel');
  47. !$goodsConfig && $this->error('当前项目未开启出行费用配置');
  48. $timeInfo = \app\api\model\service\SkillTime::where(['id'=>$post['time_id'],'state'=>0])->field('id,starttime')->find();
  49. $hour = date("H",$timeInfo['starttime']);
  50. $cityConfig = \app\api\model\service\CityConfig::where(['city'=>$post['city']])->find();
  51. if($post['traveltype'] == 2)
  52. {
  53. ($hour>=$cityConfig['bus_end'] || $hour<$cityConfig['bus_start']) && $this->error('当前城市'.$cityConfig['bus_start'].'点-'.$cityConfig['bus_end'].'点可选择公交地铁出行方式');
  54. }
  55. $re = '';
  56. try{
  57. $re = \app\api\model\service\CityConfig::settleTravelPrice($post);
  58. $re['skillInfo'] = \app\api\model\service\Skill::checkAcceptState($post['skill_id'],$post['goods_id']);
  59. $re['cityConfig'] = $cityConfig;
  60. } catch (Exception $e) {
  61. $this->error('计算出行费用失败',$e->getMessage());
  62. }
  63. $this->success('出行费用返回成功',$re);
  64. }
  65. /**
  66. * 创建订单
  67. * @return void
  68. */
  69. public function createOrder()
  70. {
  71. if (!$this->request->isPost()) {
  72. $this->error('请求方式异常');
  73. }
  74. $uid = $this->auth->id;
  75. $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
  76. $validate = Loader::validate('service.Order');
  77. if(!$validate->scene('add')->check($post)){
  78. $this->error($validate->getError());
  79. }
  80. $re = '';
  81. Db::startTrans();
  82. try{
  83. $post['user_id'] = $uid;
  84. $re = \app\api\model\service\Order::createOrder($post);
  85. Db::commit();
  86. } catch (Exception $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. }
  90. $this->success('支付信息返回成功',$re);
  91. }
  92. public function pay()
  93. {
  94. if (!$this->request->isPost()) {
  95. $this->error('请求方式异常');
  96. }
  97. $uid = $this->auth->id;
  98. $id = input('id/d','');
  99. $paytype = input('paytype/d','');
  100. $re = '';
  101. Db::startTrans();
  102. try{
  103. $re = \app\api\model\service\Order::pay(['id'=>$id,'user_id'=>$uid,'paytype'=>$paytype]);
  104. Db::commit();
  105. } catch (Exception $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. }
  109. $this->success('支付信息返回成功',$re);
  110. }
  111. /**
  112. * 订单补差价
  113. * @return void
  114. */
  115. public function premiumOrder()
  116. {
  117. if (!$this->request->isPost()) {
  118. $this->error('请求方式异常');
  119. }
  120. $uid = $this->auth->id;
  121. $id = input('id/d','');
  122. $price = sprintf("%.2f",input('price',''));
  123. $price<=0 && $this->error('差价金额异常,无法支付');
  124. $paytype = input('paytype/d',0);
  125. $orderExist = \app\api\model\service\Order::where(['id'=>$id,'user_id'=>$uid,'status'=>['in',[1,2,3,4,5]]])->value('id');
  126. !$orderExist && $this->error('当前订单无法补差价,请联系管理员');
  127. $re = '';
  128. Db::startTrans();
  129. try{
  130. $re = \app\api\model\service\PremiumOrder::createOrder(['order_id'=>$id,'payprice'=>$price,'user_id'=>$uid,'paytype'=>$paytype]);
  131. Db::commit();
  132. } catch (Exception $e) {
  133. Db::rollback();
  134. $this->error('支付拉起失败',$e->getMessage());
  135. }
  136. $this->success('支付信息返回成功',$re);
  137. }
  138. /**
  139. * 附加项目创建订单
  140. * @return void
  141. */
  142. public function addOrder()
  143. {
  144. if (!$this->request->isPost()) {
  145. $this->error('请求方式异常');
  146. }
  147. $uid = $this->auth->id;
  148. $post = input('post.','','trim,strip_tags');
  149. $re = '';
  150. Db::startTrans();
  151. try{
  152. $re = \app\api\model\service\AddOrder::createOrder(['order_id'=>$post['id'],'add_ids'=>$post['add_ids'],'user_id'=>$uid,'paytype'=>$post['paytype']]);
  153. Db::commit();
  154. } catch (Exception $e) {
  155. Db::rollback();
  156. $this->error('支付拉起失败',$e->getMessage());
  157. }
  158. $this->success('支付信息返回成功',$re);
  159. }
  160. /**
  161. * 用户订单列表
  162. * @return void
  163. */
  164. public function userOrderList()
  165. {
  166. if (!$this->request->isPost()) {
  167. $this->error('请求方式异常');
  168. }
  169. $uid = $this->auth->id;
  170. $post = input('post.','','trim,strip_tags');
  171. $post['user_id'] = $uid;
  172. $re = [];
  173. try{
  174. $re = \app\api\model\service\Order::getOrderList($post);
  175. } catch (Exception $e) {
  176. $this->error($e->getMessage());
  177. }
  178. $this->success('信息返回成功',$re);
  179. }
  180. /**
  181. * 商户端订单列表
  182. * @return void
  183. */
  184. public function shopOrderList()
  185. {
  186. if (!$this->request->isPost()) {
  187. $this->error('请求方式异常');
  188. }
  189. $uid = $this->auth->id;
  190. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,goods_ids,city')->find();
  191. $post = input('post.','','trim,strip_tags');
  192. if(!isset($post['is_pool']))
  193. {
  194. $post['shop_id'] = $shop['id'];
  195. }else{
  196. $post['city'] = $shop['city'];
  197. $post['is_pool'] = 1;
  198. $post['goods_ids'] = $shop['goods_ids'];
  199. }
  200. $re = [];
  201. try{
  202. $re = \app\api\model\service\Order::getOrderList($post);
  203. } catch (Exception $e) {
  204. $this->error($e->getMessage());
  205. }
  206. $this->success('信息返回成功',$re);
  207. }
  208. /**
  209. * 服务者订单列表
  210. * @return void
  211. */
  212. public function skillOrderList()
  213. {
  214. if (!$this->request->isPost()) {
  215. $this->error('请求方式异常');
  216. }
  217. $uid = $this->auth->id;
  218. $skill = \app\api\model\service\Skill::where(['user_id'=>$uid])->field('id,goods_ids,city')->find();
  219. !$skill && $this->error('非服务者无法访问');
  220. $get = input('post.','','trim,strip_tags');
  221. if(!isset($get['is_pool']))
  222. {
  223. $get['skill_id'] = $skill['id'];
  224. }else{
  225. $get['city'] = $skill['city'];
  226. $get['goods_ids'] = $skill['goods_ids'];
  227. }
  228. $re = [];
  229. try{
  230. $re = \app\api\model\service\Order::getOrderList($get);
  231. } catch (Exception $e) {
  232. $this->error($e->getMessage());
  233. }
  234. $this->success('信息返回成功',$re);
  235. }
  236. /**
  237. * 订单详情
  238. * @return void
  239. */
  240. public function orderInfo()
  241. {
  242. if (!$this->request->isPost()) {
  243. $this->error('请求方式异常');
  244. }
  245. $id = input('id/d','');
  246. $order = \app\api\model\service\Order::getOrderInfo($id);
  247. $this->success('信息返回成功',$order);
  248. }
  249. /**
  250. * 申请退款
  251. * @return void
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. * @throws \think\exception\DbException
  255. */
  256. public function refundOrder()
  257. {
  258. $uid = $this->auth->id;
  259. $id = input('id/d','');
  260. $refundOrder = \app\api\model\service\RefundOrder::where(['user_id'=>$uid,'order_id'=>$id])->order('id desc')->find();
  261. $type = input('type','');
  262. if($type == 'refund')
  263. {
  264. $post = input('post.','','trim,strip_tags');
  265. $post['user_id'] = $uid;
  266. try{
  267. \app\api\model\service\RefundOrder::applyRefund($post);
  268. } catch (Exception $e) {
  269. $this->error($e->getMessage());
  270. }
  271. $this->success('已申请,等待管理员审核');
  272. }
  273. $this->success('信息返回成功',$refundOrder);
  274. }
  275. public function refundReason()
  276. {
  277. if (!$this->request->isPost()) {
  278. $this->error('请求方式异常');
  279. }
  280. $list = \app\api\model\service\RefundReason::where(['state'=>1])->field('id,name')->select();
  281. $this->success('信息返回成功',$list);
  282. }
  283. public function complaintReason()
  284. {
  285. if (!$this->request->isPost()) {
  286. $this->error('请求方式异常');
  287. }
  288. $list = \app\api\model\service\ComplaintReason::where(['state'=>1])->field('id,name')->select();
  289. $this->success('信息返回成功',$list);
  290. }
  291. /**
  292. * 订单投诉
  293. * @return void
  294. * @throws \think\db\exception\DataNotFoundException
  295. * @throws \think\db\exception\ModelNotFoundException
  296. * @throws \think\exception\DbException
  297. */
  298. public function complaint()
  299. {
  300. if (!$this->request->isPost()) {
  301. $this->error('请求方式异常');
  302. }
  303. $post = input('post.','','trim,strip_tags');
  304. $uid = $this->auth->id;
  305. $order = \app\api\model\service\Order::where(['user_id'=>$uid,'id'=>$post['order_id']])->field('id,user_id,is_complaint')->find();
  306. (!$order && $order['is_complaint'] == 1)&& $this->error('当前订单无法投诉');
  307. $post['user_id'] = $uid;
  308. try{
  309. \app\api\model\service\Complaint::create($post);
  310. \app\api\model\service\OrderLog::create(['order_id'=>$order['id'],'user_id'=>$order['user_id'],'type'=>11,'content'=>'订单已投诉']);
  311. \app\api\model\service\Order::where(['id'=>$post['order_id']])->update(['is_complaint'=>1]);
  312. } catch (Exception $e) {
  313. $this->error($e->getMessage());
  314. }
  315. $this->success('投诉已发送给管理员,等待管理员审核');
  316. }
  317. public function shopAccept()
  318. {
  319. if (!$this->request->isPost()) {
  320. $this->error('请求方式异常');
  321. }
  322. $uid = $this->auth->id;
  323. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id,accept_nums')->find();
  324. $id = input('id/d','');
  325. $starttime = \app\api\model\service\Order::where(['id'=>$id,'status'=>1])->value('starttime');
  326. $orderCount = \app\api\model\service\Order::getTotalAccept(['shop_id'=>$shop['id'],'starttime'=>$starttime]);
  327. $orderCount>=$shop['accept_nums'] && $this->error('订单接取已达到上限',[],2);
  328. Db::startTrans();
  329. $order = \app\api\model\service\Order::where(['id'=>$id,'status'=>1])->lock(true)->find();
  330. $order['shop_id'] = $shop['id'];
  331. try{
  332. \app\api\model\service\Order::shopAccept($order);
  333. Db::commit();
  334. } catch (Exception $e) {
  335. Db::rollback();
  336. $this->error('订单接取失败',$e->getMessage());
  337. }
  338. $this->success('订单已接取');
  339. }
  340. /**
  341. * 服务者接取订单
  342. * @return void
  343. */
  344. public function skillAccept()
  345. {
  346. if (!$this->request->isPost()) {
  347. $this->error('请求方式异常');
  348. }
  349. $uid = $this->auth->id;
  350. $skill = \app\api\model\service\Skill::where(['user_id'=>$uid])->field('id,accept_nums,shop_id')->find();
  351. $id = input('id/d','');
  352. $starttime = \app\api\model\service\Order::where(['id'=>$id,'status'=>1])->value('starttime');
  353. if($skill['shop_id'])
  354. {
  355. $shop = \app\api\model\service\Shop::where(['id'=>$skill['shop_id']])->field('id,accept_nums')->find();
  356. $orderCount = \app\api\model\service\Order::getTotalAccept(['shop_id'=>$skill['shop_id'],'starttime'=>$starttime]);
  357. $orderCount>=$shop['accept_nums'] && $this->error('商户订单接取已达到上限',[],2);
  358. }else{
  359. $orderCount = \app\api\model\service\Order::getTotalAccept(['skill_id'=>$skill['id'],'starttime'=>$starttime]);
  360. $orderCount>=$skill['accept_nums'] && $this->error('服务者订单接取已达到上限',[],2);
  361. }
  362. Db::startTrans();
  363. $order = \app\api\model\service\Order::where(['id'=>$id,'status'=>1])->lock(true)->find();
  364. try{
  365. \app\api\model\service\Order::accept(['id'=>$order['id'],'skill_id'=>$skill['id'],'shop_id'=>$skill['shop_id']]);
  366. Db::commit();
  367. } catch (Exception $e) {
  368. Db::rollback();
  369. $this->error('订单接取失败',$e->getMessage());
  370. }
  371. $this->success('订单已接取');
  372. }
  373. public function skillGo()
  374. {
  375. if (!$this->request->isPost()) {
  376. $this->error('请求方式异常');
  377. }
  378. $uid = $this->auth->id;
  379. $id = input('id/d','');
  380. try{
  381. \app\api\model\service\Order::go(['id'=>$id,'user_id'=>$uid]);
  382. } catch (Exception $e) {
  383. $this->error($e->getMessage());
  384. }
  385. $this->success('服务人员已出发');
  386. }
  387. public function skillReach()
  388. {
  389. if (!$this->request->isPost()) {
  390. $this->error('请求方式异常');
  391. }
  392. $uid = $this->auth->id;
  393. $id = input('id/d','');
  394. $reachImages = input('reach_images','','trim,strip_tags');
  395. try{
  396. \app\api\model\service\Order::reach(['id'=>$id,'user_id'=>$uid,'reach_images'=>$reachImages]);
  397. } catch (Exception $e) {
  398. $this->error($e->getMessage());
  399. }
  400. $this->success('服务人员已到达');
  401. }
  402. public function skillStart()
  403. {
  404. if (!$this->request->isPost()) {
  405. $this->error('请求方式异常');
  406. }
  407. $id = input('id/d','');
  408. try{
  409. \app\api\model\service\Order::skillStart(['id'=>$id]);
  410. } catch (Exception $e) {
  411. $this->error($e->getMessage());
  412. }
  413. $this->success('服务人员已开始服务');
  414. }
  415. public function skillFinish()
  416. {
  417. if (!$this->request->isPost()) {
  418. $this->error('请求方式异常');
  419. }
  420. $uid = $this->auth->id;
  421. $skillId = \app\api\model\service\Skill::where(['user_id'=>$uid])->value('id');
  422. $get = input('post.','','trim,strip_tags');
  423. $get['skill_id'] = $skillId;
  424. Db::startTrans();
  425. $order = \app\api\model\service\Order::where(['id'=>$get['id']])->lock(true)->find();
  426. try{
  427. \app\api\model\service\Order::skillFinish($get);
  428. Db::commit();
  429. } catch (Exception $e) {
  430. Db::rollback();
  431. $this->error($e->getMessage());
  432. }
  433. $this->success('订单已确认完成');
  434. }
  435. public function checkOrder()
  436. {
  437. if (!$this->request->isPost()) {
  438. $this->error('请求方式异常');
  439. }
  440. $uid = $this->auth->id;
  441. $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
  442. $checkName = input('check_name','','trim,strip_tags');
  443. Db::startTrans();
  444. $order = \app\api\model\service\Order::where(['check_name'=>$checkName,'shop_id'=>$shopId,'status'=>2])->lock(true)->find();
  445. try{
  446. \app\api\model\service\Order::checkOrder($order);
  447. Db::commit();
  448. } catch (Exception $e) {
  449. Db::rollback();
  450. $this->error($e->getMessage());
  451. }
  452. $this->success('核销成功');
  453. }
  454. public function skillSettleOrder()
  455. {
  456. if (!$this->request->isPost()) {
  457. $this->error('请求方式异常');
  458. }
  459. $uid = $this->auth->id;
  460. $skill = \app\api\model\service\Skill::where(['user_id'=>$uid])->field('id,shop_id')->find();
  461. $post = input('post.','','trim,strip_tags');
  462. $post['user_id'] = $uid;
  463. $post['skill_id'] = $skill['id'];
  464. if($post['types'] == 1)
  465. {
  466. $post['shop_id'] = $skill['shop_id'];
  467. }
  468. $post['type'] = 0;
  469. $re = '';
  470. try{
  471. $re = \app\api\model\service\Order::searchSettleOrder($post);
  472. } catch (Exception $e) {
  473. $this->error($e->getMessage());
  474. }
  475. $this->success('信息返回成功',$re);
  476. }
  477. public function shopSettleOrder()
  478. {
  479. if (!$this->request->isPost()) {
  480. $this->error('请求方式异常');
  481. }
  482. $uid = $this->auth->id;
  483. $shop = \app\api\model\service\Shop::where(['user_id'=>$uid])->field('id')->find();
  484. $post = input('post.','','trim,strip_tags');
  485. $post['user_id'] = $uid;
  486. $post['shop_id'] = $shop['id'];
  487. $post['type'] = 1;
  488. $re = '';
  489. try{
  490. $re = \app\api\model\service\Order::searchSettleOrder($post);
  491. } catch (Exception $e) {
  492. $this->error($e->getMessage());
  493. }
  494. $this->success('信息返回成功',$re);
  495. }
  496. public function priceInfo()
  497. {
  498. if (!$this->request->isPost()) {
  499. $this->error('请求方式异常');
  500. }
  501. $id = input('id/d','');
  502. $order = \app\api\model\service\Order::where(['id'=>$id])->field('id,skill_id,shop_id,price,travel_price,sumprice,goods_total_price,coupon_price,premium_price,add_price,payprice,act_travel_price,refund_price,settle_price')->find();
  503. $order['skillPrice'] = $order['skill_id']?\app\api\model\service\Rebate::where(['order_id'=>$id,'type'=>0,'rebatetype'=>['<>',2]])->value('num'):0;
  504. $order['shopPrice'] = $order['shop_id']?\app\api\model\service\Rebate::where(['order_id'=>$id,'type'=>1,'rebatetype'=>['<>',2]])->value('num'):0;
  505. $this->success('信息返回成功',$order);
  506. }
  507. public function refundInfo()
  508. {
  509. $id = input('order_id','');
  510. $refundOrder = \app\api\model\service\RefundOrder::where('order_id',$id)->order('id desc')->find();
  511. if($refundOrder){
  512. $refundOrder['images'] = $refundOrder['images']?explode(',',$refundOrder['images']):'';
  513. }
  514. $this->success('信息返回成功',$refundOrder);
  515. }
  516. public function cancelRefund()
  517. {
  518. if (!$this->request->isPost()) {
  519. $this->error('请求方式异常');
  520. }
  521. $id = input('order_id','');
  522. $refundOrder = \app\api\model\service\RefundOrder::where(['order_id'=>$id,'state'=>0])->find();
  523. !$refundOrder && $this->error('无法取消申请,请联系管理员');
  524. try{
  525. \app\api\model\service\RefundOrder::cancelRefundOrder($refundOrder);
  526. $this->success('申请退款已取消');
  527. } catch (Exception $e) {
  528. $this->error($e->getMessage());
  529. }
  530. }
  531. public function complaintInfo()
  532. {
  533. if (!$this->request->isPost()) {
  534. $this->error('请求方式异常');
  535. }
  536. $id = input('order_id','');
  537. $complaint = \app\api\model\service\Complaint::where('order_id',$id)->find();
  538. if($complaint)
  539. {
  540. $complaint['images'] = $complaint['images']?explode(',',$complaint['images']):'';
  541. }
  542. $this->success('信息返回成功',$complaint);
  543. }
  544. }