<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 基類實戰--構造函數實戰 構造函數 在類的初始化時候執行的一個方法, > 切記 不要在構造函數里 使用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] 注意 這里微信支付成功并沒有進行數據入庫,為了數據安全,數據入庫在微信的支付回調中
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看