This commit is contained in:
wispx
2018-12-05 17:31:04 +08:00
parent 4cd50d4815
commit 4ecca1574c
7 changed files with 179 additions and 89 deletions
-85
View File
@@ -1,85 +0,0 @@
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Api extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace app\index\controller\api;
use app\common\model\Users;
use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Response;
class Base extends Controller
{
protected $format = 'json';
protected $token = null;
protected $user = null;
protected $config = null;
/**
* 构造方法
*
* @param bool $auth 是否认证
*
* @throws \think\Exception\DbException
*/
public function initialize($auth = true)
{
parent::initialize();
$this->token = $this->param('token');
$configs = \app\common\model\Config::all();
foreach ($configs as $key => &$value) {
$this->config[$value->name] = $value->value;
}
if (!$this->config['open_api']) {
$this->response('API is not open yet.', 500);
}
$format = $this->param('format');
if ($format && in_array(strtolower($format), ['json', 'jsonp', 'xml'])) {
$this->format = $format;
}
$auth && $this->auth();
}
/**
* 权限认证
*
* @param null $token
*
* @throws \think\Exception\DbException
*/
protected function auth($token = null)
{
$token = $token ? $token : $this->token;
if ($token) {
$this->user = Users::get(['token' => $token]);
} else {
return $this->response('Token does not exist.', 500);
}
if (!$this->user) {
return $this->response('Authentication failed', 500);
}
}
/**
* 返回数据给客户端并中断输出
*
* @param $msg 提示信息
* @param int $code 状态码
* @param array $data 数据
*
*/
protected function response($msg, $code = 200, $data = [])
{
$response = Response::create([
'code' => $code,
'msg' => $msg,
'data' => $data
], $this->format, $code);
throw new HttpResponseException($response);
}
/**
* 获取客户端传过来的参数
*
* @param string $name 参数名
* @param null $default 默认值
* @param string $filter 过滤方法
*
* @return mixed|string
*/
protected function param($name = '', $default = null, $filter = '')
{
$data = $this->request->param($name, $default, $filter);
if (is_array($data)) {
foreach ($data as &$value) {
if (is_string($value)) {
$value = trim($value);
}
}
}
if (is_string($data)) {
return trim($data);
}
return $data;
}
}
@@ -0,0 +1,41 @@
<?php
namespace app\index\controller\api;
use app\common\model\Users;
use think\Exception;
class Token extends Base
{
public function initialize($auth = false)
{
parent::initialize($auth);
}
/**
* @param null $email 邮箱
* @param null $password 密码
* @param bool $refresh 是否刷新token
*/
public function index($email = null, $password = null, $refresh = false)
{
try {
if (!$user = Users::get(['email' => $email])) {
throw new Exception('账号不存在');
}
if ($user->password != md5($password)) {
throw new Exception('账号密码错误');
}
if ('true' == $refresh) {
$token = make_token();
$user->token = $token;
if (!$user->save()) {
throw new Exception('Token刷新失败');
}
}
} catch (Exception $e) {
return $this->response($e->getMessage(), 500);
}
return $this->response('success', 200, ['token' => $user->token]);
}
}
@@ -0,0 +1,14 @@
<?php
/**
* User: Wisp X
* Date: 2018-12-05
* Time: 16:33
* Link: https://github.com/wisp-x
*/
namespace app\index\controller\api;
class Upload extends Base
{
}
+3 -2
View File
@@ -84,7 +84,8 @@ INSERT INTO `lsky_config` (`id`, `key`, `type`, `input_type`, `name`, `title`, `
(42, 'audit', 'bool', 'checkbox', 'open_audit', '开启图片鉴黄', '鉴黄接口申请地址:https://www.moderatecontent.com', '0', ''),
(43, 'audit', 'text', 'text', 'audit_key', 'Key', NULL, '', ''),
(44, 'audit', 'select', 'text', 'audit_index', '内容评级', '1=所有人,2=少年,3=成人', '3', '{\"1\": \"所有人\", \"2\": \"少年\", \"3\": \"成人\"}');
(44, 'audit', 'select', 'text', 'audit_index', '内容评级', '1=所有人,2=少年,3=成人', '3', '{\"1\": \"所有人\", \"2\": \"少年\", \"3\": \"成人\"}'),
(45, 'other', 'bool', 'checkbox', 'open_api', '开启API', '是否开放接口', '1', '');
-- --------------------------------------------------------
@@ -164,7 +165,7 @@ ALTER TABLE `lsky_users`
-- 使用表AUTO_INCREMENT `lsky_config`
--
ALTER TABLE `lsky_config`
MODIFY `id` smallint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45;
MODIFY `id` smallint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=46;
--
-- 使用表AUTO_INCREMENT `lsky_images`
+3 -1
View File
@@ -10,6 +10,8 @@
// +----------------------------------------------------------------------
Route::view('compatibility', 'index@tpl/compatibility');
Route::resource('api', 'index/Api');
// [RESTFul Api route]
Route::resource('api.auth', 'index/api/auth');
return [];
+2 -1
View File
@@ -9,4 +9,5 @@ UPDATE `lsky_config` SET `value` = '1.2.1' WHERE `lsky_config`.`name` = 'system_
INSERT INTO `lsky_config` (`id`, `key`, `type`, `input_type`, `name`, `title`, `tip`, `value`, `extend`) VALUES
(NULL, 'audit', 'bool', 'checkbox', 'open_audit', '开启图片鉴黄', '鉴黄接口申请地址:https://www.moderatecontent.com', '0', ''),
(NULL, 'audit', 'text', 'text', 'audit_key', 'Key', NULL, '', ''),
(NULL, 'audit', 'select', 'text', 'audit_index', '内容评级', '1=所有人,2=少年,3=成人', '3', '{\"1\": \"所有人\", \"2\": \"少年\", \"3\": \"成人\"}');
(NULL, 'audit', 'select', 'text', 'audit_index', '内容评级', '1=所有人,2=少年,3=成人', '3', '{\"1\": \"所有人\", \"2\": \"少年\", \"3\": \"成人\"}'),
(NULL, 'other', 'bool', 'checkbox', 'open_api', '开启API', '是否开放接口', '1', '');