作者 liyuhang

gx

<?php
use Illuminate\Support\Facades\Log;
define('HTTP_OPENAI_URL','http://openai.waimaoq.com');
//ai自动生成文本
function send_openai_msg($url , $command , $param){
$url = HTTP_OPENAI_URL.$url;
$data = [
'messages'=>[
['role'=>$command['key'],'content'=>$command['scene']],
['role'=>$param['key'],'content'=>$param['scene']],
]
];
return http_post($url,json_encode($data));
}
if(!function_exists('http_post')){
/**
* 发送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);
}
}
if(!function_exists('http_get')){
/**
* 发送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);
}
}
if(!function_exists('_get_child')){
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = _get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
}
<?php
define('HTTP_OPENAI_URL','http://openai.waimaoq.com/');
/**
* 生成路由标识
* @param $string
... ... @@ -10,3 +10,81 @@
function generateRoute($string){
return trim(strtolower(preg_replace( '/[\W]+/', '-', trim($string))), '-');
}
if(!function_exists('http_post')){
/**
* 发送http post请求
* @param type $url
* @param type $post_data
*/
function http_post($url, $post_data)
{
$header = array(
"Accept: application/json",
"Content-Type:application/json;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);
}
}
if(!function_exists('http_get')){
/**
* 发送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);
}
}
if(!function_exists('_get_child')){
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = _get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
}
... ...
<?php
namespace App\Http\Controllers\Bside;
namespace App\Http\Controllers\Aside;
use App\Enums\Common\Code;
use App\Http\Controllers\Aside\BaseController;
use App\Models\AiCommand as AiCommandModel;
use function App\Helper\send_openai_msg;
class AiCommandController extends BaseController
{
... ... @@ -12,12 +15,9 @@ class AiCommandController extends BaseController
* @author :liyuhang
* @method
*/
public function lists(){
$url = 'v2/openai_chat';
$command = ['key'=>'user','scene'=>'system'];
$param = ['key'=>'user','scene'=>'请问你是谁?'];
$data = send_openai_msg($url,$command,$param);
$this->response('success',Code::SUCCESS,$data);
public function lists(AiCommandModel $aiCommandModel){
$lists = $aiCommandModel->lists($this->map,);
$this->response('success',Code::SUCCESS,$lists);
}
/**
... ...
... ... @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class BaseController extends Controller
... ... @@ -14,7 +15,6 @@ class BaseController extends Controller
protected $param = [];//所有请求参数
protected $token = ''; //token
protected $request = [];//助手函数
protected $allCount = 0;//总条数
protected $page = 1;//当前页
protected $row = 20;//每页条数
protected $header = [];//设置请求头参数
... ... @@ -22,7 +22,6 @@ class BaseController extends Controller
protected $map = [];//处理后的参数
protected $uid = 0;
protected $user = [];//当前登录用户详情
/**
* 获取所有参数
*/
... ... @@ -30,24 +29,24 @@ class BaseController extends Controller
{
$this->request = $request;
$this->param = $this->request->all();
$this->token = $this->request->header('token');
if(!empty($this->token) && !empty(Cache::get($this->token))){
$info = Cache::get($this->token);
$this->user = $info;
$this->uid = $info['id'];
}else{
return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
}
$this->get_param();
}
/**
* @return mixed
* @author zbj
* @date 2023/4/19
*/
public function manage(){
return Session::get('manage');
}
/**
* 成功返回
* @param array $data
* @param string $code
* @param bool $objectData
* @return JsonResponse
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
{
... ... @@ -60,6 +59,7 @@ class BaseController extends Controller
'data' => $data,
'msg' => $code->description,
];
$this->header['token'] = $this->token;
return response()->json($response,200,$this->header);
}
/**
... ... @@ -116,11 +116,53 @@ class BaseController extends Controller
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $data,
'data' => $this->_extents($data),
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
$response = response($result,$result_code,$this->header);;
throw new HttpResponseException($response);
}
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
public function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = $this->_get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
protected function _extents($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
switch ((string) $k) {
case 'image':
$v['image_link'] = file_get_contents($v);
break;
}
}
}
return $data;
}
}
... ...
... ... @@ -4,12 +4,11 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Utils\EncryptUtils;
use App\Models\AiCommand as AiCommandModel;
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
{
... ... @@ -166,4 +165,29 @@ class BaseController extends Controller
}
return $data;
}
/**
* @name :ai自动生成
* @return mixed
* @author :liyuhang
* @method
*/
public function send_openai_msg($url , $key){
$url = HTTP_OPENAI_URL.$url;
$aiCommandModel = New AiCommandModel();
//指定库获取指令
$info = $aiCommandModel->read(['key'=>$key]);
if($info === false){
$this->response('error',Code::USER_ERROR);
}
//替换关键字
$content = str_replace('$keyword$', $this->param['keywords'], $info['ai']);
$data = [
'messages'=>[
['role'=>'system','content'=>$info['scene']],
['role'=>'assistant','content'=>$content],
]
];
return http_post($url,json_encode($data));
}
}
... ...
... ... @@ -112,4 +112,43 @@ class BlogController extends BaseController
$blogLogic->blog_del();
$this->response('success');
}
/**
* @name :ai生成名称
* @return void
* @author :liyuhang
* @method
*/
public function ai_blog_name(Request $request){
# id, key, scene, ai
$request->validate([
'keywords'=>['required'],
],[
'keywords.required' => '关键字不能为空',
]);
$key = 'blog_title';
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = $this->send_openai_msg($url,$key);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @name :ai生成内容
* @return void
* @author :liyuhang
* @method
*/
public function ai_blog_content(Request $request){
$request->validate([
'keywords'=>['required'],
],[
'keywords.required' => '关键字不能为空',
]);
$key = 'blog_content';
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = $this->send_openai_msg($url,$key);
$this->response('success',Code::SUCCESS,$data);
}
}
... ...
... ... @@ -5,12 +5,9 @@ namespace App\Http\Controllers\Bside\News;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsCategoryLogic;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Requests\Bside\News\NewsCategoryRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class NewsCategoryController extends BaseController
{
... ... @@ -103,4 +100,5 @@ class NewsCategoryController extends BaseController
$newsCategoryLogic->del_news_category();
$this->response('success');
}
}
... ...
... ... @@ -7,7 +7,6 @@ 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;
use Illuminate\Http\Request;
/**
... ... @@ -115,4 +114,43 @@ class NewsController extends BaseController
//TODO::清空相关资源/写入日志
$this->response('success');
}
/**
* @name :ai生成名称
* @return void
* @author :liyuhang
* @method
*/
public function ai_news_name(Request $request){
# id, key, scene, ai
$request->validate([
'keywords'=>['required'],
],[
'keywords.required' => '关键字不能为空',
]);
$key = 'new_title';
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = $this->send_openai_msg($url,$key);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @name :ai生成内容
* @return void
* @author :liyuhang
* @method
*/
public function ai_news_content(Request $request){
$request->validate([
'keywords'=>['required'],
],[
'keywords.required' => '关键字不能为空',
]);
$key = 'new_content';
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = $this->send_openai_msg($url,$key);
$this->response('success',Code::SUCCESS,$data);
}
}
... ...
<?php
namespace App\Http\Logic\Bside;
namespace App\Http\Logic\Aside;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\AiCommand as AiCommandModel;
class AiCommandLogic extends BaseLogic
{
... ... @@ -8,6 +11,7 @@ class AiCommandLogic extends BaseLogic
{
parent::__construct();
$this->model = new Department();
$this->model = new AiCommandModel();
}
}
... ...
... ... @@ -85,7 +85,12 @@ Route::middleware(['bloginauth'])->group(function () {
});
//ai指令
Route::prefix('command')->group(function () {
Route::any('/', [\App\Http\Controllers\Bside\AiCommandController::class, 'lists'])->name('command_lists');
//新闻自动生成
Route::any('/ai_news_name', [\App\Http\Controllers\Bside\News\NewsController::class, 'ai_news_name'])->name('ai_news_name');
Route::any('/ai_news_content', [\App\Http\Controllers\Bside\News\NewsController::class, 'ai_news_content'])->name('ai_news_content');
//博客自动生成
Route::any('/ai_blog_name', [\App\Http\Controllers\Bside\Blog\BlogController::class, 'ai_blog_name'])->name('ai_blog_name');
Route::any('/ai_blog_content', [\App\Http\Controllers\Bside\Blog\BlogController::class, 'ai_blog_content'])->name('ai_blog_content');
});
//产品
... ...