123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace app\api\controller;
- use app\admin\model\cost\CostBill;
- use app\admin\model\cost\CostBillPaid;
- use app\admin\model\Cost\CostBillPay;
- use app\admin\model\cost\CostItem;
- use app\admin\model\cost\CostPay;
- use app\admin\model\house\HuList;
- use app\api\model\MemberHouseModel;
- use app\api\model\MemberModel;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 费用接口
- * @ApiWeigh (95)
- */
- class Cost extends Api
- {
- protected $noNeedLogin = ['edit_order','bill_pay1','arrayToXml'];//,'my_keys','my_detail'
- protected $noNeedRight = ['*'];
- /**
- * 缴费类别
- *
- * @ApiTitle (缴费类别)
- * @ApiSummary (缴费类别)
- * @ApiMethod (POST)
- * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
- * @ApiRoute (/api/cost/get_item)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- * @ApiParams (name="token", type="int", required=true, description="请求的Token")
- * @ApiParams (name="house_id", type="string", required=true, description="所选房屋id")
- * @ApiReturn ()
- */
- public function get_item(){
- $userId = $this->request->post('user_id');
- $house_id= $this->request->post('house_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- if (empty($house_id)){
- $this->result('房产信息不全,补齐房产信息再来');
- }
- $get_house=MemberHouseModel::where('mid',$userId)->where('id',$house_id)->field('property_id,village_id,id,mid')->find();
- if (empty($get_house)){
- $this->result('房产信息错误');
- }
- $get_item=CostItem::where(['property_id'=>$get_house['property_id'],'village_id'=>$get_house['village_id'],'is_delete'=>'0'])->field('id,item')->select();
- foreach ($get_item as $k=>$v){
- if ($v['item']=='1'){
- $get_item[$k]['item_name']='物业费';
- }elseif($v['item']=='2'){
- $get_item[$k]['item_name']='垃圾处理费';
- }elseif($v['item']=='3'){
- $get_item[$k]['item_name']='车位服务费';
- }else{
- $get_item[$k]['item_name']='未知';
- }
- }
- $data = [
- [
- 'id'=>1,'item'=>'1','item_name'=>'水费',
- ],
- [
- 'id'=>2,'item'=>'2','item_name'=>'电费',
- ]
- ];
- $data=array_merge($data,$get_item);
- // var_dump($data);
- $this->result('success', $data, 1);
- }
- /**
- * 获取可以缴费的月份
- *
- * @ApiTitle (获取可以缴费的月份)
- * @ApiSummary (获取可以缴费的月份)
- * @ApiMethod (POST)
- * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
- * @ApiRoute (/api/cost/bill_list)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- * @ApiParams (name="token", type="int", required=true, description="请求的Token")
- * @ApiParams (name="house_id", type="string", required=true, description="所选房屋id")
- * @ApiParams (name="item_id", type="string", required=true, description="缴费类别id")
- * @ApiReturn ()
- */
- public function bill_list(){
- $userId = $this->request->post('user_id');
- $house_id= $this->request->post('house_id');
- $item_id= $this->request->post('item_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- if (empty($house_id)){
- $this->result('房产信息不全,补齐房产信息再来');
- }
- //房屋信息
- $get_house=MemberHouseModel::where(['mid'=>$userId,'id'=>$house_id])
- ->field('property_id,village_id,id,mid,dong_id,danyuan_id,hu_id')
- ->find();
- if (empty($get_house)){
- $this->result('房产信息错误');
- }
- //查询这个房屋没有缴费的单子
- $list=CostBill::where(['is_delete'=>'0','status'=>'1','hu_id'=>$get_house['hu_id'],'pay_status'=>'0','item_id'=>$item_id])
- ->field('id,price,start_time,end_time,order_number,true_price')
- ->select();
- foreach ($list as $k=>$v){
- if ($v['start_time_text']==$v['end_time_text']){
- $list[$k]['mouth']=$v['start_time_text'];
- }else{
- $list[$k]['mouth']=$v['start_time_text']."~".$v['end_time_text'];
- }
- }
- $this->result('success', $list, 1);
- }
- /**
- * 选择账单支付
- *
- * @ApiTitle (选择账单支付)
- * @ApiSummary (选择账单支付)
- * @ApiMethod (POST)
- * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
- * @ApiRoute (/api/cost/bill_pay)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- * @ApiParams (name="token", type="int", required=true, description="请求的Token")
- * @ApiParams (name="cost_id", type="string", required=true, description="所选缴费记录id ,拼接")
- * @ApiReturn ({
- "code": "1",
- "msg": "success",
- "time": "1606273002",
- "data": {
- "return_code": "SUCCESS",
- "return_msg": "OK",
- "appid": "wxbe636a16aae015f9",
- "mch_id": "1581511381",
- "nonce_str": "jb8O3ZCCvpR9BFio",
- "sign": "997C8399896E92F25AF90ACD7A04AB44",
- "result_code": "SUCCESS",
- "prepay_id": "wx25105642452237c4a92ad65aeb87a70000",
- "trade_type": "JSAPI"
- }
- })
- */
- public function bill_pay(){
- $userId = $this->request->post('user_id');
- $cost_id= $this->request->post('cost_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- if (empty($cost_id)){
- $this->result('选择缴费账单');
- }
- $cost_array=explode(',',$cost_id);
- $list=[];
- foreach ($cost_array as $k=>$v){
- $bill=CostBill::where(['is_delete'=>'0','status'=>'1','id'=>$v,'pay_status'=>'0'])
- ->field('id,price,order_number,true_price')
- ->find();
- if (empty($bill)){
- $this->result('您选择的账单已经缴费,请刷新重新选择');
- }else{
- $list[]=$bill;
- }
- }
- $get_openid=MemberModel::where('id',$userId)->field('openid')->find();
- if (empty($get_openid['openid'])){
- $this->result('请微信授权登录之后,再来缴费');
- }
- $price=0;
- $order_number=time().rand('1000','9999');
- foreach ($list as $k=>$v){
- CostBill::where('id',$v['id'])->update(['order_number'=>$order_number]);
- $price=$price+$v['true_price'];
- }
- $pay=new WxPay();
- $notify_url = config('site.cdnurl').'/api/cost/edit_order';//回调地址
- $order_pay=$pay->getPrePayOrder('账单缴费', $order_number, $price*100, $notify_url,$get_openid['openid']);
- if ($order_pay['return_msg']=='OK'){
- if ($order_pay['result_code']=='FAIL'){
- $this->result($order_pay['err_code_des']);
- }else{
- $order1 = $pay->getOrder($order_pay['prepay_id']);//执行二次签名返回参数
- $insert=[
- 'mid'=>$userId,
- 'order_number'=>$order_number
- ];
- CostPay::insertGetId($insert);
- $this->result('success',$order1,'1');
- }
- }else{
- $this->result($order_pay['return_msg']);
- }
- }
- /**
- * 我的缴费记录
- *
- * @ApiTitle (我的缴费记录)
- * @ApiSummary (我的缴费记录)
- * @ApiMethod (POST)
- * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
- * @ApiRoute (/api/cost/my_bill)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- * @ApiParams (name="token", type="int", required=true, description="请求的Token")
- * @ApiReturn ()
- */
- public function my_bill(){
- $userId = $this->request->post('user_id');
- if (empty($userId)){
- $this->result('登录信息丢失');
- }
- $get_list=CostPay::where('mid',$userId)->where('status','2')->order('id desc')->select();
- $list=[];
- $item_name='';
- foreach ($get_list as $k=>$v){
- $get_bill=CostBill::where('order_number',$v['order_number'])->where('pay_status','1')->with('item')->find();
- $get_price=CostBill::where('order_number',$v['order_number'])->where('pay_status','1')->sum('true_price');
- if ($get_bill['item_id']=='1'){
- $item_name='水费';
- }elseif($get_bill['item_id']=='2'){
- $item_name='电费';
- }elseif($get_bill['item_id']=='999999999'){
- $item_name='车位服务费';
- }else{
- if ($get_bill['item']['item']=='1'){
- $item_name='物业费';
- }elseif($get_bill['item']['item']=='2'){
- $item_name='垃圾处理费';
- }elseif($get_bill['item']['item']=='3'){
- $item_name='车位服务费';
- }else{
- $item_name='未知';
- }
- }
- $list[$k]['itme']=$item_name;
- $list[$k]['price']=$get_price;
- $list[$k]['time']=date('Y-m-d H:i:s',$get_bill['pay_time']);
- }
- $this->result('success', $list, 1);
- }
- public function edit_order(){
- $payXml = file_get_contents("php://input");
- //将xml格式转化为json格式
- $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
- //将json格式转成数组格式 $result['out_trade_no']
- $result = json_decode($jsonXml, true);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $order = CostBill::where('order_number', $result['out_trade_no'])->where('pay_status','0')->find();
- if (!empty($order)){
- //修改订单状态
- CostBill::where('order_number', $result['out_trade_no'])->update(['pay_status' => 1,'pay_time'=>time()]);
- CostPay::where('order_number', $result['out_trade_no'])->update(['status' => 2,'pay_time'=>time()]);
- $row['price']=$order['price'];
- $row['true_price']=$order['true_price'];
- $row['hu_id']=$order['hu_id'];
- $row['village_id']=$order['village_id'];
- $row['dong_id']=$order['dong_id'];
- $row['danyuan_id']=$order['danyuan_id'];
- $row['cost_bill_id']=$order['id'];
- $row['order_number']=time().rand('1000','9999');
- $row['createtime']=$row['updatetime']= $row['pay_time']=time();
- $row['mark']=$order['mark'];
- $row['pay_type']='微信';
- CostBillPay::insert($row);
- CostBill::where('order_number', $result['out_trade_no'])->update(['pay_status' => 1,'pay_time'=>time()]);
- HuList::where('id',$order['hu_id'])->update(['last_date'=>date('Y-m-d')]);
- }
- $arr = array(
- 'return_code' => 'SUCCESS',
- 'return_msg' => 'OK',
- );
- return $this->arrayToXml($arr);
- }else{
- $arr = array(
- 'return_code' => 'FAIL',
- 'return_msg' => 'FAIL',
- );
- return $this->arrayToXml($arr);
- }
- }
- //数组转xml
- function arrayToXml($arr)
- {
- $xml = "<xml>";
- foreach ($arr as $key=>$val)
- {
- if (is_numeric($val))
- {
- $xml.="<".$key.">".$val."</".$key.">";
- }
- else
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
- }
- $xml.="</xml>";
- return $xml;
- }
- }
|