为接口开发做的基础
This commit is contained in:
76
application/admin/controller/Client.php
Normal file
76
application/admin/controller/Client.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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){
|
||||
|
||||
}
|
||||
}
|
||||
44
application/admin/view/client/add.html
Normal file
44
application/admin/view/client/add.html
Normal file
@@ -0,0 +1,44 @@
|
||||
{extend name="public/base"/}
|
||||
{block name="body"}
|
||||
<div class="main-box clearfix">
|
||||
<header class="main-box-header clearfix">
|
||||
<div class="pull-left">
|
||||
<h2>{$meta_title}</h2>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
<form method="post" class="form form-horizontal">
|
||||
<div class="form-group">
|
||||
<label class="col-md-2 control-label">客户端名称</label>
|
||||
<div class="col-md-10">
|
||||
<input type="text" class="form-control" style="width:400px;" name="title" value="{$info['title']|default=''}">
|
||||
<span class="help-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-md-2 control-label">APPID</label>
|
||||
<div class="col-md-10">
|
||||
<input type="text" class="form-control" style="width:400px;" name="appid" value="{$info['appid']|default=''}">
|
||||
<span class="help-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-md-2 control-label">APPSECRET</label>
|
||||
<div class="col-md-10">
|
||||
<input type="text" class="form-control" style="width:400px;" name="appsecret" value="{$info['appsecret']|default=''}">
|
||||
<span class="help-block"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-2 col-md-10">
|
||||
<input type="hidden" name="id" value="{$info['id']|default=''}">
|
||||
<button class="btn btn-success submit-btn ajax-post" type="submit" target-form="form-horizontal">确 定</button>
|
||||
<button class="btn btn-danger btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
48
application/admin/view/client/index.html
Normal file
48
application/admin/view/client/index.html
Normal file
@@ -0,0 +1,48 @@
|
||||
{extend name="public/base"/}
|
||||
{block name="body"}
|
||||
<div class="main-box clearfix">
|
||||
<header class="main-box-header clearfix">
|
||||
<div class="pull-left">
|
||||
<h2>{$meta_title}</h2>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
<a class="btn btn-primary" href="{:url('add')}">新 增</a>
|
||||
<button class="btn btn-danger ajax-post confirm" url="{:url('del')}" target-form="ids">删 除</button>
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
<div class="table-responsive clearfix">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>名称</th>
|
||||
<th>APPID</th>
|
||||
<th>APPSECRET</th>
|
||||
<th>创建时间</th>
|
||||
<th>更新时间</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{volist name="list" id="item"}
|
||||
<tr>
|
||||
<td>{$item['id']}</td>
|
||||
<td>{$item['title']}</td>
|
||||
<td>{$item['appid']}</td>
|
||||
<td>{$item['appsecret']}</td>
|
||||
<td>{$item['create_time']}</td>
|
||||
<td>{$item['update_time']}</td>
|
||||
<td>
|
||||
<a href="{:url('client/edit?id='.$item['id'])}">编辑</a>
|
||||
<a href="{:url('client/del?id='.$item['id'])}" class="ajax-post confirm">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
</tbody>
|
||||
</table>
|
||||
{$page}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
@@ -7,7 +7,41 @@
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\admin\controller;
|
||||
namespace app\Api\controller;
|
||||
use app\common\controller\Api;
|
||||
|
||||
class Index extends Api {}
|
||||
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;
|
||||
}
|
||||
}
|
||||
23
application/api/controller/User.php
Normal file
23
application/api/controller/User.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
application/common/model/Client.php
Normal file
20
application/common/model/Client.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | OneThink [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
/**
|
||||
* Client模型
|
||||
*/
|
||||
class Client extends Base{
|
||||
protected $auto = array('update_time');
|
||||
protected $insert = array('create_time');
|
||||
|
||||
|
||||
}
|
||||
36
application/common/validate/Client.php
Normal file
36
application/common/validate/Client.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
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')
|
||||
);
|
||||
}
|
||||
34
data/sql.sql
34
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`
|
||||
|
||||
Reference in New Issue
Block a user