## 基类实战--构造函数实战 构造函数 在类的初始化时候执行的一个方法, > 切记 不要在构造函数里 使用return方法 那么我们经常在构造函数里面做做什么呢? * 数据初始化 注入对象等 * 前置工作的处理 * 权限判断 我们看一下TP5 控制器的构造函数,他在初始化时候注入了request对象,我们继承了控制器,直接就可以使用$this->request调用,同时也封装了View类.同时对前置的beforeActionList属性的方法 进行处理. >[danger] 注意:下面的源码可能会看不懂,这节课我们只是了解构造函数的主要功能就好 ~~~ /** * 构造方法 * @param Request $request Request对象 * @access public */ public function __construct(Request $request = null) { if (is_null($request)) { $request = Request::instance(); } $this->view = View::instance(Config::get('template'), Config::get('view_replace_str')); $this->request = $request; // 控制器初始化 $this->_initialize(); // 前置操作方法 if ($this->beforeActionList) { foreach ($this->beforeActionList as $method => $options) { is_numeric($method) ? $this->beforeAction($options) : $this->beforeAction($method, $options); } } } ~~~ 构造函数常见用处就是权限判断,我们来看一段代码 ~~~ /** * 权限类初始化 * Power by Mikkle * QQ:776329498 */ public function _initialize() { parent::_initialize(); // TODO: Change the autogenerated stub $user_agent = $this->request->server('HTTP_USER_AGENT'); if (! strpos($user_agent, 'MicroMessenger') === false ) $this->isWechatBrowser = true; //判断提交方式和是否微信浏览器 if ($this->request->method() == 'GET' && $this->isWechatBrowser === true){ //未登录 重新登录 if (!$this->checkAuth()&& !$this->no_login ) $this->wxoauth(); $this->isLogin=true; //设置全局登录 $this->loginGlobal(); if(!$this->isReg){ if(!$this->checkUuidMobile()) $this->redirect('user/user_blind.html'); } } } ~~~ 这个是TP5初始化方法,在这个构造函数中,我判断了用户浏览器, >[info] $user_agent = $this->request->server('HTTP_USER_AGENT'); > if (! strpos($user_agent, 'MicroMessenger') === false ) $this->isWechatBrowser = true; 接下来 我判断在get提交的环境微信环境下,进行了权限判断,设置已登录状态,甚至检测该用户是否是注册用户 并跳转到不同的页面... >[info] //判断提交方式和是否微信浏览器 > if ($this->request->method() == 'GET' && $this->isWechatBrowser === true){ > //未登录 重新登录 > if (!$this->checkAuth()&& !$this->no_login ) $this->wxoauth(); > $this->isLogin=true; > //设置全局登录 > $this->loginGlobal(); > if($this->isReg){ > // if(!$this->checkUuidMobile()) $this->redirect('/index/WC_html_1/mainContainer.html#user/user_blind.html'); > } > } ok,那以前写好了,当控制器继承了这个权限类,将会节省很多繁琐的步骤, ~~~ <?php /** * Created by PhpStorm. * User: Mikkle * Email:776329498@qq.com * Date: 2017/2/8 * Time: 11:32 */ namespace app\api\controller; class WxpayAction extends Auth { public function _initialize() { config('default_return_type','html'); parent::_initialize(); // TODO: Change the autogenerated stub } public function index($order_no='2017020453102495'){ if(!$this->isWechatBrowser){ //******* 为了演示方便 这里省略了订单的是否支付验证 另外 微信支付本身就有支付重复验证 这里并没有加入乐观锁过滤 //******* $data=controller('base/WxPay')->payByOrderNo($order_no); $assign_data=[ 'title'=>'为了家健康--订单支付', 'amount'=>$data['amount'], 'order_no'=>$order_no, "jsApiParameters" =>$data['jsApiParameters'], 'openid'=>$this->open_id, 'data_md5'=>md5($order_no.$this->open_id.$data['amount']), //md5验证( 订单号 openid 金额) ]; $this->assign($assign_data); return $this->fetch('wxpay/index'); } public function showOrdersPayOk($order_no,$order_amount,$data_md5){ //md5验证( 订单号 openid 金额) if (md5($order_no.$this->open_id.$order_amount)<>$data_md5){ $assign_data=[ 'title'=>'为了家健康--支付出错了', 'content'=>'你要支付的订单号不存在请核对后再支付!', ]; $this->assign($assign_data); return $this->fetch('wxpay/err'); }else{ $assign_data=[ 'title'=>'为了家健康--该订单已经支付成功', 'amount'=>$order_amount, 'order_no'=>$order_no, ]; $this->assign($assign_data); return $this->fetch('wxpay/ok'); } } public function jsSign($url=''){ $url= $url ? : $this->request->server('HTTP_REFERER'); return json(controller('base/WxApi')->getJsSign($url)); } ~~~ >[danger] 注意 这里微信支付成功并没有进行数据入库,为了数据安全,数据入库在微信的支付回调中