作者 liyuhang

gx

<?php
use Illuminate\Support\Facades\Log;
function set_openai(){
// {
// "messages": [
// {
// "role": "system",
// "content": "You are a helpful assistant."
// },
// {
// "role": "user",
// "content": "宁波大学地址?"
// },
// {
// "role": "assistant",
// "content": "宁波大学的地址是浙江省宁波市江北区风华路818号。"
// },
// {
// "role": "user",
// "content": "占地面积是多少?"
// }
// ]
//}
}
/**
* 发送http post请求
* @param type $url
* @param type $post_data
*/
function http_post($url, $post_data)
{
$header[] = "charset = UTF-8";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_errno($ch)) {
Log::write(print_r(curl_errno($ch),1),'debug---1');
}
curl_close($ch);
return json_decode($res, true);
}
/**
* 发送http get请求
* @param type $url
* @return type
*/
function http_get($url)
{
$header[] = "content-type: application/x-www-form-urlencoded;
charset = UTF-8";
$ch1 = curl_init();
$timeout = 5;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
$access_txt = curl_exec($ch1);
curl_close($ch1);
return json_decode($access_txt, true);
}
... ...
... ... @@ -9,6 +9,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class BaseController extends Controller
{
... ... @@ -165,4 +166,5 @@ class BaseController extends Controller
}
return $url.$filename;
}
}
... ...
... ... @@ -4,9 +4,10 @@ namespace App\Http\Controllers\Bside\Blog;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Blog\BlogLogic;
use App\Http\Requests\Bside\Blog\BlogRequest;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\RouteMap;
use Illuminate\Http\Request;
class BlogController extends BaseController
... ... @@ -29,16 +30,13 @@ class BlogController extends BaseController
* @author :liyuhang
* @method
*/
public function info(Request $request,BlogModel $blogModel){
public function info(Request $request,BlogLogic $blogLogic){
$request->validate([
'id'=>['required']
],[
'id.required' => 'ID不能为空'
]);
$info = $blogModel->read($this->param);
if($info === false){
$this->response('error',Code::USER_ERROR);
}
$info = $blogLogic->blog_info();
$this->response('success',Code::SUCCESS,$info);
}
/**
... ... @@ -47,17 +45,9 @@ class BlogController extends BaseController
* @author :liyuhang
* @method
*/
public function add(BlogRequest $request,BlogModel $blogModel){
public function add(BlogRequest $request,BlogLogic $blogLogic){
$request->validated();
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['project_id'] = $this->user['project_id'];
//TODO::路由映射
$rs = $blogModel->add($this->param);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
//TODO::写入日志
$blogLogic->blog_add();
$this->response('success');
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside\News;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Requests\Bside\News\NewsRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
... ... @@ -55,18 +56,15 @@ class NewsController extends BaseController
* @author :liyuhang
* @method
*/
public function info(Request $request,NewsModel $news){
public function info(Request $request,NewsLogic $newsLogic){
$request->validate([
'id'=>['required'],
],[
'id.required' => 'ID不能为空',
]);
$rs = $news->read($this->param);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
$info = $newsLogic->news_info();
//TODO::清空相关资源
$this->response('success');
$this->response('success',Code::SUCCESS,$info);
}
/**
* @name :添加新闻
... ... @@ -74,18 +72,9 @@ class NewsController extends BaseController
* @author :liyuhang
* @method
*/
public function add(NewsRequest $newsRequest,NewsModel $news){
public function add(NewsRequest $newsRequest,NewsLogic $newsLogic){
$newsRequest->validated();
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['project_id'] = $this->user['project_id'];
//多个分类按逗号隔开
$this->param['category_id'] = ','.$this->param['category_id'].',';
//TODO::路由映射
$rs = $news->add($this->param);
if($rs === false){
$this->response('error',Code::USER_ERROR);
}
$newsLogic->news_add();
//TODO::写入日志
$this->response('success');
}
... ...
... ... @@ -2,9 +2,66 @@
namespace App\Http\Logic\Bside\Blog;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
class BlogLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new Blog();
$this->param = $this->requestAll;
}
/**
* @name :添加博客
* @return void
* @author :liyuhang
* @method
*/
public function blog_add(){
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['project_id'] = $this->user['project_id'];
DB::beginTransaction();
try {
$rs = $this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error',Code::USER_ERROR);
}
//TODO::写入日志
$this->success();
}
/**
* @name :获取数据详情
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function blog_info(){
$info = $this->model->read($this->param);
//获取分类名称
$info['category_id'] = explode(',',trim($info['category_id'],','));
$blogCategoryModel = new BlogCategoryModel();
$category_list = $blogCategoryModel->list(['id'=>['in',$info['category_id']]],'id',['name']);
$str = '';
foreach ($category_list as $v){
$str .= $v['name'].',';
}
$info['category_id'] = trim($str,',');
if($info === false){
$this->fail('error',Code::USER_ERROR);
}
return $this->success($info);
}
}
... ...
... ... @@ -2,9 +2,69 @@
namespace App\Http\Logic\Bside\News;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
class NewsLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new News();
$this->param = $this->requestAll;
}
/**
* @name :添加博客
* @return void
* @author :liyuhang
* @method
*/
public function news_add(){
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['project_id'] = $this->user['project_id'];
DB::beginTransaction();
try {
$rs = $this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error',Code::USER_ERROR);
}
//TODO::写入日志
$this->success();
}
/**
* @name :获取详情
* @return array
* @throws \App\Exceptions\BsideGlobalException
* @author :liyuhang
* @method
*/
public function news_info(){
$this->param = $this->requestAll;
$info = $this->model->read($this->param);
//获取分类名称
$info['category_id'] = explode(',',trim($info['category_id'],','));
$newsCategoryModel = new NewsCategoryModel();
$category_list = $newsCategoryModel->list(['id'=>['in',$info['category_id']]],'id',['name']);
$str = '';
foreach ($category_list as $v){
$str .= $v['name'].',';
}
$info['category_id'] = trim($str,',');
if($info === false){
$this->fail('error',Code::USER_ERROR);
}
return $this->success($info);
}
}
... ...
... ... @@ -22,7 +22,9 @@ class RouteMap extends Model
const SOURCE_PRODUCT_CATE = 'product_category';
const SOURCE_PRODUCT_KEYWORD = 'product_keyword';
//路由类型
const SOURCE_BLOG = 'blog';
const SOURCE_NEWS = 'news';
/**
* 生成路由标识
* @param $title
... ...
... ... @@ -28,7 +28,8 @@
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helper/helper.php"
"app/Helper/helper.php",
"app/Helper/common.php"
]
},
"autoload-dev": {
... ...