StoreOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\model\general\Express;
  4. use app\api\controller\WxPay;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use app\admin\model\order\StoreEvaluate;
  10. /**
  11. * 订单管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class StoreOrder extends Backend
  16. {
  17. /**
  18. * StoreOrder模型对象
  19. * @var \app\admin\model\order\StoreOrder
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\order\StoreOrder;
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model
  46. ->where($where)
  47. ->where('is_system_del','0')
  48. ->where('is_del','0')
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->where($where)
  53. ->with(['shopuser','product','member'])
  54. ->where('is_system_del','0')
  55. ->where('is_del','0')
  56. ->order($sort, $order)
  57. ->limit($offset, $limit)
  58. ->select();
  59. $list = collection($list)->toArray();
  60. $result = array("total" => $total, "rows" => $list);
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = null)
  69. {
  70. $row = $this->model->with(['shopuser','product','member'])->find($ids);
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $adminIds = $this->getDataLimitAdminIds();
  75. if (is_array($adminIds)) {
  76. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. }
  80. if ($this->request->isPost()) {
  81. $params = $this->request->post("row/a");
  82. $params=[];//订单不可以修改
  83. if ($params) {
  84. $params = $this->preExcludeFields($params);
  85. $result = false;
  86. Db::startTrans();
  87. try {
  88. //是否采用模型验证
  89. if ($this->modelValidate) {
  90. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  91. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  92. $row->validateFailException(true)->validate($validate);
  93. }
  94. $result = $row->allowField(true)->save($params);
  95. Db::commit();
  96. } catch (ValidateException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (PDOException $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. } catch (Exception $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. }
  106. if ($result !== false) {
  107. $this->success();
  108. } else {
  109. $this->error(__('No rows were updated'));
  110. }
  111. }
  112. $this->success();
  113. $this->error(__('Parameter %s can not be empty', ''));
  114. }
  115. $this->view->assign("row", $row);
  116. return $this->view->fetch();
  117. }
  118. //发货
  119. /**
  120. * //发货
  121. */
  122. public function express($ids)
  123. {
  124. $row = $this->model->where(['id' => $ids])->field('id,delivery_name,delivery_id,status')->find();
  125. if (!$row) {
  126. $this->error(__('No Results were found'));
  127. }
  128. if ($this->request->isAjax()) {
  129. $post=$this->request->request();
  130. $delivery_name=$post['delivery_name'];
  131. $delivery_id=$post['delivery_id'];
  132. if (!empty($delivery_name) && !empty($delivery_id)){
  133. if ($row['status']=='1' || $row['status']==2){
  134. $this->model->where(['id' => $ids])->update(['delivery_name'=>$delivery_name,'delivery_id'=>$delivery_id,'status'=>'2']);
  135. $this->success("成功", null, ['id' => $ids]);
  136. }
  137. }
  138. $this->error("失败");
  139. }
  140. $get_express=Express::where('is_show',1)->select();
  141. $this->view->assign("row", $row->toArray());
  142. $this->view->assign("express_list", $get_express);
  143. return $this->view->fetch();
  144. }
  145. /**
  146. * //退款
  147. */
  148. public function refund($ids)
  149. {
  150. $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();
  151. if (!$row) {
  152. $this->error(__('No Results were found'));
  153. }
  154. if ($this->request->isAjax()) {
  155. $post=$this->request->request();
  156. $refund_reason=$post['refund_reason'];
  157. $refund_status=$post['refund_status'];
  158. if ($refund_status==1 || $refund_status==3){
  159. if ($row['refund_status'] !='4' && $row['refund_status'] !='5'){
  160. $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
  161. $this->success("成功", null, ['id' => $ids]);
  162. }
  163. }elseif($refund_status==2){
  164. if ($row['refund_type']=='0'){
  165. if ($row['refund_status'] !='4' && $row['refund_status'] !='5'){
  166. $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
  167. $this->success("成功", null, ['id' => $ids]);
  168. }
  169. }else{
  170. if ($row['refund_status'] !='5'){
  171. $reoderSn=$row['order_id'];//订单编号
  172. //获取支付金额
  173. $money =$row['pay_price'];
  174. $wx = new WxPay();//实例化微信支付控制器
  175. $body = '订单号' . $row['order_id'];//支付说明
  176. $out_trade_no = $reoderSn;//订单号
  177. $total_fee = $money * 100;//支付金额(乘以100)
  178. $refund_fee=$row['refund_price']*100;
  179. $order = $wx->wxrefund($out_trade_no,date('YmdHis').time(),$total_fee,$refund_fee);
  180. // dump($order);
  181. if(($order['return_code']=='SUCCESS') && ($order['result_code']=='SUCCESS')){
  182. $this->model->where(['id' => $ids])->update(['refund_status'=>'5','refund_reason'=>$refund_reason,'status'=>'-5']);
  183. $this->success("成功", null, ['id' => $ids]);
  184. }else if(($order['return_code']=='FAIL') || $order['result_code']=='FAIL' ){
  185. $reason = (empty($order['err_code_des'])?$order['return_msg']:$order['err_code_des']);
  186. $this->error($reason);
  187. }else{
  188. $this->error("退款失败");
  189. }
  190. }
  191. }
  192. }elseif($refund_status=5){
  193. if ($row['refund_status'] !='5'){
  194. $reoderSn=$row['order_id'];//订单编号
  195. //获取支付金额
  196. $money =$row['pay_price'];
  197. $wx = new WxPay();//实例化微信支付控制器
  198. $body = '订单号' . $row['order_id'];//支付说明
  199. $out_trade_no = $reoderSn;//订单号
  200. $total_fee = $money * 100;//支付金额(乘以100)
  201. $refund_fee=$row['refund_price']*100;
  202. $order = $wx->wxrefund($out_trade_no,date('YmdHis').time(),$total_fee,$refund_fee);
  203. // dump($order);
  204. if(($order['return_code']=='SUCCESS') && ($order['result_code']=='SUCCESS')){
  205. $this->model->where(['id' => $ids])->update(['refund_status'=>$refund_status,'refund_reason'=>$refund_reason,'status'=>0-$refund_status]);
  206. $this->success("成功", null, ['id' => $ids]);
  207. }else if(($order['return_code']=='FAIL') || $order['result_code']=='FAIL' ){
  208. $reason = (empty($order['err_code_des'])?$order['return_msg']:$order['err_code_des']);
  209. $this->error($reason);
  210. }else{
  211. $this->error("退款失败");
  212. }
  213. }
  214. $this->error("失败");
  215. }
  216. $this->error("失败");
  217. }
  218. $get_express=Express::where('is_show',1)->select();
  219. $images='';
  220. if (!empty($row['refund_reason_wap_img'])){
  221. $images=explode(',',$row['refund_reason_wap_img']);
  222. if (!empty($images)){
  223. foreach ($images as $k=>$v){
  224. $images[$k]=config('site.siteUrl').$v;
  225. }
  226. }
  227. }
  228. $this->view->assign("images", $images);
  229. $this->view->assign("row", $row->toArray());
  230. $this->view->assign("express_list", $get_express);
  231. return $this->view->fetch();
  232. }
  233. /**
  234. * //评价
  235. */
  236. public function evaluate($ids)
  237. {
  238. $row = StoreEvaluate::where(['order_id' => $ids,'pid'=>'0'])->find();
  239. if (!$row) {
  240. $this->error(__('No Results were found'));
  241. }
  242. if ($this->request->isAjax()) {
  243. $post=$this->request->request();
  244. $huifu_content=$post['huifu_content'];
  245. $huifu_id=$post['huifu_id'];
  246. if (!empty($huifu_id)){
  247. if (empty($huifu_content)){
  248. StoreEvaluate::where('id',$huifu_id)->delete();
  249. }else{
  250. StoreEvaluate::where('id',$huifu_id)->update(['content'=>$huifu_content,'updatetime'=>time()]);
  251. }
  252. }else{
  253. if (!empty($huifu_content)){
  254. $insert['mid']='';
  255. $insert['store_id']=$row['store_id'];
  256. $insert['order_id']=$row['order_id'];
  257. $insert['suser_id']=$row['suser_id'];
  258. $insert['xj']='0';
  259. $insert['content']=$huifu_content;
  260. $insert['pid']=$row['id'];
  261. $insert['status']=1;
  262. $insert['createtime']=$insert['updatetime']=time();
  263. StoreEvaluate::insert($insert);
  264. }
  265. }
  266. $this->success("success");
  267. }
  268. $get_huifu=StoreEvaluate::where('pid',$row['id'])->find();
  269. if (empty($get_huifu)){
  270. $get_huifu['id']='0';
  271. $get_huifu['content']='';
  272. }
  273. $this->view->assign("row", $row->toArray());
  274. $this->view->assign("huifu", $get_huifu);
  275. return $this->view->fetch();
  276. }
  277. /**
  278. * 删除
  279. */
  280. public function del($ids = "")
  281. {
  282. if ($ids) {
  283. $pk = $this->model->getPk();
  284. $adminIds = $this->getDataLimitAdminIds();
  285. if (is_array($adminIds)) {
  286. $this->model->where($this->dataLimitField, 'in', $adminIds);
  287. }
  288. $list = $this->model->where($pk, 'in', $ids)->select();
  289. $count = 0;
  290. Db::startTrans();
  291. try {
  292. foreach ($list as $k => $v) {
  293. $count += $v->save(['is_system_del'=>1]);
  294. }
  295. Db::commit();
  296. } catch (PDOException $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage());
  299. } catch (Exception $e) {
  300. Db::rollback();
  301. $this->error($e->getMessage());
  302. }
  303. if ($count) {
  304. $this->success();
  305. } else {
  306. $this->error(__('No rows were deleted'));
  307. }
  308. }
  309. $this->error(__('Parameter %s can not be empty', 'ids'));
  310. }
  311. }