WxOrders.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace app\api\controller\h5;
  3. use app\hander\HelpHander;
  4. use EasyWeChat\Factory;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. use app\api\controller\Base;
  9. class WxOrders extends Base
  10. {
  11. public function __construct(App $app = null) {
  12. parent::__construct($app);
  13. // $this->orgId = 3;
  14. }
  15. //订单结算页详情
  16. public function checkOrderDetail(){
  17. $type = input('type/d',0);
  18. $goodsId = input('goodsId/d',0);
  19. $cartId = input('cartId','');
  20. $num = input('num/d',0);
  21. $ids = [];
  22. if($type==0){
  23. if(empty($goodsId) || empty($num)){
  24. HelpHander::error('参数错误');
  25. }
  26. $ids[] = ['id'=>$goodsId,'num'=>$num];
  27. }else if($type==1){
  28. if(empty($cartId)){
  29. HelpHander::error('参数错误');
  30. }
  31. $gId = Db::name('wx_goods_cart')
  32. ->where('id','in',explode(',',$cartId))
  33. ->where('user_id',$this->userId)
  34. ->group('goods_id')
  35. ->select();
  36. foreach ($gId as $k=>$v){
  37. $ids[] = [
  38. 'id'=>$v['goods_id'],
  39. 'num'=>$v['nums'],
  40. ];
  41. }
  42. }
  43. $data = model('WxOrders')->formatOrder($ids);
  44. HelpHander::success($data,'操作成功');
  45. }
  46. public function create(){
  47. $type = input('type/d',0);
  48. $goodsId = input('goodsId/d',0);
  49. $cartId = input('cartId','');
  50. $addressId=input('addressId/d',0);
  51. $isHasBarrel=input('isHasBarrel/d',0);
  52. $deliveryDate=input('deliveryDate','');
  53. $deliveryType=input('deliveryType/d',1);
  54. $orderRemark=input('orderRemark','');
  55. $payType=input('payType/d',1);
  56. $num = input('num/d',0);
  57. if($this->orgId <= 0){
  58. HelpHander::error('参数错误');
  59. }
  60. $ret = model('WxOrders')->add($cartId,$addressId,$this->userId,$this->orgId,$type,$goodsId,$isHasBarrel,$deliveryDate,$deliveryType,$orderRemark,$payType,$num);
  61. if(!$ret){
  62. HelpHander::error(model('WxOrders')->getError());
  63. }
  64. HelpHander::success(['id'=>$ret],'操作成功');
  65. }
  66. //商城订单列表
  67. public function orderList(){
  68. $status=input('status/d',0);
  69. $page=input('page/d',1);
  70. $size=input('size/d',20);
  71. $ret = model('WxOrders')->lists($page,$size,$this->userId,$this->orgId,$status);
  72. HelpHander::success($ret);
  73. }
  74. //订单详情
  75. public function orderDetails(){
  76. $orderId=input('orderId/d',0);
  77. $ret = model('WxOrders')->details($orderId);
  78. HelpHander::success($ret);
  79. }
  80. //取消订单
  81. public function cancelOrder(){
  82. $id = input('id',0);
  83. $info = Db::name('wx_orders')
  84. ->where('id',$id)
  85. ->where('user_id',$this->userId)
  86. ->find();
  87. if(empty($info)){
  88. HelpHander::error('订单不存在');
  89. }
  90. if($info['status']!=0){
  91. HelpHander::error('订单已取消');
  92. }
  93. $data = [
  94. 'status'=>4,
  95. 'cancel_time'=>date('Y-m-d H:i:s')
  96. ];
  97. $ret = Db::name('wx_orders')->where('id',$id)->update($data);
  98. //取消订单添加库存
  99. // $orderGoods = Db::name('g_order_goods')
  100. // ->where('order_id',$id)
  101. // ->select();
  102. // foreach ($orderGoods as $k=>$v){
  103. // Db::name('g_goods')
  104. // ->where('id',$v['goods_id'])
  105. // ->inc('stock',$v['nums'])
  106. // ->update();
  107. // }
  108. if($ret){
  109. HelpHander::success([],'操作成功');
  110. }else{
  111. HelpHander::error('操作失败');
  112. }
  113. }
  114. public function confirmOrder(){
  115. $id = input('id',0);
  116. $info = Db::name('wx_orders')
  117. ->where('id',$id)
  118. ->where('user_id',$this->userId)
  119. ->find();
  120. if(empty($info)){
  121. HelpHander::error('订单不存在');
  122. }
  123. if($info['status']!=2){
  124. HelpHander::error('订单已确认');
  125. }
  126. $data = [
  127. 'status'=>3,
  128. 'finish_time'=>date('Y-m-d H:i:s')
  129. ];
  130. $ret = Db::name('wx_orders')->where('id',$id)->update($data);
  131. if($ret){
  132. HelpHander::success([],'操作成功');
  133. }else{
  134. HelpHander::error('操作失败');
  135. }
  136. }
  137. public function delOrder(){
  138. $id = input('id',0);
  139. $info = Db::name('wx_orders')
  140. ->where('id',$id)
  141. ->where('user_id',$this->userId)
  142. ->find();
  143. if(empty($info)){
  144. HelpHander::error('订单不存在');
  145. }
  146. if(!in_array($info['status'],[4,5,7])){
  147. HelpHander::error('订单状态不能删除');
  148. }
  149. $data = [
  150. 'del'=>1,
  151. 'del_time'=>date('Y-m-d H:i:s')
  152. ];
  153. $ret = Db::name('wx_orders')->where('id',$id)->update($data);
  154. if($ret){
  155. Db::name('wx_orders_refund')->where('order_id',$id)->update([
  156. 'del'=>1
  157. ]);
  158. HelpHander::success([],'操作成功');
  159. }else{
  160. HelpHander::error('操作失败');
  161. }
  162. }
  163. //继续支付
  164. public function goPay(){
  165. $id = input('id',0);
  166. $order = Db::name('wx_orders')
  167. ->where('id',$id)
  168. ->where('status',0)->find();
  169. if(!$order){
  170. HelpHander::error('订单不存在');
  171. }
  172. $config = config('app.wx_mini_config');
  173. $notify = request()->domain().'/api/v1/notify/WxOrder';
  174. $openid = Db::name('wxuser')->where('id',$this->userId)->value('openid');
  175. $app = Factory::payment($config);
  176. $result = $app->order->unify([
  177. 'body' => '订单-'.$order['order_sn'],
  178. 'out_trade_no' => $order['order_sn'],
  179. 'total_fee' => $order['amount']*100,
  180. 'notify_url' => $notify, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  181. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  182. 'openid' => $openid,
  183. ]);
  184. if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  185. $jssdk = $app->jssdk;
  186. $ret = $jssdk->bridgeConfig($result['prepay_id'],false); // 返回数组
  187. HelpHander::success($ret,'成功');
  188. }else{
  189. HelpHander::error($result['err_code_des']);
  190. }
  191. }
  192. public function refuse(){
  193. $id = input('id',0);
  194. $order = Db::name('g_orders')->where('id',$id)->find();
  195. if(!$order){
  196. HelpHander::error('订单不存在');
  197. }
  198. if($order['status']!=1 || time() > (strtotime($order['create_time'])+(30*24*3600))){
  199. HelpHander::error('当前订单不可退款');
  200. }
  201. $refund_money = input('refundMoney');
  202. $refund_remark = input('refundRemark','');
  203. if(empty($refund_money) ||$refund_money==0 ){
  204. HelpHander::error('请输入退款金额');
  205. }
  206. $data = [
  207. 'money' =>0,
  208. 'refund_money' =>$refund_money,
  209. 'refund_remark' =>$refund_remark,
  210. 'order_id' => $id,
  211. 'user_id' => $this->userId,
  212. 'org_id' => $this->orgId,
  213. 'sn' => get_unique_id(),
  214. 'from' =>1,
  215. 'create_time' => date('Y-m-d H:i:s'),
  216. ];
  217. $r = Db::name('g_order_refund')
  218. ->insertGetId($data);
  219. Db::name('g_orders')
  220. ->where('id',$id)
  221. ->update([
  222. 'status'=>6
  223. ]);
  224. if($r){
  225. HelpHander::success([],'操作成功');
  226. }
  227. HelpHander::error('操作失败');
  228. }
  229. public function buyBarrel(){
  230. $id = input('id/d',0);
  231. $num = input('num/d',1);
  232. $ret = model('WxOrders')->buyBarrel($this->orgId,$this->userId,$id,$num);
  233. HelpHander::success(['id'=>$ret]);
  234. }
  235. public function payBarrel(){
  236. $id = input('id/d',0);
  237. $info = Db::name('wx_cash_pay_log')
  238. ->where('id',$id)
  239. ->find();
  240. if(empty($info)){
  241. HelpHander::error('押金支付记录不存在');
  242. }
  243. if($info['status'] !=0){
  244. HelpHander::error('押金已支付');
  245. }
  246. $config = config('app.wx_mini_config');
  247. $notify = request()->domain().'/api/v1/notify/cashOrder';
  248. $openid = Db::name('wxuser')->where('id',$this->userId)->value('openid');
  249. $app = Factory::payment($config);
  250. $result = $app->order->unify([
  251. 'body' => '订单-'.$info['sn'],
  252. 'out_trade_no' => $info['sn'],
  253. 'total_fee' => $info['amount']*100,
  254. 'notify_url' => $notify, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  255. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  256. 'openid' => $openid,
  257. ]);
  258. if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  259. $jssdk = $app->jssdk;
  260. $ret = $jssdk->bridgeConfig($result['prepay_id'],false); // 返回数组
  261. HelpHander::success($ret,'成功');
  262. }else{
  263. HelpHander::error($result['return_msg']);
  264. }
  265. }
  266. public function refundBarrel(){
  267. $id = input('id/d',0);
  268. $num = input('num/d',1);
  269. $ret = model('WxOrders')->refundBarrel($this->orgId,$this->userId,$id,$num);
  270. HelpHander::success($ret,'操作成功');
  271. }
  272. public function commentOrder(){
  273. $id = input('orderId',0);
  274. $info = Db::name('wx_orders')
  275. ->where('id',$id)
  276. ->where('user_id',$this->userId)
  277. ->find();
  278. if(empty($info)){
  279. HelpHander::error('订单不存在');
  280. }
  281. if($info['status']!=3){
  282. HelpHander::error('该订单不能评价');
  283. }
  284. $data = [
  285. 'status'=>5,
  286. 'finish_time'=>date('Y-m-d H:i:s')
  287. ];
  288. Db::startTrans();
  289. try{
  290. $ret = Db::name('wx_orders')->where('id',$id)->update($data);
  291. if(!$ret){
  292. \exception('操作失败');
  293. }
  294. $data=[
  295. 'score'=>input('score',''),
  296. 'content'=>input('content',''),
  297. 'user_id'=>$this->userId,
  298. 'order_id'=>$id,
  299. 'org_id'=>$this->orgId,
  300. 'create_time'=>date('Y-m-d H:i:s'),
  301. 'create_yyyymm'=>date('Ym'),
  302. 'create_yyyy'=>date('Y'),
  303. 'create_yyyymmdd'=>date('Ymd'),
  304. ];
  305. if(!$data['score']){
  306. \exception('得分不能为空');
  307. }
  308. $res = Db::name('wx_comment')->insertGetId($data);
  309. if (!$res) {
  310. exception('保存失败');
  311. }
  312. Db::commit();
  313. HelpHander::success([],'操作成功');
  314. }catch (Exception $e){
  315. Db::rollback();
  316. HelpHander::error($e->getMessage());
  317. }
  318. }
  319. public function commentDetail(){
  320. $id = input('orderId',0);
  321. $info = Db::name('wx_comment')
  322. ->field('create_yyyy,create_yyyymm,create_yyyymmdd',true)
  323. ->where('order_id',$id)
  324. ->find();
  325. if(empty($info)){
  326. HelpHander::error('评价不存在');
  327. }
  328. HelpHander::success($info,'操作成功');
  329. }
  330. public function checkoutApplyRefund(){
  331. $type = input('type/d',0);
  332. $orderId = input('orderId/d',0);
  333. $goodsId = input('goodsId/d',0);
  334. $ret = model('WxOrders')->checkoutApplyRefund($this->userId,$this->orgId,$type,$orderId,$goodsId);
  335. $ret?HelpHander::success($ret,'操作成功'):HelpHander::error(model('WxOrders')->getError());
  336. }
  337. public function checkoutApplyRefund1(){
  338. $orderId = input('orderId/d',0);
  339. $goodsId = input('goodsId','');
  340. $ret = model('WxOrders')->checkoutApplyRefund1($orderId,$goodsId);
  341. $ret?HelpHander::success($ret,'操作成功'):HelpHander::error(model('WxOrders')->getError());
  342. }
  343. public function addApplyRefund(){
  344. $goodsJson = input('goodsJson','');
  345. $orderId = input('orderId/d',0);
  346. $remark = input('reason','');
  347. $ret = model('WxOrders')->addApplyRefund($this->userId,$this->orgId,$goodsJson,$orderId,$remark);
  348. $ret?HelpHander::success($ret,'操作成功'):HelpHander::error(model('WxOrders')->getError());
  349. }
  350. public function orderRefundDetail(){
  351. $orderId = input('orderId/d',0);
  352. $goodsId = input('goodsId','');
  353. $ret = model('WxOrders')->orderRefundDetail($this->orgId,$orderId,$goodsId);
  354. HelpHander::success($ret,'操作成功');
  355. }
  356. public function goodsRefundDetail(){
  357. $orderId = input('orderId/d',0);
  358. $goodsId = input('goodsId/d',0);
  359. $ret = model('WxOrders')->goodsRefundDetail($this->orgId,$orderId,$goodsId);
  360. HelpHander::success($ret,'操作成功');
  361. }
  362. public function rBuy(){
  363. $orderId =input('orderId/d',0);
  364. $goods_id = Db::name('wx_orders_goods')
  365. ->where('order_id', $orderId)
  366. ->column('goods_id');
  367. foreach ($goods_id as $k=>$v){
  368. $check = Db::name('wx_goods_cart')
  369. ->where('user_id',$this->userId)
  370. ->where('goods_id',$v)
  371. ->find();
  372. if(empty($check)){
  373. $data=[
  374. 'user_id'=>$this->userId,
  375. 'goods_id'=>$v,
  376. 'nums'=>1,
  377. 'create_time'=>date('Y-m-d H:i:s'),
  378. ];
  379. Db::name('wx_goods_cart')->insert($data);
  380. }
  381. }
  382. HelpHander::success([],'操作成功');
  383. }
  384. public function checkRefundNum(){
  385. $orderId = input('orderId/d',0);
  386. $goodsId = input('goodsId/d',0);
  387. $num = input('num/d',0);
  388. $goodsList = Db::name('wx_orders_goods')
  389. ->alias('og')
  390. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  391. ->join('wx_goods g', 'g.id=og.goods_id')
  392. ->where('og.order_id', $orderId)
  393. ->where('og.goods_id', $goodsId)
  394. ->find();
  395. if($goodsList['num'] <$num){
  396. HelpHander::error('退款数量不能超过购买数量',1,['num'=>$goodsList['num']]);
  397. }
  398. HelpHander::success(['num'=>$num],'可以退款');
  399. }
  400. public function checkRefundChangeNum(){
  401. $type = input('type/d',0);
  402. $orderId = input('orderId/d',0);
  403. $goodsId = input('goodsId/d',0);
  404. $nums = input('nums',0);
  405. if(empty($orderId)){
  406. HelpHander::error('参数错误');
  407. }
  408. if($type==0 && empty($goodsId)){
  409. HelpHander::error('参数错误');
  410. }
  411. if($type==0){
  412. $goodsList = Db::name('wx_orders_goods')
  413. ->alias('og')
  414. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  415. ->join('wx_goods g', 'g.id=og.goods_id')
  416. ->where('og.order_id', $orderId)
  417. ->where('og.goods_id', $goodsId)
  418. ->select();
  419. foreach ($goodsList as $k=>$v){
  420. if($nums > 0){
  421. $goodsList[$k]['num'] = $nums;
  422. }
  423. }
  424. }else{
  425. $goodsList = Db::name('wx_orders_goods')
  426. ->alias('og')
  427. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  428. ->join('wx_goods g', 'g.id=og.goods_id')
  429. ->where('og.order_id', $orderId)
  430. ->select();
  431. }
  432. foreach ($goodsList as $k=>$v){
  433. //$rNum = $this->getRefundGoodsNum($v['goods_id'],$orderId);
  434. //$goodsList[$k]['refund_num'] = $v['num']-$rNum;
  435. // $goodsList[$k]['refund_num'] = $v['num'];
  436. $goodsList[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  437. ->where('id',$v['label'])
  438. ->value('title'):"";
  439. $flag= Db::name('wx_orders_refund')
  440. ->where('del',0)
  441. ->where('goods_id',$v['goods_id'])
  442. ->where('order_id',$orderId)
  443. ->find();
  444. if($flag){
  445. unset($goodsList[$k]);
  446. }
  447. }
  448. HelpHander::success($goodsList);
  449. }
  450. }