WxCashRefund.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class WxCashRefund extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()){
  9. //分页参数
  10. $length = input('rows',10,'intval'); //每页条数
  11. $page = input('page',1,'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. //排序
  14. $sortRow = input('sidx','sort','trim'); //排序列
  15. $sort = input('sord','asc','trim'); //排序方式
  16. $order = $sortRow.' '.$sort.' ,id desc';
  17. $order_sn = input('order_sn','','trim');
  18. if($order_sn){
  19. $map[] = ['sn','=',$order_sn];
  20. }
  21. $name = input('name','','trim');
  22. if($name){
  23. $map1[] = ['nickname','like','%'.$name.'%'];
  24. $u = Db::name('wxuser')
  25. ->where($map1)
  26. ->where('type',1)
  27. ->column('id');
  28. if(empty($u)){
  29. $map[] = ['id','=',-1];
  30. }else{
  31. $map[] = ['user_id','in',$u];
  32. }
  33. }
  34. $status = input('status','','trim');
  35. if($status != ''){
  36. $map[] = ['status','=',$status];
  37. }
  38. // if($this->user_id > 1){
  39. // $map[] = ['user_id','=',$this->user_id];
  40. // }
  41. $b = input('begin','','trim');
  42. $e = input('end','','trim');
  43. if($b){
  44. $b = date('Y-m-d 00:00:00',strtotime($b));
  45. $map[] = ['create_time','>=',$b];
  46. }
  47. if($e){
  48. $e = date('Y-m-d 23:59:59',strtotime($e));
  49. $map[] = ['create_time','<=',$e];
  50. }
  51. $map[] = ['org_id','=',cur_org_id()];
  52. $map[] = ['del','=',0];
  53. $map = empty($map) ? true: $map;
  54. //数据查询
  55. $lists = Db::name('wx_cash_refund')->where($map)
  56. ->limit($start,$length)
  57. ->order($order)->select();
  58. foreach ($lists as $k=>$v){
  59. $lists[$k]['status_name'] =isset(model('WxCash')->status[$v['status']])?model('WxCash')->status[$v['status']]:'';
  60. $lists[$k]['barrel_name'] = Db::name('wx_barrel')
  61. ->where('id',$v['barrel_id'])
  62. ->value('title');
  63. $lists[$k]['user_name'] = Db::name('wxuser')
  64. ->where('id',$v['user_id'])
  65. ->value('phone');
  66. }
  67. //数据返回
  68. $totalCount = Db::name('wx_cash_refund')->where($map)->count();
  69. $totalPage = ceil($totalCount/$length);
  70. $result['page'] = $page;
  71. $result['total'] = $totalPage;
  72. $result['records'] = $totalCount;
  73. $result['rows'] = $lists;
  74. return json($result);
  75. }else{
  76. $status = model('WxCash')->status;
  77. $this->assign('status',$status);
  78. $this->assign('meta_title','押金退款列表');
  79. return $this->fetch();
  80. }
  81. }
  82. public function details($id){
  83. if(!$id){
  84. $this->error('参数错误');
  85. }
  86. $ret = Db::name('wx_cash_refund')->where('id',$id)
  87. ->find();
  88. $ret['status_name'] =isset(model('WxCash')->status[$ret['status']])?model('WxCash')->status[$ret['status']]:'';
  89. $ret['barrel_name'] = Db::name('wx_barrel')
  90. ->where('id',$ret['barrel_id'])
  91. ->value('title');
  92. $ret['user_name'] = Db::name('wxuser')
  93. ->where('id',$ret['user_id'])
  94. ->value('phone');
  95. $ret['deal_user_name'] = Db::name('user')
  96. ->where('id',$ret['deal_user_id'])
  97. ->value('real_name');
  98. $this->assign('info',$ret);
  99. $this->assign('meta_title','押金退款详情');
  100. return $this->fetch();
  101. }
  102. public function option($id = 0){
  103. $pay = Db::name('wx_cash_refund')->where('id',$id)->find();
  104. if(request()->isPost()){
  105. if(empty($pay)){
  106. $this->error('退款申请不存在');
  107. }
  108. if($pay['status']!=0){
  109. $this->error('当前状态不能操作');
  110. }
  111. $status = input('status/d',0);
  112. if(!in_array($status,[1,2])){
  113. $this->error('状态值错误');
  114. }
  115. Db::startTrans();
  116. try{
  117. $res = Db::name('wx_cash_refund')
  118. ->where('id',$id)
  119. ->update([
  120. 'deal_user_id'=>$this->userId,
  121. 'deal_time'=>getTime(),
  122. 'status'=>$status,
  123. 'update_time'=>getTime(),
  124. 'remark'=>input('remark'),
  125. ]);
  126. if(!$res){
  127. \exception('操作失败');
  128. }
  129. if($status==1){
  130. $cash = Db::name('wx_cash')
  131. ->where('id',$pay['cash_id'])
  132. ->find();
  133. $tkSn = get_unique_id('TPH');
  134. $config = config('app.wx_mini_config');
  135. $app = \EasyWeChat\Factory::payment($config);
  136. $ret = $app->refund->byOutTradeNumber($cash['sn'], $tkSn, $cash['total_money']*100, $pay['amount']*100);
  137. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  138. trace(json_encode($ret));
  139. \exception('退款申请提交失败');
  140. }
  141. Db::name('wx_cash')
  142. ->where('del',0)
  143. ->where('num',0)
  144. ->update(['del'=>1]);
  145. Db::commit();
  146. $this->success('操作成功',url('index'));
  147. }
  148. if($status==2){
  149. $res =Db::name('wx_cash')
  150. ->where('id',$pay['cash_id'])
  151. ->setInc('num',$pay['num']);
  152. if(!$res){
  153. \exception('操作失败');
  154. }
  155. $res =Db::name('wx_cash')
  156. ->where('id',$pay['cash_id'])
  157. ->setInc('amount',$pay['amount']);
  158. if(!$res){
  159. \exception('操作失败');
  160. }
  161. Db::commit();
  162. $this->success('操作成功',url('index'));
  163. }
  164. }catch (Exception $e){
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. }
  168. if($res){
  169. $this->success('操作成功',url('index'));
  170. }else{
  171. $this->error(model('WxCash')->getError());
  172. }
  173. }else{
  174. $this->assign('pay',$pay);
  175. $this->assign('id',$id);
  176. return $this->fetch();
  177. }
  178. }
  179. }