diff --git a/api.php b/api.php new file mode 100644 index 0000000..ed9d0c9 --- /dev/null +++ b/api.php @@ -0,0 +1,170 @@ +beforecheckpara(); + $this->checkpara(); + $this->aftercheckpara(); + $this->action(); + $this->display(); + } + + private function beforecheckpara() { + } + + private function checkpara() { + $argv = trim(trim($_GET['argv']),'/'); + if(empty($argv)) { + $this->control_name = 'weibo'; + $this->control_func = 'home'; + } else { + $array = explode('/', $argv); + $this->control_name = $array[0]; + $this->control_func = !isset($array[1]) || $array[1]=='' ? 'home' : $array[1]; + + if(preg_match('/^(login|loginout).*/', $this->control_name)){ + $this->control_name = 'index'; + $this->control_func = $array[0]; + } + } + + // 还原GET + if(!empty($array)) { + unset($_GET['argv']); + unset($array[0]); + unset($array[1]); + + $count = count($array); + for($i=1;$i<=$count/2;$i++) { + $_GET[$array[$i*2]] = $array[$i*2+1]; + } + } + + // 如果URI带有常规传参形式,并入$_GET + $pos = strpos($_SERVER['REQUEST_URI'], '?'); + if($pos!==false) { + $new_uri = substr($_SERVER['REQUEST_URI'], $pos+1); + $new_uri_arr = explode("&", $new_uri); + foreach($new_uri_arr as $v) { + $new_para = explode("=", $v); + $_GET[$new_para[0]] = $new_para[1]; + } + } + } + + private function aftercheckpara() { + session_start(); + session_write_close(); + + //登陆检查 + $needlogin = true; + if ($this->control_name == 'index') { + if (preg_match('/^(ajax_login).*/', $this->control_func)) { + $needlogin = false; + } + }elseif($this->control_name == 'callback'){ + $needlogin = false; + } + + if($_POST['str'] == FREE_LOGIN_STR){ + $needlogin = false; + + if($this->control_name !== 'weibo' || $this->control_func == 'home') $this->ajax_json(false, '暂无权限'); + } + + if ($needlogin) { + $data = $this->checkAuth(); + if (!$data) $this->ajax_json(false, $this->getError()); + + $uobj = new mUser(); + $user_info = $uobj->getAdminUserByOpenid($data['openid']); + if(empty($user_info) || $user_info['status'] != 1) $this->ajax_json(false, '登陆失败'); + + if($this->control_name == 'weibo' && $user_info['aid'] != 1 && $user_info['is_super_admin'] != 1) $this->ajax_json(false, '暂无权限'); + + $this->view['_user_info'] = $this->para['_user_info'] = $user_info; + } + } + + private function action() { + $control_func = empty($this->control_func) ? 'home' : $this->control_func; + + // 判断控制层文件是否存在 + $control_path = dirname(__FILE__).'/control/'.$this->control_name.'.php'; + if(!file_exists($control_path)) { + die('/'.$this->control_name.'.php not exist.'); + } + include_once ($control_path); + + // 判断控制层方法是否存在 + $obj = new $this->control_name; + ##如下根据情况传递公共变量值################## + if (is_array($this->para)) { + foreach ($this->para as $k=>$v) { + $func = 'set' . $k; + $obj->$func($v); + } + } + ##如上根据情况传递公共变量值################## + if (method_exists($obj, $control_func)) { + $res = $obj->$control_func(); + } else { + die('method '.$this->control_func.' not exist.'); + } + + if($obj->getViewFormat()=='json' && $res===false) { + echo urldecode($obj->getError()); + exit; + } + + $this->view = array_merge($this->view, $obj->getView()); + $this->viewFormat = $obj->getViewFormat(); + $this->viewTpl = $obj->getViewTpl(); + } + + private function display() { + if($this->viewFormat=='json') { + $display = new DisplayJson(); + } elseif($this->viewFormat=='string') { + $display = new DisplayNone(); + } else { + $tpl_path = $this->viewTpl=='' ? $this->control_name.'/'.$this->control_func.'.html' : $this->viewTpl; + if(!file_exists(dirname(__FILE__) . '/view/templates/'.$tpl_path)) { // 判断模板是否存在 + die("{$tpl_path} not exist."); + } + $display = new DisplaySmarty($tpl_path); + } + + $display->setView($this->view); + $display->execute(); + } + + private function checkAuth() { + $auth = $_SERVER['HTTP_AUTHORIZATION']; + if (empty($auth)) { + $this->setError('token为空'); + return false; + } + + $jwtobj = new mJwt(); + $data = $jwtobj->getJwtDecode($auth); + if (!$data) { + $this->setError($jwtobj->getError()); + return false; + } + if (time() > $data['exp']) { + $this->setError('token过期'); + return false; + } + + return $data; + } + } + + new run(); + + diff --git a/config/define.php b/config/define.php index 3bafc3b..d507d58 100644 --- a/config/define.php +++ b/config/define.php @@ -30,13 +30,18 @@ define('ADMIN_USER_OPEN', 1); define('ADMIN_USER_CLOSE', 2); + // 小程序获取openid + define('MP_APPID',""); // 公众号APPID + define('MP_SECRET',""); // 公众号秘钥 + define('MP_GET_OPENID_HREF',"https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&grant_type=authorization_code&js_code=%s"); + define('FREE_LOGIN_STR', '2c131fa45a19a7aa6d9d0123g'); define('WEIXIN_OPEN_APPID','wx68a1060c4ec4722f'); define('WEIXIN_OPEN_APPSCRET','9f8a1dd8352e1150a40ebf3262429f69'); define('WEIXIN_OPEN_GET_ACCESS_TOKEN','https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code'); define('WEIXIN_OPEN_GET_PERSONAL_INFORMATION','https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s'); - define('CSS_URL', '//zhishiku.yizherenxin.cn'); + define('CSS_URL', '//know.checkcopy.com'); //css/js版本 define('CSS_JS_VERSION', '1.0.0000007'); diff --git a/control/index.php b/control/index.php index 20027b9..ed8ff4a 100644 --- a/control/index.php +++ b/control/index.php @@ -26,6 +26,19 @@ class index extends publicBase { exit(); } + public function ajax_login() { + $code = $this->post('code'); + if (!$code) $this->ajax_json(false, '参数错误'); + + $obj = new mManage(); + $user = $obj->weixinMpLogin($this->get('code')); + if (!$user) $this->ajax_json(false, $obj->getError()); + + $this->ajax_json(true, '登录成功', array( + 'token' => $user['jwttoken'] + )); + } + public function user_list() { $user_info = $this->get_user_info(); if (!$user_info['is_super_admin']) $this->show_message(false, '您还有此权限'); diff --git a/model/mManage.php b/model/mManage.php index fb02930..669f058 100644 --- a/model/mManage.php +++ b/model/mManage.php @@ -132,4 +132,43 @@ class mManage extends mBase { return $id; } + + /** + * 微信扫码登录 + * @param unknown $code + * @param unknown $state + * @return boolean|string[]|number[] + */ + public function weixinMpLogin($code) { + if (empty($code)) { + $this->setError("缺少回调参数code"); + return false; + } + + $obj = new mWeixinMp(); + $openid = $obj->getOpenid($code); + if (!$openid) { + $this->setError('登录失败'); + return false; + } + + $uobj = new mUser(); + $agent_login_info = $uobj->getAdminUserByOpenid($openid); + if (empty($agent_login_info)) { + $this->setError("账号不存在"); + return false; + } + + $jwttoken = $this->getJwtToken(array('openid' => $openid)); + if (empty($jwttoken)) { + $this->setError($this->getError()); + return false; + } + + $data = array(); + $data['jwttoken'] = $jwttoken; + $data['openid'] = $agent_login_info['openid']; + + return $data; + } } \ No newline at end of file diff --git a/model/mWeixinMp.php b/model/mWeixinMp.php new file mode 100644 index 0000000..2d3ebe2 --- /dev/null +++ b/model/mWeixinMp.php @@ -0,0 +1,25 @@ +getCUrl($get_openid_url); + $get_openid_res = json_decode($jsonres, true); + if (empty($get_openid_res['openid'])) { + $this->writeLog('user', 'get_openid_error_log', $get_openid_url . "|" . $jsonres); + return false; + } + + return $get_openid_res['openid']; + } + + +}