1.首先需要在新的一个控制器上设置一个进行安全验证的方法。

public function checksession($code){    if(empty($code)) return false;    $appid="wxdee91227065a7dd8";    $secret="30c1beb9b55063b9ed3bde35c6a95fc7";    $url="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";    $ret=https_get($url);    // echo $ret;    // exit;    $arr=json_decode($ret,true);    if(isset($arr['openid']) && isset($arr['session_key'])){      session('openid',$arr['openid']);      session('session_key',$arr['session_key']);      $session_id=session_id();      echo $session_id;    }else{      die($ret);    }      }

2.然后需要进行前台的操作了,在微信小程序app.js中设置,进行向服务器进行请求,然后把session_id存储到本地上来。

    wx.request({          url: 'http://ducaijia.hk01.bdysite.com/phpweb/index.php/home/demo/checksession',          data:{            code:res.code          },          success:res=>{            console.log(res);            wx.setStorage({              key: 'session_id',              data: res.header['Set-Cookie'],            })          }        })

3.然后需要在各个页面上进行header的填写,进行同步获取到session_id.

header:{      Cookie: wx.getStorageSync('session_id')          },

4.在控制器中创造初始化的方法,让每个页面都可以进行安全认证。

public function _initialize(){     //安全认证        if(!session('?openid') || !session('?session_key')){          die('非法用户访问ma');        }  }

TAG标签: 小程序微信认证

http://www.xiaochengxub.com/read-103581.html