作者 赵彬吉

update

<?php
namespace App\Helper;
use Illuminate\Support\Facades\Http;
/**
* 翻译接口
* Class Translate
* @package App\Service
* @author zbj
* @date 2023/2/7
*/
class Translate
{
//接口地址
public static $url = 'https://translate.hbb618.cn/translates';
public static $tls_list = [
'en' => [
'text' => '英语',
'lang_text' => 'English',
'con_flag' => 'con_flag/en.jfif',
'shop_lang' => 'en-gb',
],
'zh' => [
'text' => '中文',
'lang_text' => '简体中文',
'con_flag' => 'con_flag/zh.jfif',
'shop_lang' => 'zh-cn',
],
'fr' => [
'text' => '法语',
'lang_text' => 'En français',
'con_flag' => '',
],
'de' => [
'text' => '德语',
'lang_text' => 'Das ist Deutsch.',
'con_flag' => '',
],
'ko' => [
'text' => '韩语',
'lang_text' => '',
'con_flag' => '',
],
'ja' => [
'text' => '日语',
'lang_text' => '',
'con_flag' => '',
],
'es' => [
'text' => '西班牙语',
'lang_text' => 'Español.',
'con_flag' => '',
],
'ar' => [
'text' => '阿拉伯语',
'lang_text' => '',
'con_flag' => '',
],
'pt' => [
'text' => '葡萄牙语(葡萄牙、巴西)',
'lang_text' => 'Língua portuguesa',
'con_flag' => '',
],
'ru' => [
'text' => '俄语',
'lang_text' => '',
'con_flag' => '',
],
'af' => [
'text' => '南非荷兰语',
'lang_text' => '',
'con_flag' => '',
],
'sq' => [
'text' => '阿尔巴尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'am' => [
'text' => '阿姆哈拉语',
'lang_text' => '',
'con_flag' => '',
],
'hy' => [
'text' => '亚美尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'az' => [
'text' => '阿塞拜疆语',
'lang_text' => '',
'con_flag' => '',
],
'eu' => [
'text' => '巴斯克语',
'lang_text' => '',
'con_flag' => '',
],
'be' => [
'text' => '白俄罗斯语',
'lang_text' => '',
'con_flag' => '',
],
'bn' => [
'text' => '孟加拉语',
'lang_text' => '',
'con_flag' => '',
],
'bs' => [
'text' => '波斯尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'bg' => [
'text' => '保加利亚语',
'lang_text' => '',
'con_flag' => '',
],
'ca' => [
'text' => '加泰罗尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'ceb' => [
'text' => '宿务语',
'lang_text' => '',
'con_flag' => '',
],
'zh-CN' => [
'text' => '中文(简体)',
'lang_text' => '简体中文',
'con_flag' => 'con_flag/zh.jfif',
'shop_lang' => 'zh-cn',
],
'zh-TW' => [
'text' => '中文(繁体)',
'lang_text' => '繁体中文',
'con_flag' => 'con_flag/zh.jfif',
],
'co' => [
'text' => '科西嘉语',
'lang_text' => '',
'con_flag' => '',
],
'hr' => [
'text' => '克罗地亚语',
'lang_text' => '',
'con_flag' => '',
],
'cs' => [
'text' => '捷克语',
'lang_text' => '',
'con_flag' => '',
],
'da' => [
'text' => '丹麦语',
'lang_text' => '',
'con_flag' => '',
],
'nl' => [
'text' => '荷兰语',
'lang_text' => '',
'con_flag' => '',
],
'eo' => [
'text' => '世界语',
'lang_text' => '',
'con_flag' => '',
],
'et' => [
'text' => '爱沙尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'fi' => [
'text' => '芬兰语',
'lang_text' => '',
'con_flag' => '',
],
'fy' => [
'text' => '弗里斯兰语',
'lang_text' => '',
'con_flag' => '',
],
'gl' => [
'text' => '加利西亚语',
'lang_text' => '',
'con_flag' => '',
],
'ka' => [
'text' => '格鲁吉亚语',
'lang_text' => '',
'con_flag' => '',
],
'el' => [
'text' => '希腊语',
'lang_text' => '',
'con_flag' => '',
],
'gu' => [
'text' => '古吉拉特语',
'lang_text' => '',
'con_flag' => '',
],
'ht' => [
'text' => '海地克里奥尔语',
'lang_text' => '',
'con_flag' => '',
],
'ha' => [
'text' => '豪萨语',
'lang_text' => '',
'con_flag' => '',
],
'haw' => [
'text' => '夏威夷语',
'lang_text' => '',
'con_flag' => '',
],
'iw' => [
'text' => '希伯来语',
'lang_text' => '',
'con_flag' => '',
],
'hi' => [
'text' => '印地语',
'lang_text' => '',
'con_flag' => '',
],
'hmn' => [
'text' => '苗语',
'lang_text' => '',
'con_flag' => '',
],
'hu' => [
'text' => '匈牙利语',
'lang_text' => '',
'con_flag' => '',
],
'is' => [
'text' => '冰岛语',
'lang_text' => '',
'con_flag' => '',
],
'ig' => [
'text' => '伊博语',
'lang_text' => '',
'con_flag' => '',
],
'id' => [
'text' => '印度尼西亚语',
'lang_text' => 'Bahasa Indonesia',
'con_flag' => 'con_flag/id.jfif',
'shop_lang' => 'id',
],
'ga' => [
'text' => '爱尔兰语',
'lang_text' => '',
'con_flag' => '',
],
'it' => [
'text' => '意大利语',
'lang_text' => 'Lingua italiana',
'con_flag' => '',
],
'jw' => [
'text' => '爪哇语',
'lang_text' => '',
'con_flag' => '',
],
'kn' => [
'text' => '卡纳达语',
'lang_text' => '',
'con_flag' => '',
],
'kk' => [
'text' => '哈萨克语',
'lang_text' => '',
'con_flag' => '',
],
'km' => [
'text' => '高棉语',
'lang_text' => '',
'con_flag' => '',
],
'rw' => [
'text' => '卢旺达语',
'lang_text' => '',
'con_flag' => '',
],
'ku' => [
'text' => '库尔德语',
'lang_text' => '',
'con_flag' => '',
],
'ky' => [
'text' => '吉尔吉斯语',
'lang_text' => '',
'con_flag' => '',
],
'lo' => [
'text' => '老挝文',
'lang_text' => '',
'con_flag' => '',
],
'la' => [
'text' => '拉丁文',
'lang_text' => '',
'con_flag' => '',
],
'lv' => [
'text' => '拉脱维亚语',
'lang_text' => '',
'con_flag' => '',
],
'lt' => [
'text' => '立陶宛语',
'lang_text' => '',
'con_flag' => '',
],
'lb' => [
'text' => '卢森堡语',
'lang_text' => '',
'con_flag' => '',
],
'mk' => [
'text' => '马其顿语',
'lang_text' => '',
'con_flag' => '',
],
'mg' => [
'text' => '马尔加什语',
'lang_text' => '',
'con_flag' => '',
],
'ms' => [
'text' => '马来语',
'lang_text' => 'Bahasa Melayu',
'con_flag' => 'con_flag/ms.jfif',
'shop_lang' => 'ms-my',
],
'ml' => [
'text' => '马拉雅拉姆文',
'lang_text' => '',
'con_flag' => '',
],
'mt' => [
'text' => '马耳他语',
'lang_text' => '',
'con_flag' => '',
],
'mi' => [
'text' => '毛利语',
'lang_text' => '',
'con_flag' => '',
],
'mr' => [
'text' => '马拉地语',
'lang_text' => '',
'con_flag' => '',
],
'mn' => [
'text' => '蒙古文',
'lang_text' => '',
'con_flag' => '',
],
'my' => [
'text' => '缅甸语',
'lang_text' => '',
'con_flag' => '',
],
'ne' => [
'text' => '尼泊尔语',
'lang_text' => '',
'con_flag' => '',
],
'no' => [
'text' => '挪威语',
'lang_text' => '',
'con_flag' => '',
],
'ny' => [
'text' => '尼杨扎语(齐切瓦语)',
'lang_text' => '',
'con_flag' => '',
],
'or' => [
'text' => '奥里亚语(奥里亚)',
'lang_text' => '',
'con_flag' => '',
],
'ps' => [
'text' => '普什图语',
'lang_text' => '',
'con_flag' => '',
],
'fa' => [
'text' => '波斯语',
'lang_text' => '',
'con_flag' => '',
],
'pl' => [
'text' => '波兰语',
'lang_text' => '',
'con_flag' => '',
],
'pa' => [
'text' => '旁遮普语',
'lang_text' => '',
'con_flag' => '',
],
'ro' => [
'text' => '罗马尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'sm' => [
'text' => '萨摩亚语',
'lang_text' => '',
'con_flag' => '',
],
'gd' => [
'text' => '苏格兰盖尔语',
'lang_text' => '',
'con_flag' => '',
],
'sr' => [
'text' => '塞尔维亚语',
'lang_text' => '',
'con_flag' => '',
],
'st' => [
'text' => '塞索托语',
'lang_text' => '',
'con_flag' => '',
],
'sn' => [
'text' => '修纳语',
'lang_text' => '',
'con_flag' => '',
],
'sd' => [
'text' => '信德语',
'lang_text' => '',
'con_flag' => '',
],
'si' => [
'text' => '僧伽罗语',
'lang_text' => '',
'con_flag' => '',
],
'sk' => [
'text' => '斯洛伐克语',
'lang_text' => '',
'con_flag' => '',
],
'sl' => [
'text' => '斯洛文尼亚语',
'lang_text' => '',
'con_flag' => '',
],
'so' => [
'text' => '索马里语',
'lang_text' => '',
'con_flag' => '',
],
'su' => [
'text' => '巽他语',
'lang_text' => '',
'con_flag' => '',
],
'sw' => [
'text' => '斯瓦希里语',
'lang_text' => '',
'con_flag' => '',
],
'sv' => [
'text' => '瑞典语',
'lang_text' => '',
'con_flag' => '',
],
'tl' => [
'text' => '塔加路语(菲律宾语)',
'lang_text' => 'Pilipino',
'con_flag' => 'con_flag/tl.jfif',
'shop_lang' => 'tl',
],
'tg' => [
'text' => '塔吉克语',
'lang_text' => '',
'con_flag' => '',
],
'ta' => [
'text' => '泰米尔语',
'lang_text' => '',
'con_flag' => '',
],
'tt' => [
'text' => '鞑靼语',
'lang_text' => '',
'con_flag' => '',
],
'te' => [
'text' => '泰卢固语',
'lang_text' => '',
'con_flag' => '',
],
'th' => [
'text' => '泰文',
'lang_text' => 'ไทย',
'con_flag' => 'con_flag/th.jfif',
'shop_lang' => 'th',
],
'tr' => [
'text' => '土耳其语',
'lang_text' => '',
'con_flag' => '',
],
'tk' => [
'text' => '土库曼语',
'lang_text' => '',
'con_flag' => '',
],
'uk' => [
'text' => '乌克兰语',
'lang_text' => '',
'con_flag' => '',
],
'ur' => [
'text' => '乌尔都语',
'lang_text' => '',
'con_flag' => '',
],
'ug' => [
'text' => '维吾尔语',
'lang_text' => '',
'con_flag' => '',
],
'uz' => [
'text' => '乌兹别克语',
'lang_text' => '',
'con_flag' => '',
],
'vi' => [
'text' => '越南语',
'lang_text' => '',
'con_flag' => '',
],
'cy' => [
'text' => '威尔士语',
'lang_text' => '',
'con_flag' => '',
],
'xh' => [
'text' => '班图语',
'lang_text' => '',
'con_flag' => '',
],
'yi' => [
'text' => '意第绪语',
'lang_text' => '',
'con_flag' => '',
],
'yo' => [
'text' => '约鲁巴语',
'lang_text' => '',
'con_flag' => '',
],
'zu' => [
'text' => '祖鲁语',
'lang_text' => '',
'con_flag' => '',
],
];
/**
* 获取语种列表
* @return array|string
* @author:dc
* @time 2022/3/30 13:52
*/
public static function getTls($lang = null){
$tls = array_combine(array_keys(static::$tls_list),array_column(static::$tls_list, 'text'));
if($lang === null){
return $tls;
}
if (is_array($lang)){
foreach ($lang as $key=>$item){
unset($lang[$key]);
$lang[$item] = $tls[$item]??'';
}
return $lang;
}
return $tls[$lang]??'';
}
/**
* 翻译
* @param $texts
* @param $tls
* @return \Illuminate\Http\Client\Response
* @time 2022/3/30 15:58
*/
public static function translate($texts, $tls)
{
if (is_string($texts)) {
$texts = [$texts];
}
if (is_string($tls)) {
$tls = [$tls];
}
$data = [
'texts' => $texts,
'sl' => 'auto',
'tls' => $tls,
];
return Http::post(self::$url, $data);
}
/**
* @param $texts
* @param $tls
* @return string|array
* @author:dc
* @time 2022/3/30 15:59
*/
public static function tran($texts, $tls)
{
if (!$texts) {
return '';
}
$retsult = self::translate($texts, $tls)->json();
return $retsult[0]['texts'] ?? '';
}
}
... ...
... ... @@ -10,10 +10,15 @@ define('HTTP_OPENAI_URL','http://openai.waimaoq.com/');
* @author zbj
* @date 2023/4/15
*/
function generateRoute($string){
return trim(strtolower(preg_replace( '/[\W]+/', '-', trim($string))), '-');
if (!function_exists('generateRoute')) {
function generateRoute($string)
{
return trim(strtolower(preg_replace('/[\W]+/', '-', trim($string))), '-');
}
}
/**
* 手动记录错误日志
* @param $title
... ... @@ -107,3 +112,31 @@ if(!function_exists('_get_child')){
return $new_arr ? $new_arr : false;
}
}
if (!function_exists('checkDomain')) {
/**
* 检查并补全域名协议
* @return false|string
* @author zbj
* @date 2023/5/5
*/
function checkDomain($value)
{
$urlParts = parse_url(strtolower($value));
if(empty($urlParts['host'])){
$urlParts = parse_url('https://' . $value);
}
$host = $urlParts['host'] ?? '';
$scheme = $urlParts['scheme'] ?? 'https';
if(!in_array($scheme, ['http', 'https'])){
return false;
}
if (preg_match('/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/', $host)) {
return $scheme . '://' . $host;
} else {
return false;
}
}
}
... ...
... ... @@ -55,7 +55,7 @@ class ProductController extends BaseController
]);
$data = $logic->getInfo($this->param['id']);
return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'gallery', 'attrs', 'category_id', 'keyword_id', 'attr_id', 'describe_id', 'intro', 'content',
'describe', 'seo_mate', 'related_product_id', 'status', 'category_id_text', 'keyword_id_text', 'status_text', 'created_uid', 'created_uid_text']));
'describe', 'seo_mate', 'related_product_id', 'status', 'category_id_text', 'keyword_id_text', 'status_text', 'created_uid', 'created_uid_text', 'route']));
}
public function save(ProductRequest $request, ProductLogic $logic)
... ...
<?php
namespace App\Http\Controllers\Bside;
use App\Helper\Translate;
use App\Models\RouteMap;
use App\Rules\Ids;
use Illuminate\Http\Request;
class RouteController extends BaseController
{
/**
* 生成路由
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @author zbj
* @date 2023/5/5
*/
public function create(Request $request){
$title = $request->input('title');
$source = $request->input('source');
$source_id = $request->input('source_id');
$project_id = $this->user['project_id'];
//有中文先翻译
if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $title)){
$title = Translate::tran($title, 'en');
}
$route = RouteMap::generateRoute($title, $source, $source_id, $project_id);
return $this->success(['route' => $route]);
}
}
... ...
... ... @@ -39,6 +39,19 @@ class ProjectLogic extends BaseLogic
}
public function save($param){
if(!empty($param['test_domain'])){
$param['test_domain'] = checkDomain($param['test_domain']);
if(!$param['test_domain']){
$this->fail('测试域名格式不正确');
}
}
if(!empty($param['domain'])){
$param['domain'] = checkDomain($param['domain']);
if(!$param['domain']){
$this->fail('正式域名格式不正确');
}
}
DB::beginTransaction();
try {
$res = parent::save($param);
... ...
... ... @@ -37,7 +37,7 @@ class ProductRequest extends FormRequest
{
return [
'title' => 'required|max:200',
'route' => 'required|max:100',
'route' => 'required|max:200',
'gallery' => ['required', 'array', function ($attribute, $value, $fail) {
foreach ($value as $v) {
if (empty($v['url'])) {
... ... @@ -88,7 +88,7 @@ class ProductRequest extends FormRequest
'title.required' => '请输入产品标题',
'title.max' => '产品标题不能超过20个字符',
'route.required' => '请输入产品链接',
'route.max' => '产品链接不能超过100个字符',
'route.max' => '产品链接不能超过200个字符',
'gallery.required' => '请上传产品图片',
'gallery.array' => '产品图片格式异常',
'attrs.required' => '请添加产品参数',
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
use App\Models\RouteMap;
use App\Services\Facades\Upload;
use Illuminate\Database\Eloquent\SoftDeletes;
... ... @@ -18,6 +19,7 @@ class Product extends Base
const STATUS_ON = 1;
const STATUS_RECYCLE = 2;
protected $appends = ['route'];
public static function statusMap(){
return [
... ... @@ -117,4 +119,8 @@ class Product extends Base
return Arr::setToArr($value);
}
public function getRouteAttribute(){
return RouteMap::getRoute(RouteMap::SOURCE_PRODUCT, $this->id, $this->project_id);
}
}
... ...
... ... @@ -188,6 +188,11 @@ Route::middleware(['bloginauth'])->group(function () {
Route::get('/info', [\App\Http\Controllers\Bside\InquiryController::class, 'info'])->name('inquiry_info');
Route::any('/delete', [\App\Http\Controllers\Bside\InquiryController::class, 'delete'])->name('inquiry_delete');
});
//生成路由
Route::prefix('route')->group(function () {
Route::any('/create', [\App\Http\Controllers\Bside\RouteController::class, 'create'])->name('create_route');
});
});
//无需登录验证的路由组
... ... @@ -196,7 +201,4 @@ Route::group([], function () {
Route::any('/', [\App\Http\Controllers\Bside\ComController::class, 'get_country'])->name('get_country');
Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download');
Route::any('/image/{hash}/{w?}/{h?}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
//添加询盘信息
Route::post('/inquiry/save', [\App\Http\Controllers\Bside\InquiryController::class, 'save'])->name('inquiry_save');
});
... ...