123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- <?php
- namespace app\data\controller\api;
- use app\data\model\BaseUserPayment;
- use app\data\model\DataUser;
- use app\data\model\DataUserVipOrder;
- use app\data\model\ShopGoodsCart;
- use app\data\model\ShopGoods;
- use app\data\model\ShopGoodsItem;
- use app\data\model\ShopOrder;
- use app\data\model\ShopOrderItem;
- use app\data\model\ShopOrderPay;
- use app\data\model\ShopOrderSend;
- use app\data\model\SystemUserAmount;
- use app\data\model\SystemUserMessage;
- use app\data\service\OrderService;
- use app\data\service\PaymentService;
- use think\admin\Controller;
- use hg\apidoc\annotation\Title;
- use hg\apidoc\annotation\Method;
- use hg\apidoc\annotation\Param;
- use hg\apidoc\annotation\Returned;
- use hg\apidoc\annotation\Header;
- use app\data\model\SystemUser;
- use think\db\Where;
- use think\exception\ErrorException;
- use think\facade\Db;
- use Yansongda\Pay\Pay;
- /**
- * 商品订单接口
- * Class Order
- * @package app\data\controller\api
- */
- class Order extends Auth
- {
- protected $noNeedLogin=['order_list','text','uploadmore','order_create'];
- /**
- * @Title("商品规格下单")
- * @Method("post")
- * @Param("admin_id",desc="商家id")
- * @Param("item_id",desc="规格id")
- * @Param("num",desc="数量")
- * @Param ("address_id",desc="地址id")
- */
- public function order_create(){
- $user = $this->getUser();
- // $user = ['id'=>40];
- $item_id =input('item_id');
- $address_id=input('address_id');
- $number = input('num');
- $pay_no =$this->pay_no($user['id']);
- $order_remark =input('order_remark');
- $item_total = ShopGoodsItem::mk()->where('id',$item_id)->value('stock_total');
- if($number>$item_total){
- $this->error('商品没有库存了');
- }
- $data=[
- 'uuid'=>$user['id'],
- 'address_id'=>$address_id,
- 'item_id'=>$item_id,
- 'num'=>$number,
- 'pay_no'=>$pay_no,
- 'order_remark'=>$order_remark
- ];
- $shop_order_model = new ShopOrder();
- $order_id = $shop_order_model->order_create($data);
- $order_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'payment_trade'=>$pay_no))->sum('payment_amount');
- $pay_data=[
- 'pay_no'=>$pay_no,
- 'money'=>$order_money,
- 'status'=>1,
- 'create_at'=>date('Y-m-d H:i:s')
- ];
- ShopOrderPay::mk()->insert($pay_data);
- $info = ['order_id'=>$order_id,'pay_no'=>$pay_no];
- $this->success('快去支付吧',$info);
- }
- /**
- * @Title("购物车下单")
- * @Method("post")
- * @Param("cart_ids",desc="购物车id(数组)")
- * @Param ("address_id",desc="地址id")
- */
- public function crat_create(){
- $user =$this->getUser();
- $cart_ids = input('cart_ids');
- $item_ids = ShopGoodsCart::mk()->whereIn('id',$cart_ids)->select();
- foreach ($item_ids as &$v){
- $item_total = ShopGoodsItem::mk()->where('id',$v['item_id'])->field('stock_total,goods_spec')->find();
- if($v['num']>$item_total['stock_total']){
- $this->error($item_total['goods_spec'].'规格没有库存了',null);
- }
- }
- $address_id = input('address_id');
- $order_remark =input('order_remark');
- $pay_no =$this->pay_no($user['id']);
- $data=[
- 'uuid'=>$user['id'],
- 'cart_ids'=>$cart_ids,
- 'address_id'=>$address_id,
- 'pay_no'=>$pay_no,
- 'order_remark'=>$order_remark
- ];
- $shop_order_model = new ShopOrder();
- $shop_order_model->cart_create($data);
- $order_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'payment_trade'=>$pay_no))->sum('payment_amount');
- $pay_data=[
- 'pay_no'=>$pay_no,
- 'money'=>$order_money,
- 'status'=>1,
- 'create_at'=>date('Y-m-d H:i:s'),
- 'cart_ids' => $cart_ids
- ];
- ShopGoodsCart::mk()->whereIn('id',$cart_ids)->delete();
- if($order_money <= 0){
- $pay_a = ShopOrderPay::mk()->where(array('cart_ids'=>$cart_ids))->find();
- $pay_no = $pay_a['pay_no'];
- }else{
- ShopOrderPay::mk()->insert($pay_data);
- }
- $this->success('快去支付吧',$pay_no);
- }
- /**
- * @Title("商城下单支付")
- * @Method("post")
- * @Header("api-name",type="string",require=1,default="",desc="访问类型")
- * @Header("api-token",type="string",require=1,default="",desc="token")
- * @Param("pay_code",type="string",require=1,default="",desc="支付方式code")
- * @Param("pay_no",type="string",require=1,default="",desc="下单支付单号")
- * @Param("type",type="string",require=1,default="",desc="支付类型 alipay/wechat")
- */
- public function order_pay(){
- $user = $this->getUser();
- $data = $this->_vali([
- 'pay_code.require'=>'支付code有误',
- 'pay_no.require'=>'支付订单好不能为空',
- 'type.require'=>'支付类型不能为空'
- ]);
- $code = $data['pay_code'];
- $pay_type = $data['type'];
- $pay_no = $data['pay_no'];
- $pay_money= ShopOrderPay::mk()->where(['status' => 1, 'pay_no'=>$pay_no])->value('money');
- /* $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');
- if($is_goods == 0){
- $this->error('商品已下架');
- }*/
- if(empty($pay_money)){
- $this->error('找不到订单');
- }
- ShopOrderPay::mk()->where(['status' => 1, 'pay_no'=>$pay_no])->save(['pay_type'=>$pay_type]);
- $all_price = $pay_money;
- $order_no = $pay_no;
- $error = 1;
- Db::startTrans();
- $retrun_data = [];
- try {
- if (stripos($pay_type, 'alipay') === 0) {
- $notify_name = 'orderzfb';
- $payment_type = '支付宝支付';
- } elseif (stripos($pay_type, 'wechat') === 0) {
- $notify_name = 'orderwx';
- $payment_type = '微信支付';
- }
- ShopOrder::mk()->where('payment_trade',$order_no)->save(['payment_type'=>$payment_type]);
- $notify_url = sysuri("@data/api.notify/{$notify_name}/scene/order/param/{$code}", [], false, true);
- $parm = ['uuid'=>$this->uuid];
- $pay =PaymentService::instance($code)->create($user['openid1'],$order_no,$all_price,'商品下单支付','商品下单支付','','',$notify_url,$parm);
- $retrun_data['order_no'] = $order_no;
- $pay['packages'] = isset($pay['package']) ? $pay['package'] : '';
- $retrun_data['pay'] = $pay;
- $retrun_data['success'] = isset($pay['result']) && $pay['result']=='success' ? 1 : 0;
- Db::commit();
- } catch (\Exception $e) {
- dump($e);
- // 回滚事务
- Db::rollback();
- $error = 0;
- $errorinfo = '未知错误';
- }
- if ($error){
- $this->success('成功',$retrun_data);
- }
- $this->error($errorinfo);
- }
- /**
- * @Title("我的订单")
- * @Method("post")
- * @Header("api-name",type="string",require=1,default="",desc="访问类型")
- * @Header("api-token",type="string",require=1,default="",desc="token")
- * @Param("order_name",desc="搜索名称")
- * @Param ("status",desc="1全部 2 待支付 3代发货 4 待收货 5退款/已退款 6 已完成")
- */
- public function order_list(){
- $user = $this->getUser();
- $status = input('status',1);
- $where= [];
- if(!empty($status)){
- switch ($status){
- case 1:
- $array = [0,1,2,3,4,5,6,7,8];
- break;
- case 2:
- $array = [2];
- break;
- case 3:
- $array = [4,7];
- break;
- case 4:
- $array = [5];
- break;
- case 5:
- $array = [4,7,8];
- $where = [
- 'user_refund_del'=>1,
- ];
- break;
- case 6:
- $array = [6];
- break;
- }
- }
- $query = ShopOrder::mQuery()->like('order_name');
- if($status != 5){
- $list = $query ->where('uuid',$user['id'])
- ->whereIn('status',$array)->where($where)->order('id desc')->page(true, false, false, 10);
- }else{
- $list = $query ->where('uuid',$user['id'])
- ->whereIn('status',$array)->where($where)->whereNotNull('refund_status')->order('id desc')->page(true, false, false, 10);
- }
- //自动确认收货时间(天)
- $order_timeout_end_time = SystemConfig('merchant_config.order_timeout_end_time');
- //未支付取消时间(分钟)
- $cancellation_time = SystemConfig('merchant_config.cancellation_time');
- foreach ($list['list'] as $k=>$v){
- $list['list'][$k]['time']=0;
- if($list['list'][$k]['status']==2){
- $list['list'][$k]['time'] = strtotime($list['list'][$k]['create_at'])+$cancellation_time*60-time();
- if($list['list'][$k]['time']<=3){
- ShopOrder::mk()->where('id',$list['list'][$k]['id'])->save(['status'=>0]);
- $list['list'][$k]['status'] = 0;
- }
- }
- $list['list'][$k]['admin_name']= SystemUser::mk()->where('id',$list['list'][$k]['admin_id'])->value('nickname');
- $list['list'][$k]['headimg']= SystemUser::mk()->where('id',$list['list'][$k]['admin_id'])->value('headimg');
- $list['list'][$k]['goods_item']=ShopOrderItem::mk()->where('order_no',$list['list'][$k]['order_no'])->select();
- foreach ($list['list'][$k]['goods_item'] as &$vv){
- $vv['item_id'] = ShopGoodsItem::mk()->where('goods_sku',$vv['goods_sku'])->value('id');
- if(count(explode('|',$vv['goods_cover']))>1){
- $vv['goods_cover'] = ShopGoodsItem::mk()->where('goods_spec',$vv['goods_spec'])->value('show_image');
- }
- $vv['goods_cover'] = $vv['goods_cover'] ? : ShopGoods::mk()->where('code',$vv['goods_code'])->value('cover');
- $arr = explode("|", $vv['goods_cover']);
- $vv['goods_cover'] = $arr[0];
- }
- $list['list'][$k]['goods_id'] = ShopGoods::mk()->where('code',$list['list'][$k]['goods_item'][0]['goods_code'])->value('id');
- }
- $this->success('我的订单列表',$list);
- }
- /**
- * @Title ("订单详情")
- * @Method ("post")
- * @Param ("order_id",desc="订单id")
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function order_info(){
- $user = $this->getUser();
- $order_id = input('order_id');
- $list = ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->find();
- if($list['status']==2){
- $list['time'] = strtotime($list['create_at'])+30*60-time();
- if($list['time']<=3){
- ShopOrder::mk()->where('id',$list['id'])->save(['status'=>0]);
- }
- }
- $config = SystemConfig('merchant_config');
- //取消待支付剩余时间
- if($list['status']==2){
- $list['cancel_datetime_ing'] = strtotime($list['create_at'])+$config['cancellation_time']*60-time();
- }
- //退款剩余时间
- if($list['status']==7){
- $list['refund_time_ing'] = strtotime($list['refund_time'])+$config['refund_time']*60*60-time();
- }
- //自动确认收货时间(天)
- if($list['status']==5){
- $list['order_timeout_end_time'] = strtotime($list['create_at'])+$config['order_timeout_end_time']*60*60*24-time();
- }
- $list['goods_item']=ShopOrderItem::mk()->where('order_no',$list['order_no'])->select();
- foreach ($list['goods_item'] as &$vv){
- // $vv['id'] = ShopGoodsItem::mk()->where('goods_sku',$vv['goods_sku'])->value('id');
- if(count(explode('|',$vv['goods_cover']))>1){
- $vv['goods_cover'] = ShopGoodsItem::mk()->where('goods_spec',$vv['goods_spec'])->value('show_image');
- }
- $vv['goods_cover'] = $vv['goods_cover'] ? : ShopGoods::mk()->where('code',$vv['goods_code'])->value('cover');
- $vv['goods_id'] = ShopGoods::mk()->where('code',$vv['goods_code'])->value('id');
- }
- $list['user_address']=ShopOrderSend::mk()->where('order_no',$list['order_no'])->find();
- $list['admin_name']= SystemUser::mk()->where('id',$list['admin_id'])->value('nickname');
- $list['admin_headimag']= SystemUser::mk()->where('id',$list['admin_id'])->value('headimg');
- $list['user_info']=DataUser::mk()->where('id',$list['uuid'])->field('headimg,nickname')->find();
- $this->success('店铺订单列表',$list);
- }
- /**
- * @Title ("订单类型数量统计")
- * @Method ("post")
- * @return void
- */
- public function order_num(){
- $user = $this->getUser();
- $type = [
- '1'=>[2],
- '2'=>[4,7],
- '3'=>[5],
- // '4'=>[4,7,8],
- '4'=>[7],
- ];
- $where['uuid'] = $user['id'];
- $value = [];
- foreach ($type as $k=>$v){
- if($k==4){
- $where['user_refund_del']=1;
- array_unshift($value,ShopOrder::mk()->whereIn('status',$v)->Where($where)->whereNotNull('refund_status')->count());
- }else{
- array_unshift($value,ShopOrder::mk()->whereIn('status',$v)->Where($where)->count());
- }
- }
- $this->success('请求成功',array_reverse($value));
- }
- /**
- * @Title ("提醒发货")
- * @Method ("post")
- * @Param ("order_id",desc="订单id")
- * @return void
- */
- public function remind(){
- $user = $this->getUser();
- $order_id = input('order_id');
- ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->save(['remind'=>1]);
- $this->success('已提醒发货');
- }
- /**
- * @Title ("取消订单")
- * @Method ("post")
- * @Param ("order_id",desc="订单id")
- * @Param ("cancel_remark",desc="取消原因")
- * @return void
- */
- public function cancel_order(){
- $user = $this->getUser();
- $order_id = input('order_id');
- $cancel_remark = input('cancel_remark');
- ShopOrder::mk()->where('uuid',$user['id'])->where('id',$order_id)->save(['status'=>0,'cancel_remark'=>$cancel_remark]);
- $mes = ShopOrderItem::mk()->where('order_no',ShopOrder::mk()->where('id',$order_id)->value('order_no'))->find();
- $admin_id = ShopOrder::mk()->where('order_no',$mes['order_no'])->value('admin_id');
- $data = [
- 'type'=>2,
- 'uuid'=>$admin_id,
- 'title'=>'订单被取消了',
- 'content'=>$mes['goods_name'],
- 'image'=>$mes['goods_cover'],
- 'remark'=>'取消原因'.$cancel_remark
- ];
- $shop_order_model = new SystemUserMessage();
- $shop_order_model->systemMessageadd($data);
- $this->success('已取消订单');
- }
- /**
- * @Title ("申请退款")
- * @Method ("post")
- * @Param ("type",desc="退款类型")
- * @Param ("status",desc="收货状态")
- * @Param ("order_id",desc="订单id")
- * @Param ("reason",desc="退款原因")
- * @Param("desc",desc="描述")
- * @param ("images",desc="图片")
- */
- public function refund(){
- $user = $this->getUser();
- $refund_type = input('type');
- $goods_status = input('status','已收货');
- $order_id = input('order_id');
- $refund_money = ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->value('payment_amount');
- $refund_reason = input('reason');
- $images = input('images');
- if(empty($refund_reason)){
- $this->error('退款原因不能为空');
- }
- $refund_desc = input('desc');
- $refund_data = [
- 'refund_money'=>$refund_money,
- 'refund_reason'=>$refund_reason,
- 'refund_desc'=>$refund_desc,
- 'refund_nr'=>$images,
- 'refund_type'=>$refund_type,
- 'goods_status'=>$goods_status,
- 'refund_status'=>1,
- 'refund_del'=>1,
- 'status'=>7,
- 'refund_time'=>date('Y-m-d H:i:s')
- ];
- ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save($refund_data);
- $this->success('退款已提交');
- }
- /**
- * @Title ("填写退回物流信息")
- * @Method ("post")
- * @Param ("express_name",desc="快递公司")
- * @Param ("express_no",desc="快递单号")
- * @return void
- */
- public function refund_return(){
- $user = $this->getUser();
- $order_id = input("order_id");
- $express_name = input('express_name');
- $express_no = input('express_no');
- ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['express_name'=>$express_name,'express_no'=>$express_no]);
- $this->success("退款退货物流信息");
- }
- /**
- * @Title ("确认收货")
- * @Method ("post")
- * @Param ("order_id",desc="订单id")
- * @return void
- */
- public function order_end(){
- $user = $this->getUser();
- $order_id = input("order_id");
- Db::startTrans();
- try {
- $order_info =ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->field('amount_total,admin_id')->find();
- $user_money = SystemUser::mk()->where('id',$order_info['admin_id'])->value('money');
- ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['status'=>6]);
- $result = $user_money+$order_info['amount_total'];
- SystemUser::mk()->where('id',$order_info['admin_id'])->save(['money'=>$result]);
- $record = [
- 'admin_id' => $order_info['admin_id'],
- 'before' => $user_money,
- 'after' => $result,
- 'type' => 1,
- 'amount' => $order_info['amount_total'],
- 'form' => '商品订单',
- 'create_time' => date('y-m-d h:i:s')
- ];
- SystemUserAmount::mk()->insert($record);
- Db::commit();
- $this->success("确认收货");
- } catch (ErrorException $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e);
- }
- }
- /**
- * @Title ("删除退款记录")
- * @Method ("post")
- * @Param ("order_id",desc="订单id")
- * @return void
- */
- public function del_refund(){
- $user = $this->getUser();
- $data = $this->_vali(
- [
- 'order_id.require'=>'订单id不能为空',
- ]
- );
- ShopOrder::mk()->where('id',$data['order_id'])->save(['user_refund_del'=>2]);
- $this->success('退款记录已清除');
- }
- /**
- * @Title ("撤销退款申请表")
- * @Method ("post")
- * @Param ("order_id",desc="订单号")
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cancel_refund(){
- $user = $this->getUser();
- $order_id = input('order_id');
- $refund_order = ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id,'status'=>7))->find();
- if(empty($refund_order)){
- $this->error('订单信息错误');
- }
- ShopOrder::mk()->where(array('uuid'=>$user['id'],'id'=>$order_id))->save(['status'=>4,'refund_status'=>0]);
- $this->success('订单退款申请已撤回');
- }
- public function pay_no($uuid){
- $time_str = date('YmdHi');
- $pay_no = 'PAY'.$time_str . rand(00000,99999).$uuid;
- return $pay_no;
- }
- }
|