perf: 新增doo模块

This commit is contained in:
kuaifan
2023-03-14 00:56:52 +08:00
parent 1ccf6e49b7
commit 13ca9031a8
11 changed files with 300 additions and 98 deletions

View File

@@ -1458,73 +1458,6 @@ class Base
}
}
/**
* 国际化(替换国际语言)
* @param $val
* @return mixed
*/
public static function Lang($val)
{
$data = self::langData();
if (isset($data[$val])) {
return $data[$val] ?: $val;
}
foreach ($data as $key => $item) {
if (str_contains($key, "(*)")) {
$regex = str_replace("(*)", "~%~", $key);
$regex = preg_quote($regex);
$regex = str_replace("~%~", "(.*?)", $regex);
$regex = "/^" . $regex . "$/";
if (preg_match($regex, $val)) {
$r = $item ?: $key;
return preg_replace_callback($regex, function($m) use ($r) {
$i = 0;
foreach ($m as $v) {
if ($i > 0) {
$r = preg_replace("/\(\*\)/", $v, $r, 1);
}
$i++;
}
return $r;
}, $val);
}
}
}
//
$undefinedPath = base_path('language/undefined-api.txt');
if (self::$undefinedLang === null) {
self::$undefinedLang = [];
if (file_exists($undefinedPath)) {
self::$undefinedLang = explode("\n", file_get_contents($undefinedPath));
self::$undefinedLang = array_values(array_filter(array_unique(self::$undefinedLang)));
}
}
if (!in_array($val, self::$undefinedLang)) {
self::$undefinedLang[] = $val;
@file_put_contents(base_path('language/undefined-api.txt'), "$val\n", FILE_APPEND);
}
return $val;
}
private static $undefinedLang = null;
/**
* 加载语言数据
* @return array
*/
public static function langData()
{
global $_A;
$language = trim(self::headerOrInput('language'));
if (!isset($_A["__static_langdata_" . $language])) {
$_A["__static_langdata_" . $language] = [];
$langpath = public_path('language/api/' . $language . '.json');
if (file_exists($langpath) && $data = Base::json2array(file_get_contents($langpath))) {
$_A["__static_langdata_" . $language] = $data;
}
}
return $_A["__static_langdata_" . $language];
}
/**
* JSON返回
* @param $param
@@ -1552,7 +1485,7 @@ class Base
{
return [
'ret' => $ret,
'msg' => self::Lang($msg),
'msg' => Doo::translate($msg),
'data' => $data
];
}
@@ -1568,7 +1501,7 @@ class Base
{
return [
'ret' => $ret,
'msg' => self::Lang($msg),
'msg' => Doo::translate($msg),
'data' => $data
];
}

170
app/Module/Doo.php Normal file
View File

@@ -0,0 +1,170 @@
<?php
namespace App\Module;
use Carbon\Carbon;
use FFI;
class Doo
{
public static $doo = null;
public function __construct()
{
return self::init();
}
/**
* 初始化
* @return mixed
*/
public static function init()
{
if (self::$doo === null) {
$doo = FFI::cdef(<<<EOF
void initialize();
void setWorkDir(char* val);
void setDefaultLanguage(char* val);
void setUserToken(char* val);
char* license();
int userId();
char* userExpiredAt();
char* userEmail();
char* userToken();
char* tokenEncode(int userid, char* email, char* encrypt, int days);
char* tokenDecode(char* val);
char* translate(char* val);
char* translateSpecified(char* val, char* val);
EOF, app_path("Module/Lib/doo.so"));
$doo->initialize();
$doo->setWorkDir("/var/www");
$doo->setDefaultLanguage(Base::headerOrInput('language'));
$doo->setUserToken(Base::getToken());
self::$doo = $doo;
}
return self::$doo;
}
/**
* char转为字符串
* @param $text
* @return string
*/
public static function string($text)
{
return FFI::string($text);
}
/**
* License
* @return array
*/
public static function license()
{
$array = Base::json2array(self::string(self::init()->license()));
$ips = explode(",", $array['ip']);
$array['ip'] = [];
foreach ($ips as $ip) {
if (Base::is_ipv4($ip)) {
$array['ip'][] = $ip;
}
}
$domains = explode(",", $array['domain']);
$array['domain'] = [];
foreach ($domains as $domain) {
if (Base::is_domain($domain)) {
$array['domain'][] = $domain;
}
}
$emails = explode(",", $array['email']);
$array['email'] = [];
foreach ($emails as $email) {
if (Base::isEmail($email)) {
$array['email'][] = $email;
}
}
return $array;
}
/**
* 当前会员ID(来自请求的token)
* @return int
*/
public static function userId()
{
return self::init()->userId();
}
/**
* token是否过期(来自请求的token)
* @return bool
*/
public static function userExpired()
{
return Carbon::parse(self::string(self::init()->userExpiredAt()))->isBefore(Carbon::now());
}
/**
* 当前会员邮箱地址(来自请求的token)
* @return string
*/
public static function userEmail()
{
return self::string(self::init()->userEmail());
}
/**
* 当前会员token(来自请求的token)
* @return string
*/
public static function userToken()
{
return self::string(self::init()->userToken());
}
/**
* 生成token(编码token)
* @param $userid
* @param $email
* @param $encrypt
* @param int $days 有效时间(天)
* @return string
*/
public static function tokenEncode($userid, $email, $encrypt, $days = 7)
{
return self::string(self::init()->tokenEncode($userid, $email, $encrypt, $days));
}
/**
* 解码token
* @param $token
* @return array
*/
public static function tokenDecode($token)
{
$array = Base::json2array(self::string(self::init()->tokenDecode($token)));
$array['is_expired'] = Carbon::parse($array['expired_at'])->isBefore(Carbon::now());
return $array;
}
/**
* 翻译
* @param string $text
* @param string|null $type
* @return string
*/
public static function translate($text, $type = null)
{
if ($type) {
$test = self::init()->translateSpecified($text, $type);
} else {
$test = self::init()->translate($text);
}
return self::string($test);
}
}

93
app/Module/Lib/doo.h Normal file
View File

@@ -0,0 +1,93 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-export-prolog"
#include <stddef.h>
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
#endif
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern void initialize();
extern void setWorkDir(char* val);
extern void setDefaultLanguage(char* val);
extern void setUserToken(char* token);
extern char* license();
extern int userId();
extern char* userExpiredAt();
extern char* userEmail();
extern char* userToken();
extern char* tokenEncode(int userid, char* email, char* encrypt, int days);
extern char* tokenDecode(char* token);
extern char* translate(char* text);
extern char* translateSpecified(char* text, char* langType);
#ifdef __cplusplus
}
#endif

BIN
app/Module/Lib/doo.so Normal file

Binary file not shown.