Order.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\api\controller\Alipay;
  16. use app\api\controller\Base;
  17. use think\Db;
  18. use think\Model;
  19. use app\api\controller\Area;
  20. use EasyWeChat\Factory;
  21. /**
  22. * @title 订单管理
  23. * @controller Order
  24. * @group member
  25. */
  26. class Order extends Base
  27. {
  28. function initialize(){
  29. $this->check_login();
  30. }
  31. /**
  32. * @title 提交订单
  33. * @desc 提交订单
  34. * @author QGF
  35. * @url /api/Order/create_order
  36. * @method POST
  37. * @tag 提交订单
  38. * @header name:Authorization require:1 desc:Token
  39. * @param name:goods_id type:int require:1 default:-- desc:服务ID
  40. * @param name:serve_type_id type:int require:1 default:-- desc:服务类型ID
  41. * @param name:phone_type_id type:int require:0 default:-- desc:手机型号ID(金刚区服务需要(cate_id等于0的就是金刚区服务))
  42. * @param name:lose_at type:string require:0 default:-- desc:丢失时间(金刚区服务需要(cate_id等于0的就是金刚区服务))
  43. * @param name:client_tel type:string require:1 default:-- desc:联系手机
  44. * @param name:client_qq type:string require:1 default:-- desc:联系QQ
  45. * @param name:pay_type type:int require:1 default:-- desc:支付类型(1:微信,2:支付宝)
  46. * @param name:describe type:string require:0 default:-- desc:附加信息
  47. * @param name:coupon_id type:int require:0 default:-- desc:选择的优惠券ID
  48. * @return name:config type:array default:-- desc:支付配置(微信和支付宝返回值不同)
  49. */
  50. public function create_order(){
  51. $uid = $this->uid;
  52. $goods_id = input('goods_id');
  53. $serve_type_id = input('serve_type_id');
  54. $phone_type_id = input('phone_type_id');
  55. $lose_at = input('lose_at');
  56. $client_tel = input('client_tel');
  57. $client_qq = input('client_qq');
  58. $describe = input('describe');
  59. $pay_type = input('pay_type');
  60. $coupon_id = input('coupon_id');
  61. if(empty($goods_id) || empty($serve_type_id) || empty($pay_type)){
  62. $this->error('参数错误');
  63. }
  64. $goods_info = Db::name('store_goods')->field('id,cate_id')->where('status',1)->where('id',$goods_id)->where('is_deleted',0)->find();
  65. if(empty($goods_info)){
  66. $this->error('服务信息有误');
  67. }
  68. $goods_price = get_goods_price($goods_id,$serve_type_id,$phone_type_id);
  69. if($goods_price['code'] == 0){
  70. $this->error($goods_price['msg']);
  71. }
  72. $price_total = $price_amount = $goods_price['price'];
  73. $coupon_amount = 0;
  74. if($coupon_id > 0){
  75. $coupon_info = Db::name('store_coupon_list')->field('id,amount')->where('id',$coupon_id)->where('user_id',$uid)->where('status',0)->where('end','>=',date('Y-m-d'))->where('low_amount','<=',$price_total)->find();
  76. if(empty($coupon_info)){
  77. $this->error('所选优惠券有误');
  78. }
  79. $coupon_amount = $coupon_info['amount'];
  80. $price_total = $price_total - $coupon_amount;
  81. }
  82. $serve_type_name = Db::name('store_goods_type')->where('id',$serve_type_id)->value('spec_name');
  83. $order_no = get_order_sn();
  84. $pay_no = get_order_sn();
  85. $order_data = array(
  86. 'user_id' => $uid,
  87. 'order_no' => $order_no,
  88. 'goods_id' => $goods_id,
  89. 'serve_type' => $serve_type_name,
  90. 'client_tel' => $client_tel,
  91. 'client_qq' => $client_qq,
  92. 'describe' => $describe,
  93. 'price_total' => $price_total,
  94. 'price_amount' => $price_amount,
  95. 'coupon_id' => $coupon_id,
  96. 'coupon_amount' => $coupon_amount,
  97. 'pay_type' => $pay_type,
  98. 'cate_id' => $goods_info['cate_id'],
  99. 'pay_no' => $pay_no
  100. );
  101. if($goods_info['cate_id'] == 0){ //金刚区
  102. $phone_type_name = Db::name('store_goods_type')->where('id',$phone_type_id)->value('spec_name');
  103. $order_data['phone_type'] = $phone_type_name;
  104. $order_data['lose_at'] = $lose_at;
  105. }
  106. Db::startTrans();
  107. $order_res = Db::name('store_order')->insert($order_data);
  108. $error = 0;
  109. if(!$order_res){
  110. $error = 1;
  111. Db::rollback();
  112. }
  113. //优惠券改为已用
  114. if($coupon_id){
  115. $coupon_res = Db::name('store_coupon_list')->where('id',$coupon_id)->update(array('status'=>1));
  116. if(!$coupon_res){
  117. $error = 2;
  118. Db::rollback();
  119. }
  120. }
  121. if($pay_type == 1){ //微信支付
  122. $notify_url = $this->request->root(true) . '/api/Pay/order_notify';
  123. //$price_total = 0.01;
  124. $config = Pay::wx_pay('订单支付',$pay_no,$price_total,$notify_url);
  125. if($error == 0 && $config){
  126. Db::commit();
  127. $this->success('支付成功',['config'=>$config]);
  128. }else{
  129. Db::rollback();
  130. $this->error('支付失败');
  131. }
  132. }else{
  133. $notify_url = $this->request->root(true) . '/api/Alipay/order_notify';
  134. //$price_total = 0.01;
  135. $config = Alipay::ali_pay('订单支付',$pay_no,$price_total,$notify_url);
  136. if($error == 0 && $config){
  137. Db::commit();
  138. $this->success('支付成功',['config'=>array('alipay'=>$config)]);
  139. }else{
  140. Db::rollback();
  141. $this->error('支付失败');
  142. }
  143. }
  144. }
  145. /**
  146. * @title 立即支付
  147. * @desc 立即支付
  148. * @author QGF
  149. * @url /api/Order/pay_order
  150. * @method POST
  151. * @tag 支付
  152. * @header name:Authorization require:1 desc:Token
  153. * @param name:order_id type:int require:1 default:-- desc:订单ID
  154. * @param name:pay_type type:int require:0 default:-- desc:支付类型(1:微信,2:支付宝)
  155. * @return name:config type:array default:-- desc:支付配置(微信和支付宝返回值不同)
  156. */
  157. public function pay_order(){
  158. $uid = $this->uid;
  159. $order_id = input('order_id');
  160. $pay_type = input('pay_type',0);
  161. if(empty($order_id)){
  162. $this->error('参数错误');
  163. }
  164. $order_info = Db::name('store_order')->field('id,price_total,pay_type')->where('user_id',$uid)->where('id',$order_id)->where('status',0)->find();
  165. if(empty($order_info)){
  166. $this->error('订单信息有误');
  167. }
  168. if(empty($pay_type)){
  169. $pay_type = $order_info['pay_type'];
  170. }
  171. $price_total = $order_info['price_total'];
  172. $pay_no = get_order_sn();
  173. Db::name('store_order')->where('id',$order_id)->update(array('pay_no'=>$pay_no,'pay_type'=>$pay_type));
  174. if($pay_type == 1){ //微信支付
  175. $notify_url = $this->request->root(true) . '/api/Pay/order_notify';
  176. //$price_total = 0.01;
  177. $config = Pay::wx_pay('订单支付',$pay_no,$price_total,$notify_url);
  178. if($config){
  179. Db::commit();
  180. $this->success('支付成功',['config'=>$config]);
  181. }else{
  182. Db::rollback();
  183. $this->error('支付失败');
  184. }
  185. }else{
  186. $notify_url = $this->request->root(true) . '/api/Alipay/order_notify';
  187. //$price_total = 0.01;
  188. $config = Alipay::ali_pay('订单支付',$pay_no,$price_total,$notify_url);
  189. if($config){
  190. Db::commit();
  191. $this->success('支付成功',['config'=>array('alipay'=>$config)]);
  192. }else{
  193. Db::rollback();
  194. $this->error('支付失败');
  195. }
  196. }
  197. }
  198. /**
  199. * @title 订单列表
  200. * @desc 订单列表
  201. * @author QGF
  202. * @url /api/Order/order_list
  203. * @method GET
  204. * @tag 订单列表
  205. * @header name:Authorization require:1 desc:Token
  206. * @param name:status type:tinyint require:0 default:-1 desc:订单的状态(-1:全部订单(默认),0:待支付,1:待接单,2:进行中,3:已完成)
  207. * @param name:keyword type:string require:0 default:-- desc:搜索的服务类目名称
  208. * @param name:page type:int require:0 default:1 desc:页数(默认为1)
  209. * @param name:page_size type:int require:0 default:10 desc:每页数量(默认为10)
  210. * @return name:id type:int default:-- desc:订单ID
  211. * @return name:serve_type type:string default:-- desc:服务类型
  212. * @return name:price_amount type:string default:-- desc:服务费用
  213. * @return name:status type:int default:-- desc:订单状态(0:待支付,1:待接单,2:进行中,3:已完成)
  214. * @return name:is_evaluate type:int default:-- desc:是否已评价(0:未评价,1:已评价。注:只有已完成的订单才能评价)
  215. * @return name:serve_title type:string default:-- desc:服务类型
  216. */
  217. public function order_list(){
  218. $uid = $this->uid;
  219. $status = input('status',-1);
  220. $keyword = input('keyword');
  221. $page = input('page',1);
  222. $pageSize = input('page_size',10);
  223. if($status == -1){
  224. $where = 'is_deleted = 0 and user_id = '.$uid;
  225. }else{
  226. $where = 'is_deleted = 0 and user_id = '.$uid.' and status = '.$status;
  227. }
  228. if($keyword){
  229. $goods_id_arr = Db::name('store_goods')->where('title','like','%'.$keyword.'%')->column('id');
  230. if($goods_id_arr){
  231. $goods_id_str = implode(',',$goods_id_arr);
  232. $where .= ' and goods_id in ('.$goods_id_str.')';
  233. }else{
  234. $where .= ' and id = 0';
  235. }
  236. }
  237. $field = 'id,goods_id,serve_type,price_amount,status,is_evaluate';
  238. $order_list = Db::name('store_order')->field($field)->where($where)->order('id desc')->page($page,$pageSize)->select();
  239. foreach ($order_list as &$value){
  240. $value['serve_title'] = Db::name('store_goods')->where('id',$value['goods_id'])->value('title');
  241. unset($value['goods_id']);
  242. }
  243. $this->success('获取成功',$order_list);
  244. }
  245. /**
  246. * @title 订单数量
  247. * @desc 每个状态下的订单数量
  248. * @author QGF
  249. * @url /api/Order/order_status_number
  250. * @method GET
  251. * @tag 订单列表
  252. * @header name:Authorization require:1 desc:Token
  253. * @return name:qb_order_count type:int default:-- desc:全部订单数量
  254. * @return name:dzf_order_count type:int default:-- desc:待支付的订单数量
  255. * @return name:djd_order_count type:int default:-- desc:待接单的订单数量
  256. * @return name:jxz_order_count type:int default:-- desc:进行中的订单数量
  257. * @return name:ywc_order_count type:int default:-- desc:已完成的订单数量
  258. */
  259. public function order_status_number(){
  260. $uid = $this->uid;
  261. $where = 'is_deleted = 0 and user_id = '.$uid;
  262. $qb_order_count = Db::name('store_order')->where($where)->count('id');
  263. $dzf_order_count = Db::name('store_order')->where($where)->where('status',0)->count('id');
  264. $djd_order_count = Db::name('store_order')->where($where)->where('status',1)->count('id');
  265. $jxz_order_count = Db::name('store_order')->where($where)->where('status',2)->count('id');
  266. $ywc_order_count = Db::name('store_order')->where($where)->where('status',3)->count('id');
  267. $data = array(
  268. 'qb_order_count' => $qb_order_count,
  269. 'dzf_order_count' => $dzf_order_count,
  270. 'djd_order_count' => $djd_order_count,
  271. 'jxz_order_count' => $jxz_order_count,
  272. 'ywc_order_count' => $ywc_order_count
  273. );
  274. $this->success('获取成功',$data);
  275. }
  276. /**
  277. * @title 订单详情
  278. * @desc 订单详情
  279. * @author QGF
  280. * @url /api/Order/order_detail
  281. * @method GET
  282. * @tag 订单详情
  283. * @header name:Authorization require:1 desc:Token
  284. * @param name:id type:int require:1 default:1 desc:订单ID
  285. * @return name:id type:int default:-- desc:订单ID
  286. * @return name:worker_id type:int default:-- desc:工程师工号(0:代表咱无工程师接单)
  287. * @return name:order_no type:string default:-- desc:订单编号
  288. * @return name:serve_type type:string default:-- desc:服务类型
  289. * @return name:phone_type type:string default:-- desc:手机型号
  290. * @return name:lose_at type:string default:-- desc:丢失时间
  291. * @return name:client_tel type:string default:-- desc:联系手机
  292. * @return name:client_qq type:string default:-- desc:联系QQ
  293. * @return name:describe type:string default:-- desc:附加信息
  294. * @return name:price_amount type:string default:-- desc:订单金额(实付金额等于订单金额减去优惠券抵用金额)
  295. * @return name:coupon_amount type:string default:-- desc:优惠券抵用金额(平台优惠)
  296. * @return name:pay_type type:int default:-- desc:支付方式(1:微信,2:支付宝)
  297. * @return name:pay_at type:string default:-- desc:支付时间
  298. * @return name:start_at type:string default:-- desc:开始时间
  299. * @return name:end_at type:string default:-- desc:完成时间
  300. * @return name:status type:int default:-- desc:订单状态(0:待支付,1:待接单,2:进行中,3:已完成)
  301. * @return name:is_evaluate type:int default:-- desc:是否已评价(0:未评价,1:已评价。注:只有已完成的订单才能评价)
  302. * @return name:serve_title type:string default:-- desc:服务类目
  303. * @return name:worker_qq type:string default:-- desc:工程师QQ号
  304. */
  305. public function order_detail(){
  306. $uid = $this->uid;
  307. $id = input('id');
  308. if(empty($id)){
  309. $this->error('参数错误');
  310. }
  311. $field = 'id,goods_id,worker_id,order_no,serve_type,phone_type,lose_at,client_tel,client_qq,describe,price_amount,coupon_amount,pay_type,pay_at,start_at,end_at,status,is_evaluate';
  312. $order_detail = Db::name('store_order')->field($field)->where('user_id',$uid)->where('id',$id)->find();
  313. if(empty($order_detail)){
  314. $this->error('订单信息有误');
  315. }
  316. $order_detail['serve_title'] = Db::name('store_goods')->where('id',$order_detail['goods_id'])->value('title');
  317. $order_detail['worker_qq'] = '';
  318. if($order_detail['worker_id']){
  319. $order_detail['worker_qq'] = Db::name('store_engineer')->where('id',$order_detail['worker_id'])->value('qq');
  320. //重置工程师编号
  321. $order_detail['worker_id'] = str_pad($order_detail['worker_id'],3,'0',STR_PAD_LEFT);
  322. }
  323. unset($order_detail['goods_id']);
  324. $this->success('获取成功',$order_detail);
  325. }
  326. /**
  327. * @title 确认完成
  328. * @desc 确认完成
  329. * @author QGF
  330. * @url /api/Order/confirm_completed
  331. * @method GET
  332. * @tag 确认完成
  333. * @header name:Authorization require:1 desc:Token
  334. * @param name:id type:int require:1 default:1 desc:订单ID
  335. */
  336. public function confirm_completed(){
  337. $uid = $this->uid;
  338. $id = input('id');
  339. if(empty($id)){
  340. $this->error('参数错误');
  341. }
  342. $order_info = Db::name('store_order')->field('id')->where('id',$id)->where('user_id',$uid)->where('status',2)->find();
  343. if(empty($order_info)){
  344. $this->error('订单信息有误');
  345. }
  346. $res = Db::name('store_order')->where('id',$id)->update(array('status'=>3,'end_at'=>date('Y-m-d H:i:s')));
  347. if($res){
  348. $this->success('确认成功');
  349. }else{
  350. $this->error('确认失败');
  351. }
  352. }
  353. /**
  354. * @title 提交评价
  355. * @desc 提交评价
  356. * @author QGF
  357. * @url /api/Order/submit_evaluate
  358. * @method GET
  359. * @tag 提交评价
  360. * @header name:Authorization require:1 desc:Token
  361. * @param name:id type:int require:1 default:-- desc:订单ID
  362. * @param name:rank type:int require:1 default:-- desc:评价星级 1-5颗星
  363. * @param name:content type:string require:1 default:-- desc:评价内容
  364. * @param name:images type:string require:0 default:-- desc:评价图片(多张以英文逗号分隔)
  365. */
  366. public function submit_evaluate(){
  367. $uid = $this->uid;
  368. $id = input('id');
  369. $rank = input('rank');
  370. $content = input('content');
  371. $images = input('images');
  372. if(empty($id) || empty($rank) || empty($content)){
  373. $this->error('参数错误');
  374. }
  375. $order_info = Db::name('store_order')->field('id,goods_id,worker_id')->where('id',$id)->where('user_id',$uid)->where('status',3)->where('is_evaluate',0)->find();
  376. if(empty($order_info)){
  377. $this->error('订单信息有误');
  378. }
  379. $data = array(
  380. 'user_id' => $uid,
  381. 'goods_id' => $order_info['goods_id'],
  382. 'worker_id' => $order_info['worker_id'],
  383. 'images' => $images,
  384. 'rank' => $rank,
  385. 'content' => $content
  386. );
  387. Db::name('store_evaluate')->insert($data);
  388. Db::name('store_order')->where('id',$id)->update(array('is_evaluate'=>1));
  389. $this->success('评分成功');
  390. }
  391. /**
  392. * @title 删除订单
  393. * @desc 删除订单
  394. * @author QGF
  395. * @url /api/Order/delete_order
  396. * @method GET
  397. * @tag 删除订单
  398. * @header name:Authorization require:1 desc:Token
  399. * @param name:id type:int require:1 default:1 desc:订单的ID
  400. */
  401. public function delete_order(){
  402. $uid = $this->uid;
  403. $id = input('id');
  404. if(empty($id)){
  405. $this->error('参数错误');
  406. }
  407. $order_info = Db::name('store_order')->field('id')->where('id',$id)->where('user_id',$uid)->where('status',3)->where('is_evaluate',1)->find();
  408. if(empty($order_info)){
  409. $this->error('订单信息有误');
  410. }
  411. $res = Db::name('store_order')->where('id',$order_info['id'])->update(array('is_deleted'=>1));
  412. if($res){
  413. $this->success('删除成功');
  414. }else{
  415. $this->error('删除失败');
  416. }
  417. }
  418. function test_mail(){
  419. $engineer_mail_arr = Db::name('store_engineer')->field('id,headimg')->where('mail is not null')->where('is_deleted',0)->column('mail');
  420. if($engineer_mail_arr){
  421. $mail_str = implode(',',$engineer_mail_arr);
  422. mail_push($mail_str,27);
  423. }
  424. }
  425. }