From e404954769f746ff4a1f3ca5572ff2565303c65a Mon Sep 17 00:00:00 2001 From: molong Date: Mon, 18 Sep 2017 23:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E6=8E=A5=E5=8F=A3=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=81=9A=E7=9A=84=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Client.php | 76 ++++++++++++++++++++++++ application/admin/view/client/add.html | 44 ++++++++++++++ application/admin/view/client/index.html | 48 +++++++++++++++ application/api/controller/Index.php | 38 +++++++++++- application/api/controller/User.php | 23 +++++++ application/common/controller/Api.php | 24 ++++++-- application/common/model/Client.php | 20 +++++++ application/common/validate/Client.php | 36 +++++++++++ data/sql.sql | 34 ++++++++++- 9 files changed, 335 insertions(+), 8 deletions(-) create mode 100644 application/admin/controller/Client.php create mode 100644 application/admin/view/client/add.html create mode 100644 application/admin/view/client/index.html create mode 100644 application/api/controller/User.php create mode 100644 application/common/model/Client.php create mode 100644 application/common/validate/Client.php diff --git a/application/admin/controller/Client.php b/application/admin/controller/Client.php new file mode 100644 index 00000000..440fdd57 --- /dev/null +++ b/application/admin/controller/Client.php @@ -0,0 +1,76 @@ + +// +---------------------------------------------------------------------- + +namespace app\admin\controller; +use app\common\controller\Admin; + +class Client extends Admin { + + public function _initialize() { + parent::_initialize(); + $this->model = model('Client'); + } + + + public function index(){ + $list = $this->model->paginate(25); + $data = array( + 'list' => $list, + 'page' => $list->render() + ); + $this->assign($data); + $this->setMeta('客户端列表'); + return $this->fetch(); + } + + public function add(\think\Request $request){ + if (IS_POST) { + $data = $request->param(); + $result = $this->model->validate(true)->save($data); + if (false !== $result) { + return $this->success('成功添加', url('client/index')); + }else{ + return $this->error($this->model->getError()); + } + }else{ + $info['appid'] = rand_string(10, 1); //八位数字appid + $info['appsecret'] = rand_string(32); //32位数字加字母秘钥 + $data = array( + 'info' => $info + ); + $this->assign($data); + $this->setMeta('添加客户端'); + return $this->fetch('add'); + } + } + + public function edit(\think\Request $request){ + if (IS_POST) { + $data = $request->param(); + $result = $this->model->validate(true)->save($data, array('id'=>$request->param('id'))); + if (false !== $result) { + return $this->success('修改添加', url('client/index')); + }else{ + return $this->error($this->model->getError()); + } + }else{ + $info = $this->model->where('id', $request->param('id'))->find(); + $data = array( + 'info' => $info + ); + $this->assign($data); + $this->setMeta('编辑客户端'); + return $this->fetch('add'); + } + } + + public function del(\think\Request $request){ + + } +} \ No newline at end of file diff --git a/application/admin/view/client/add.html b/application/admin/view/client/add.html new file mode 100644 index 00000000..bdb93ec2 --- /dev/null +++ b/application/admin/view/client/add.html @@ -0,0 +1,44 @@ +{extend name="public/base"/} +{block name="body"} +
+
+
+

{$meta_title}

+
+
+
+
+
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+
+ + + +
+
+
+
+
+{/block} \ No newline at end of file diff --git a/application/admin/view/client/index.html b/application/admin/view/client/index.html new file mode 100644 index 00000000..ab10f9ee --- /dev/null +++ b/application/admin/view/client/index.html @@ -0,0 +1,48 @@ +{extend name="public/base"/} +{block name="body"} +
+
+
+

{$meta_title}

+
+
+ 新 增 + +
+
+
+
+ + + + + + + + + + + + + + {volist name="list" id="item"} + + + + + + + + + + {/volist} + +
ID名称APPIDAPPSECRET创建时间更新时间操作
{$item['id']}{$item['title']}{$item['appid']}{$item['appsecret']}{$item['create_time']}{$item['update_time']} + 编辑 + 删除 +
+ {$page} +
+
+
+{/block} \ No newline at end of file diff --git a/application/api/controller/Index.php b/application/api/controller/Index.php index fc819786..ed3fb98b 100644 --- a/application/api/controller/Index.php +++ b/application/api/controller/Index.php @@ -7,7 +7,41 @@ // | Author: molong // +---------------------------------------------------------------------- -namespace app\admin\controller; +namespace app\Api\controller; use app\common\controller\Api; -class Index extends Api {} \ No newline at end of file +class Index extends Api { + + public function index(){ + return $this->fetch(); + } + + public function getToken(\think\Request $request){ + $appid = $request->post('appid', ''); + $appsecret = $request->post('appsecret', ''); + + $appid = "32432452345324"; + $appsecret = "a2b1Yubmej8qFLZbijcEenj9CoKWgratNdIpWha8LZ64xVfSt1YM5"; + //$client = db('Client')->where('appid', $appid)->where('appsecret', $appsecret)->value('id'); + $client = 1; + if ($client) { + $this->data['time'] = time(); + $tokens = $appid . '|' . $appsecret . '|' . $this->data['time']; + $this->data['token'] = authcode($tokens, 'ENCODE'); + $this->data['code'] = 1; + return $this->data; + }else{ + $data['msg'] = '未知信息,请联系管理员!'; + return $this->data; + } + } + + public function getOauthUrl(\think\Request $request){ + $oauth = &load_wechat('Oauth'); + $url = $request->param('url'); + $uri = $oauth->getOauthRedirect('http://wx.tensent.cn' . $url); + $this->data['code'] = 1; + $this->data['data'] = $uri; + return $this->data; + } +} \ No newline at end of file diff --git a/application/api/controller/User.php b/application/api/controller/User.php new file mode 100644 index 00000000..8ba93bb9 --- /dev/null +++ b/application/api/controller/User.php @@ -0,0 +1,23 @@ + +// +---------------------------------------------------------------------- + +namespace app\Api\controller; +use app\common\controller\Api; + +class User extends Api { + + public function login(){ + //$this->data['code'] = 1; + return $this->data; + } + + public function getuser(){ + + } +} \ No newline at end of file diff --git a/application/common/controller/Api.php b/application/common/controller/Api.php index 400a8562..1e3066c3 100644 --- a/application/common/controller/Api.php +++ b/application/common/controller/Api.php @@ -16,15 +16,31 @@ class Api { public function __construct() { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); - header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept , token"); + header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"); + $header = getallheaders(); $this->data = array('code' => 0, 'msg' => '', 'time' => time(), 'data' => ''); - if (!$this->checkToken()) { + $isCheck = $this->checkToken($header); + $url = request()->module() . '/' . request()->controller() . '/' . request()->action(); + if (!$isCheck && 'api/index/gettoken' !== strtolower($url)) { $this->data['code'] = '301'; $this->data['data'] = '非法请求!'; + echo json($this->data); + exit(); } } - protected function checkToken(){ - return true; + protected function checkToken($header){return true; + if (isset($header['Authorization']) && $header['Authorization']) { + $token = authcode($header['Authorization']); + list($appid, $appsecret, $currentTime) = explode('|', $token); + $client = db('Client')->where('appid', $appid)->where('appsecret', $appsecret)->value('id'); + if ($client && ($currentTime+86400) < time()) { + return true; + }else{ + return false; + } + }else{ + return false; + } } } \ No newline at end of file diff --git a/application/common/model/Client.php b/application/common/model/Client.php new file mode 100644 index 00000000..bb7b84fe --- /dev/null +++ b/application/common/model/Client.php @@ -0,0 +1,20 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +/** + * Client模型 + */ +class Client extends Base{ + protected $auto = array('update_time'); + protected $insert = array('create_time'); + + +} \ No newline at end of file diff --git a/application/common/validate/Client.php b/application/common/validate/Client.php new file mode 100644 index 00000000..ccdcd840 --- /dev/null +++ b/application/common/validate/Client.php @@ -0,0 +1,36 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\validate; + +/** +* 设置模型 +*/ +class Client extends Base{ + + protected $rule = array( + 'appid' => 'require|number|unique:client', + 'appsecret' => 'require|alphaNum', + 'title' => 'require' + ); + + protected $message = array( + 'appid.require' => 'appid必须', + 'appid.unique' => 'appid已经存在', + 'appid.number' => 'appid只能为数字', + 'appsecret.require' => 'appsecret必须', + 'appsecret.alphaNum' => 'appsecret只能为数字和字母', + 'title' => '客户端名称必须', + ); + + protected $scene = array( + 'add' => array('appid', 'appsecret', 'title'), + 'edit' => array('appid', 'appsecret', 'title') + ); +} \ No newline at end of file diff --git a/data/sql.sql b/data/sql.sql index 6906603e..0480115f 100644 --- a/data/sql.sql +++ b/data/sql.sql @@ -425,7 +425,7 @@ CREATE TABLE `sent_config` ( `status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '状态', `value` text COMMENT '配置值', `sort` smallint(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序' -) ENGINE=MyISAM DEFAULT CHARSET=utf8; +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '配置详情表'; -- -- 转存表中的数据 `sent_config` @@ -470,6 +470,22 @@ INSERT INTO `sent_config` (`id`, `name`, `type`, `title`, `group`, `extra`, `rem -- -------------------------------------------------------- +-- +-- 表的结构 `sent_client` +-- + +DROP TABLE IF EXISTS `sent_client`; +CREATE TABLE `sent_client` ( + `id` int(11) NOT NULL, + `title` varchar(100) NOT NULL COMMENT '客户端名称', + `appid` varchar(32) NOT NULL COMMENT 'appid', + `appsecret` varchar(32) NOT NULL COMMENT 'appsecret', + `create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间', + `update_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间' +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT '客户端信息表'; + +-- -------------------------------------------------------- + -- -- 表的结构 `sent_district` -- @@ -1253,7 +1269,9 @@ INSERT INTO `sent_menu` (`id`, `title`, `type`, `icon`, `pid`, `sort`, `url`, `h (24, '钩子列表', 'admin', 'code', 6, 0, 'admin/addons/hooks', 0, '', '插件管理', 0, 0), (25, '自定义表单', 'admin', 'object-group', 5, 0, 'admin/form/index', 0, '', '运营管理', 0, 0), (26, '伪静态规则', 'admin', 'magnet', 2, 0, 'admin/seo/rewrite', 0, '', '优化设置', 0, 0), -(27, '主题管理', 'admin', 'heartbeat', 2, 0, 'admin/config/themes', 0, '', '系统配置', 0, 0); +(27, '主题管理', 'admin', 'heartbeat', 2, 0, 'admin/config/themes', 0, '', '系统配置', 0, 0), +(28, '接口', 'admin', 'database', 0, 66, 'admin/client/index', 0, '', '', 0, 0), +(29, '客户端列表', 'admin', 'inbox', 28, 0, 'admin/client/index', 0, '', '客户端管理', 0, 0); -- -------------------------------------------------------- @@ -1456,6 +1474,13 @@ ALTER TABLE `sent_config` ADD PRIMARY KEY (`id`), ADD KEY `type` (`type`), ADD KEY `group` (`group`); + +-- +-- Indexes for table `sent_client` +-- +ALTER TABLE `sent_client` + ADD PRIMARY KEY (`id`), + ADD UNIQUE KEY `id` (`id`); -- -- Indexes for table `sent_district` @@ -1610,6 +1635,11 @@ ALTER TABLE `sent_channel` ALTER TABLE `sent_config` MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '配置ID', AUTO_INCREMENT=44; -- +-- 使用表AUTO_INCREMENT `sent_client` +-- +ALTER TABLE `sent_client` + MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; +-- -- 使用表AUTO_INCREMENT `sent_district` -- ALTER TABLE `sent_district`