Order.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\BaseUserPayment;
  4. use app\data\model\DataUser;
  5. use app\data\model\DataUserVipOrder;
  6. use app\data\model\ShopGoodsCart;
  7. use app\data\model\ShopGoods;
  8. use app\data\model\ShopGoodsItem;
  9. use app\data\model\ShopOrder;
  10. use app\data\model\ShopOrderItem;
  11. use app\data\model\ShopOrderPay;
  12. use app\data\model\ShopOrderSend;
  13. use app\data\model\SystemUserAmount;
  14. use app\data\model\SystemUserMessage;
  15. use app\data\service\OrderService;
  16. use app\data\service\PaymentService;
  17. use think\admin\Controller;
  18. use hg\apidoc\annotation\Title;
  19. use hg\apidoc\annotation\Method;
  20. use hg\apidoc\annotation\Param;
  21. use hg\apidoc\annotation\Returned;
  22. use hg\apidoc\annotation\Header;
  23. use app\data\model\SystemUser;
  24. use think\db\Where;
  25. use think\exception\ErrorException;
  26. use think\facade\Db;
  27. use Yansongda\Pay\Pay;
  28. /**
  29. * 商品订单接口
  30. * Class Order
  31. * @package app\data\controller\api
  32. */
  33. class Order extends Auth
  34. {
  35. protected $noNeedLogin=['order_list','text','uploadmore','order_create'];
  36. /**
  37. * @Title("商品规格下单")
  38. * @Method("post")
  39. * @Param("admin_id",desc="商家id")
  40. * @Param("item_id",desc="规格id")
  41. * @Param("num",desc="数量")
  42. * @Param ("address_id",desc="地址id")
  43. */
  44. public function order_create(){
  45. $user = $this->getUser();
  46. // $user = ['id'=>40];
  47. $item_id =input('item_id');
  48. $address_id=input('address_id');
  49. $number = input('num');
  50. $pay_no =$this->pay_no($user['id']);
  51. $order_remark =input('order_remark');
  52. $item_total = ShopGoodsItem::mk()->where('id',$item_id)->value('stock_total');
  53. if($number>$item_total){
  54. $this->error('商品没有库存了');
  55. }
  56. $data=[
  57. 'uuid'=>$user['id'],
  58. 'address_id'=>$address_id,
  59. 'item_id'=>$item_id,
  60. 'num'=>$number,
  61. 'pay_no'=>$pay_no,
  62. 'order_remark'=>$order_remark
  63. ];
  64. $shop_order_model = new ShopOrder();
  65. $order_id = $shop_order_model->order_create($data);
  66. $order_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'payment_trade'=>$pay_no))->sum('payment_amount');
  67. $pay_data=[
  68. 'pay_no'=>$pay_no,
  69. 'money'=>$order_money,
  70. 'status'=>1,
  71. 'create_at'=>date('Y-m-d H:i:s')
  72. ];
  73. ShopOrderPay::mk()->insert($pay_data);
  74. $info = ['order_id'=>$order_id,'pay_no'=>$pay_no];
  75. $this->success('快去支付吧',$info);
  76. }
  77. /**
  78. * @Title("购物车下单")
  79. * @Method("post")
  80. * @Param("cart_ids",desc="购物车id(数组)")
  81. * @Param ("address_id",desc="地址id")
  82. */
  83. public function crat_create(){
  84. $user =$this->getUser();
  85. $cart_ids = input('cart_ids');
  86. $item_ids = ShopGoodsCart::mk()->whereIn('id',$cart_ids)->select();
  87. foreach ($item_ids as &$v){
  88. $item_total = ShopGoodsItem::mk()->where('id',$v['item_id'])->field('stock_total,goods_spec')->find();
  89. if($v['num']>$item_total['stock_total']){
  90. $this->error($item_total['goods_spec'].'规格没有库存了',null);
  91. }
  92. }
  93. $address_id = input('address_id');
  94. $order_remark =input('order_remark');
  95. $pay_no =$this->pay_no($user['id']);
  96. $data=[
  97. 'uuid'=>$user['id'],
  98. 'cart_ids'=>$cart_ids,
  99. 'address_id'=>$address_id,
  100. 'pay_no'=>$pay_no,
  101. 'order_remark'=>$order_remark
  102. ];
  103. $shop_order_model = new ShopOrder();
  104. $shop_order_model->cart_create($data);
  105. $order_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'payment_trade'=>$pay_no))->sum('payment_amount');
  106. $pay_data=[
  107. 'pay_no'=>$pay_no,
  108. 'money'=>$order_money,
  109. 'status'=>1,
  110. 'create_at'=>date('Y-m-d H:i:s'),
  111. 'cart_ids' => $cart_ids
  112. ];
  113. ShopGoodsCart::mk()->whereIn('id',$cart_ids)->delete();
  114. if($order_money <= 0){
  115. $pay_a = ShopOrderPay::mk()->where(array('cart_ids'=>$cart_ids))->find();
  116. $pay_no = $pay_a['pay_no'];
  117. }else{
  118. ShopOrderPay::mk()->insert($pay_data);
  119. }
  120. $this->success('快去支付吧',$pay_no);
  121. }
  122. /**
  123. * @Title("商城下单支付")
  124. * @Method("post")
  125. * @Header("api-name",type="string",require=1,default="",desc="访问类型")
  126. * @Header("api-token",type="string",require=1,default="",desc="token")
  127. * @Param("pay_code",type="string",require=1,default="",desc="支付方式code")
  128. * @Param("pay_no",type="string",require=1,default="",desc="下单支付单号")
  129. * @Param("type",type="string",require=1,default="",desc="支付类型 alipay/wechat")
  130. */
  131. public function order_pay(){
  132. $user = $this->getUser();
  133. $data = $this->_vali([
  134. 'pay_code.require'=>'支付code有误',
  135. 'pay_no.require'=>'支付订单好不能为空',
  136. 'type.require'=>'支付类型不能为空'
  137. ]);
  138. $code = $data['pay_code'];
  139. $pay_type = $data['type'];
  140. $pay_no = $data['pay_no'];
  141. $pay_money= ShopOrderPay::mk()->where(['status' => 1, 'pay_no'=>$pay_no])->value('money');
  142. /* $is_goods = ShopGoods::mk()->where('code',ShopGoodsItem::mk()->where('order_no',ShopOrder::mk()->where('payment_trade',$pay_no)->value('order_no'))->value('goods_code'))->value('status');
  143. if($is_goods == 0){
  144. $this->error('商品已下架');
  145. }*/
  146. if(empty($pay_money)){
  147. $this->error('找不到订单');
  148. }
  149. ShopOrderPay::mk()->where(['status' => 1, 'pay_no'=>$pay_no])->save(['pay_type'=>$pay_type]);
  150. $all_price = $pay_money;
  151. $order_no = $pay_no;
  152. $error = 1;
  153. Db::startTrans();
  154. $retrun_data = [];
  155. try {
  156. if (stripos($pay_type, 'alipay') === 0) {
  157. $notify_name = 'orderzfb';
  158. $payment_type = '支付宝支付';
  159. } elseif (stripos($pay_type, 'wechat') === 0) {
  160. $notify_name = 'orderwx';
  161. $payment_type = '微信支付';
  162. }
  163. ShopOrder::mk()->where('payment_trade',$order_no)->save(['payment_type'=>$payment_type]);
  164. $notify_url = sysuri("@data/api.notify/{$notify_name}/scene/order/param/{$code}", [], false, true);
  165. $parm = ['uuid'=>$this->uuid];
  166. $pay =PaymentService::instance($code)->create($user['openid1'],$order_no,$all_price,'商品下单支付','商品下单支付','','',$notify_url,$parm);
  167. $retrun_data['order_no'] = $order_no;
  168. $pay['packages'] = isset($pay['package']) ? $pay['package'] : '';
  169. $retrun_data['pay'] = $pay;
  170. $retrun_data['success'] = isset($pay['result']) && $pay['result']=='success' ? 1 : 0;
  171. Db::commit();
  172. } catch (\Exception $e) {
  173. dump($e);
  174. // 回滚事务
  175. Db::rollback();
  176. $error = 0;
  177. $errorinfo = '未知错误';
  178. }
  179. if ($error){
  180. $this->success('成功',$retrun_data);
  181. }
  182. $this->error($errorinfo);
  183. }
  184. /**
  185. * @Title("我的订单")
  186. * @Method("post")
  187. * @Header("api-name",type="string",require=1,default="",desc="访问类型")
  188. * @Header("api-token",type="string",require=1,default="",desc="token")
  189. * @Param("order_name",desc="搜索名称")
  190. * @Param ("status",desc="1全部 2 待支付 3代发货 4 待收货 5退款/已退款 6 已完成")
  191. */
  192. public function order_list(){
  193. $user = $this->getUser();
  194. $status = input('status',1);
  195. $where= [];
  196. if(!empty($status)){
  197. switch ($status){
  198. case 1:
  199. $array = [0,1,2,3,4,5,6,7,8];
  200. break;
  201. case 2:
  202. $array = [2];
  203. break;
  204. case 3:
  205. $array = [4,7];
  206. break;
  207. case 4:
  208. $array = [5];
  209. break;
  210. case 5:
  211. $array = [4,7,8];
  212. $where = [
  213. 'user_refund_del'=>1,
  214. ];
  215. break;
  216. case 6:
  217. $array = [6];
  218. break;
  219. }
  220. }
  221. $query = ShopOrder::mQuery()->like('order_name');
  222. if($status != 5){
  223. $list = $query ->where('uuid',$user['id'])
  224. ->whereIn('status',$array)->where($where)->order('id desc')->page(true, false, false, 10);
  225. }else{
  226. $list = $query ->where('uuid',$user['id'])
  227. ->whereIn('status',$array)->where($where)->whereNotNull('refund_status')->order('id desc')->page(true, false, false, 10);
  228. }
  229. //自动确认收货时间(天)
  230. $order_timeout_end_time = SystemConfig('merchant_config.order_timeout_end_time');
  231. //未支付取消时间(分钟)
  232. $cancellation_time = SystemConfig('merchant_config.cancellation_time');
  233. foreach ($list['list'] as $k=>$v){
  234. $list['list'][$k]['time']=0;
  235. if($list['list'][$k]['status']==2){
  236. $list['list'][$k]['time'] = strtotime($list['list'][$k]['create_at'])+$cancellation_time*60-time();
  237. if($list['list'][$k]['time']<=3){
  238. ShopOrder::mk()->where('id',$list['list'][$k]['id'])->save(['status'=>0]);
  239. $list['list'][$k]['status'] = 0;
  240. }
  241. }
  242. $list['list'][$k]['admin_name']= SystemUser::mk()->where('id',$list['list'][$k]['admin_id'])->value('nickname');
  243. $list['list'][$k]['headimg']= SystemUser::mk()->where('id',$list['list'][$k]['admin_id'])->value('headimg');
  244. $list['list'][$k]['goods_item']=ShopOrderItem::mk()->where('order_no',$list['list'][$k]['order_no'])->select();
  245. foreach ($list['list'][$k]['goods_item'] as &$vv){
  246. $vv['item_id'] = ShopGoodsItem::mk()->where('goods_sku',$vv['goods_sku'])->value('id');
  247. if(count(explode('|',$vv['goods_cover']))>1){
  248. $vv['goods_cover'] = ShopGoodsItem::mk()->where('goods_spec',$vv['goods_spec'])->value('show_image');
  249. }
  250. $vv['goods_cover'] = $vv['goods_cover'] ? : ShopGoods::mk()->where('code',$vv['goods_code'])->value('cover');
  251. $arr = explode("|", $vv['goods_cover']);
  252. $vv['goods_cover'] = $arr[0];
  253. }
  254. $list['list'][$k]['goods_id'] = ShopGoods::mk()->where('code',$list['list'][$k]['goods_item'][0]['goods_code'])->value('id');
  255. }
  256. $this->success('我的订单列表',$list);
  257. }
  258. /**
  259. * @Title ("订单详情")
  260. * @Method ("post")
  261. * @Param ("order_id",desc="订单id")
  262. * @return void
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\DbException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. */
  267. public function order_info(){
  268. $user = $this->getUser();
  269. $order_id = input('order_id');
  270. $list = ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->find();
  271. if($list['status']==2){
  272. $list['time'] = strtotime($list['create_at'])+30*60-time();
  273. if($list['time']<=3){
  274. ShopOrder::mk()->where('id',$list['id'])->save(['status'=>0]);
  275. }
  276. }
  277. $config = SystemConfig('merchant_config');
  278. //取消待支付剩余时间
  279. if($list['status']==2){
  280. $list['cancel_datetime_ing'] = strtotime($list['create_at'])+$config['cancellation_time']*60-time();
  281. }
  282. //退款剩余时间
  283. if($list['status']==7){
  284. $list['refund_time_ing'] = strtotime($list['refund_time'])+$config['refund_time']*60*60-time();
  285. }
  286. //自动确认收货时间(天)
  287. if($list['status']==5){
  288. $list['order_timeout_end_time'] = strtotime($list['create_at'])+$config['order_timeout_end_time']*60*60*24-time();
  289. }
  290. $list['goods_item']=ShopOrderItem::mk()->where('order_no',$list['order_no'])->select();
  291. foreach ($list['goods_item'] as &$vv){
  292. // $vv['id'] = ShopGoodsItem::mk()->where('goods_sku',$vv['goods_sku'])->value('id');
  293. if(count(explode('|',$vv['goods_cover']))>1){
  294. $vv['goods_cover'] = ShopGoodsItem::mk()->where('goods_spec',$vv['goods_spec'])->value('show_image');
  295. }
  296. $vv['goods_cover'] = $vv['goods_cover'] ? : ShopGoods::mk()->where('code',$vv['goods_code'])->value('cover');
  297. $vv['goods_id'] = ShopGoods::mk()->where('code',$vv['goods_code'])->value('id');
  298. }
  299. $list['user_address']=ShopOrderSend::mk()->where('order_no',$list['order_no'])->find();
  300. $list['admin_name']= SystemUser::mk()->where('id',$list['admin_id'])->value('nickname');
  301. $list['admin_headimag']= SystemUser::mk()->where('id',$list['admin_id'])->value('headimg');
  302. $list['user_info']=DataUser::mk()->where('id',$list['uuid'])->field('headimg,nickname')->find();
  303. $this->success('店铺订单列表',$list);
  304. }
  305. /**
  306. * @Title ("订单类型数量统计")
  307. * @Method ("post")
  308. * @return void
  309. */
  310. public function order_num(){
  311. $user = $this->getUser();
  312. $type = [
  313. '1'=>[2],
  314. '2'=>[4,7],
  315. '3'=>[5],
  316. // '4'=>[4,7,8],
  317. '4'=>[7],
  318. ];
  319. $where['uuid'] = $user['id'];
  320. $value = [];
  321. foreach ($type as $k=>$v){
  322. if($k==4){
  323. $where['user_refund_del']=1;
  324. array_unshift($value,ShopOrder::mk()->whereIn('status',$v)->Where($where)->whereNotNull('refund_status')->count());
  325. }else{
  326. array_unshift($value,ShopOrder::mk()->whereIn('status',$v)->Where($where)->count());
  327. }
  328. }
  329. $this->success('请求成功',array_reverse($value));
  330. }
  331. /**
  332. * @Title ("提醒发货")
  333. * @Method ("post")
  334. * @Param ("order_id",desc="订单id")
  335. * @return void
  336. */
  337. public function remind(){
  338. $user = $this->getUser();
  339. $order_id = input('order_id');
  340. ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->save(['remind'=>1]);
  341. $this->success('已提醒发货');
  342. }
  343. /**
  344. * @Title ("取消订单")
  345. * @Method ("post")
  346. * @Param ("order_id",desc="订单id")
  347. * @Param ("cancel_remark",desc="取消原因")
  348. * @return void
  349. */
  350. public function cancel_order(){
  351. $user = $this->getUser();
  352. $order_id = input('order_id');
  353. $cancel_remark = input('cancel_remark');
  354. ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->save(['status'=>0,'cancel_remark'=>$cancel_remark]);
  355. $mes = ShopOrderItem::mk()->where('order_no',ShopOrder::mk()->where('id',$order_id)->value('order_no'))->find();
  356. $admin_id = ShopOrder::mk()->where('order_no',$mes['order_no'])->value('admin_id');
  357. $data = [
  358. 'type'=>2,
  359. 'uuid'=>$admin_id,
  360. 'title'=>'订单被取消了',
  361. 'content'=>$mes['goods_name'],
  362. 'image'=>$mes['goods_cover'],
  363. 'remark'=>'取消原因'.$cancel_remark
  364. ];
  365. $shop_order_model = new SystemUserMessage();
  366. $shop_order_model->systemMessageadd($data);
  367. $this->success('已取消订单');
  368. }
  369. /**
  370. * @Title ("申请退款")
  371. * @Method ("post")
  372. * @Param ("type",desc="退款类型")
  373. * @Param ("status",desc="收货状态")
  374. * @Param ("order_id",desc="订单id")
  375. * @Param ("reason",desc="退款原因")
  376. * @Param("desc",desc="描述")
  377. * @param ("images",desc="图片")
  378. */
  379. public function refund(){
  380. $user = $this->getUser();
  381. $refund_type = input('type');
  382. $goods_status = input('status','已收货');
  383. $order_id = input('order_id');
  384. $refund_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->value('payment_amount');
  385. $refund_reason = input('reason');
  386. $images = input('images');
  387. if(empty($refund_reason)){
  388. $this->error('退款原因不能为空');
  389. }
  390. $refund_desc = input('desc');
  391. $refund_data = [
  392. 'refund_money'=>$refund_money,
  393. 'refund_reason'=>$refund_reason,
  394. 'refund_desc'=>$refund_desc,
  395. 'refund_nr'=>$images,
  396. 'refund_type'=>$refund_type,
  397. 'goods_status'=>$goods_status,
  398. 'refund_status'=>1,
  399. 'refund_del'=>1,
  400. 'status'=>7,
  401. 'refund_time'=>date('Y-m-d H:i:s')
  402. ];
  403. ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save($refund_data);
  404. $this->success('退款已提交');
  405. }
  406. /**
  407. * @Title ("填写退回物流信息")
  408. * @Method ("post")
  409. * @Param ("express_name",desc="快递公司")
  410. * @Param ("express_no",desc="快递单号")
  411. * @return void
  412. */
  413. public function refund_return(){
  414. $user = $this->getUser();
  415. $order_id = input("order_id");
  416. $express_name = input('express_name');
  417. $express_no = input('express_no');
  418. ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['express_name'=>$express_name,'express_no'=>$express_no]);
  419. $this->success("退款退货物流信息");
  420. }
  421. /**
  422. * @Title ("确认收货")
  423. * @Method ("post")
  424. * @Param ("order_id",desc="订单id")
  425. * @return void
  426. */
  427. public function order_end(){
  428. $user = $this->getUser();
  429. $order_id = input("order_id");
  430. Db::startTrans();
  431. try {
  432. $order_info =ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->field('amount_total,admin_id')->find();
  433. $user_money = SystemUser::mk()->where('id',$order_info['admin_id'])->value('money');
  434. ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['status'=>6]);
  435. $result = $user_money+$order_info['amount_total'];
  436. SystemUser::mk()->where('id',$order_info['admin_id'])->save(['money'=>$result]);
  437. $record = [
  438. 'admin_id' => $order_info['admin_id'],
  439. 'before' => $user_money,
  440. 'after' => $result,
  441. 'type' => 1,
  442. 'amount' => $order_info['amount_total'],
  443. 'form' => '商品订单',
  444. 'create_time' => date('y-m-d h:i:s')
  445. ];
  446. SystemUserAmount::mk()->insert($record);
  447. Db::commit();
  448. $this->success("确认收货");
  449. } catch (ErrorException $e) {
  450. // 回滚事务
  451. Db::rollback();
  452. $this->error($e);
  453. }
  454. }
  455. /**
  456. * @Title ("删除退款记录")
  457. * @Method ("post")
  458. * @Param ("order_id",desc="订单id")
  459. * @return void
  460. */
  461. public function del_refund(){
  462. $user = $this->getUser();
  463. $data = $this->_vali(
  464. [
  465. 'order_id.require'=>'订单id不能为空',
  466. ]
  467. );
  468. ShopOrder::mk()->where('id',$data['order_id'])->save(['user_refund_del'=>2]);
  469. $this->success('退款记录已清除');
  470. }
  471. /**
  472. * @Title ("撤销退款申请表")
  473. * @Method ("post")
  474. * @Param ("order_id",desc="订单号")
  475. * @return void
  476. * @throws \think\db\exception\DataNotFoundException
  477. * @throws \think\db\exception\DbException
  478. * @throws \think\db\exception\ModelNotFoundException
  479. */
  480. public function cancel_refund(){
  481. $user = $this->getUser();
  482. $order_id = input('order_id');
  483. $refund_order = ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id,'status'=>7))->find();
  484. if(empty($refund_order)){
  485. $this->error('订单信息错误');
  486. }
  487. ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['status'=>4,'refund_status'=>0]);
  488. $this->success('订单退款申请已撤回');
  489. }
  490. public function pay_no($uuid){
  491. $time_str = date('YmdHi');
  492. $pay_no = 'PAY'.$time_str . rand(00000,99999).$uuid;
  493. return $pay_no;
  494. }
  495. }