mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
Merge remote-tracking branch 'origin/apiadmin-v2.0' into apiadmin-v2.0
This commit is contained in:
commit
06edc42d81
108
Application/Admin/Controller/DocumentController.class.php
Normal file
108
Application/Admin/Controller/DocumentController.class.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/06/23 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Admin\Controller;
|
||||
|
||||
|
||||
class DocumentController extends BaseController {
|
||||
public function index() {
|
||||
$this->display();
|
||||
}
|
||||
|
||||
public function ajaxGetIndex() {
|
||||
$postData = I('post.');
|
||||
$start = $postData['start'] ? $postData['start'] : 0;
|
||||
$limit = $postData['length'] ? $postData['length'] : 20;
|
||||
$draw = $postData['draw'];
|
||||
$total = D('ApiDocument')->count();
|
||||
$info = D('ApiDocument')->limit($start, $limit)->select();
|
||||
$data = array(
|
||||
'draw' => $draw,
|
||||
'recordsTotal' => $total,
|
||||
'recordsFiltered' => $total,
|
||||
'data' => $info
|
||||
);
|
||||
$this->ajaxReturn($data, 'json');
|
||||
}
|
||||
|
||||
public function add() {
|
||||
if (IS_POST) {
|
||||
$data['createTime'] = NOW_TIME;
|
||||
$data['endTime'] = I('post.keep') * 3600 + NOW_TIME;
|
||||
$data['key'] = I('post.key');
|
||||
D('ApiDocument')->add($data);
|
||||
$this->ajaxSuccess('添加成功');
|
||||
} else {
|
||||
$key = md5(microtime());
|
||||
$this->assign('key', $key);
|
||||
$this->display();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function open() {
|
||||
$key = I('post.key');
|
||||
$res = D('ApiDocument')->where(array('key' => $key))->save(array('status' => 1));
|
||||
if ($res === false) {
|
||||
$this->ajaxError('操作失败');
|
||||
} else {
|
||||
S($key, null);
|
||||
$this->ajaxSuccess('操作成功');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function close() {
|
||||
$key = I('post.key');
|
||||
$res = D('ApiDocument')->where(array('key' => $key))->save(array('status' => 0));
|
||||
if ($res === false) {
|
||||
$this->ajaxError('操作失败');
|
||||
} else {
|
||||
S($key, null);
|
||||
$this->ajaxSuccess('操作成功');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function del() {
|
||||
$key = I('post.key');
|
||||
$res = D('ApiDocument')->where(array('key' => $key))->delete();
|
||||
if ($res === false) {
|
||||
$this->ajaxError('操作失败');
|
||||
} else {
|
||||
S($key, null);
|
||||
$this->ajaxSuccess('操作成功');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Key延时
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function addTime() {
|
||||
if (IS_POST) {
|
||||
$addTime = I('post.keep') * 3600;
|
||||
$key = I('post.key');
|
||||
S($key, null);
|
||||
D('ApiDocument')->where(array('key' => $key))->save(array('endTime' => array('exp', 'endTime+' . $addTime)));
|
||||
$this->ajaxSuccess('修改成功');
|
||||
} else {
|
||||
$key = I('get.key');
|
||||
$this->assign('key', $key);
|
||||
$this->display();
|
||||
}
|
||||
}
|
||||
}
|
13
Application/Admin/Model/ApiDocumentModel.class.php
Normal file
13
Application/Admin/Model/ApiDocumentModel.class.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/06/26 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Admin\Model;
|
||||
|
||||
|
||||
class ApiDocumentModel extends BaseModel {
|
||||
|
||||
}
|
57
Application/Admin/View/Document/add.html
Normal file
57
Application/Admin/View/Document/add.html
Normal file
@ -0,0 +1,57 @@
|
||||
add.html<extend name="Public/base" />
|
||||
<block name="main">
|
||||
<fieldset class="layui-elem-field">
|
||||
<legend>文档秘钥管理 - 新增秘钥</legend>
|
||||
<div class="layui-field-box">
|
||||
<form class="layui-form" action="">
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">访问秘钥</label>
|
||||
<div class="layui-input-inline" style="width: 280px">
|
||||
<input name="key" value="{$key}" readonly class="layui-input">
|
||||
</div>
|
||||
<div class="layui-form-mid layui-word-aux">系统自动生成,不允许修改</div>
|
||||
</div>
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">有效时长</label>
|
||||
<div class="layui-input-inline" style="width: 280px">
|
||||
<input type="text" name="keep" value="" placeholder="请输入有效时长" class="layui-input">
|
||||
</div>
|
||||
<div class="layui-form-mid layui-word-aux">单位:小时</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-block">
|
||||
<button class="layui-btn" lay-submit lay-filter="admin-form">立即提交</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</fieldset>
|
||||
</block>
|
||||
<block name="myScript">
|
||||
<script>
|
||||
layui.use('form', function(){
|
||||
var form = layui.form();
|
||||
form.on('submit(admin-form)', function(data){
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: '{:U("add")}',
|
||||
data: data.field,
|
||||
success: function(msg){
|
||||
if( msg.code == 1 ){
|
||||
parent.location.reload();
|
||||
}else{
|
||||
parent.layer.msg(msg.msg, {
|
||||
icon: 5,
|
||||
shade: [0.6, '#393D49'],
|
||||
time:1500
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
</block>
|
57
Application/Admin/View/Document/addTime.html
Normal file
57
Application/Admin/View/Document/addTime.html
Normal file
@ -0,0 +1,57 @@
|
||||
add.html<extend name="Public/base" />
|
||||
<block name="main">
|
||||
<fieldset class="layui-elem-field">
|
||||
<legend>文档秘钥管理 - 延时秘钥</legend>
|
||||
<div class="layui-field-box">
|
||||
<form class="layui-form" action="">
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">访问秘钥</label>
|
||||
<div class="layui-input-inline" style="width: 280px">
|
||||
<input name="key" value="{$key}" readonly class="layui-input">
|
||||
</div>
|
||||
<div class="layui-form-mid layui-word-aux">系统自动生成,不允许修改</div>
|
||||
</div>
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">延长时间</label>
|
||||
<div class="layui-input-inline" style="width: 280px">
|
||||
<input type="text" name="keep" value="" placeholder="请输入有效时长" class="layui-input">
|
||||
</div>
|
||||
<div class="layui-form-mid layui-word-aux">单位:小时</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-block">
|
||||
<button class="layui-btn" lay-submit lay-filter="admin-form">立即提交</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</fieldset>
|
||||
</block>
|
||||
<block name="myScript">
|
||||
<script>
|
||||
layui.use('form', function(){
|
||||
var form = layui.form();
|
||||
form.on('submit(admin-form)', function(data){
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: '{:U("addTime")}',
|
||||
data: data.field,
|
||||
success: function(msg){
|
||||
if( msg.code == 1 ){
|
||||
parent.location.reload();
|
||||
}else{
|
||||
parent.layer.msg(msg.msg, {
|
||||
icon: 5,
|
||||
shade: [0.6, '#393D49'],
|
||||
time:1500
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
</block>
|
176
Application/Admin/View/Document/index.html
Normal file
176
Application/Admin/View/Document/index.html
Normal file
@ -0,0 +1,176 @@
|
||||
<extend name="Public/base" />
|
||||
<block name="main">
|
||||
<script type="text/javascript" src="__PUBLIC__/dataTable/jquery.dataTables.min.js"></script>
|
||||
<link rel="stylesheet" href="__PUBLIC__/css/dataTable.css">
|
||||
<fieldset class="layui-elem-field">
|
||||
<legend>秘钥列表</legend>
|
||||
<div class="layui-field-box">
|
||||
<span class="layui-btn layui-btn-normal api-add"><i class="layui-icon"></i> 新增</span>
|
||||
<table class="layui-table" id="list-admin" lay-even>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>访问秘钥</th>
|
||||
<th>过期时间</th>
|
||||
<th>访问次数</th>
|
||||
<th>最近访问</th>
|
||||
<th>状态</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
</fieldset>
|
||||
</block>
|
||||
<block name="myScript">
|
||||
<script>
|
||||
/**
|
||||
* 格式化时间戳
|
||||
* @param fmt
|
||||
* @returns {*}
|
||||
* @constructor
|
||||
*/
|
||||
Date.prototype.Format = function (fmt) {
|
||||
var o = {
|
||||
"M+": this.getMonth() + 1, //月份
|
||||
"d+": this.getDate(), //日
|
||||
"h+": this.getHours(), //小时
|
||||
"m+": this.getMinutes(), //分
|
||||
"s+": this.getSeconds(), //秒
|
||||
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
||||
"S": this.getMilliseconds() //毫秒
|
||||
};
|
||||
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
for (var k in o)
|
||||
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
||||
return fmt;
|
||||
};
|
||||
|
||||
layui.use(['layer', 'form'], function() {
|
||||
$(document).on('click', '.confirm', function () {
|
||||
var ownObj = $(this);
|
||||
layer.confirm(ownObj.attr('data-info'), {
|
||||
btn: ['确定','取消'] //按钮
|
||||
}, function(){
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: ownObj.attr('data-url'),
|
||||
data: {key:ownObj.attr('data-id')},
|
||||
success: function(msg){
|
||||
if( msg.code == 1 ){
|
||||
location.reload();
|
||||
}else{
|
||||
layer.msg(msg.msg, {
|
||||
icon: 5,
|
||||
shade: [0.6, '#393D49'],
|
||||
time:1500
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.addTime', function () {
|
||||
var ownObj = $(this);
|
||||
layer.open({
|
||||
type: 2,
|
||||
area: ['80%', '80%'],
|
||||
maxmin: true,
|
||||
content: ownObj.attr('data-url')+'&key='+ownObj.attr('data-id')
|
||||
});
|
||||
});
|
||||
|
||||
$('.api-add').on('click', function () {
|
||||
layer.open({
|
||||
type: 2,
|
||||
area: ['80%', '80%'],
|
||||
maxmin: true,
|
||||
content: '{:U("add")}'
|
||||
});
|
||||
});
|
||||
|
||||
var myFun = function (query) {
|
||||
query = query || '';
|
||||
return $('#list-admin').DataTable({
|
||||
dom: 'rt<"bottom"ifpl><"clear">',
|
||||
ordering: false,
|
||||
autoWidth: false,
|
||||
searching:false,
|
||||
serverSide: true,
|
||||
ajax: {
|
||||
url:'{:U("ajaxGetIndex")}' + query,
|
||||
type: 'POST',
|
||||
dataSrc: function ( json ) {
|
||||
if( json.code == 0 ){
|
||||
parent.layer.msg(json.msg, {
|
||||
icon: 5,
|
||||
shade: [0.6, '#393D49'],
|
||||
time:1500
|
||||
});
|
||||
}else{
|
||||
return json.data;
|
||||
}
|
||||
}
|
||||
},
|
||||
columnDefs:[
|
||||
{
|
||||
"targets":1,
|
||||
"render": function(data){
|
||||
return new Date(data*1000).Format("yyyy-MM-dd hh:mm:ss");
|
||||
}
|
||||
},
|
||||
{
|
||||
"targets":3,
|
||||
"render": function(data){
|
||||
return new Date(data*1000).Format("yyyy-MM-dd hh:mm:ss");
|
||||
}
|
||||
},
|
||||
{
|
||||
"targets":4,
|
||||
"render": function(data){
|
||||
if(data == 1){
|
||||
return '<span style="border-radius: 2px;background-color: #5FB878;padding:5px 10px;color: #ffffff">已启用</span>';
|
||||
}else{
|
||||
return '<span style="border-radius: 2px;background-color: #FF5722;padding:5px 10px;color: #ffffff">已禁用</span>';
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"targets":5,
|
||||
"render":function(data, type, row){
|
||||
var returnStr = '';
|
||||
if(row.status == 1){
|
||||
returnStr += '<span class="layui-btn layui-btn-danger confirm" ' +
|
||||
'data-id="' + row.key +'" data-info="你确定禁用当前Key么?" data-url="{:U(\'close\')}">禁用</span>';
|
||||
}else{
|
||||
returnStr += '<span class="layui-btn layui-btn-warm confirm" ' +
|
||||
'data-id="' + row.key +'" data-info="你确定启用当前Key么?" data-url="{:U(\'open\')}">启用</span>';
|
||||
}
|
||||
returnStr += '<span class="layui-btn addTime layui-btn-normal" ' +
|
||||
'data-id="' + row.key +'" data-url="{:U(\'addTime\')}">延长时间</span>';
|
||||
returnStr += '<span class="layui-btn layui-btn-danger confirm" ' +
|
||||
'data-id="' + row.key +'" data-info="你确定删除当前菜单么?" data-url="{:U(\'del\')}">删除</span>';
|
||||
return returnStr;
|
||||
}
|
||||
}
|
||||
],
|
||||
iDisplayLength : 20,
|
||||
aLengthMenu : [20, 30, 50],
|
||||
columns: [
|
||||
{"data": "key"},
|
||||
{"data": "endTime"},
|
||||
{"data": "times" },
|
||||
{"data": "lastTime" },
|
||||
{"data": "status" },
|
||||
{"data": null }
|
||||
]
|
||||
});
|
||||
};
|
||||
var myTable = myFun();
|
||||
$('.sub').on("click", function(){
|
||||
myTable.destroy();
|
||||
myTable = myFun('&'+ $('#form-admin-add').serialize());
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</block>
|
@ -6,6 +6,11 @@
|
||||
<title>{:C('APP_NAME')}管理后台</title>
|
||||
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
|
||||
<link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css">
|
||||
<style>
|
||||
.layui-nav-child .layui-nav-item{
|
||||
padding-left: 15px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -205,7 +205,14 @@ VALUES
|
||||
(58,'删除秘钥分类',61,'ApiKey/del',0,0,1,'',0),
|
||||
(59,'启用秘钥分类',61,'ApiKey/open',0,0,1,'',0),
|
||||
(60,'禁用秘钥分类',61,'ApiKey/close',0,0,1,'',0),
|
||||
(61,'秘钥管理',48,'ApiKey/index',0,1,0,'',0);
|
||||
(61,'秘钥管理',48,'ApiKey/index',0,1,0,'',0),
|
||||
(66, '文档管理', 0, '', 0, 5, 0, '', 0),
|
||||
(67, '秘钥管理', 66, 'Document/index', 0, 0, 0, '', 0),
|
||||
(68, 'Ajax获取文档记录', 67, 'Document/ajaxGetIndex', 0, 1, 1, '', 0),
|
||||
(69, '创建访问秘钥', 67, 'Document/add', 0, 2, 1, '', 0),
|
||||
(70, '延长Key时间', 67, 'Document/addTime', 0, 3, 1, '', 0),
|
||||
(71, '启用Key', 67, 'Document/open', 0, 4, 1, '', 0),
|
||||
(72, '禁用Key', 67, 'Document/close', 0, 5, 1, '', 0);
|
||||
|
||||
/*!40000 ALTER TABLE `api_menu` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
|
78
Application/Home/ApiStore/ApiAdminSDK.class.php
Normal file
78
Application/Home/ApiStore/ApiAdminSDK.class.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/05/10 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK;
|
||||
|
||||
|
||||
use Home\ApiStore\ApiSDK\ApiAdmin\AuthSign;
|
||||
use Home\ApiStore\ApiSDK\ApiAdmin\Http;
|
||||
|
||||
class ApiAdminSDK {
|
||||
private $method; //接口名称
|
||||
private $baseUrl = 'http://tadmin.365jxj.com/api/';
|
||||
private $accessTokenHash = '5937719b95405';
|
||||
private $version = 'v2.0';
|
||||
private $appInfo = array();
|
||||
|
||||
/**
|
||||
* ApiAdminSDK constructor.
|
||||
* @param string $method 接口名称
|
||||
* @param string $appInfo 应用信息
|
||||
*/
|
||||
public function __construct($method, $appInfo) {
|
||||
$this->method = $method;
|
||||
$this->appInfo = $appInfo;
|
||||
}
|
||||
|
||||
public function updateAccessToken() {
|
||||
$cacheKey = $this->appInfo['appId'] . '_access_token';
|
||||
S($cacheKey, null);
|
||||
}
|
||||
|
||||
public function getHeader($userToken = '') {
|
||||
$signObj = new AuthSign($this->version, $this->appInfo);
|
||||
$accessToken = $this->getAccessToken();
|
||||
|
||||
return $signObj->getHeader($accessToken, $userToken);
|
||||
}
|
||||
|
||||
public function getAccessToken() {
|
||||
$cacheKey = $this->appInfo['appId'] . '_access_token';
|
||||
$accessToken = S($cacheKey);
|
||||
if (!$accessToken) {
|
||||
$signObj = new AuthSign($this->version, $this->appInfo);
|
||||
$data = $signObj->getAccessTokenData();
|
||||
$queryStr = http_build_query($data);
|
||||
$url = $this->baseUrl . $this->accessTokenHash . '?' . $queryStr;
|
||||
$header = $signObj->getHeader();
|
||||
$returnArr = Http::get($url, $header);
|
||||
if($returnArr['code'] == 1){
|
||||
$accessToken = $returnArr['data']['access_token'];
|
||||
S($cacheKey, $accessToken, 7000);
|
||||
}
|
||||
}
|
||||
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理URL,当需要GET请求,请传入GET参数数组
|
||||
* @param $data
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return string
|
||||
*/
|
||||
public function buildUrl($data = array()) {
|
||||
if ($data) {
|
||||
$queryStr = '?';
|
||||
$queryStr .= http_build_query($data);
|
||||
} else {
|
||||
$queryStr = '';
|
||||
}
|
||||
|
||||
return $this->baseUrl . $this->method . $queryStr;
|
||||
}
|
||||
}
|
62
Application/Home/ApiStore/ApiSDK/ApiAdmin/AuthSign.class.php
Normal file
62
Application/Home/ApiStore/ApiSDK/ApiAdmin/AuthSign.class.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\ApiAdmin;
|
||||
/**
|
||||
* 淘宝开放平台秘钥计算
|
||||
* @since 2017/04/20 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
class AuthSign {
|
||||
|
||||
private $version;
|
||||
private $appInfo;
|
||||
|
||||
public function __construct($version, $appInfo) {
|
||||
$this->version = $version;
|
||||
$this->appInfo = $appInfo;
|
||||
}
|
||||
|
||||
public function getHeader($accessToken = '', $userToken = false) {
|
||||
$header['version'] = $this->version;
|
||||
if ($accessToken) {
|
||||
$header['access-token'] = $accessToken;
|
||||
}
|
||||
$city = cookie('365jxj_city');
|
||||
if ($city) {
|
||||
$header['city'] = $city;
|
||||
} else {
|
||||
$header['city'] = 'nj';
|
||||
}
|
||||
if ($userToken) {
|
||||
$header['user-token'] = $userToken;
|
||||
}
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
public function getAccessTokenData() {
|
||||
$data['app_id'] = $this->appInfo['appId'];
|
||||
$data['app_secret'] = $this->appInfo['appSecret'];
|
||||
$data['device_id'] = 'zuAdmin';
|
||||
$data['rand_str'] = md5(rand(1, 10000) . microtime());
|
||||
$data['timestamp'] = time();
|
||||
$sign = $this->getSignature($data);
|
||||
$data['signature'] = $sign;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取身份秘钥
|
||||
* @param array $data
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return string
|
||||
*/
|
||||
private function getSignature($data) {
|
||||
ksort($data);
|
||||
$preStr = http_build_query($data);
|
||||
|
||||
return md5($preStr);
|
||||
}
|
||||
|
||||
}
|
77
Application/Home/ApiStore/ApiSDK/ApiAdmin/Http.class.php
Normal file
77
Application/Home/ApiStore/ApiSDK/ApiAdmin/Http.class.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* ApiAdmin通讯类
|
||||
* @since 2017/04/20 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\ApiAdmin;
|
||||
|
||||
|
||||
use Home\ORG\ReturnCode;
|
||||
|
||||
class Http {
|
||||
|
||||
public static function get($url, $header = array()) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if($header){
|
||||
$newHeader = array();
|
||||
foreach ($header as $key => $item) {
|
||||
$newHeader[] = $key.':'.$item;
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeader);
|
||||
}
|
||||
|
||||
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
E(curl_error($ch), ReturnCode::CURL_ERROR);
|
||||
}
|
||||
curl_close($ch);
|
||||
$resArr = json_decode($response, true);
|
||||
|
||||
return $resArr;
|
||||
}
|
||||
|
||||
public static function post($url, $header = array(), $body = array()) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if($header){
|
||||
$newHeader = array();
|
||||
foreach ($header as $key => $item) {
|
||||
$newHeader[] = $key.':'.$item;
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeader);
|
||||
}
|
||||
|
||||
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
E(curl_error($ch), ReturnCode::CURL_ERROR);
|
||||
}
|
||||
curl_close($ch);
|
||||
$resArr = json_decode($response, true);
|
||||
|
||||
return $resArr;
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
class Config {
|
||||
|
||||
const APP_KEY = '884fc6fb9fb466e11def006a';
|
||||
const MASTER_SECRET = '11c72c7fad4110472a688973';
|
||||
|
||||
//推送API
|
||||
const API_PUSH = 'https://api.jpush.cn/v3/push';
|
||||
//推送校验 API
|
||||
const API_VALIDATE = 'https://api.jpush.cn/v3/push/validate';
|
||||
//送达统计
|
||||
const API_RECEIVED = 'https://report.jpush.cn/v3/received';
|
||||
//消息统计
|
||||
const API_MESSAGE = 'https://report.jpush.cn/v3/messages';
|
||||
//用户统计
|
||||
const API_USERS = 'https://report.jpush.cn/v3/users';
|
||||
//查询设备的别名与标签(设置设备的别名与标签)
|
||||
const API_DEVICES = 'https://device.jpush.cn/v3/devices';
|
||||
//查询别名(删除别名)
|
||||
const API_ALIASES = 'https://device.jpush.cn/v3/aliases';
|
||||
//查询标签列表(判断设备与标签绑定关系/更新标签/删除标签)
|
||||
const API_TAG = 'https://device.jpush.cn/v3/tags';
|
||||
//获取用户在线状态
|
||||
const API_STATUS = 'https://device.jpush.cn/v3/devices/status/';
|
||||
//定时任务相关
|
||||
const API_SCHEDULES = 'https://api.jpush.cn/v3/schedules';
|
||||
|
||||
const HTTP_POST = 1;
|
||||
const HTTP_GET = 2;
|
||||
const HTTP_PUT = 3;
|
||||
const HTTP_DELETE = 4;
|
||||
|
||||
const PLATFORM_ANDROID = 'android';
|
||||
const PLATFORM_IOS = 'ios';
|
||||
|
||||
const DISABLE_SOUND = "_disable_Sound";
|
||||
const DISABLE_BADGE = 0x10000;
|
||||
const USER_AGENT = 'JPush-API-PHP-Client';
|
||||
const CONNECT_TIMEOUT = 1;
|
||||
//请求最长耗时
|
||||
const READ_TIMEOUT = 120;
|
||||
//重连次数
|
||||
const DEFAULT_MAX_RETRY_TIMES = 3;
|
||||
const DEFAULT_LOG_FILE = "./jpush.log";
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
class Device {
|
||||
|
||||
}
|
@ -1,146 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
use Home\ORG\Response;
|
||||
use Home\ORG\ReturnCode;
|
||||
|
||||
class Http {
|
||||
|
||||
public static function get($url) {
|
||||
$response = self::sendRequest($url, Config::HTTP_GET, $body = null);
|
||||
|
||||
return self::processResp($response);
|
||||
}
|
||||
|
||||
public static function post($url, $body) {
|
||||
$response = self::sendRequest($url, Config::HTTP_POST, $body);
|
||||
|
||||
return self::processResp($response);
|
||||
}
|
||||
|
||||
public static function put($url, $body) {
|
||||
$response = self::sendRequest($url, Config::HTTP_PUT, $body);
|
||||
|
||||
return self::processResp($response);
|
||||
}
|
||||
|
||||
public static function delete($url) {
|
||||
$response = self::sendRequest($url, Config::HTTP_DELETE, $body = null);
|
||||
|
||||
return self::processResp($response);
|
||||
}
|
||||
|
||||
private static function sendRequest($url, $method, $body = null, $times = 1) {
|
||||
if (!defined('CURL_HTTP_VERSION_2_0')) {
|
||||
define('CURL_HTTP_VERSION_2_0', 3);
|
||||
}
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, Config::USER_AGENT);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Config::CONNECT_TIMEOUT);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, Config::READ_TIMEOUT); // 请求最长耗时
|
||||
// 设置SSL版本 1=CURL_SSLVERSION_TLSv1, 不指定使用默认值,curl会自动获取需要使用的CURL版本
|
||||
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
// 如果报证书相关失败,可以考虑取消注释掉该行,强制指定证书版本
|
||||
//curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
|
||||
|
||||
// 设置Basic认证
|
||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
curl_setopt($ch, CURLOPT_USERPWD, Config::APP_KEY . ':' . Config::MASTER_SECRET);
|
||||
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
|
||||
|
||||
// 设置Post参数
|
||||
switch ($method) {
|
||||
case Config::HTTP_POST:
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
break;
|
||||
case Config::HTTP_DELETE:
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
||||
break;
|
||||
case Config::HTTP_PUT:
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||
break;
|
||||
}
|
||||
|
||||
if (!is_null($body)) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Connection: Keep-Alive'
|
||||
));
|
||||
|
||||
$output = curl_exec($ch);
|
||||
$response = array();
|
||||
$errorCode = curl_errno($ch);
|
||||
|
||||
$msg = '';
|
||||
if (isset($body['options']['sendno'])) {
|
||||
$sendNo = $body['options']['sendno'];
|
||||
$msg = 'sendno: ' . $sendNo;
|
||||
}
|
||||
|
||||
|
||||
if ($errorCode) {
|
||||
if ($times < Config::DEFAULT_MAX_RETRY_TIMES) {
|
||||
return self::sendRequest($url, $method, $body, ++$times);
|
||||
} else {
|
||||
if ($errorCode === 28) {
|
||||
Response::error(ReturnCode::CURL_ERROR, $msg . "Response timeout. Your request has probably be received by JPush Server,please check that whether need to be pushed again.");
|
||||
} elseif ($errorCode === 56) {
|
||||
Response::error(ReturnCode::CURL_ERROR, $msg . "Response timeout, maybe cause by old CURL version. Your request has probably be received by JPush Server, please check that whether need to be pushed again.");
|
||||
} else {
|
||||
Response::error(ReturnCode::CURL_ERROR, $msg . "Connect timeout, Please retry later. Error:" . $errorCode . " " . curl_error($ch));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$headerText = substr($output, 0, $headerSize);
|
||||
$body = substr($output, $headerSize);
|
||||
$headers = array();
|
||||
foreach (explode("\r\n", $headerText) as $i => $line) {
|
||||
if (!empty($line)) {
|
||||
if ($i === 0) {
|
||||
$headers[0] = $line;
|
||||
} else if (strpos($line, ": ")) {
|
||||
list ($key, $value) = explode(': ', $line);
|
||||
$headers[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
$response['headers'] = $headers;
|
||||
$response['body'] = $body;
|
||||
$response['httpCode'] = $httpCode;
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function processResp($response) {
|
||||
$data = json_decode($response['body'], true);
|
||||
|
||||
if (is_null($data)) {
|
||||
Response::error(ReturnCode::CURL_ERROR, '未收到返回数据');
|
||||
} elseif ($response['httpCode'] === 200) {
|
||||
$result = array();
|
||||
$result['body'] = $data;
|
||||
$result['httpCode'] = $response['httpCode'];
|
||||
$result['headers'] = $response['headers'];
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,443 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* JPush推送实现
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
use Home\ORG\Response;
|
||||
use Home\ORG\ReturnCode;
|
||||
|
||||
class Push {
|
||||
private $platform = 'all';
|
||||
private $tag = null;
|
||||
private $tagAnd = null;
|
||||
private $alias = null;
|
||||
private $registrationId = null;
|
||||
private $extras = null;
|
||||
private $notificationAlert = null;
|
||||
private $androidNotification = null;
|
||||
private $iosNotification = null;
|
||||
private $message = null;
|
||||
private $options = null;
|
||||
|
||||
/**
|
||||
* 增加推送到苹果
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function ios() {
|
||||
if (is_array($this->platform)) {
|
||||
if (!in_array(Config::PLATFORM_IOS, $this->platform)) {
|
||||
array_push($this->platform, Config::PLATFORM_IOS);
|
||||
}
|
||||
} else {
|
||||
$this->platform = array(Config::PLATFORM_IOS);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送至安卓
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function android() {
|
||||
if (is_array($this->platform)) {
|
||||
if (!in_array(Config::PLATFORM_ANDROID, $this->platform)) {
|
||||
array_push($this->platform, Config::PLATFORM_ANDROID);
|
||||
}
|
||||
} else {
|
||||
$this->platform = array(Config::PLATFORM_ANDROID);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推送tag,仅允许传入字符串和一维索引数组
|
||||
* @param string|array $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function addTag($param) {
|
||||
if (is_null($this->tag)) {
|
||||
if (is_array($this->tag)) {
|
||||
$this->tag = $param;
|
||||
} else {
|
||||
$this->tag = array($param);
|
||||
}
|
||||
} else {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $item) {
|
||||
if (!in_array($item, $this->tag)) {
|
||||
array_push($this->tag, $item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!in_array($param, $this->tag)) {
|
||||
array_push($this->tag, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推送tag_and,仅允许传入字符串和一维索引数组
|
||||
* @param string|array $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function addTagAnd($param) {
|
||||
if (is_null($this->tagAnd)) {
|
||||
if (is_array($this->tagAnd)) {
|
||||
$this->tagAnd = $param;
|
||||
} else {
|
||||
$this->tagAnd = array($param);
|
||||
}
|
||||
} else {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $item) {
|
||||
if (!in_array($item, $this->tagAnd)) {
|
||||
array_push($this->tagAnd, $item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!in_array($param, $this->tagAnd)) {
|
||||
array_push($this->tagAnd, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推送alias,仅允许传入字符串和一维索引数组
|
||||
* @param string|array $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function addAlias($param) {
|
||||
if (is_null($this->alias)) {
|
||||
if (is_array($this->alias)) {
|
||||
$this->alias = $param;
|
||||
} else {
|
||||
$this->alias = array($param);
|
||||
}
|
||||
} else {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $item) {
|
||||
if (!in_array($item, $this->alias)) {
|
||||
array_push($this->alias, $item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!in_array($param, $this->alias)) {
|
||||
array_push($this->alias, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推送registration_id,仅允许传入字符串和一维索引数组
|
||||
* @param string|array $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function addRegistrationId($param) {
|
||||
if (is_null($this->registrationId)) {
|
||||
if (is_array($this->registrationId)) {
|
||||
$this->registrationId = $param;
|
||||
} else {
|
||||
$this->registrationId = array($param);
|
||||
}
|
||||
} else {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $item) {
|
||||
if (!in_array($item, $this->registrationId)) {
|
||||
array_push($this->registrationId, $item);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!in_array($param, $this->registrationId)) {
|
||||
array_push($this->registrationId, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置公告消息
|
||||
* @param string $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function setNotificationAlert($param) {
|
||||
if (!is_string($param)) {
|
||||
Response::error(ReturnCode::EXCEPTION, 'NotificationAlert 必须是字符串');
|
||||
}
|
||||
$this->notificationAlert = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推送addExtras,新增加额外字段
|
||||
* @param array $param
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function addExtras($param) {
|
||||
if (is_null($this->extras)) {
|
||||
if (is_array($param)) {
|
||||
$this->extras = $param;
|
||||
}
|
||||
} else {
|
||||
if (is_array($param)) {
|
||||
array_merge($this->extras, $param);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置IOS的通知消息体
|
||||
* @param $alert
|
||||
* @param array $notification
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function iosNotification($alert = null, $notification = array()) {
|
||||
$ios = array();
|
||||
if (!is_null($alert)) {
|
||||
$ios['alert'] = (is_string($alert) || is_array($alert)) ? $alert : '';
|
||||
}
|
||||
if (!empty($notification)) {
|
||||
if (isset($notification['sound']) && is_string($notification['sound'])) {
|
||||
$ios['sound'] = $notification['sound'];
|
||||
}
|
||||
if (isset($notification['badge'])) {
|
||||
$ios['badge'] = (int)$notification['badge'] ? $notification['badge'] : 0;
|
||||
}
|
||||
if (isset($notification['content-available']) && is_bool($notification['content-available']) && $notification['content-available']) {
|
||||
$ios['content-available'] = $notification['content-available'];
|
||||
}
|
||||
if (isset($notification['mutable-content']) && is_bool($notification['mutable-content']) && $notification['mutable-content']) {
|
||||
$ios['mutable-content'] = $notification['mutable-content'];
|
||||
}
|
||||
if (isset($notification['category']) && is_string($notification['category'])) {
|
||||
$ios['category'] = $notification['category'];
|
||||
}
|
||||
if (isset($notification['extras']) && is_array($notification['extras']) && !empty($notification['extras'])) {
|
||||
$ios['extras'] = $notification['extras'];
|
||||
}
|
||||
}
|
||||
if (!isset($ios['sound'])) {
|
||||
$ios['sound'] = '';
|
||||
}
|
||||
if (!isset($ios['badge'])) {
|
||||
$ios['badge'] = '+1';
|
||||
}
|
||||
$this->iosNotification = $ios;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Android的通知消息体
|
||||
* @param $alert
|
||||
* @param array $notification
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function androidNotification($alert = null, array $notification = array()) {
|
||||
$android = array();
|
||||
if (!is_null($alert)) {
|
||||
$android['alert'] = is_string($alert) ? $alert : '';
|
||||
}
|
||||
if (!empty($notification)) {
|
||||
if (isset($notification['title']) && is_string($notification['title'])) {
|
||||
$android['title'] = $notification['title'];
|
||||
}
|
||||
if (isset($notification['builder_id']) && is_int($notification['builder_id'])) {
|
||||
$android['builder_id'] = $notification['builder_id'];
|
||||
}
|
||||
if (isset($notification['extras']) && is_array($notification['extras']) && !empty($notification['extras'])) {
|
||||
$android['extras'] = $notification['extras'];
|
||||
}
|
||||
if (isset($notification['priority']) && is_int($notification['priority'])) {
|
||||
$android['priority'] = $notification['priority'];
|
||||
}
|
||||
if (isset($notification['category']) && is_string($notification['category'])) {
|
||||
$android['category'] = $notification['category`'];
|
||||
}
|
||||
if (isset($notification['style']) && is_int($notification['style'])) {
|
||||
$android['style'] = $notification['style'];
|
||||
}
|
||||
if (isset($notification['big_text']) && is_string($notification['big_text'])) {
|
||||
$android['big_text'] = $notification['big_text'];
|
||||
}
|
||||
if (isset($notification['inbox']) && is_array($notification['inbox'])) {
|
||||
$android['inbox'] = $notification['inbox'];
|
||||
}
|
||||
if (isset($notification['big_pic_path']) && is_string($notification['big_pic_path'])) {
|
||||
$android['big_pic_path'] = $notification['big_pic_path'];
|
||||
}
|
||||
}
|
||||
$this->androidNotification = $android;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义消息体设置
|
||||
* @param $msgContent
|
||||
* @param array $msg
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function message($msgContent, array $msg = array()) {
|
||||
if (is_string($msgContent)) {
|
||||
$message = array();
|
||||
$message['msg_content'] = $msgContent;
|
||||
if (!empty($msg)) {
|
||||
if (isset($msg['title']) && is_string($msg['title'])) {
|
||||
$message['title'] = $msg['title'];
|
||||
}
|
||||
if (isset($msg['content_type']) && is_string($msg['content_type'])) {
|
||||
$message['content_type'] = $msg['content_type'];
|
||||
}
|
||||
if (isset($msg['extras']) && is_array($msg['extras']) && !empty($msg['extras'])) {
|
||||
$message['extras'] = $msg['extras'];
|
||||
}
|
||||
}
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 额外可选配置参数
|
||||
* @param array $opts
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return $this
|
||||
*/
|
||||
public function options(array $opts = array()) {
|
||||
$options = array();
|
||||
if (isset($opts['sendno']) && is_int($opts['sendno'])) {
|
||||
$options['sendno'] = $opts['sendno'];
|
||||
}
|
||||
if (isset($opts['time_to_live']) && is_int($opts['time_to_live']) && $opts['time_to_live'] <= 864000 && $opts['time_to_live'] >= 0) {
|
||||
$options['time_to_live'] = $opts['time_to_live'];
|
||||
}
|
||||
if (isset($opts['override_msg_id']) && is_long($opts['override_msg_id'])) {
|
||||
$options['override_msg_id'] = $opts['override_msg_id'];
|
||||
}
|
||||
if (isset($opts['apns_production']) && is_bool($opts['apns_production'])) {
|
||||
$options['apns_production'] = $opts['apns_production'];
|
||||
} else {
|
||||
$options['apns_production'] = false;
|
||||
}
|
||||
if (isset($opts['big_push_duration']) && is_int($opts['big_push_duration']) && $opts['big_push_duration'] <= 1400 && $opts['big_push_duration'] >= 0) {
|
||||
$options['big_push_duration'] = $opts['big_push_duration'];
|
||||
}
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置,整合数据
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return array
|
||||
*/
|
||||
private function buildData() {
|
||||
$payload = array();
|
||||
$payload["platform"] = $this->platform;
|
||||
|
||||
$audience = array();
|
||||
if (!is_null($this->tag)) {
|
||||
$audience["tag"] = $this->tag;
|
||||
}
|
||||
if (!is_null($this->tagAnd)) {
|
||||
$audience["tag_and"] = $this->tagAnd;
|
||||
}
|
||||
if (!is_null($this->alias)) {
|
||||
$audience["alias"] = $this->alias;
|
||||
}
|
||||
if (!is_null($this->registrationId)) {
|
||||
$audience["registration_id"] = $this->registrationId;
|
||||
}
|
||||
if (count($audience) <= 0) {
|
||||
$payload["audience"] = 'all';
|
||||
} else {
|
||||
$payload["audience"] = $audience;
|
||||
}
|
||||
|
||||
$notification = array();
|
||||
if (!is_null($this->notificationAlert)) {
|
||||
$notification['alert'] = $this->notificationAlert;
|
||||
}
|
||||
if (!is_null($this->androidNotification)) {
|
||||
$notification['android'] = $this->androidNotification;
|
||||
if (is_null($this->androidNotification['alert'])) {
|
||||
if (is_null($this->notificationAlert)) {
|
||||
Response::error(ReturnCode::EXCEPTION, 'Android alert can not be null');
|
||||
}
|
||||
} else {
|
||||
$notification['android']['alert'] = $this->androidNotification['alert'];
|
||||
}
|
||||
}
|
||||
if (!is_null($this->extras)) {
|
||||
$notification['android']['extras'] = $this->extras;
|
||||
}
|
||||
|
||||
|
||||
if (!is_null($this->iosNotification)) {
|
||||
$notification['ios'] = $this->iosNotification;
|
||||
if (is_null($this->iosNotification['alert'])) {
|
||||
if (is_null($this->notificationAlert)) {
|
||||
Response::error(ReturnCode::EXCEPTION, 'iOS alert can not be null');
|
||||
}
|
||||
} else {
|
||||
$notification['ios']['alert'] = $this->iosNotification['alert'];
|
||||
}
|
||||
}
|
||||
if (!is_null($this->extras)) {
|
||||
$notification['ios']['extras'] = $this->extras;
|
||||
}
|
||||
|
||||
if (count($notification) > 0) {
|
||||
$payload['notification'] = $notification;
|
||||
}
|
||||
if (count($this->message) > 0) {
|
||||
$payload['message'] = $this->message;
|
||||
}
|
||||
if (!array_key_exists('notification', $payload) && !array_key_exists('message', $payload)) {
|
||||
Response::error(ReturnCode::EXCEPTION, 'notification and message can not all be null');
|
||||
}
|
||||
if (!is_null($this->options)) {
|
||||
$payload['options'] = $this->options;
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
public function send() {
|
||||
return Http::post(Config::API_PUSH, $this->buildData());
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
class Report {
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\JPush;
|
||||
|
||||
|
||||
class Schedule {
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* 极光推送Manager
|
||||
* @since 2017/03/24 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK;
|
||||
|
||||
|
||||
use Home\ApiStore\ApiSDK\JPush\Device;
|
||||
use Home\ApiStore\ApiSDK\JPush\Push;
|
||||
use Home\ApiStore\ApiSDK\JPush\Report;
|
||||
use Home\ApiStore\ApiSDK\JPush\Schedule;
|
||||
|
||||
class JPushSDK {
|
||||
|
||||
public static function push() {
|
||||
return new Push();
|
||||
}
|
||||
|
||||
public static function report() {
|
||||
return new Report();
|
||||
}
|
||||
|
||||
public static function device() {
|
||||
return new Device();
|
||||
}
|
||||
|
||||
public static function schedule() {
|
||||
return new Schedule();
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
namespace Home\ApiStore\ApiSDK\TaoBao;
|
||||
/**
|
||||
* 淘宝开放平台秘钥计算
|
||||
* @since 2017/04/20 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
class AuthSign {
|
||||
|
||||
/**
|
||||
* 获取身份秘钥
|
||||
* @param array $params
|
||||
* @param array $appInfo
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
* @return string
|
||||
*/
|
||||
public static function getSign($params, $appInfo) {
|
||||
ksort($params);
|
||||
|
||||
$stringToBeSigned = $appInfo['secretKey'];
|
||||
foreach ($params as $k => $v) {
|
||||
if (is_string($v) && "@" != substr($v, 0, 1)) {
|
||||
$stringToBeSigned .= "$k$v";
|
||||
}
|
||||
}
|
||||
$stringToBeSigned .= $appInfo['secretKey'];
|
||||
|
||||
return strtoupper(md5($stringToBeSigned));
|
||||
}
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* 淘宝API通讯类
|
||||
* @since 2017/04/20 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK\TaoBao;
|
||||
|
||||
|
||||
use Home\ORG\Response;
|
||||
|
||||
class Http {
|
||||
|
||||
public static function get($url) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, "top-sdk-php");
|
||||
|
||||
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
Response::error(curl_error($ch));
|
||||
} else {
|
||||
Response::error($response);
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @since 2017/04/20 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore\ApiSDK;
|
||||
|
||||
|
||||
use Home\ApiStore\ApiSDK\TaoBao\AuthSign;
|
||||
|
||||
class TaoBaoSDK {
|
||||
|
||||
private $appInfo; //接口URL、AppID、AppSecret
|
||||
private $method; //接口名称
|
||||
private $session;
|
||||
|
||||
private $format = 'json';
|
||||
private $signMethod = 'md5';
|
||||
private $version = '2.0';
|
||||
|
||||
private $sysParams;
|
||||
private $apiParams;
|
||||
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* TaoBaoSDK constructor.
|
||||
* @param array $appInfo 应用信息
|
||||
* @param string $method 接口名称
|
||||
* @param string $session
|
||||
*/
|
||||
public function __construct($appInfo, $method, $session = null) {
|
||||
$this->appInfo = $appInfo;
|
||||
$this->method = $method;
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function buildSysParams() {
|
||||
$this->sysParams["app_key"] = $this->appInfo['appKey'];
|
||||
$this->sysParams["v"] = $this->version;
|
||||
$this->sysParams["format"] = $this->format;
|
||||
$this->sysParams["sign_method"] = $this->signMethod;
|
||||
$this->sysParams["method"] = $this->method;
|
||||
$this->sysParams["timestamp"] = date("Y-m-d H:i:s");
|
||||
|
||||
if (!is_null($this->session)) {
|
||||
$this->sysParams["session"] = $this->session;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildApiParams($params, $rule) {
|
||||
$together = array_intersect_key($params, $rule);
|
||||
$this->apiParams = array_merge($rule, $together);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildUrl() {
|
||||
$allParams = array_merge($this->sysParams, $this->apiParams);
|
||||
$allParams['sign'] = AuthSign::getSign($allParams, $this->appInfo);
|
||||
$this->url = $this->appInfo['url'].'?'.http_build_query($allParams);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @since 2017-04-20
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Home\ApiStore;
|
||||
|
||||
|
||||
use Home\ApiStore\ApiSDK\TaoBao\Http;
|
||||
use Home\ApiStore\ApiSDK\TaoBaoSDK;
|
||||
|
||||
class DayuSms {
|
||||
|
||||
private $appInfo = array(
|
||||
'secretKey' => '880ad9f6daae467f2ec2e11f50932f8f',
|
||||
'appKey' => '23762660',
|
||||
'url' => 'http://gw.api.taobao.com/router/rest'
|
||||
);
|
||||
|
||||
private $apiRule = array(
|
||||
'sms_type' => 'normal',
|
||||
'sms_free_sign_name' => '',
|
||||
'rec_num' => '',
|
||||
'sms_template_code' => ''
|
||||
);
|
||||
|
||||
public function send(){
|
||||
$sdk = new TaoBaoSDK($this->appInfo, 'alibaba.aliqin.fc.sms.num.send');
|
||||
$sdk->buildSysParams();
|
||||
$sdk->buildApiParams(array(
|
||||
'sms_free_sign_name' => 'Api管理后台',
|
||||
'rec_num' => '17366005512',
|
||||
'sms_template_code' => 'SMS_62650093'
|
||||
), $this->apiRule);
|
||||
$sdk->buildUrl();
|
||||
return Http::get($sdk->url);
|
||||
}
|
||||
}
|
@ -14,31 +14,62 @@ use Think\Controller;
|
||||
|
||||
class WikiController extends Controller {
|
||||
|
||||
public function apiList(){
|
||||
public function _initialize() {
|
||||
$uid = session('uid');
|
||||
if (!$uid) {
|
||||
$key = session('wikiKey');
|
||||
if (!$key) {
|
||||
$key = I('get.key');
|
||||
if(!$key){
|
||||
$this->error('缺少授权秘钥!', U('Index/index'));
|
||||
}
|
||||
}
|
||||
$keyInfo = S($key);
|
||||
if (!$keyInfo) {
|
||||
$keyInfo = M('ApiDocument')->where(array('key' => $key, 'status' => 1))->find();
|
||||
if (!$keyInfo) {
|
||||
$this->error('当前授权秘钥已失效!', U('Index/index'));
|
||||
} else {
|
||||
S($key, $keyInfo);
|
||||
}
|
||||
}
|
||||
if (NOW_TIME > $keyInfo['endTime']) {
|
||||
$this->error('当前授权秘钥已失效!', U('Index/index'));
|
||||
}
|
||||
session('wikiKey', $key);
|
||||
M('ApiDocument')->where(array('key' => $key))->save(array(
|
||||
'lastTime' => NOW_TIME,
|
||||
'lastIp' => get_client_ip(),
|
||||
'times' => array('exp', 'times+1')
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function apiList() {
|
||||
$listData = M('ApiList')->select();
|
||||
$this->assign('list', $listData);
|
||||
$this->display();
|
||||
}
|
||||
|
||||
public function apiField(){
|
||||
public function apiField() {
|
||||
$hash = I('get.hash');
|
||||
if( empty($hash) ){
|
||||
if (empty($hash)) {
|
||||
$this->redirect('apiList');
|
||||
}else{
|
||||
} else {
|
||||
$request = M('ApiFields')->where(array('hash' => $hash, 'type' => 0))->select();
|
||||
$response = M('ApiFields')->where(array('hash' => $hash, 'type' => 1))->select();
|
||||
$apiInfo = M('ApiList')->where(array('hash' => $hash))->find();
|
||||
$this->assign('apiInfo', $apiInfo);
|
||||
$dataType = array(
|
||||
DataType::TYPE_INTEGER => 'Integer',
|
||||
DataType::TYPE_STRING => 'String',
|
||||
DataType::TYPE_STRING => 'String',
|
||||
DataType::TYPE_BOOLEAN => 'Boolean',
|
||||
DataType::TYPE_ENUM => 'Enum',
|
||||
DataType::TYPE_FLOAT => 'Float',
|
||||
DataType::TYPE_FILE => 'File',
|
||||
DataType::TYPE_ARRAY => 'Array',
|
||||
DataType::TYPE_OBJECT => 'Object',
|
||||
DataType::TYPE_MOBILE => 'Mobile'
|
||||
DataType::TYPE_ENUM => 'Enum',
|
||||
DataType::TYPE_FLOAT => 'Float',
|
||||
DataType::TYPE_FILE => 'File',
|
||||
DataType::TYPE_ARRAY => 'Array',
|
||||
DataType::TYPE_OBJECT => 'Object',
|
||||
DataType::TYPE_MOBILE => 'Mobile'
|
||||
);
|
||||
$this->assign('dataType', $dataType);
|
||||
$this->assign('request', $request);
|
||||
@ -47,39 +78,39 @@ class WikiController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
public function errorCode(){
|
||||
public function errorCode() {
|
||||
$codeArr = ReturnCode::getConstants();
|
||||
$errorInfo = array(
|
||||
ReturnCode::SUCCESS => '请求成功',
|
||||
ReturnCode::INVALID => '非法操作',
|
||||
ReturnCode::DB_SAVE_ERROR => '数据存储失败',
|
||||
ReturnCode::DB_READ_ERROR => '数据读取失败',
|
||||
ReturnCode::CACHE_SAVE_ERROR => '缓存存储失败',
|
||||
ReturnCode::CACHE_READ_ERROR => '缓存读取失败',
|
||||
ReturnCode::FILE_SAVE_ERROR => '文件读取失败',
|
||||
ReturnCode::LOGIN_ERROR => '登录失败',
|
||||
ReturnCode::NOT_EXISTS => '不存在',
|
||||
ReturnCode::JSON_PARSE_FAIL => 'JSON数据格式错误',
|
||||
ReturnCode::TYPE_ERROR => '类型错误',
|
||||
ReturnCode::NUMBER_MATCH_ERROR => '数字匹配失败',
|
||||
ReturnCode::EMPTY_PARAMS => '丢失必要数据',
|
||||
ReturnCode::DATA_EXISTS => '数据已经存在',
|
||||
ReturnCode::AUTH_ERROR => '权限认证失败',
|
||||
ReturnCode::OTHER_LOGIN => '别的终端登录',
|
||||
ReturnCode::VERSION_INVALID => 'API版本非法',
|
||||
ReturnCode::PARAM_INVALID => '数据类型非法',
|
||||
ReturnCode::SUCCESS => '请求成功',
|
||||
ReturnCode::INVALID => '非法操作',
|
||||
ReturnCode::DB_SAVE_ERROR => '数据存储失败',
|
||||
ReturnCode::DB_READ_ERROR => '数据读取失败',
|
||||
ReturnCode::CACHE_SAVE_ERROR => '缓存存储失败',
|
||||
ReturnCode::CACHE_READ_ERROR => '缓存读取失败',
|
||||
ReturnCode::FILE_SAVE_ERROR => '文件读取失败',
|
||||
ReturnCode::LOGIN_ERROR => '登录失败',
|
||||
ReturnCode::NOT_EXISTS => '不存在',
|
||||
ReturnCode::JSON_PARSE_FAIL => 'JSON数据格式错误',
|
||||
ReturnCode::TYPE_ERROR => '类型错误',
|
||||
ReturnCode::NUMBER_MATCH_ERROR => '数字匹配失败',
|
||||
ReturnCode::EMPTY_PARAMS => '丢失必要数据',
|
||||
ReturnCode::DATA_EXISTS => '数据已经存在',
|
||||
ReturnCode::AUTH_ERROR => '权限认证失败',
|
||||
ReturnCode::OTHER_LOGIN => '别的终端登录',
|
||||
ReturnCode::VERSION_INVALID => 'API版本非法',
|
||||
ReturnCode::PARAM_INVALID => '数据类型非法',
|
||||
ReturnCode::ACCESS_TOKEN_TIMEOUT => '身份令牌过期',
|
||||
ReturnCode::SESSION_TIMEOUT => 'SESSION过期',
|
||||
ReturnCode::UNKNOWN => '未知错误',
|
||||
ReturnCode::EXCEPTION => '系统异常',
|
||||
ReturnCode::CURL_ERROR => 'CURL操作异常'
|
||||
ReturnCode::SESSION_TIMEOUT => 'SESSION过期',
|
||||
ReturnCode::UNKNOWN => '未知错误',
|
||||
ReturnCode::EXCEPTION => '系统异常',
|
||||
ReturnCode::CURL_ERROR => 'CURL操作异常'
|
||||
);
|
||||
$this->assign('errorInfo', $errorInfo);
|
||||
$this->assign('codeArr', $codeArr);
|
||||
$this->display();
|
||||
}
|
||||
|
||||
public function calculation(){
|
||||
public function calculation() {
|
||||
$this->display();
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,9 @@
|
||||
|
||||
**体验地址**
|
||||
|
||||
[http://admin.our-dream.cn/](http://admin.our-dream.cn/)
|
||||
[http://admin.apiadmin.org/](http://admin.apiadmin.org/)
|
||||
|
||||
注:线上体验项目的账号密码请移步:[http://git.oschina.net/xiaoxunzhao/apiadmin-wxpro](http://git.oschina.net/xiaoxunzhao/apiadmin-wxpro) 获取微信小程序二维码,扫描二维码获取体验账号!
|
||||
|
||||
**2.0安装指引**
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user