123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace app\admin\controller\order;
- use app\admin\model\general\Express;
- use app\api\controller\WxPay;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use app\admin\model\order\StoreEvaluate;
- /**
- * 订单管理
- *
- * @icon fa fa-circle-o
- */
- class StoreOrder extends Backend
- {
-
- /**
- * StoreOrder模型对象
- * @var \app\admin\model\order\StoreOrder
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\order\StoreOrder;
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where('is_system_del','0')
- ->where('is_del','0')
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->with(['shopuser','product','member'])
- ->where('is_system_del','0')
- ->where('is_del','0')
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->with(['shopuser','product','member'])->find($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- $params=[];//订单不可以修改
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->success();
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- //发货
- /**
- * //发货
- */
- public function express($ids)
- {
- $row = $this->model->where(['id' => $ids])->field('id,delivery_name,delivery_id,status')->find();
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if ($this->request->isAjax()) {
- $post=$this->request->request();
- $delivery_name=$post['delivery_name'];
- $delivery_id=$post['delivery_id'];
- if (!empty($delivery_name) && !empty($delivery_id)){
- if ($row['status']=='1' || $row['status']==2){
- $this->model->where(['id' => $ids])->update(['delivery_name'=>$delivery_name,'delivery_id'=>$delivery_id,'status'=>'2']);
- $this->success("成功", null, ['id' => $ids]);
- }
- }
- $this->error("失败");
- }
- $get_express=Express::where('is_show',1)->select();
- $this->view->assign("row", $row->toArray());
- $this->view->assign("express_list", $get_express);
- return $this->view->fetch();
- }
- /**
- * //退款
- */
- public function refund($ids)
- {
- $row = $this->model->where(['id' => $ids])->field('id,pay_price,order_id,refund_status,refund_type,status,refund_reason_wap_img,refund_reason_wap_explain,refund_reason_time,refund_reason_wap,refund_reason,refund_price,refund_delivery_name,refund_delivery_id')->find();
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if ($this->request->isAjax()) {
- $post=$this->request->request();
- $refund_reason=$post['refund_reason'];
- $refund_status=$post['refund_status'];
- if ($refund_status==1 || $refund_status==3){
- if ($row['refund_status'] !='4' && $row['refund_status'] !='5'){
- $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
- $this->success("成功", null, ['id' => $ids]);
- }
- }elseif($refund_status==2){
- if ($row['refund_type']=='0'){
- if ($row['refund_status'] !='4' && $row['refund_status'] !='5'){
- $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
- $this->success("成功", null, ['id' => $ids]);
- }
- }else{
- if ($row['refund_status'] !='5'){
- $reoderSn=$row['order_id'];//订单编号
- //获取支付金额
- $money =$row['pay_price'];
- $wx = new WxPay();//实例化微信支付控制器
- $body = '订单号' . $row['order_id'];//支付说明
- $out_trade_no = $reoderSn;//订单号
- $total_fee = $money * 100;//支付金额(乘以100)
- $refund_fee=$row['refund_price']*100;
- $order = $wx->wxrefund($out_trade_no,date('YmdHis').time(),$total_fee,$refund_fee);
- // dump($order);
- if(($order['return_code']=='SUCCESS') && ($order['result_code']=='SUCCESS')){
- $this->model->where(['id' => $ids])->update(['refund_status'=>'5','refund_reason'=>$refund_reason,'status'=>'-5']);
- $this->success("成功", null, ['id' => $ids]);
- }else if(($order['return_code']=='FAIL') || $order['result_code']=='FAIL' ){
- $reason = (empty($order['err_code_des'])?$order['return_msg']:$order['err_code_des']);
- $this->error($reason);
- }else{
- $this->error("退款失败");
- }
- }
- }
- }elseif($refund_status=5){
- if ($row['refund_status'] !='5'){
- $reoderSn=$row['order_id'];//订单编号
- //获取支付金额
- $money =$row['pay_price'];
- $wx = new WxPay();//实例化微信支付控制器
- $body = '订单号' . $row['order_id'];//支付说明
- $out_trade_no = $reoderSn;//订单号
- $total_fee = $money * 100;//支付金额(乘以100)
- $refund_fee=$row['refund_price']*100;
- $order = $wx->wxrefund($out_trade_no,date('YmdHis').time(),$total_fee,$refund_fee);
- // dump($order);
- if(($order['return_code']=='SUCCESS') && ($order['result_code']=='SUCCESS')){
- $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
- $this->success("成功", null, ['id' => $ids]);
- }else if(($order['return_code']=='FAIL') || $order['result_code']=='FAIL' ){
- $reason = (empty($order['err_code_des'])?$order['return_msg']:$order['err_code_des']);
- $this->error($reason);
- }else{
- $this->error("退款失败");
- }
- }
- $this->error("失败");
- }
- $this->error("失败");
- }
- $get_express=Express::where('is_show',1)->select();
- $images='';
- if (!empty($row['refund_reason_wap_img'])){
- $images=explode(',',$row['refund_reason_wap_img']);
- if (!empty($images)){
- foreach ($images as $k=>$v){
- $images[$k]=config('site.siteUrl').$v;
- }
- }
- }
- $this->view->assign("images", $images);
- $this->view->assign("row", $row->toArray());
- $this->view->assign("express_list", $get_express);
- return $this->view->fetch();
- }
- /**
- * //评价
- */
- public function evaluate($ids)
- {
- $row = StoreEvaluate::where(['order_id' => $ids,'pid'=>'0'])->find();
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if ($this->request->isAjax()) {
- $post=$this->request->request();
- $huifu_content=$post['huifu_content'];
- $huifu_id=$post['huifu_id'];
- if (!empty($huifu_id)){
- if (empty($huifu_content)){
- StoreEvaluate::where('id',$huifu_id)->delete();
- }else{
- StoreEvaluate::where('id',$huifu_id)->update(['content'=>$huifu_content,'updatetime'=>time()]);
- }
- }else{
- if (!empty($huifu_content)){
- $insert['mid']='';
- $insert['store_id']=$row['store_id'];
- $insert['order_id']=$row['order_id'];
- $insert['suser_id']=$row['suser_id'];
- $insert['xj']='0';
- $insert['content']=$huifu_content;
- $insert['pid']=$row['id'];
- $insert['status']=1;
- $insert['createtime']=$insert['updatetime']=time();
- StoreEvaluate::insert($insert);
- }
- }
- $this->success("success");
- }
- $get_huifu=StoreEvaluate::where('pid',$row['id'])->find();
- if (empty($get_huifu)){
- $get_huifu['id']='0';
- $get_huifu['content']='';
- }
- $this->view->assign("row", $row->toArray());
- $this->view->assign("huifu", $get_huifu);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- Db::startTrans();
- try {
- foreach ($list as $k => $v) {
- $count += $v->save(['is_system_del'=>1]);
- }
- Db::commit();
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- }
|