Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 订单
  7. */
  8. class Order extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //支付状态:0=待付款,1=待出行,2=已完成,10=已取消,21=售后中,22=已退款
  13. protected $order_status = [
  14. 0 => '待付款',
  15. 1 => '待出行',
  16. 2 => '已完成',
  17. 10 => '已取消',//未付取消
  18. 21 => '售后中',//已付取消
  19. 22 => '已退款',//已付已退
  20. ];
  21. //订单列表
  22. public function lists()
  23. {
  24. $showstatus = input('showstatus',1); //1=全部,2=待付款,3=待出行,4=售后
  25. $map = [
  26. 'order.user_id' => $this->auth->id,
  27. ];
  28. if($showstatus == 2){
  29. $map['order.status'] = 0;
  30. }
  31. if($showstatus == 3){
  32. $map['order.status'] = 1;
  33. }
  34. if($showstatus == 4){
  35. $map['order.status'] = 21;
  36. }
  37. $order = Db::name('order')
  38. ->field('order.id,order.order_no,order.pay_fee,order.ticket_str,order.status,
  39. p.image,p.name')
  40. ->join('product p','order.product_id = product.id','LEFT')
  41. ->order('order.id desc')->where($map)->autopage()->select();
  42. $order = list_domain_image($order,['image']);
  43. foreach($order as $key => &$val){
  44. $val['status_text'] = isset($this->order_status[$val['status']]) ? $this->order_status[$val['status']] : '';
  45. }
  46. $this->success(1,$order);
  47. }
  48. //订单取消
  49. public function apply_cancel(){
  50. $order_id = input('order_id',0);
  51. Db::startTrans();
  52. $order = Db::name('order')->where('user_id',$this->auth->id)->where('id',$order_id)->lock(true)->find();
  53. if(empty($order)){
  54. Db::rollback();
  55. $this->error('不存在的订单');
  56. }
  57. if($order['status'] != 0){
  58. Db::rollback();
  59. $this->error('当前订单的状态不能取消');
  60. }
  61. //修改订单状态
  62. $update = [
  63. 'status' => 10,//已取消
  64. 'cancle_time' => time(),
  65. ];
  66. $order = Db::name('order')->where('id',$order_id)->update($update);
  67. if($order === false){
  68. Db::rollback();
  69. $this->error('请稍后重试');
  70. }
  71. //车票还回去
  72. $order_road = Db::name('order_road')->where('order_id',$order_id)->lock(true)->select();
  73. foreach($order_road as $key => $road){
  74. $chufabanci = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->lock(true)->find();
  75. $update = [
  76. 'ticket_remain' => $chufabanci['ticket_remain'] + $road['ticket_number'],
  77. ];
  78. $rs_ticket = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->update($update);
  79. if($rs_ticket === false){
  80. Db::rollback();
  81. $this->error('请稍后重试');
  82. }
  83. }
  84. Db::commit();
  85. $this->success('取消成功');
  86. }
  87. //订单申请退款
  88. public function apply_refund(){
  89. $order_id = input('order_id',0);
  90. $reason = input('reason','','trim');
  91. Db::startTrans();
  92. $order = Db::name('order')->where('user_id',$this->auth->id)->where('id',$order_id)->lock(true)->find();
  93. if(empty($order)){
  94. Db::rollback();
  95. $this->error('不存在的订单');
  96. }
  97. if($order['status'] != 1){
  98. Db::rollback();
  99. $this->error('当前订单的状态不能申请退款');
  100. }
  101. $order_road = Db::name('order_road')->where('order_id',$order['id'])->order('roadtype asc')->lock(true)->select();
  102. foreach($order_road as $key => $road){
  103. if($road['hexiao_status'] == 1){
  104. Db::rollback();
  105. $this->error($order_road['roadname'].'已经核销,不能申请退款');
  106. }
  107. if($key == 0){//第一个班次
  108. $applyrefund_early_minute = config('site.applyrefund_early_minute') ?: 10;//提前量
  109. if((time() + ($applyrefund_early_minute * 60)) >= $road['chufatime'] ){
  110. Db::rollback();
  111. $this->error('距离发车时间不足'.$applyrefund_early_minute.'分钟,您已错过申请');
  112. }
  113. }
  114. }
  115. $update = [
  116. 'refund_reason' => $reason,
  117. 'status' => 21,//售后中
  118. ];
  119. $order_rs = Db::name('order')->where('id',$order_id)->update($update);
  120. if($order_rs === false){
  121. Db::rollback();
  122. $this->error('请稍后重试');
  123. }
  124. Db::commit();
  125. $this->success('申请成功,请等待审核');
  126. }
  127. //生成我的视频海报
  128. //生成邀请码二维码图片
  129. private function inviteimage($text) {
  130. $params['text'] = $text;
  131. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  132. // $mimetype = 'image/png';
  133. // $response = Response::create()->header("Content-Type", $mimetype);
  134. // 直接显示二维码
  135. // header('Content-Type: ' . $qrcode_service->getContentType());
  136. // $response->content($qrcode_service->writeString());
  137. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  138. if (!is_dir($qrcodePath)) {
  139. @mkdir($qrcodePath);
  140. }
  141. if (is_really_writable($qrcodePath)) {
  142. $filename = md5(implode('', $params)) . '.png';
  143. $filePath = $qrcodePath . $filename;
  144. $qrcode_service->writeFile($filePath);
  145. }
  146. return '/uploads/qrcode/' . $filename;
  147. }
  148. //某个订单的核销码
  149. public function order_qrcode(){
  150. $order_id = input('order_id',0);
  151. $order = Db::name('order')->where('user_id',$this->auth->id)->where('id',$order_id)->find();
  152. if(empty($order)){
  153. $this->error('不存在的订单');
  154. }
  155. $order_road = Db::name('order_road')->alias('road')
  156. ->field('road.*,
  157. car.chepaihao,
  158. coach.mobile as sijimobile
  159. ')
  160. ->join('product_chufabanci bc','road.chufabanci_id = bc.id','LEFT')
  161. ->join('car','bc.car_id = car.id','LEFT')
  162. ->join('coach','bc.coach_id = coach.id','LEFT')
  163. ->where('road.order_id',$order_id)->order('road.roadtype asc')->select();
  164. foreach($order_road as $key => &$road){
  165. $road['chufatime_text'] = date('Y-m-d H:i',$road['chufatime']);
  166. $road['ticket_str'] = $order['ticket_str'];
  167. $road['realname'] = $order['realname'];
  168. $road['qrcode'] = config('pay_notify_url').$this->inviteimage($road['hexiao_no']);
  169. }
  170. $this->success(1,$order_road);
  171. }
  172. //待核销//已核销
  173. public function hexiao_list(){
  174. $hexiao_status = input('hexiao_status',1);
  175. $map = [
  176. 'order.user_id' => $this->auth->id,
  177. ];
  178. if($hexiao_status == 1){
  179. $map['order.status'] = 1;
  180. }else{
  181. $map['order.status'] = 2;
  182. }
  183. $order = Db::name('order')
  184. ->field('order.id,order.order_no,order.pay_fee,order.ticket_str,order.status,
  185. p.image,p.name')
  186. ->join('product p','order.product_id = product.id','LEFT')
  187. ->order('order.id desc')->where($map)->autopage()->select();
  188. $order = list_domain_image($order,['image']);
  189. foreach($order as $key => &$val){
  190. $val['status_text'] = isset($this->order_status[$val['status']]) ? $this->order_status[$val['status']] : '';
  191. }
  192. $this->success(1,$order);
  193. }
  194. //扫码核销信息
  195. public function get_hexiao_info(){
  196. $this->error('请前往司机端');
  197. if($this->auth->group_id !=2){
  198. $this->error('您无权限核销');
  199. }
  200. $hexiao_no = input('hexiao_no','','trim');
  201. $order_road = Db::name('order_road')->where('hexiao_no',$hexiao_no)->find();
  202. if(empty($order_road)){
  203. $this->error('无效核销码');
  204. }
  205. $order = Db::name('order')->where('id',$order_road['order_id'])->find();
  206. if($order['status'] != 1 && $order['status'] != 2){
  207. $error = '无法核销的码';
  208. if($order['status'] == 0){
  209. $error = '订单未支付';
  210. }
  211. if($order['status'] == 10){
  212. $error = '订单已取消';
  213. }
  214. if($order['status'] > 20){
  215. $error = '订单已售后';
  216. }
  217. $this->error($error);
  218. }
  219. //辅助
  220. $roadtype_arr = [
  221. 1 => '单程票',
  222. 2 => '单程票',
  223. 3 => '往返票',
  224. ];
  225. $order_roadtype_text = isset($roadtype_arr[$order['roadtype']]) ? $roadtype_arr[$order['roadtype']] : '';
  226. $roadtype_arr = [
  227. 1 => '去程',
  228. 2 => '返程',
  229. ];
  230. $road_roadtype_text = isset($roadtype_arr[$order_road['roadtype']]) ? $roadtype_arr[$order_road['roadtype']] : '';
  231. //结果
  232. $result = [
  233. 'product_name' => $order['product_name'],
  234. 'roadname' => $order_roadtype_text.' '.$order_road['roadname'].' '.$road_roadtype_text,
  235. 'chufatime' => date('Y-m-d H:i',$order_road['chufatime']),
  236. 'ticket_str' => $order['ticket_str'],
  237. 'realname' => $order['realname'],
  238. 'mobile' => $order['mobile'],
  239. 'hexiao_no' => $hexiao_no,
  240. 'hexiao_status' => $order_road['hexiao_status'],
  241. ];
  242. $this->success(1,$result);
  243. }
  244. //核销提交
  245. public function hexiao_submit(){
  246. $this->error('请前往司机端');
  247. if($this->auth->group_id !=2){
  248. $this->error('无权限核销');
  249. }
  250. $hexiao_no = input('hexiao_no','','trim');
  251. Db::startTrans();
  252. $order_road = Db::name('order_road')->where('hexiao_no',$hexiao_no)->lock(true)->find();
  253. if(empty($order_road)){
  254. Db::rollback();
  255. $this->error('无效核销码');
  256. }
  257. if($order_road['hexiao_status'] != 0){
  258. Db::rollback();
  259. $this->error('此核销码已核销');
  260. }
  261. $order = Db::name('order')->where('id',$order_road['order_id'])->lock(true)->find();
  262. if($order['status'] != 1){
  263. $error = '无法核销的码';
  264. if($order['status'] == 0){
  265. $error = '订单未支付';
  266. }
  267. if($order['status'] == 2){
  268. $error = '订单已核销';
  269. }
  270. if($order['status'] == 10){
  271. $error = '订单已取消';
  272. }
  273. if($order['status'] > 20){
  274. $error = '订单已售后';
  275. }
  276. Db::rollback();
  277. $this->error($error);
  278. }
  279. //
  280. $update = [
  281. 'hexiao_status' => 1,
  282. 'hexiao_uid' => $this->auth->id,
  283. ];
  284. $rs1 = Db::name('order_road')->where('id',$order_road['id'])->update($update);
  285. if($rs1 === false){
  286. Db::rollback();
  287. $this->error('核销失败');
  288. }
  289. //找到没核销的
  290. $road_find = Db::name('order_road')->where('order_id',$order_road['order_id'])->where('hexiao_status',0)->count();
  291. if($road_find == 0){
  292. //如果都核销了,修改订单状态
  293. $update = [
  294. 'finishtime' => time(),
  295. 'status'=>2
  296. ];
  297. $rs2 = Db::name('order')->where('id',$order_road['order_id'])->update($update);
  298. if($rs1 === false){
  299. Db::rollback();
  300. $this->error('核销失败,请稍后再试');
  301. }
  302. }
  303. Db::commit();
  304. $this->success('核销成功');
  305. }
  306. }