Cost.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\cost\CostBill;
  4. use app\admin\model\cost\CostBillPaid;
  5. use app\admin\model\Cost\CostBillPay;
  6. use app\admin\model\cost\CostItem;
  7. use app\admin\model\cost\CostPay;
  8. use app\admin\model\house\HuList;
  9. use app\api\model\MemberHouseModel;
  10. use app\api\model\MemberModel;
  11. use app\common\controller\Api;
  12. use think\Db;
  13. /**
  14. * 费用接口
  15. * @ApiWeigh (95)
  16. */
  17. class Cost extends Api
  18. {
  19. protected $noNeedLogin = ['edit_order','bill_pay1','arrayToXml'];//,'my_keys','my_detail'
  20. protected $noNeedRight = ['*'];
  21. /**
  22. * 缴费类别
  23. *
  24. * @ApiTitle (缴费类别)
  25. * @ApiSummary (缴费类别)
  26. * @ApiMethod (POST)
  27. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  28. * @ApiRoute (/api/cost/get_item)
  29. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  30. * @ApiParams (name="token", type="int", required=true, description="请求的Token")
  31. * @ApiParams (name="house_id", type="string", required=true, description="所选房屋id")
  32. * @ApiReturn ()
  33. */
  34. public function get_item(){
  35. $userId = $this->request->post('user_id');
  36. $house_id= $this->request->post('house_id');
  37. if (empty($userId)){
  38. $this->result('登录信息丢失');
  39. }
  40. if (empty($house_id)){
  41. $this->result('房产信息不全,补齐房产信息再来');
  42. }
  43. $get_house=MemberHouseModel::where('mid',$userId)->where('id',$house_id)->field('property_id,village_id,id,mid')->find();
  44. if (empty($get_house)){
  45. $this->result('房产信息错误');
  46. }
  47. $get_item=CostItem::where(['property_id'=>$get_house['property_id'],'village_id'=>$get_house['village_id'],'is_delete'=>'0'])->field('id,item')->select();
  48. foreach ($get_item as $k=>$v){
  49. if ($v['item']=='1'){
  50. $get_item[$k]['item_name']='物业费';
  51. }elseif($v['item']=='2'){
  52. $get_item[$k]['item_name']='垃圾处理费';
  53. }elseif($v['item']=='3'){
  54. $get_item[$k]['item_name']='车位服务费';
  55. }else{
  56. $get_item[$k]['item_name']='未知';
  57. }
  58. }
  59. $data = [
  60. [
  61. 'id'=>1,'item'=>'1','item_name'=>'水费',
  62. ],
  63. [
  64. 'id'=>2,'item'=>'2','item_name'=>'电费',
  65. ]
  66. ];
  67. $data=array_merge($data,$get_item);
  68. // var_dump($data);
  69. $this->result('success', $data, 1);
  70. }
  71. /**
  72. * 获取可以缴费的月份
  73. *
  74. * @ApiTitle (获取可以缴费的月份)
  75. * @ApiSummary (获取可以缴费的月份)
  76. * @ApiMethod (POST)
  77. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  78. * @ApiRoute (/api/cost/bill_list)
  79. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  80. * @ApiParams (name="token", type="int", required=true, description="请求的Token")
  81. * @ApiParams (name="house_id", type="string", required=true, description="所选房屋id")
  82. * @ApiParams (name="item_id", type="string", required=true, description="缴费类别id")
  83. * @ApiReturn ()
  84. */
  85. public function bill_list(){
  86. $userId = $this->request->post('user_id');
  87. $house_id= $this->request->post('house_id');
  88. $item_id= $this->request->post('item_id');
  89. if (empty($userId)){
  90. $this->result('登录信息丢失');
  91. }
  92. if (empty($house_id)){
  93. $this->result('房产信息不全,补齐房产信息再来');
  94. }
  95. //房屋信息
  96. $get_house=MemberHouseModel::where(['mid'=>$userId,'id'=>$house_id])
  97. ->field('property_id,village_id,id,mid,dong_id,danyuan_id,hu_id')
  98. ->find();
  99. if (empty($get_house)){
  100. $this->result('房产信息错误');
  101. }
  102. //查询这个房屋没有缴费的单子
  103. $list=CostBill::where(['is_delete'=>'0','status'=>'1','hu_id'=>$get_house['hu_id'],'pay_status'=>'0','item_id'=>$item_id])
  104. ->field('id,price,start_time,end_time,order_number,true_price')
  105. ->select();
  106. foreach ($list as $k=>$v){
  107. if ($v['start_time_text']==$v['end_time_text']){
  108. $list[$k]['mouth']=$v['start_time_text'];
  109. }else{
  110. $list[$k]['mouth']=$v['start_time_text']."~".$v['end_time_text'];
  111. }
  112. }
  113. $this->result('success', $list, 1);
  114. }
  115. /**
  116. * 选择账单支付
  117. *
  118. * @ApiTitle (选择账单支付)
  119. * @ApiSummary (选择账单支付)
  120. * @ApiMethod (POST)
  121. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  122. * @ApiRoute (/api/cost/bill_pay)
  123. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  124. * @ApiParams (name="token", type="int", required=true, description="请求的Token")
  125. * @ApiParams (name="cost_id", type="string", required=true, description="所选缴费记录id ,拼接")
  126. * @ApiReturn ({
  127. "code": "1",
  128. "msg": "success",
  129. "time": "1606273002",
  130. "data": {
  131. "return_code": "SUCCESS",
  132. "return_msg": "OK",
  133. "appid": "wxbe636a16aae015f9",
  134. "mch_id": "1581511381",
  135. "nonce_str": "jb8O3ZCCvpR9BFio",
  136. "sign": "997C8399896E92F25AF90ACD7A04AB44",
  137. "result_code": "SUCCESS",
  138. "prepay_id": "wx25105642452237c4a92ad65aeb87a70000",
  139. "trade_type": "JSAPI"
  140. }
  141. })
  142. */
  143. public function bill_pay(){
  144. $userId = $this->request->post('user_id');
  145. $cost_id= $this->request->post('cost_id');
  146. if (empty($userId)){
  147. $this->result('登录信息丢失');
  148. }
  149. if (empty($cost_id)){
  150. $this->result('选择缴费账单');
  151. }
  152. $cost_array=explode(',',$cost_id);
  153. $list=[];
  154. foreach ($cost_array as $k=>$v){
  155. $bill=CostBill::where(['is_delete'=>'0','status'=>'1','id'=>$v,'pay_status'=>'0'])
  156. ->field('id,price,order_number,true_price')
  157. ->find();
  158. if (empty($bill)){
  159. $this->result('您选择的账单已经缴费,请刷新重新选择');
  160. }else{
  161. $list[]=$bill;
  162. }
  163. }
  164. $get_openid=MemberModel::where('id',$userId)->field('openid')->find();
  165. if (empty($get_openid['openid'])){
  166. $this->result('请微信授权登录之后,再来缴费');
  167. }
  168. $price=0;
  169. $order_number=time().rand('1000','9999');
  170. foreach ($list as $k=>$v){
  171. CostBill::where('id',$v['id'])->update(['order_number'=>$order_number]);
  172. $price=$price+$v['true_price'];
  173. }
  174. $pay=new WxPay();
  175. $notify_url = config('site.cdnurl').'/api/cost/edit_order';//回调地址
  176. $order_pay=$pay->getPrePayOrder('账单缴费', $order_number, $price*100, $notify_url,$get_openid['openid']);
  177. if ($order_pay['return_msg']=='OK'){
  178. if ($order_pay['result_code']=='FAIL'){
  179. $this->result($order_pay['err_code_des']);
  180. }else{
  181. $order1 = $pay->getOrder($order_pay['prepay_id']);//执行二次签名返回参数
  182. $insert=[
  183. 'mid'=>$userId,
  184. 'order_number'=>$order_number
  185. ];
  186. CostPay::insertGetId($insert);
  187. $this->result('success',$order1,'1');
  188. }
  189. }else{
  190. $this->result($order_pay['return_msg']);
  191. }
  192. }
  193. /**
  194. * 我的缴费记录
  195. *
  196. * @ApiTitle (我的缴费记录)
  197. * @ApiSummary (我的缴费记录)
  198. * @ApiMethod (POST)
  199. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  200. * @ApiRoute (/api/cost/my_bill)
  201. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  202. * @ApiParams (name="token", type="int", required=true, description="请求的Token")
  203. * @ApiReturn ()
  204. */
  205. public function my_bill(){
  206. $userId = $this->request->post('user_id');
  207. if (empty($userId)){
  208. $this->result('登录信息丢失');
  209. }
  210. $get_list=CostPay::where('mid',$userId)->where('status','2')->order('id desc')->select();
  211. $list=[];
  212. $item_name='';
  213. foreach ($get_list as $k=>$v){
  214. $get_bill=CostBill::where('order_number',$v['order_number'])->where('pay_status','1')->with('item')->find();
  215. $get_price=CostBill::where('order_number',$v['order_number'])->where('pay_status','1')->sum('true_price');
  216. if ($get_bill['item_id']=='1'){
  217. $item_name='水费';
  218. }elseif($get_bill['item_id']=='2'){
  219. $item_name='电费';
  220. }elseif($get_bill['item_id']=='999999999'){
  221. $item_name='车位服务费';
  222. }else{
  223. if ($get_bill['item']['item']=='1'){
  224. $item_name='物业费';
  225. }elseif($get_bill['item']['item']=='2'){
  226. $item_name='垃圾处理费';
  227. }elseif($get_bill['item']['item']=='3'){
  228. $item_name='车位服务费';
  229. }else{
  230. $item_name='未知';
  231. }
  232. }
  233. $list[$k]['itme']=$item_name;
  234. $list[$k]['price']=$get_price;
  235. $list[$k]['time']=date('Y-m-d H:i:s',$get_bill['pay_time']);
  236. }
  237. $this->result('success', $list, 1);
  238. }
  239. public function edit_order(){
  240. $payXml = file_get_contents("php://input");
  241. //将xml格式转化为json格式
  242. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  243. //将json格式转成数组格式 $result['out_trade_no']
  244. $result = json_decode($jsonXml, true);
  245. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  246. $order = CostBill::where('order_number', $result['out_trade_no'])->where('pay_status','0')->find();
  247. if (!empty($order)){
  248. //修改订单状态
  249. CostBill::where('order_number', $result['out_trade_no'])->update(['pay_status' => 1,'pay_time'=>time()]);
  250. CostPay::where('order_number', $result['out_trade_no'])->update(['status' => 2,'pay_time'=>time()]);
  251. $row['price']=$order['price'];
  252. $row['true_price']=$order['true_price'];
  253. $row['hu_id']=$order['hu_id'];
  254. $row['village_id']=$order['village_id'];
  255. $row['dong_id']=$order['dong_id'];
  256. $row['danyuan_id']=$order['danyuan_id'];
  257. $row['cost_bill_id']=$order['id'];
  258. $row['order_number']=time().rand('1000','9999');
  259. $row['createtime']=$row['updatetime']= $row['pay_time']=time();
  260. $row['mark']=$order['mark'];
  261. $row['pay_type']='微信';
  262. CostBillPay::insert($row);
  263. CostBill::where('order_number', $result['out_trade_no'])->update(['pay_status' => 1,'pay_time'=>time()]);
  264. HuList::where('id',$order['hu_id'])->update(['last_date'=>date('Y-m-d')]);
  265. }
  266. $arr = array(
  267. 'return_code' => 'SUCCESS',
  268. 'return_msg' => 'OK',
  269. );
  270. return $this->arrayToXml($arr);
  271. }else{
  272. $arr = array(
  273. 'return_code' => 'FAIL',
  274. 'return_msg' => 'FAIL',
  275. );
  276. return $this->arrayToXml($arr);
  277. }
  278. }
  279. //数组转xml
  280. function arrayToXml($arr)
  281. {
  282. $xml = "<xml>";
  283. foreach ($arr as $key=>$val)
  284. {
  285. if (is_numeric($val))
  286. {
  287. $xml.="<".$key.">".$val."</".$key.">";
  288. }
  289. else
  290. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  291. }
  292. $xml.="</xml>";
  293. return $xml;
  294. }
  295. }