Commit d9bbdcf9 by 庄欣

测试

parent f9ad0752
<?php
namespace App\Http\City\Controller;
use App\Http\Controllers\ControllerBase;
class City extends ControllerBase
{
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class ControllerBase
{
public function getAuth()
{
return 12;
return request("uuid",12);
static $id;
if (!$id) {
$code = request()->header("token");
......@@ -58,8 +58,8 @@ class ControllerBase
$post = array_merge($request->all(),['uid' => $this->getAuth()]);
$client->setData($post);
$client->setUrl($this->prefix);
$client->curl();
return Response::ok();
$model_id = $client->curl();
return Response::ok($model_id);
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
......@@ -75,8 +75,8 @@ class ControllerBase
$client->setMethod("put");
$post = array_merge($request->all(),['uid' => $this->getAuth()]);
$client->setData($post);
$client->curl();
return Response::ok();
$model_id = $client->curl();
return Response::ok($model_id);
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
......@@ -106,6 +106,34 @@ class ControllerBase
}
}
public function destory($id)
{
try {
$client = app("client");
$this->isAllowed($id);
$client->put($id, ['is_del' => 1]);
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
public function resume($id)
{
try {
$client = app("client");
$this->isAllowed($id);
$client->put($id, ['is_del' => 0]);
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
public function prop($id,$key,$value)
{
$client = app("client");
......
......@@ -74,6 +74,7 @@ class File extends Controller
'url' => Config("app.upyun.addr") . $path,
'height' => $res['x-upyun-height'],
'width' => $res['x-upyun-width'],
'file_name' => $file['tmp_name']
];
}
......
......@@ -8,9 +8,8 @@ use App\Http\Controllers\ControllerBase;
use App\Lib\Response\HttpStatus;
use App\Lib\Response\Response;
use App\Exceptions\ApiUnauthorized;
use App\Exceptions\ApiValidationFailed;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class User extends ControllerBase
{
......@@ -49,6 +48,11 @@ class User extends ControllerBase
$client->setUrl("user/show/" . $code);
$client->setMethod("get");
$res = $client->curl();
if (is_array($res)) {
$res['avatar'] = $this->ImgEncode($res['avatar']);
} elseif (is_object($res)) {
$res->avatar = $this->ImgEncode($res->avatar);
}
return Response::success($res);
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
......@@ -67,7 +71,7 @@ class User extends ControllerBase
imagepng($resource);
$stream = base64_encode(ob_get_contents());
ob_end_clean();
return $stream;
return "data:imge/png;base64,".$stream;
}
/**
......@@ -119,4 +123,53 @@ class User extends ControllerBase
}
}
/**
* 模糊搜索用户
*/
public function search()
{
$query = request()->getQueryString();
try {
$client = app("client");
$client->setUrl("user/search?".$query);
$client->setMethod("get");
$res = $client->curl();
return Response::success($res);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
protected function ImgEncode($url)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,"HEAD");
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$info = curl_getinfo($curl);
if ($info['http_code'] >= 400) {
$url = "http://yuepai.b0.upaiyun.com/default/default_avatar.png";
}
$token = base64_encode($url);
if (!Cache::has($token));
{
$image_info = getimagesize($url);
$image_data = chunk_split(
base64_encode(
file_get_contents($url, false,
stream_context_create([
'http' => [
'method' => "GET",
'timeout' => 10
]
])
)
)
);
Cache::put($token, "data:{$image_info['mime']};base64," . $image_data , 60*24);
}
return Cache::get($token);
}
}
\ No newline at end of file
<?php
namespace App\Http\Controllers\Photo\Controller;
use App\Exceptions\ApiNotFound;
use App\Http\Controllers\ControllerBase;
use Illuminate\Http\Request;
use App\Lib\Response\Response;
class Photo extends ControllerBase
{
protected $prefix = "photo";
/**
* 添加照片
* @method post
* route /photo/{id}
*/
public function images($id,Request $request)
{
try {
$client = app("client");
$client->post($this->prefix."/".$id,$request->all());
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
/**
* 删除照片
* @method delete
* @route /photo/id/ids;
*/
public function image($id,$ids)
{
try {
parent::isAllowed($id);
$client = app("client");
$client->delete($this->prefix."/$id/$ids");
return Response::ok();
} catch (ApiNotFound $e) {
return Response::error($e->getMessage());
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
public function show($id,$query = "",$checkUid = true)
{
try {
$client = app("client");
$res = $client->get($this->prefix."/".$id);
$this->isAllowed($id,$res);
return Response::success($res);
} catch (ApiNotFound $e) {
return Response::error($e->getMessage());
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
/**
* 新建
*/
public function store(Request $request)
{
try {
$client = app("client");
$client->post($this->prefix,array_merge($request->all(),['pg_id' => $this->getAuth()]));
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
/**
* 列表
*/
public function lists($id)
{
try {
$this->isAllowed($id);
$client = app("client");
$client->setMethod("get");
$client->setUrl($this->prefix."/$id/list");
$res = $client->curl();
return Response::success($res);
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
protected function isAllowed($id, $data = [])
{
$uid = $this->getAuth();
$result = false;
if (!empty($data)) {
$result = ($data->pg_id == $uid);
} else {
$client = app("client");
$client->setUrl($this->prefix."/".$id);
$client->setMethod("get");
$res = $client->curl();
$result = ($res->pg_id == $uid);
}
if ($result == false) {
throw new ApiNotFound("未找到");
}
}
}
\ No newline at end of file
......@@ -14,6 +14,30 @@ class Works extends ControllerBase
protected $prefix = "works";
/**
* test index ok
* test del
*
*
*
*/
public function store(Request $request,$sets = null)
{
if (!$request->has("cover")) {
if ($request->has("details")) {
$details = $request->get("details");
if (count($details) > 0) {
$cover = array_pop($details)["works_url"];
$request->merge(['cover' => $cover]);
}
}
}
if ($sets == 1) {
$request->merge(['FROM_SETS' => 1]);
}
return parent::store($request);
}
public function hidden($id)
{
......@@ -21,6 +45,8 @@ class Works extends ControllerBase
$this->isAllowed($id);
$this->prop($id,"is_hidden",1);
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
......@@ -32,6 +58,27 @@ class Works extends ControllerBase
$this->isAllowed($id);
$this->prop($id,"is_hidden",0);
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
}
/**
* 设置封面
* @param $id
* @param $cover_id
*/
public function setCover($id,$url)
{
try {
$this->isAllowed($id);
$client = app("client");
$client->put("/works/$id",['cover' => $url]);
return Response::ok();
} catch (ApiUnauthorized $e) {
return Response::error($e->getMessage(), HttpStatus::HttpUnauthorized);
} catch (\Exception $e) {
return Response::error($e->getMessage());
}
......
......@@ -4,6 +4,7 @@ namespace App\Lib\Client;
use App\Lib\Response\HttpStatus;
class CurlClient
{
protected $error;
......@@ -26,7 +27,7 @@ class CurlClient
public function __construct($header = [])
{
$this->base = "http://api.yuepai.com/";
$this->base = config("app.apiurl");
}
public function setUrl($url)
......@@ -63,6 +64,18 @@ class CurlClient
$this->header = array($this->header,$header);
}
public function __call($name, $arguments)
{
foreach ($arguments as $arg) {
if (is_string($arg)) {
$this->url = $arg;
}elseif(is_array($arg)) {
$this->data = $arg;
}
}
$this->setMethod($name);
return $this->curl();
}
public function curl()
{
......@@ -81,8 +94,27 @@ class CurlClient
}
curl_setopt($http,CURLOPT_HTTPHEADER,$this->header);
curl_setopt($http,CURLOPT_POSTFIELDS,$this->data);
$res = curl_exec($http);
$curlinfo = curl_getinfo($http);
if ($this->method === "GET") {
$NOW_TIME = time();
static $response_from_api;
$sign = $this->getSign();
$curlinfo = true;
if (!isset($response_from_api[$sign])) {
$response_from_api[$sign] = curl_exec($http);
//对于重复获取,缓存1S
$response_from_api["timeout"] = $NOW_TIME + 1;
$curlinfo = curl_getinfo($http);
} else {
if ($response_from_api["timeout"] > $NOW_TIME) {
$response_from_api[$sign] = curl_exec($http);
$response_from_api["timeout"] = $NOW_TIME + 1;
}
}
$res = $response_from_api[$sign];
} else {
$res = curl_exec($http);
$curlinfo = curl_getinfo($http);
}
if ($curlinfo == false || (is_array($curlinfo) && $curlinfo['http_code'] != 200)) {
var_dump($res);
throw new \Exception("返回错误");
......@@ -92,11 +124,7 @@ class CurlClient
throw new \Exception(json_last_error_msg());
}
if ($res->code >= 200 && $res->code < 400) {
if ($this->method == "POST" || $this->method == "PUT") {
return "";
} else {
return $res->data;
}
return $res->data;
} else {
$exception = self::Exceptions[$res->code];
$msg = is_array($res->message)?implode(";",$res->message):$res->message;
......@@ -109,4 +137,9 @@ class CurlClient
{
return $this->error;
}
protected function getSign()
{
return sha1("md5",$this->method.$this->url.implode(".",$this->header));
}
}
\ No newline at end of file
......@@ -22,10 +22,11 @@ class Response
}
//method put,post,head return
public static function ok ($code = HttpStatus::HttpOk)
public static function ok ($data = "",$code = HttpStatus::HttpOk)
{
return [
'code' => $code,
'data' => $data,
'api_ver' => RouteServiceProvider::getVersion()
];
}
......
......@@ -235,6 +235,7 @@ return [
'timeout' => 5,
'password' => env('UPYUN_PASSWORD',""),
'username' => env('UPYUN_USERNAME',""),
]
],
'apiurl' => env('APP_URL',"http://127.0.0.1")
];
......@@ -30,6 +30,8 @@ Route::group(["prefix" => "user", 'namespace' => Provider::getNamespace("member"
Route::post("/apply","User@Apply");
//获取用户微信openid
Route::get("/openid/{platform?}","User@getOAuth");
//模糊搜索用户 支持使用mobile=?或nickname=? 方式,?表示字符串
Route::get("/search","User@search");
});
......@@ -48,23 +50,33 @@ Route::group(["prefix" => "article", 'namespace' => Provider::getNamespace("arti
Route::put ("/{id}/hidden" , "Article@hidden")->where(['id' => '\d+']);
//显示
Route::put ("/{id}/show" , "Article@display")->where(['id' => '\d+']);
//禁用
Route::delete("/{id}" ,"Article@destory");
//解禁
Route::put("/{id}/resume" ,"Article@resume");
});
Route::group(["prefix" => "works", 'namespace' => Provider::getNamespace("works")],function(){
//攻略列表
//攻略列表 test ok
Route::get ("/" , "Works@index");
//改
Route::put ("/{id}" , "Works@update")->where(['id' => '\d+']);
//新增
Route::post("/" , "Works@store");
//新增 如果是从套系中新建,sets填1
Route::post("/{sets?}" , "Works@store");
//详情
Route::get ("/{id}" , "Works@show")->where(['id' => '\d+']);
//关系
Route::get ("/{id}/relation/{relation}","Works@getRelate")->where(['id' => '\d+','relation'=>'\S+']);
//隐藏
//隐藏 test ok
Route::put ("/{id}/hidden" , "Works@hidden")->where(['id' => '\d+']);
//显示
Route::put ("/{id}/show" , "Works@display")->where(['id' => '\d+']);
//禁用
Route::delete("/{id}" ,"Works@destory");
//解禁
Route::put("/{id}/resume" ,"Works@resume");
//设置封面
Route::put("/{id}/{url}" , "Works@setCover");
});
Route::group(["prefix" => "keywords", 'namespace' => Provider::getNamespace("keywords")],function(){
......@@ -72,4 +84,35 @@ Route::group(["prefix" => "keywords", 'namespace' => Provider::getNamespace("key
Route::get ("/" , "Keywords@index");
//热门关键词
Route::get ("/hot" , "Keywords@gethot");
});
Route::group(["prefix" => "photo", 'namespace' => Provider::getNamespace("photo")],function(){
//全部选片
Route::get ("/" , "Photo@index");
//新建
Route::post("/" , "Photo@store");
//添加照片
Route::post("/{id}" , "Photo@images");
//删除照片
Route::delete("/{id}/{ids}","Photo@image");
//照片列表
Route::get ("/{id}/list" , "Photo@lists");
//详情
Route::get ("/{id}" , "Photo@show");
});
Route::group(["prefix" => "sets", 'namespace' => Provider::getNamespace("sets")],function(){
//列表
Route::get ("/" , "Sets@index");
//新建
Route::post("/" , "Sets@store");
//改
Route::put ("/{id}" , "Sets@update")->where(['id' => '\d+']);
//详情
Route::get ("/{id}" , "Sets@show");
//禁用
Route::delete("/{id}" ,"Sets@destory");
//解禁
Route::put("/{id}/resume" ,"Sets@resume");
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment