正在显示
8 个修改的文件
包含
209 行增加
和
35 行删除
app/Helper/common.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +use Illuminate\Support\Facades\Log; | ||
| 4 | + | ||
| 5 | +function set_openai(){ | ||
| 6 | +// { | ||
| 7 | +// "messages": [ | ||
| 8 | +// { | ||
| 9 | +// "role": "system", | ||
| 10 | +// "content": "You are a helpful assistant." | ||
| 11 | +// }, | ||
| 12 | +// { | ||
| 13 | +// "role": "user", | ||
| 14 | +// "content": "宁波大学地址?" | ||
| 15 | +// }, | ||
| 16 | +// { | ||
| 17 | +// "role": "assistant", | ||
| 18 | +// "content": "宁波大学的地址是浙江省宁波市江北区风华路818号。" | ||
| 19 | +// }, | ||
| 20 | +// { | ||
| 21 | +// "role": "user", | ||
| 22 | +// "content": "占地面积是多少?" | ||
| 23 | +// } | ||
| 24 | +// ] | ||
| 25 | +//} | ||
| 26 | +} | ||
| 27 | +/** | ||
| 28 | + * 发送http post请求 | ||
| 29 | + * @param type $url | ||
| 30 | + * @param type $post_data | ||
| 31 | + */ | ||
| 32 | +function http_post($url, $post_data) | ||
| 33 | +{ | ||
| 34 | + $header[] = "charset = UTF-8"; | ||
| 35 | + $ch = curl_init(); | ||
| 36 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 37 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | ||
| 38 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
| 39 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | ||
| 40 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | ||
| 41 | + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); | ||
| 42 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | ||
| 43 | + curl_setopt($ch, CURLOPT_AUTOREFERER, 1); | ||
| 44 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | ||
| 45 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 46 | + $res = curl_exec($ch); | ||
| 47 | + if (curl_errno($ch)) { | ||
| 48 | + Log::write(print_r(curl_errno($ch),1),'debug---1'); | ||
| 49 | + } | ||
| 50 | + curl_close($ch); | ||
| 51 | + return json_decode($res, true); | ||
| 52 | +} | ||
| 53 | +/** | ||
| 54 | + * 发送http get请求 | ||
| 55 | + * @param type $url | ||
| 56 | + * @return type | ||
| 57 | + */ | ||
| 58 | +function http_get($url) | ||
| 59 | +{ | ||
| 60 | + $header[] = "content-type: application/x-www-form-urlencoded; | ||
| 61 | + charset = UTF-8"; | ||
| 62 | + $ch1 = curl_init(); | ||
| 63 | + $timeout = 5; | ||
| 64 | + curl_setopt($ch1, CURLOPT_URL, $url); | ||
| 65 | + curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); | ||
| 66 | + curl_setopt($ch1, CURLOPT_HTTPHEADER, $header); | ||
| 67 | + curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
| 68 | + curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false); | ||
| 69 | + curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false); | ||
| 70 | + $access_txt = curl_exec($ch1); | ||
| 71 | + curl_close($ch1); | ||
| 72 | + return json_decode($access_txt, true); | ||
| 73 | +} |
| @@ -9,6 +9,7 @@ use Illuminate\Http\JsonResponse; | @@ -9,6 +9,7 @@ use Illuminate\Http\JsonResponse; | ||
| 9 | use Illuminate\Http\Request; | 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\Http\Exceptions\HttpResponseException; | 10 | use Illuminate\Http\Exceptions\HttpResponseException; |
| 11 | use Illuminate\Support\Facades\Cache; | 11 | use Illuminate\Support\Facades\Cache; |
| 12 | +use Illuminate\Support\Facades\Log; | ||
| 12 | 13 | ||
| 13 | class BaseController extends Controller | 14 | class BaseController extends Controller |
| 14 | { | 15 | { |
| @@ -165,4 +166,5 @@ class BaseController extends Controller | @@ -165,4 +166,5 @@ class BaseController extends Controller | ||
| 165 | } | 166 | } |
| 166 | return $url.$filename; | 167 | return $url.$filename; |
| 167 | } | 168 | } |
| 169 | + | ||
| 168 | } | 170 | } |
| @@ -4,9 +4,10 @@ namespace App\Http\Controllers\Bside\Blog; | @@ -4,9 +4,10 @@ namespace App\Http\Controllers\Bside\Blog; | ||
| 4 | 4 | ||
| 5 | use App\Enums\Common\Code; | 5 | use App\Enums\Common\Code; |
| 6 | use App\Http\Controllers\Bside\BaseController; | 6 | use App\Http\Controllers\Bside\BaseController; |
| 7 | +use App\Http\Logic\Bside\Blog\BlogLogic; | ||
| 7 | use App\Http\Requests\Bside\Blog\BlogRequest; | 8 | use App\Http\Requests\Bside\Blog\BlogRequest; |
| 8 | use App\Models\Blog\Blog as BlogModel; | 9 | use App\Models\Blog\Blog as BlogModel; |
| 9 | -use App\Models\Blog\BlogCategory as BlogCategoryModel; | 10 | +use App\Models\RouteMap; |
| 10 | use Illuminate\Http\Request; | 11 | use Illuminate\Http\Request; |
| 11 | 12 | ||
| 12 | class BlogController extends BaseController | 13 | class BlogController extends BaseController |
| @@ -29,16 +30,13 @@ class BlogController extends BaseController | @@ -29,16 +30,13 @@ class BlogController extends BaseController | ||
| 29 | * @author :liyuhang | 30 | * @author :liyuhang |
| 30 | * @method | 31 | * @method |
| 31 | */ | 32 | */ |
| 32 | - public function info(Request $request,BlogModel $blogModel){ | 33 | + public function info(Request $request,BlogLogic $blogLogic){ |
| 33 | $request->validate([ | 34 | $request->validate([ |
| 34 | 'id'=>['required'] | 35 | 'id'=>['required'] |
| 35 | ],[ | 36 | ],[ |
| 36 | 'id.required' => 'ID不能为空' | 37 | 'id.required' => 'ID不能为空' |
| 37 | ]); | 38 | ]); |
| 38 | - $info = $blogModel->read($this->param); | ||
| 39 | - if($info === false){ | ||
| 40 | - $this->response('error',Code::USER_ERROR); | ||
| 41 | - } | 39 | + $info = $blogLogic->blog_info(); |
| 42 | $this->response('success',Code::SUCCESS,$info); | 40 | $this->response('success',Code::SUCCESS,$info); |
| 43 | } | 41 | } |
| 44 | /** | 42 | /** |
| @@ -47,17 +45,9 @@ class BlogController extends BaseController | @@ -47,17 +45,9 @@ class BlogController extends BaseController | ||
| 47 | * @author :liyuhang | 45 | * @author :liyuhang |
| 48 | * @method | 46 | * @method |
| 49 | */ | 47 | */ |
| 50 | - public function add(BlogRequest $request,BlogModel $blogModel){ | 48 | + public function add(BlogRequest $request,BlogLogic $blogLogic){ |
| 51 | $request->validated(); | 49 | $request->validated(); |
| 52 | - $this->param['create_id'] = $this->uid; | ||
| 53 | - $this->param['operator_id'] = $this->uid; | ||
| 54 | - $this->param['project_id'] = $this->user['project_id']; | ||
| 55 | - //TODO::路由映射 | ||
| 56 | - $rs = $blogModel->add($this->param); | ||
| 57 | - if($rs === false){ | ||
| 58 | - $this->response('error',Code::USER_ERROR); | ||
| 59 | - } | ||
| 60 | - //TODO::写入日志 | 50 | + $blogLogic->blog_add(); |
| 61 | $this->response('success'); | 51 | $this->response('success'); |
| 62 | } | 52 | } |
| 63 | 53 |
| @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside\News; | @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside\News; | ||
| 4 | 4 | ||
| 5 | use App\Enums\Common\Code; | 5 | use App\Enums\Common\Code; |
| 6 | use App\Http\Controllers\Bside\BaseController; | 6 | use App\Http\Controllers\Bside\BaseController; |
| 7 | +use App\Http\Logic\Bside\News\NewsLogic; | ||
| 7 | use App\Http\Requests\Bside\News\NewsRequest; | 8 | use App\Http\Requests\Bside\News\NewsRequest; |
| 8 | use App\Models\News\News as NewsModel; | 9 | use App\Models\News\News as NewsModel; |
| 9 | use App\Models\News\NewsCategory as NewsCategoryModel; | 10 | use App\Models\News\NewsCategory as NewsCategoryModel; |
| @@ -55,18 +56,15 @@ class NewsController extends BaseController | @@ -55,18 +56,15 @@ class NewsController extends BaseController | ||
| 55 | * @author :liyuhang | 56 | * @author :liyuhang |
| 56 | * @method | 57 | * @method |
| 57 | */ | 58 | */ |
| 58 | - public function info(Request $request,NewsModel $news){ | 59 | + public function info(Request $request,NewsLogic $newsLogic){ |
| 59 | $request->validate([ | 60 | $request->validate([ |
| 60 | 'id'=>['required'], | 61 | 'id'=>['required'], |
| 61 | ],[ | 62 | ],[ |
| 62 | 'id.required' => 'ID不能为空', | 63 | 'id.required' => 'ID不能为空', |
| 63 | ]); | 64 | ]); |
| 64 | - $rs = $news->read($this->param); | ||
| 65 | - if($rs === false){ | ||
| 66 | - $this->response('error',Code::USER_ERROR); | ||
| 67 | - } | 65 | + $info = $newsLogic->news_info(); |
| 68 | //TODO::清空相关资源 | 66 | //TODO::清空相关资源 |
| 69 | - $this->response('success'); | 67 | + $this->response('success',Code::SUCCESS,$info); |
| 70 | } | 68 | } |
| 71 | /** | 69 | /** |
| 72 | * @name :添加新闻 | 70 | * @name :添加新闻 |
| @@ -74,18 +72,9 @@ class NewsController extends BaseController | @@ -74,18 +72,9 @@ class NewsController extends BaseController | ||
| 74 | * @author :liyuhang | 72 | * @author :liyuhang |
| 75 | * @method | 73 | * @method |
| 76 | */ | 74 | */ |
| 77 | - public function add(NewsRequest $newsRequest,NewsModel $news){ | 75 | + public function add(NewsRequest $newsRequest,NewsLogic $newsLogic){ |
| 78 | $newsRequest->validated(); | 76 | $newsRequest->validated(); |
| 79 | - $this->param['create_id'] = $this->uid; | ||
| 80 | - $this->param['operator_id'] = $this->uid; | ||
| 81 | - $this->param['project_id'] = $this->user['project_id']; | ||
| 82 | - //多个分类按逗号隔开 | ||
| 83 | - $this->param['category_id'] = ','.$this->param['category_id'].','; | ||
| 84 | - //TODO::路由映射 | ||
| 85 | - $rs = $news->add($this->param); | ||
| 86 | - if($rs === false){ | ||
| 87 | - $this->response('error',Code::USER_ERROR); | ||
| 88 | - } | 77 | + $newsLogic->news_add(); |
| 89 | //TODO::写入日志 | 78 | //TODO::写入日志 |
| 90 | $this->response('success'); | 79 | $this->response('success'); |
| 91 | } | 80 | } |
| @@ -2,9 +2,66 @@ | @@ -2,9 +2,66 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Http\Logic\Bside\Blog; | 3 | namespace App\Http\Logic\Bside\Blog; |
| 4 | 4 | ||
| 5 | +use App\Enums\Common\Code; | ||
| 5 | use App\Http\Logic\Bside\BaseLogic; | 6 | use App\Http\Logic\Bside\BaseLogic; |
| 7 | +use App\Models\Blog\Blog; | ||
| 8 | +use App\Models\Blog\BlogCategory as BlogCategoryModel; | ||
| 9 | +use App\Models\RouteMap; | ||
| 10 | +use Illuminate\Support\Facades\DB; | ||
| 6 | 11 | ||
| 7 | class BlogLogic extends BaseLogic | 12 | class BlogLogic extends BaseLogic |
| 8 | { | 13 | { |
| 14 | + public function __construct() | ||
| 15 | + { | ||
| 16 | + parent::__construct(); | ||
| 9 | 17 | ||
| 18 | + $this->model = new Blog(); | ||
| 19 | + $this->param = $this->requestAll; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * @name :添加博客 | ||
| 24 | + * @return void | ||
| 25 | + * @author :liyuhang | ||
| 26 | + * @method | ||
| 27 | + */ | ||
| 28 | + public function blog_add(){ | ||
| 29 | + $this->param['create_id'] = $this->uid; | ||
| 30 | + $this->param['operator_id'] = $this->uid; | ||
| 31 | + $this->param['project_id'] = $this->user['project_id']; | ||
| 32 | + DB::beginTransaction(); | ||
| 33 | + try { | ||
| 34 | + $rs = $this->model->insertGetId($this->param); | ||
| 35 | + RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']); | ||
| 36 | + DB::commit(); | ||
| 37 | + }catch (\Exception $e){ | ||
| 38 | + DB::rollBack(); | ||
| 39 | + $this->fail('error',Code::USER_ERROR); | ||
| 40 | + } | ||
| 41 | + //TODO::写入日志 | ||
| 42 | + $this->success(); | ||
| 43 | + } | ||
| 44 | + /** | ||
| 45 | + * @name :获取数据详情 | ||
| 46 | + * @return array | ||
| 47 | + * @throws \App\Exceptions\BsideGlobalException | ||
| 48 | + * @author :liyuhang | ||
| 49 | + * @method | ||
| 50 | + */ | ||
| 51 | + public function blog_info(){ | ||
| 52 | + $info = $this->model->read($this->param); | ||
| 53 | + //获取分类名称 | ||
| 54 | + $info['category_id'] = explode(',',trim($info['category_id'],',')); | ||
| 55 | + $blogCategoryModel = new BlogCategoryModel(); | ||
| 56 | + $category_list = $blogCategoryModel->list(['id'=>['in',$info['category_id']]],'id',['name']); | ||
| 57 | + $str = ''; | ||
| 58 | + foreach ($category_list as $v){ | ||
| 59 | + $str .= $v['name'].','; | ||
| 60 | + } | ||
| 61 | + $info['category_id'] = trim($str,','); | ||
| 62 | + if($info === false){ | ||
| 63 | + $this->fail('error',Code::USER_ERROR); | ||
| 64 | + } | ||
| 65 | + return $this->success($info); | ||
| 66 | + } | ||
| 10 | } | 67 | } |
| @@ -2,9 +2,69 @@ | @@ -2,9 +2,69 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Http\Logic\Bside\News; | 3 | namespace App\Http\Logic\Bside\News; |
| 4 | 4 | ||
| 5 | +use App\Enums\Common\Code; | ||
| 5 | use App\Http\Logic\Bside\BaseLogic; | 6 | use App\Http\Logic\Bside\BaseLogic; |
| 7 | +use App\Models\News\News; | ||
| 8 | +use App\Models\News\NewsCategory as NewsCategoryModel; | ||
| 9 | +use App\Models\RouteMap; | ||
| 10 | +use Illuminate\Support\Facades\DB; | ||
| 6 | 11 | ||
| 7 | class NewsLogic extends BaseLogic | 12 | class NewsLogic extends BaseLogic |
| 8 | { | 13 | { |
| 14 | + public function __construct() | ||
| 15 | + { | ||
| 16 | + parent::__construct(); | ||
| 9 | 17 | ||
| 18 | + $this->model = new News(); | ||
| 19 | + $this->param = $this->requestAll; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * @name :添加博客 | ||
| 24 | + * @return void | ||
| 25 | + * @author :liyuhang | ||
| 26 | + * @method | ||
| 27 | + */ | ||
| 28 | + public function news_add(){ | ||
| 29 | + $this->param['create_id'] = $this->uid; | ||
| 30 | + $this->param['operator_id'] = $this->uid; | ||
| 31 | + $this->param['project_id'] = $this->user['project_id']; | ||
| 32 | + DB::beginTransaction(); | ||
| 33 | + try { | ||
| 34 | + $rs = $this->model->insertGetId($this->param); | ||
| 35 | + RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']); | ||
| 36 | + DB::commit(); | ||
| 37 | + }catch (\Exception $e){ | ||
| 38 | + DB::rollBack(); | ||
| 39 | + $this->fail('error',Code::USER_ERROR); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + //TODO::写入日志 | ||
| 43 | + $this->success(); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * @name :获取详情 | ||
| 48 | + * @return array | ||
| 49 | + * @throws \App\Exceptions\BsideGlobalException | ||
| 50 | + * @author :liyuhang | ||
| 51 | + * @method | ||
| 52 | + */ | ||
| 53 | + public function news_info(){ | ||
| 54 | + $this->param = $this->requestAll; | ||
| 55 | + $info = $this->model->read($this->param); | ||
| 56 | + //获取分类名称 | ||
| 57 | + $info['category_id'] = explode(',',trim($info['category_id'],',')); | ||
| 58 | + $newsCategoryModel = new NewsCategoryModel(); | ||
| 59 | + $category_list = $newsCategoryModel->list(['id'=>['in',$info['category_id']]],'id',['name']); | ||
| 60 | + $str = ''; | ||
| 61 | + foreach ($category_list as $v){ | ||
| 62 | + $str .= $v['name'].','; | ||
| 63 | + } | ||
| 64 | + $info['category_id'] = trim($str,','); | ||
| 65 | + if($info === false){ | ||
| 66 | + $this->fail('error',Code::USER_ERROR); | ||
| 67 | + } | ||
| 68 | + return $this->success($info); | ||
| 69 | + } | ||
| 10 | } | 70 | } |
| @@ -22,7 +22,9 @@ class RouteMap extends Model | @@ -22,7 +22,9 @@ class RouteMap extends Model | ||
| 22 | const SOURCE_PRODUCT_CATE = 'product_category'; | 22 | const SOURCE_PRODUCT_CATE = 'product_category'; |
| 23 | const SOURCE_PRODUCT_KEYWORD = 'product_keyword'; | 23 | const SOURCE_PRODUCT_KEYWORD = 'product_keyword'; |
| 24 | 24 | ||
| 25 | - | 25 | + //路由类型 |
| 26 | + const SOURCE_BLOG = 'blog'; | ||
| 27 | + const SOURCE_NEWS = 'news'; | ||
| 26 | /** | 28 | /** |
| 27 | * 生成路由标识 | 29 | * 生成路由标识 |
| 28 | * @param $title | 30 | * @param $title |
| @@ -28,7 +28,8 @@ | @@ -28,7 +28,8 @@ | ||
| 28 | "Database\\Seeders\\": "database/seeders/" | 28 | "Database\\Seeders\\": "database/seeders/" |
| 29 | }, | 29 | }, |
| 30 | "files": [ | 30 | "files": [ |
| 31 | - "app/Helper/helper.php" | 31 | + "app/Helper/helper.php", |
| 32 | + "app/Helper/common.php" | ||
| 32 | ] | 33 | ] |
| 33 | }, | 34 | }, |
| 34 | "autoload-dev": { | 35 | "autoload-dev": { |
-
请 注册 或 登录 后发表评论