UsersController.php 7.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/23
 * Time: 18:26
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use wxapp\pay\WeixinPay;

/**
 * @title 用户相关接口
 * @description 用户相关接口
 * @group 用户相关接口
 */
class UsersController extends HomeBaseController{
    protected $options;
    function _initialize()
    {
        parent::_initialize();
        $this->options = [
            'app_id'  => config('wechat_config.app_id'),
            'secret'  => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
    }
    /**
     * @title 状态验证
     * @description 开锁前判断是否有未支付订单与是否提交押金
     * @author 董瑞恩
     * @url /portal/users/lock_check
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return is_use:是否在使用设备
     * @return is_deposit:是否交付押金
     * @return is_order:是否有未支付订单
     */
    public function lock_check(){
        $users_id=$this->request->param('users_id');
        //获取提交押金的状态
        $users=Db::name('users')->where('id',$users_id)->find();
        $data=[
            'is_use'=>$users['is_use'],
            'is_deposit' => $users['is_deposit']
        ];
        $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
        if (empty($order)){
            $data['is_order']=0;
        }else{
            $data['is_order']=1;
        }
        $this->apiResponse(200,'success',$data);
    }

//    /**
//     * @title 用户使用状态验证
//     * @description 判断用户当前是否在使用设备
//     * @author 董瑞恩
//     * @url /portal/users/isUse
//     * @method GET
//     *
//     * @param name:users_id type:String require:1 default:无 other: desc:用户id
//     *
//     *
//     */
    public function isUse(){
        $users_id=$this->request->param('users_id');
        $users=Db::name('users')->where('id',$users_id)->find();
        if ($users['is_use']==1){
            $this->apiResponse(200,'用户正在使用设备');
        }else{
            $this->apiResponse(301,'用户尚未使用设备');
        }
    }

    /**
     * @title 用户查询订单
     * @description 用户查询订单
     * @author 董瑞恩
     * @url /portal/users/getOrder
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return id:订单id
     * @return order_no:订单号
     * @return eq_name:设备名称
     * @return users_id:用户id
     * @return start_time:开始使用时间
     * @return end_time:结束使用时间
     * @return time:使用时长(小时)
     * @return price:金额(元)
     * @return state:订单状态(1:没完成 2:已完成 3:已支付)
     */
    public function getOrder(){
        $users_id=$this->request->param('users_id');
        $order=Db::name('order')->where(['users_id'=>$users_id])->select();
        $this->apiResponse(200,'success',$order);
    }


    /**
     * @title 用户交纳押金
     * @description 交纳押金
     * @author 董瑞恩
     * @url /portal/users/payDeposit
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return data:下单返回值
     */
    public function payDeposit(){
        $users_id=$this->request->param('users_id');
        $users=Db::name('users')->where('id',$users_id)->find();
        $openId=$users['open_id'];
        $order_no = cmf_get_order_sn();
        $body="押金-支付";
        $price=1;//数据库查询
        $notify_url=url('users/notify','','',true);//回调地址
        $wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
        $data=$wxPay->pay();
        if (isset($data['package'])){
            try{
                Db::name('users')->where(['id'=>$users_id])->update(['deposit_order_no'=>$order_no]);
            }catch (\Exception $exception){

            }
            $this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
        }
    }

    //支付回调接口
    public function notify(){
        $param = $this->request->param();
        if ($param == null) {
            $param = file_get_contents("php://input");
            if ($param == null) {
                $param = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        $wxPay=new WeixinPay();
        $data = $wxPay->xmlToArray($param);
        $Sign = $data['sign'];
        //支付成功回调后变更订单状态
        $mySign = $wxPay->getSign($data);
        $order_no = $data['out_trade_no'];
        if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
            $data=[
                'is_deposit' => 1,
                'deposit' => $data['total_fee'],
            ];
            try{
                Db::name('order')->where(['order_no'=>$order_no])->update($data);
            }catch (\Exception $exception){
                $this->apiResponse(301,'error:'.$exception->getMessage());
            }
            return "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>";
        }
    }

    /**
     * @title 用户退回押金
     * @description 用户退回押金
     * @author 董瑞恩
     * @url /portal/users/refundDeposit
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return result:退款接口返回值
     */
    public function refundDeposit(){
        $users_id=$this->request->param('users_id');
        $users=Db::name('users')->where('id',$users_id)->find();
        $orderNo=$users['deposit_order_no'];//需要退款的订单
        $price=$users['deposit'];
        Db::startTrans();
        try{
            Db::name('users')->where('id',$users_id)->update(['is_deposit'=>0,'deposit'=>null,'deposit_order_no'=>null]);
        }catch (\Exception $exception){
             $this->apiResponse(301,'数据库修改失败');
        }
        $app= new Application($this->options);
        $payment = $app->payment;
        //使用商户订单号退款  PS.其他形式参考文档
        $refundNo =cmf_get_order_sn();//退款单号
        $result = $payment->refund($orderNo, $refundNo, $price); // 总金额 100, 退款 80,refundFee可选(为空时全额退款)
        if ($result['return_code']==='SUCCESS' && $result['result_code']==='SUCCESS'){
            Db::commit();
        }else{
            Db::rollback();
        }
        return json_decode($result,true);
    }

    /**
     * 退款结果回调
     */
    public function refundNotify() {
        $app = new Application($this->options);
        $response = $app->payment->handleRefundNotify(function ($message, $reqInfo) {
            cache('message',$message);
            cache('reqInfo',$reqInfo);
            // 其中 $message['req_info'] 获取到的是加密信息
            // $reqInfo 为 message['req_info'] 解密后的信息
            // 你的业务逻辑...
            return true; // 返回 true 告诉微信“我已处理完成”
            // 或返回错误原因 $fail('参数格式校验错误');
        });
        $response->send();
    }
}