StoreOrder.php 15 KB

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