为接口开发做的基础
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')
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user