123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace app\api\controller;
- use app\common\model\GoodsOrder;
- use app\common\model\GoodsOrderItem;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- class Order extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
-
- public function createOrder()
- {
- $goods_json = input('post.goods_json','');
- $pro_name = input('post.pro_name','');
- $city_name = input('post.city_name','');
- $county_name = input('post.county_name','');
- $add_detail = input('post.add_detail','');
- $user_name = input('post.user_name','');
- $phone = input('post.phone','');
- $remark = input('post.remark','');
- if(!$goods_json) $this->error('请选择商品');
- if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善收货地址');
- if(!$user_name || !$phone) $this->error('请完善收货人信息');
- $goods_data = json_decode($goods_json,true);
- $goods_ids = array_column($goods_data,'goods_id');
- $goods_list = StoreGoods::field('specs,lists',true)->where('id','in',implode(',',$goods_ids))->where('status',1)->where('is_deleted',0)->with('itemList')->select()->toArray();
- if(count($goods_list) != count($goods_ids)) $this->error('所选商品已下架');
- $goods_column = array_column($goods_list,null,'id');
- $ret_data = ['pay_status'=>0,'config'=>[],'order_id'=>0];
- Db::startTrans();
- try{
-
- $check_goods = 0;
- $error_goods = '';
- $price_goods = 0;
- $original_total = 0;
- $order_item = [];
- foreach ($goods_data as $gv) {
- $goods_spec = $goods_column[$gv['goods_id']]['item_list'];
- $spec_column = array_column($goods_spec,null,'id');
- if(!isset($spec_column[$gv['spec_id']])){
- $check_goods = 1;
- $error_goods = $goods_column[$gv['goods_id']]['name'];
- break;
- }else if($spec_column[$gv['spec_id']]['stock'] < $gv['num']){
- $check_goods = 2;
- $error_goods = $goods_column[$gv['goods_id']]['name'];
- break;
- }
- $price_goods += $gv['num'] * $spec_column[$gv['spec_id']]['sell_price'];
- $original_total += $gv['num'] * $spec_column[$gv['spec_id']]['original_price'];
- $order_item[] = [
- 'user_id' => $this->user_id,
- 'goods_id' => $gv['goods_id'],
- 'goods_no' => $spec_column[$gv['spec_id']]['goods_no'],
- 'goods_name' => $goods_column[$gv['goods_id']]['name'],
- 'goods_spec' => $spec_column[$gv['spec_id']]['goods_spec'],
- 'spec_title' => $spec_column[$gv['spec_id']]['spec_title'],
- 'spec_id' => $gv['spec_id'],
- 'cover' => $spec_column[$gv['spec_id']]['cover'],
- 'original_price' => $spec_column[$gv['spec_id']]['original_price'],
- 'sell_price' => $spec_column[$gv['spec_id']]['sell_price'],
- 'num' => $gv['num'],
- ];
- StoreGoods::where('id',$gv['goods_id'])->setDec('stock',$gv['num']);
- StoreGoodsItem::where('id',$gv['spec_id'])->setDec('stock',$gv['num']);
- }
- if($check_goods == 1) $this->exception('所选商品'.$error_goods.'已下架');
- if($check_goods == 2) $this->exception('所选商品'.$error_goods.'规格库存不足');
-
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'goods_num' => array_sum(array_column($goods_data,'num')),
- 'pro_name' => $pro_name,
- 'city_name' =>$city_name,
- 'county_name' => $county_name,
- 'add_detail' => $add_detail,
- 'user_name' => $user_name,
- 'phone' => $phone,
- 'remark' => $remark,
- ];
- $order_insert['price_total'] = $price_goods;
- $order_insert['price_goods'] = $price_goods ;
- $order_insert['original_total'] = $original_total ;
- $order_insert['price_express'] = 0 ;
- $order_info = GoodsOrder::create($order_insert);
- array_walk($order_item,function (&$v,$k)use ($order_info){
- $v['order_id'] = $order_info->id;
- });
- (new GoodsOrderItem())->insertAll($order_item);
- $ret_data['order_id'] = $order_info->id;
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success($ret_data);
- }
-
- public function payOrder()
- {
- $order_id = input('post.order_id');
- $order_info = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($order_info['status'] != 0) $this->error('订单状态错误');
- if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
- if($order_info['price_total'] <= 0) $this->error('订单金额错误');
- $user_info = User::where('id',$this->user_id)->find()->toArray();
- $ret_data = ['pay_status'=>0,'config'=>[]];
- Db::startTrans();
- try {
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/goodsOrderNotify';
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- Db::name('store_order')->where(['uid'=>$this->user_id,'id'=>$order_id])->update(['pay_no'=>$pay_no]);
- $config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if(!$config) throw new Exception('支付配置错误');
- $ret_data['config'] = $config;
- Db::commit();
- }catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok',$ret_data);
- }
-
- public function getOrderList()
- {
- $status = input('get.status',-1);
- $pay_state = input('get.pay_state',-1);
- $cancel_state = input('get.cancel_state',-1);
- $refund_state = input('get.apply_refund',-1);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- if($status > -1) $where[] = ['status','=',$status];
- if($pay_state > -1) $where[] = ['pay_state','=',$pay_state];
- if($cancel_state > -1) $where[] = ['cancel_state','=',$cancel_state];
- if($refund_state == 0) $where[] = ['refund_state','=',$refund_state];
- if($refund_state == 1) $where[] = ['refund_state','in','1,2,3,4'];
- $list = GoodsOrder::field('id,order_no,status,create_at,remark')->with(['orderItem'])->where($where)->order('id desc ')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
-
- public function getOrderDetail()
- {
- $order_id = input('get.order_id');
- $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
- $detail['order_item'] = (new GoodsOrderItem())->getOrderItem($order_id);
- $this->success('ok',['detail'=>$detail]);
- }
-
- public function cancelOrder()
- {
- $order_id = input('post.order_id');
- Db::startTrans();
- try {
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->exception('订单状态有误');
-
- GoodsOrder::update(['status'=>9],['id'=>$order_id]);
-
- foreach ($detail['order_item'] as $item_info) {
-
- GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
-
- StoreGoodsItem::where('id',$item_info['spec_id'])->setInc('stock',$item_info['num']);
-
- StoreGoods::where('id',$item_info['goods_id'])->setInc('stock',$item_info['num']);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
- }
-
- public function confirmReceipt()
- {
- $order_id = input('post.order_id');
- $detail = GoodsOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['status'] == 0) $this->error('订单未支付');
- if($detail['status'] == 1) $this->error('订单未发货');
- if($detail['status'] == 3) $this->error('订单已收货');
- GoodsOrder::update(['status'=>3,'express_state'=>2],['id'=>$order_id]);
- GoodsOrderItem::update(['status'=>3,'express_state'=>2],['order_id'=>$order_id]);
- $this->success('已确认收货');
- }
- }
|