first commit
This commit is contained in:
131
pgserver/application/api/controller/Base.php
Normal file
131
pgserver/application/api/controller/Base.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\util\ReturnCode;
|
||||
use think\Controller;
|
||||
use think\facade\Response;
|
||||
|
||||
/**
|
||||
* 接口基础控制器
|
||||
*/
|
||||
class Base extends Controller {
|
||||
|
||||
public function _initialize() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回封装后的API数据到客户端
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param mixed $msg 提示信息
|
||||
* @param integer $code 返回的code
|
||||
* @return \think\Response
|
||||
*/
|
||||
protected function buildSuccess($data = [], $msg = '操作成功', $code = 1) {
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
$response = Response::create($result, 'json');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回封装后的API数据到客户端
|
||||
* @access protected
|
||||
* @param mixed $msg 提示信息
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param integer $code 返回的code
|
||||
* @return \think\Response
|
||||
*/
|
||||
protected function buildFailed($msg = '', $data = [], $code = -1) {
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
$response = Response::create($result, 'json');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口调用时参数检查appkey、sign、timestamp
|
||||
*/
|
||||
public function checkParam() {
|
||||
$param = $this->request->post();
|
||||
|
||||
$appkey = $param['appkey'];
|
||||
$sign = $param['sign'];
|
||||
$timestamp = $param['timestamp'];
|
||||
|
||||
//加一个验证ip
|
||||
|
||||
if (empty($appkey) || empty($sign) || empty($timestamp)) {
|
||||
return $this->output(-1, '参数错误');
|
||||
}
|
||||
if (strlen($timestamp) == 10) {
|
||||
$nowtime = date('Ymdhis', time());
|
||||
$oldtime = date('Ymdhis', $timestamp);
|
||||
if ($nowtime - $oldtime > 300) {
|
||||
return $this->output(-1, 'timestamp超时');
|
||||
}
|
||||
} else {
|
||||
return $this->output(-1, 'timestamp参数错误');
|
||||
}
|
||||
$appsecret = $this->account($appkey);
|
||||
if (empty($appsecret)) {
|
||||
return $this->output(-1, '参数错误key');
|
||||
}
|
||||
$newsign = md5($appkey . $appsecret . $timestamp);
|
||||
|
||||
if ($sign !== $newsign) {
|
||||
return $this->output(-1, '签名错误');
|
||||
}
|
||||
|
||||
return true;
|
||||
//查询sign是否已使用
|
||||
//$signCount = M("ApiLog")->where(array('sign' => $sign, 'appkey' => $appkey))->count();
|
||||
// if ($signCount > 1) {
|
||||
// $this->error('', 20022);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据appkey查询信息
|
||||
*/
|
||||
public function account($appkey) {
|
||||
$infos = config('API_USERS');
|
||||
$appsecret = FALSE;
|
||||
foreach ($infos as $k => $v) {
|
||||
if ($v['appkey'] == $appkey) {
|
||||
$appsecret = $v['appsecret'];
|
||||
}
|
||||
}
|
||||
return $appsecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一输出方法
|
||||
* @param int $code
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @return json
|
||||
*/
|
||||
public function output($code = '-1', $msg = '', $data = []) {
|
||||
$result = json([
|
||||
'code' => intval($code),
|
||||
'msg' => strval($msg),
|
||||
'data' => $data ?: (object) $data
|
||||
]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user