0
0

GOrders.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\api\controller\h5;
  3. use app\hander\HelpHander;
  4. use EasyWeChat\Factory;
  5. use think\Db;
  6. class GOrders extends Base
  7. {
  8. //从购物车去结算生成订单
  9. public function create(){
  10. $cartId=input('cart_id/s');
  11. $addressId=input('address_id/d',0);
  12. $delivery_type=input('delivery_type/d',0);
  13. $delivery_date=input('delivery_date');
  14. $ret = model('GOrders')->add($cartId,$addressId,$this->userId,$this->orgId,$delivery_type,$delivery_date);
  15. if(!$ret){
  16. HelpHander::error(model('GOrders')->getError());
  17. }
  18. HelpHander::success(['id'=>$ret],'成功');
  19. }
  20. //立即购买生成订单
  21. public function createOne(){
  22. $goodsId=input('goods_id/d');
  23. $addressId=input('address_id/d',0);
  24. $nums=input('nums/d',0);
  25. $delivery_type=input('delivery_type/d',0);
  26. $delivery_date=input('delivery_date');
  27. $ret = model('GOrders')->addOne($goodsId,$addressId,$nums,$this->userId,$this->orgId,$delivery_type,$delivery_date);
  28. if(!$ret){
  29. HelpHander::error(model('GOrders')->getError());
  30. }
  31. HelpHander::success(['id'=>$ret],'成功');
  32. }
  33. //商城订单列表
  34. public function orderList(){
  35. $page=input('page/d',1);
  36. $size=input('size/d',20);
  37. $ret = model('GOrders')->lists($page,$size,$this->userId,$this->orgId);
  38. HelpHander::success($ret);
  39. }
  40. //订单详情
  41. public function orderDetails(){
  42. $orderId=input('order_id/d',0);
  43. $ret = model('GOrders')->details($orderId);
  44. HelpHander::success($ret);
  45. }
  46. //订单完成
  47. public function orderAccomplish(){
  48. $ordersId=input('order_id/d',0);
  49. model('GOrders')->accomplish($ordersId);
  50. HelpHander::success([],'订单已完成');
  51. }
  52. //取消订单
  53. public function cancelOrder(){
  54. $id = input('id',0);
  55. $data = [
  56. 'status'=>3,
  57. 'cancel_time'=>date('Y-m-d H:i:s')
  58. ];
  59. $ret = Db::name('g_orders')->where('id',$id)->update($data);
  60. //取消订单添加库存
  61. $orderGoods = Db::name('g_order_goods')
  62. ->where('order_id',$id)
  63. ->select();
  64. foreach ($orderGoods as $k=>$v){
  65. Db::name('g_goods')
  66. ->where('id',$v['goods_id'])
  67. ->inc('stock',$v['nums'])
  68. ->update();
  69. }
  70. if($ret){
  71. HelpHander::success([],'取消成功');
  72. }else{
  73. HelpHander::error([],'取消失败');
  74. }
  75. }
  76. //继续支付
  77. public function goPay(){
  78. $id = input('id',0);
  79. $order = Db::name('g_orders')->where('id',$id)->where('status',0)->find();
  80. if(!$order){
  81. HelpHander::error('订单不存在');
  82. }
  83. $config = config('app.wx_config');
  84. $notify = config("app.app_host").'/api/h5/notify/xshopOrder';
  85. $openid = Db::name('wxuser')->where('id',$this->userId)->where('type',1)->value('openid');
  86. $app = Factory::payment($config);
  87. $result = $app->order->unify([
  88. 'body' => '商城',
  89. 'out_trade_no' => $order['order_sn'],
  90. 'total_fee' => $order['amount']*100,
  91. 'notify_url' => $notify, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  92. 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
  93. 'openid' => $openid,
  94. ]);
  95. if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  96. $jssdk = $app->jssdk;
  97. $ret = $jssdk->sdkConfig($result['prepay_id']); // 返回数组
  98. HelpHander::success($ret,'成功');
  99. }else{
  100. HelpHander::error('支付失败');
  101. }
  102. }
  103. }