123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace battery\portal\controller;
- use think\Config;
- use think\Request;
- class BatteryBaseController extends BaseController
- {
- function __construct(Request $request)
- {
- $wx_openid=$this->battery_openid_check();
- if($wx_openid){
- $this->battery_wx_autologin($wx_openid);
- }
- $filterInfo=$request->module().'|'.$request->controller().'|'.$request->action();
- $arrayAction=array(
- 'portal|Brand|index',
- 'portal|Brand|goods',
- 'portal|Brand|details',
- 'portal|Brand|searchbrand',
- 'portal|Brand|searchseries',
- 'portal|Brand|searchyear',
- 'portal|Brand|searchmodel',
- 'portal|Brand|getpay',
- 'portal|Brand|payorder',
- );
- $isFilter=false;
- if(in_array($filterInfo,$arrayAction)){
- $isFilter=true;
- }
- if (battery_is_user_login()||$isFilter) {
-
- } else {
- $this->redirect('login/index');
- }
- parent::__construct($request);
- }
- function battery_wx_autologin($openid)
- {
- }
- function battery_openid_check()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
- $wx_appid=Config::get('WX_APPID');
- $wx_appsecret=Config::get('WX_APPSECRET');
- $wx_data_auth_key=Config::get('WX_DATA_AUTH_KEY');
- if (cookie('openid')) {
- $openidsign = md5(cookie('openid'), $wx_data_auth_key);
- if (!cookie('openidsign') or $openidsign != cookie('openidsign')) {
- cookie('openid', null);
- cookie('openidsign', null);
- battery_update_current_user(null);
- } else {
- return cookie('openid');
- }
- } else {
- $request = Request::instance();
- $data=$request->param();
- if (!isset($data['code'])) {
- $redirect_uri = url('index/index','',true,true);
- $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $wx_appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=login#wechat_redirect';
- $this->redirect($url);
- } else {
- $code = $data['code'];
- $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' .$wx_appid . '&secret=' . $wx_appsecret . '&code=' . $code . '&grant_type=authorization_code';
-
- $result = curl_get_contents($url);
- $result = json_decode($result, true);
- $openid = $result['openid'];
- cookie('openid', $openid, 604800);
- $openidsign = md5($openid, $wx_data_auth_key);
- cookie('openidsign', $openidsign, 604800);
- return $openid;
- }
- }
- }
- }
- public function setTitleName($title='默认'){
- $this->assign('title_name',$title);
- }
- }
|