审查视图

app/portal/controller/UsersController.php 12.1 KB
董瑞恩 authored
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/23
 * Time: 18:26
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
董瑞恩 authored
13
use EasyWeChat\Foundation\Application;
董瑞恩 authored
14
use think\Db;
董瑞恩 authored
15 16
use wxapp\pay\WeixinPay;
董瑞恩 authored
17 18 19 20 21
/**
 * @title 用户相关接口
 * @description 用户相关接口
 * @group 用户相关接口
 */
董瑞恩 authored
22
class UsersController extends HomeBaseController{
董瑞恩 authored
23 24 25 26 27 28 29 30 31 32
    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'),
        ];
    }
董瑞恩 authored
33 34 35 36
    /**
     * @title 状态验证
     * @description 开锁前判断是否有未支付订单与是否提交押金
     * @author 董瑞恩
董瑞恩 authored
37
     * @url /portal/users/lock_check
董瑞恩 authored
38 39 40
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
董瑞恩 authored
41 42 43 44
     *
     * @return is_use:是否在使用设备
     * @return is_deposit:是否交付押金
     * @return is_order:是否有未支付订单
董瑞恩 authored
45 46 47 48 49
     */
    public function lock_check(){
        $users_id=$this->request->param('users_id');
        //获取提交押金的状态
        $users=Db::name('users')->where('id',$users_id)->find();
董瑞恩 authored
50
        $data=[
董瑞恩 authored
51
            'is_use'=>$users['is_use'],
董瑞恩 authored
52 53 54 55 56
            'is_deposit' => $users['is_deposit']
        ];
        $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
        if (empty($order)){
            $data['is_order']=0;
董瑞恩 authored
57
        }else{
董瑞恩 authored
58
            $data['is_order']=1;
董瑞恩 authored
59
        }
董瑞恩 authored
60
        $this->apiResponse(200,'success',$data);
董瑞恩 authored
61
    }
董瑞恩 authored
62
董瑞恩 authored
63 64 65 66 67 68 69 70 71 72 73
//    /**
//     * @title 用户使用状态验证
//     * @description 判断用户当前是否在使用设备
//     * @author 董瑞恩
//     * @url /portal/users/isUse
//     * @method GET
//     *
//     * @param name:users_id type:String require:1 default:无 other: desc:用户id
//     *
//     *
//     */
董瑞恩 authored
74 75 76 77 78 79 80 81 82 83
    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,'用户尚未使用设备');
        }
    }
董瑞恩 authored
84
    /**
董瑞恩 authored
85 86
     * @title 获取用户信息
     * @description 获取用户信息
董瑞恩 authored
87
     * @author 董瑞恩
董瑞恩 authored
88 89 90 91 92 93 94 95
     * @url /portal/users/getUsersInfo
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return users_id:用户id
     * @return phone:用户手机号
     * @return is_deposit:用户是否提交押金
董瑞恩 authored
96
     * @return deposit:用户提交押金金额(单位:元)
董瑞恩 authored
97 98 99
     */
    public function getUsersInfo(){
        $users_id=$this->request->param('users_id');
董瑞恩 authored
100
        $data=Db::name('users')->field('id as users_id,phone,is_deposit,deposit')->where('id',$users_id)->find();
董瑞恩 authored
101 102 103
        $this->apiResponse(200,'success',$data);
    }
董瑞恩 authored
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    /**
     * @title 获取计费规则
     * @description 获取计费规则
     * @author 董瑞恩
     * @url /portal/users/bill_rules
     * @method GET
     *
     * @return bill_rules:计费规则
     */
    public function bill_rules(){
        $data=Db::name('set_up')->field('bill_rules')->where('id',1)->find();
        $data['bill_rules']=html_entity_decode($data['bill_rules']);
        $this->apiResponse(200,'success',$data);
    }

    /**
     * @title 获取使用须知
     * @description 获取使用须知
     * @author 董瑞恩
     * @url /portal/users/directions
     * @method GET
董瑞恩 authored
126
     * @return directions:使用须知
董瑞恩 authored
127 128 129 130 131 132
     */
    public function directions(){
        $data=Db::name('set_up')->field('directions')->where('id',1)->find();
        $data['directions']=html_entity_decode($data['directions']);
        $this->apiResponse(200,'success',$data);
    }
董瑞恩 authored
133 134

    /**
董瑞恩 authored
135 136 137 138 139 140 141 142 143 144 145 146 147 148
     * @title 获取客服电话
     * @description 获取客服电话
     * @author 董瑞恩
     * @url /portal/users/service_phone
     * @method GET

     * @return service_phone:客服电话
     */
    public function service_phone(){
        $data=Db::name('set_up')->field('service_phone')->where('id',1)->find();
        $this->apiResponse(200,'success',$data);
    }

    /**
董瑞恩 authored
149 150 151
     * @title 用户查询订单列表
     * @description 用户查询订单列表
     * @author 董瑞恩
董瑞恩 authored
152 153 154 155 156
     * @url /portal/users/getOrder
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
董瑞恩 authored
157 158 159
     * @return page:当前页
     * @return page:当前页
     *
董瑞恩 authored
160 161 162 163 164 165 166 167 168
     * @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:已支付)
董瑞恩 authored
169 170 171
     */
    public function getOrder(){
        $users_id=$this->request->param('users_id');
董瑞恩 authored
172
        $order=Db::name('order')->where(['users_id'=>$users_id])->paginate(1);
董瑞恩 authored
173
        dump($order.$this->options);
董瑞恩 authored
174
        dump($order);
董瑞恩 authored
175 176 177 178
//        $data=[
//            ''
//        ];
//        $this->apiResponse(200,'success',$order);
董瑞恩 authored
179
    }
董瑞恩 authored
180 181 182 183
    /**
     * @title 用户查询未完成订单信息
     * @description 用户查询在使用订单信息
     * @author 董瑞恩
董瑞恩 authored
184
     * @url /portal/users/getOrderById1
董瑞恩 authored
185 186 187 188 189 190 191 192 193 194 195
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
     * @return order_no:订单号
     * @return users_id:用户id
     * @return start_time:开始使用时间
     * @return end_time:结束使用时间
     * @return time:使用时长(小时)
     * @return price:金额(元)
     */
董瑞恩 authored
196
董瑞恩 authored
197
    public function getOrderById1(){
董瑞恩 authored
198
        $users_id=$this->request->param('users_id');
董瑞恩 authored
199
        $order= Db::name('order')->field('order_no,users_id,start_time')->where(['users_id'=>$users_id,'state'=>1])->find();
董瑞恩 authored
200 201 202
        if (empty($order)){
            $this->apiResponse(200,'没有未完成订单');
        }
董瑞恩 authored
203 204 205
        $order['end_time']=time();
        $order['time']=ceil(($order['end_time']-$order['start_time'])/3600);
        $price=new OrderController();
董瑞恩 authored
206
        $order['price']=$price->getPrice($users_id,$order['start_time'],$order['end_time']);
董瑞恩 authored
207 208
        $this->apiResponse(200,'success',$order);
    }
董瑞恩 authored
209
董瑞恩 authored
210
    /**
董瑞恩 authored
211 212 213 214 215 216 217 218
     * @title 用户查询未支付订单信息
     * @description 用户查询未支付订单信息
     * @author 董瑞恩
     * @url /portal/users/getOrderById2
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     *
董瑞恩 authored
219
     * @return id:订单id
董瑞恩 authored
220
     * @return order_no:订单号
董瑞恩 authored
221
     * @return eq_name:设备名称
董瑞恩 authored
222 223 224 225 226
     * @return users_id:用户id
     * @return start_time:开始使用时间
     * @return end_time:结束使用时间
     * @return time:使用时长(小时)
     * @return price:金额(元)
董瑞恩 authored
227
     * @return state:订单状态(1:没完成 2:已完成 3:已支付)
董瑞恩 authored
228 229 230 231
     */

    public function getOrderById2(){
        $users_id=$this->request->param('users_id');
董瑞恩 authored
232
        $order= Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
董瑞恩 authored
233 234 235 236 237 238 239
        if (empty($order)){
            $this->apiResponse(200,'没有未支付订单');
        }
        $this->apiResponse(200,'success',$order);
    }

    /**
董瑞恩 authored
240 241 242 243 244 245 246 247 248 249 250 251
     * @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');
董瑞恩 authored
252
        $users=Db::name('users')->where('id',$users_id)->find();
董瑞恩 authored
253 254 255
        $openId=$users['open_id'];
        $order_no = cmf_get_order_sn();
        $body="押金-支付";
董瑞恩 authored
256
        $price=1;//数据库查询
董瑞恩 authored
257 258 259 260 261
        $notify_url=url('users/notify','','',true);//回调地址
        $wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
        $data=$wxPay->pay();
        if (isset($data['package'])){
            try{
董瑞恩 authored
262
                Db::name('users')->where(['id'=>$users_id])->update(['deposit_order_no'=>$order_no]);
董瑞恩 authored
263 264 265 266 267 268
            }catch (\Exception $exception){

            }
            $this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
        }
    }
董瑞恩 authored
269
董瑞恩 authored
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    //支付回调接口
    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{
董瑞恩 authored
291
                Db::name('users')->where(['deposit_order_no'=>$order_no])->update($data);
董瑞恩 authored
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
            }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
     *
     */
    public function refundDeposit(){
        $users_id=$this->request->param('users_id');
董瑞恩 authored
314
        $users=Db::name('users')->where('id',$users_id)->find();
董瑞恩 authored
315 316 317 318 319 320 321
        if ($users['is_deposit']==0){
            $this->apiResponse(302,'用户未缴纳押金');
        }
        $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>['in',[1,2]]])->find();
        if (!empty($order)){
            $this->apiResponse(303,'用户还有未支付订单');
        }
董瑞恩 authored
322 323 324 325
        $orderNo=$users['deposit_order_no'];//需要退款的订单
        $price=$users['deposit'];
        Db::startTrans();
        try{
董瑞恩 authored
326
            Db::name('users')->where('id',$users_id)->update(['is_deposit'=>0,'deposit'=>null,'deposit_order_no'=>null]);
董瑞恩 authored
327 328 329 330 331 332 333 334 335 336 337 338
        }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();
董瑞恩 authored
339
            $this->apiResponse(302,'error:'.$result);
董瑞恩 authored
340
        }
董瑞恩 authored
341
        $this->apiResponse(200,'success');
董瑞恩 authored
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    }

    /**
     * 退款结果回调
     */
    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();
    }
董瑞恩 authored
360 361 362 363 364 365


    //每天重置免费试用权限
    public function resetFree(){
        Db::name('users')->where('is_free',1)->update(['is_free'=>0]);
    }
董瑞恩 authored
366
}