alias('hub') ->rightJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id') ->where(['hub.type'=>0,'hub.status'=>1]) ->field('hub.house_user_bind_id,hv.vacancy_id,hv.vacancy_code,hv.layout_id') ->select()->toArray(); if($vacancys) { $convertArr = $this->convertArr; $array_keys = array_keys($convertArr); foreach ($vacancys as $k =>$v) { $where['vacancy_id'] = $v['vacancy_id']; $where['is_pay'] = 0; $res = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']); $vacancys[$k]['vacancy_address'] = $res['vacancy_address']; foreach ($convertArr as $key=>$value) { $where['type'] = $key; //总金额 $data= Db::name('cost')->where($where)->sum('pay_money'); $all_list[$key] = $data; } $vacancys[$k]['pay_list'] = $all_list; //获取自定义的 unset($where['type']); $vacancys[$k]['pay_list']['other'] = Db::name('cost')->where($where)->whereNotIn('type',array_keys($convertArr))->sum('pay_money'); } foreach ($vacancys as $k =>$v) { foreach ($array_keys as $val ) { //删除水电物业费为0的记录 if($v['pay_list'][$val] ==0) { unset($vacancys[$k]['pay_list'][$val]); } } if($v['pay_list']['other'] ==0) { unset($vacancys[$k]['pay_list']['other']); } } } else { $vacancys = []; } return $this->returnJson($vacancys); } //查看已缴费的列表 public function getPayList() { //获取是业主身份的房间 $vacancys = Db::name('house_user_bind')->alias('hub') ->rightJoin('house_vacancy hv','hv.vacancy_id = hub.vacancy_id') ->where(['hub.type'=>0,'hub.status'=>1]) ->field('hub.house_user_bind_id,hv.vacancy_id,hv.vacancy_code,hv.layout_id') ->select()->toArray(); if($vacancys) { $convertArr = $this->convertArr; $array_keys = array_keys($convertArr); $unit = $this->myUnit; foreach ($vacancys as $k =>$v) { $where['vacancy_id'] = $v['vacancy_id']; $where['is_pay'] = 1; $res = Common::getVacancyAddress($v['vacancy_code'],$v['layout_id']); $vacancys[$k]['vacancy_address'] = $res['vacancy_address']; foreach ($convertArr as $key=>$value) { $where['type'] = $key; //总金额 $data= Db::name('cost')->where($where)->sum('pay_money'); $rest['total_money'] = $data; if($key == "property" || $key =="park"){ $count = Db::name('cost')->where($where)->count(); $total_num = $count."个月"; } else if($key == "water" || $key=="electric" || $key == "gas") { $num = Db::name('cost')->where($where)->sum('area'); $total_num = $num.$unit[$key]; } $rest['total_num'] = $total_num; $all[$key] = $rest; //$vacancys[$k][$key] = $rest; } //获取自定义的 unset($where['type']); $count = Db::name('cost')->where($where)->whereNotIn('type',$array_keys)->sum('pay_money'); // $vacancys[$k]['other'] = $count; $all['other'] = $count; $vacancys[$k]['pay_list'] = $all; } //如果已缴费金额为0,删除,不显示 foreach ($vacancys as $k =>$v) { foreach ($array_keys as $val ) { //删除水电物业费为0的记录 if($v['pay_list'][$val]['total_money'] ==0) { unset($vacancys[$k]['pay_list'][$val]); } else { $pay_time = Db::name('cost')->where(['is_pay'=>1,'type'=>$val,'vacancy_id'=>$v['vacancy_id']])->order('pay_time','desc')->value('pay_time'); $vacancys[$k]['pay_list'][$val]['pay_time'] = date('Y-m-d H:i:s',$pay_time); } } if($v['pay_list']['other'] ==0) { unset($vacancys[$k]['pay_list']['other']); } else { $pay_time = Db::name('cost')->where(['is_pay'=>1,'vacancy_id'=>$v['vacancy_id']])->whereNotIn('type',$array_keys)->order('pay_time','desc')->value('pay_time'); $vacancys[$k]['pay_list'][$val]['pay_time'] = date('Y-m-d H:i:s',$pay_time); } } //要分开循环删除 foreach ($vacancys as $k => $v) { if(!isset($v['pay_list']['water']) && !isset($v['pay_list']['property']) && !isset($v['pay_list']['electric']) && !isset($v['pay_list']['gas']) && !isset($v['pay_list']['park']) && !isset($v['pay_list']['other'])){ unset($vacancys[$k]); } } } else { $vacancys = []; } return $this->returnJson($vacancys); } //某个房间下的某个收费项的以缴费缴费列表 public function vacancyPayedList(){ $vacancy_id = Request::param('vacancy_id'); $type = Request::param('type'); //property|water|electric|gas|park|other,除了other,其他都能对应表中的type $page = Request::param('page',1); $convertArr = $this->convertArr; if(in_array($type,array_keys($convertArr))){ $where[] = ['type','=',$type]; } else { $where[] = ['type','not in',array_keys($convertArr)]; } $where[] = ['vacancy_id','=',$vacancy_id]; $where[] = ['is_pay','=',1]; //先查询是否合法 $is_exit = Db::name('house_user_bind')->where(['uid'=>$this->uid,'vacancy_id'=>$vacancy_id,'type'=>0])->find(); if(!$is_exit) { return $this->returnJson([],'查询错误!',400); } $data = Db::name('cost')->where($where)->page($page,config('app.limit'))->order('cost_month','desc')->field('cost_id,cost_month,pay_money,type')->select()->toArray(); $total = Db::name('cost')->where($where)->count(); $res['total'] = $total; $res['data'] = $data; return $this->returnJson($res); } //某个房间下的某个收费项的未缴费缴费列表 public function vacancyPayList() { $vacancy_id = Request::param('vacancy_id'); $type = Request::param('type'); //property|water|electric|gas|park|other,除了other,其他都能对应表中的type $page = Request::param('page',1); $convertArr = $this->convertArr; if(in_array($type,array_keys($convertArr))){ $where[] = ['type','=',$type]; } else { $where[] = ['type','not in',array_keys($convertArr)]; } $where[] = ['vacancy_id','=',$vacancy_id]; $where[] = ['is_pay','=',0]; //先查询是否合法 $is_exit = Db::name('house_user_bind')->where(['uid'=>$this->uid,'vacancy_id'=>$vacancy_id,'type'=>0])->find(); if(!$is_exit) { return $this->returnJson([],'查询错误!',400); } $data = Db::name('cost')->where($where)->page($page,config('app.limit'))->order('cost_month','desc')->field('cost_id,cost_month,pay_money,type')->select()->toArray(); $total = Db::name('cost')->where($where)->count(); $res['total'] = $total; $res['data'] = $data; return $this->returnJson($res); } //查看具体的订单详情 public function getDetailPay(){ $cost_id = Request::param('cost_id'); $data = Db::name('cost')->where(['cost_id'=>$cost_id])->find(); return $this->returnJson(Common::changeField($data)); } //生成订单 public function createOrder(){ $vacancy_id = Request::param('vacancy_id'); $bind_id = Request::param('bind_id'); $cost_ids = Request::param('cost_ids'); $total_money = Request::param('total_money',0); //验证物业费订单是否是连续的 $where['type'] = 'property'; $where['is_pay'] = 0; $where['vacancy_id'] = $vacancy_id; $property_cost_ids = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->column('cost_id'); //上传的未缴物业费的连续cost_id $limit = count($property_cost_ids); $cost_property_ids = Db::name('cost')->where($where)->limit($limit)->column('cost_id'); if($cost_property_ids != $property_cost_ids) { return $this->returnJson([],'物业费缴费必须连续!'); } //判断金额是否一致 unset($where['type']); $total = Db::name('cost')->where($where)->whereIn('cost_id',$cost_ids)->sum('pay_money'); if($total != $total_money) { return $this->returnJson([],'支付金额不正确!',400); } $data['uid'] = $this->uid; if(count($cost_ids)>1){ $data['order_type'] = "多订单收费"; } else { $type =Db::name('cost')->whereIn('cost_id',$cost_ids)->value('type'); $data['order_type'] = $type; } $data['order_name'] = "社区收费"; $data['order_num'] = createOrderNum(); $data['vacancy_id'] = $vacancy_id; $res = $this->getProperty($data['vacancy_id']); if($res['code'] !=200){ return $this->returnJson([],$res['data'],400); } $data['property_id'] =$res['data']['property_id']; $data['village_id'] =$res['data']['village_id']; $data['bind_id'] = $bind_id; $data['money'] = $total; $data['create_time'] = time(); $data['cost_ids'] = json_encode($cost_ids); $data['money'] = $total; $order_id = Db::name('pay_order')->insertGetId($data); if($order_id) { unset($res); $res['order_id'] = $order_id; return $this->returnJson($res); } else{ return $this->returnJson([],'添加失败!',400); } } //根据房间id获取当前的物业id和小区id public function getProperty($vacancy_id) { $data = Db::name('house_vacancy')->alias('hv')->leftJoin('house_village hvi','hvi.village_id = hv.village_id')->where(['hv.vacancy_id'=>$vacancy_id])->field('hvi.village_id,hvi.property_id')->find(); if($data){ return ['code'=>200,'data'=>$data]; } else { return ['code'=>400,'data'=>'数据错误!']; } } //生成支付参数 public function createPaySign() { $order_id = Request::param('order_id'); $total_money = Request::param('total_money'); $vacancy_id = Request::param('vacancy_id'); $pay_type = Request::param('pay_type','alipay'); //weixin|alipay if(empty($order_id)) { return $this->returnJson([],'订单未找到!',400); } if($this->checkOrder($order_id,$total_money,$vacancy_id)){ $orderInfo = Db::name('pay_order')->where(['order_id'=>$order_id])->find(); //支付宝支付 if($pay_type =='alipay') { $param['out_trade_no'] = $orderInfo['order_num']; $param['subject'] = $orderInfo['order_name']; $param['total_amount'] = floatval($orderInfo['money']); $res= $this->getAliPaySign($param); //微信支付 } else { $param['order_id'] = $orderInfo['order_id']; $param['total_money'] = $orderInfo['money']; $param['order_name'] = $orderInfo['order_name']; $param['order_num'] = $orderInfo['order_num']; $res= $this->getWeiXinPaySign($param); } return $this->returnJson($res); } else { return $this->returnJson([],'非法的订单信息!',400); } } //获取支付宝支付参数 public function getAliPaySign($param) { require_once "../extend/aliPay/AopClient.php"; require_once "../extend/aliPay/request/AlipayTradeAppPayRequest.php"; $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = config('app.pay_alipay_appid'); $aop->rsaPrivateKey = config('app.pay_alipay_merchant_private_key'); $aop->alipayrsaPublicKey = config('app.pay_alipay_public_key'); $aop->apiVersion = '1.0'; $aop->signType = config('app.pay_alipay_sign_type'); $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeAppPayRequest(); $request->setNotifyUrl(config('app.notifyurl')); $request->setBizContent(json_encode($param)); $response = $aop->sdkExecute($request); return $response; } //获取微信支付参数 public function getWeiXinPaySign($param) { require_once "../extend/weixin/Weixin.php"; $configArr = config('app.weixinPay'); $configArr['notifyurl'] = config('app.notifyurl'); $weixin = new \Weixin($param,$configArr); return $weixin->pay(); } //检查订单是否合法 public function checkOrder($order_id,$total_money,$vacancy_id) { $where['order_id'] = $order_id; $where['uid'] = $this->uid; $where['vacancy_id'] = $vacancy_id; $money = Db::name('pay_order')->where($where)->value('money'); if($money != $total_money) { return false; } return true; } }