Initial commit: queue workspace

Made-with: Cursor
This commit is contained in:
apple
2026-03-21 02:55:24 +08:00
commit 78de918c37
12388 changed files with 1840126 additions and 0 deletions

View File

@@ -0,0 +1,188 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\printer;
use app\services\other\CacheServices;
use crmeb\services\HttpService;
use think\facade\Config;
use think\helper\Str;
/**
*
* Class AccessToken
* @package crmeb\services\printer
*/
class AccessToken extends HttpService
{
/**
* token
* @var array
*/
protected $accessToken;
/**
* 请求接口
* @var string
*/
protected $apiUrl;
/**
* @var string
*/
protected $clientId;
/**
* 终端号码
* @var string
*/
protected $machineCode;
/**
* 开发者id
* @var string
*/
protected $partner;
/**
* 驱动类型
* @var string
*/
protected $name;
/**
* 配置文件名
* @var string
*/
protected $configFile;
/**
* api key
* @var string
*/
protected $apiKey;
/**
* 飞鹅云SN
* @var string
*/
protected $feySn;
/**
* 飞鹅云UYEK
* @var string
*/
protected $feyUkey;
/**
* 飞鹅云USER
* @var string
*/
protected $feyUser;
public function __construct(array $config = [], string $name = '', string $configFile = '')
{
$this->clientId = $config['clientId'] ?? null;
$this->apiKey = $config['apiKey'] ?? null;
$this->partner = $config['partner'] ?? null;
$this->machineCode = $config['terminal'] ?? null;
$this->feyUser = $config['feyUser'] ?? null;
$this->feyUkey = $config['feyUkey'] ?? null;
$this->feySn = $config['feySn'] ?? null;
$this->name = $name;
$this->configFile = $configFile;
$this->apiUrl = Config::get($this->configFile . '.stores.' . $this->name . '.apiUrl', 'https://open-api.10ss.net/');
}
/**
* 获取token
* @return mixed|null|string
* @throws \Exception
*/
public function getAccessToken()
{
if (isset($this->accessToken[$this->name])) {
return $this->accessToken[$this->name];
}
$action = 'get' . Str::studly($this->name) . 'AccessToken';
if (method_exists($this, $action)) {
return $this->{$action}();
} else {
throw new \RuntimeException(__CLASS__ . '->' . $action . '(),Method not worn in');
}
}
/**
* 获取易联云token
* @return mixed|null|string
* @throws \Exception
*/
protected function getYiLianYunAccessToken()
{
/** @var CacheServices $cacheServices */
$cacheServices = app()->make(CacheServices::class);
$this->accessToken[$this->name] = $cacheServices->getDbCache('YLY_access_token', function () {
$request = self::postRequest($this->apiUrl . 'oauth/oauth', [
'client_id' => $this->clientId,
'grant_type' => 'client_credentials',
'sign' => strtolower(md5($this->clientId . time() . $this->apiKey)),
'scope' => 'all',
'timestamp' => time(),
'id' => $this->createUuid(),
]);
$request = json_decode($request, true);
$request['error'] = $request['error'] ?? 0;
$request['error_description'] = $request['error_description'] ?? '';
if ($request['error'] == 0 && $request['error_description'] == 'success') {
return $request['body']['access_token'] ?? '';
}
return '';
},86400);
if (!$this->accessToken[$this->name])
throw new \Exception('获取access_token获取失败');
return $this->accessToken[$this->name];
}
/**
* 获取请求链接
* @return string
*/
public function getApiUrl(string $url = '')
{
return $url ? $this->apiUrl . $url : $this->apiUrl;
}
/**
* 生成UUID4
* @return string
*/
public function createUuid()
{
mt_srand();
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}
/**
* 获取属性
* @param $name
* @return mixed
*/
public function __get($name)
{
if (in_array($name, ['clientId', 'apiKey', 'accessToken', 'partner', 'terminal', 'machineCode', 'feyUser', 'feyUkey', 'feySn'])) {
return $this->{$name};
}
}
}

View File

@@ -0,0 +1,72 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\printer;
use crmeb\basic\BaseManager;
use think\facade\Config;
use think\Container;
/**
* Class Printer
* @package crmeb\services\auth
* @mixin \crmeb\services\printer\storage\YiLianYun
* @mixin \crmeb\services\printer\storage\FeiEYun
*/
class Printer extends BaseManager
{
/**
* 空间名
* @var string
*/
protected string $namespace = '\\crmeb\\services\\printer\\storage\\';
/**
* @var object
*/
protected $handleAccessToken;
/**
* 默认驱动
* @return mixed
*/
protected function getDefaultDriver()
{
return Config::get('printer.default', 'yi_lian_yun');
}
/**
* 获取类的实例
* @param $class
* @return mixed|void
*/
protected function invokeClass($class)
{
if (!class_exists($class)) {
throw new \RuntimeException('class not exists: ' . $class);
}
$this->getConfigFile();
if (!$this->config) {
$this->config = Config::get($this->configFile . '.stores.' . $this->name, []);
}
if (!$this->handleAccessToken) {
$this->handleAccessToken = new AccessToken($this->config, $this->name, $this->configFile);
}
$handle = Container::getInstance()->invokeClass($class, [$this->name, $this->handleAccessToken, $this->configFile]);
$this->config = [];
return $handle;
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace crmeb\services\printer\storage;
use app\services\activity\table\TableQrcodeServices;
use crmeb\basic\BasePrinter;
class FeiEYun extends BasePrinter
{
protected int $times;
/**
* 初始化
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config)
{
}
/**
* 开始打印
* @return bool|mixed|string
* @throws \Exception
*/
public function startPrinter()
{
if (!$this->printerContent) {
return $this->setError('Missing print');
}
$time = time();
$request = $this->accessToken->postRequest('http://api.feieyun.cn/Api/Open/', [
'user' => $this->accessToken->feyUser,
'stime' => $time,
'sig' => sha1($this->accessToken->feyUser . $this->accessToken->feyUkey . $time),
'apiname' => 'Open_printMsg',
'sn' => $this->accessToken->feySn,
'content' => $this->printerContent,
'times' => $this->times
]);
$res = json_decode($request, true);
if ($res['msg'] == 'ok') {
return $res;
} else {
return $this->setError($res['msg']);
}
}
/**
* 设置打印内容
* @param $content
* @param int $times
* @return FeiEYun
*/
public function setPrinterContent($content, $times = 1): self
{
$this->times = $times;
$this->printerContent = $content;
return $this;
}
/**
* 设置桌码打印内容
* @param array $config
* @return YiLianYun
*/
public function setPrinterTableContent(array $config): self
{
$timeYmd = date('Y-m-d', time());
$timeHis = date('H:i:s', time());
$product = $config['product'];
$tableInfo = $config['tableInfo'];
$name = $config['name'];
/** @var TableQrcodeServices $qrcodeService */
$qrcodeService = app()->make(TableQrcodeServices::class);
$Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['cateName']);
$this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
$this->printerContent .= '--------------------------------<BR>';
$this->printerContent .= '桌码流水:' . $tableInfo['serial_number'] . '<BR>';
$this->printerContent .= '桌码分类: ' . $Info['cateName']['name'] . '<BR>';
$this->printerContent .= '桌码编号: ' . $Info['table_number'] . '<BR>';
$this->printerContent .= '日 期: ' . $timeYmd . '<BR>';
$this->printerContent .= '时 间:' . $timeHis . '<BR>';
$this->printerContent .= '**************商品**************<BR>';
$this->printerContent .= '名称 单价 数量 金额<BR>';
$this->printerContent .= '--------------------------------<BR>';
foreach ($product as $item) {
$name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
$price = $item['truePrice'];
$num = $item['cart_num'];
$prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
$kw3 = '';
$kw1 = '';
$kw2 = '';
$kw4 = '';
$str = $name;
$blankNum = 14;//名称控制为14个字节
$lan = mb_strlen($str, 'utf-8');
$m = 0;
$j = 1;
$blankNum++;
$result = array();
if (strlen($price) < 6) {
$k1 = 6 - strlen($price);
for ($q = 0; $q < $k1; $q++) {
$kw1 .= ' ';
}
$price = $price . $kw1;
}
if (strlen($num) < 3) {
$k2 = 3 - strlen($num);
for ($q = 0; $q < $k2; $q++) {
$kw2 .= ' ';
}
$num = $num . $kw2;
}
if (strlen($prices) < 6) {
$k3 = 6 - strlen($prices);
for ($q = 0; $q < $k3; $q++) {
$kw4 .= ' ';
}
$prices = $prices . $kw4;
}
for ($i = 0; $i < $lan; $i++) {
$new = mb_substr($str, $m, $j, 'utf-8');
$j++;
if (mb_strwidth($new, 'utf-8') < $blankNum) {
if ($m + $j > $lan) {
$m = $m + $j;
$tail = $new;
$lenght = iconv("UTF-8", "GBK//IGNORE", $new);
$k = 14 - strlen($lenght);
for ($q = 0; $q < $k; $q++) {
$kw3 .= ' ';
}
if ($m == $j) {
$tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
} else {
$tail .= $kw3 . '<BR>';
}
break;
} else {
$next_new = mb_substr($str, $m, $j, 'utf-8');
if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
continue;
} else {
$m = $i + 1;
$result[] = $new;
$j = 1;
}
}
}
}
$head = '';
foreach ($result as $key => $value) {
if ($key < 1) {
$v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
$v_lenght = strlen($v_lenght);
if ($v_lenght == 13) $value = $value . " ";
$head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
} else {
$head .= $value . '<BR>';
}
}
$this->printerContent .= $head . $tail;
unset($price);
}
$this->printerContent .= '--------------------------------<BR>';
$this->printerContent .= '商品金额:' . number_format(array_sum(array_column($product, 'price')), 1) . '元<BR>';
return $this;
}
}

View File

@@ -0,0 +1,89 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\printer\storage;
use crmeb\basic\BasePrinter;
use crmeb\services\printer\AccessToken;
/**
* Class YiLianYun
* @package crmeb\services\printer\storage
*/
class YiLianYun extends BasePrinter
{
/**
* 初始化
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config)
{
}
/**
* 开始打印
* @return bool|mixed|string
* @throws \Exception
*/
public function startPrinter()
{
if (!$this->printerContent) {
return $this->setError('Missing print');
}
$time = time();
$requestData = [
'client_id' => $this->accessToken->clientId,
'access_token' => $this->accessToken->getAccessToken(),
'machine_code' => $this->accessToken->machineCode,
'content' => $this->printerContent,
'origin_id' => 'crmeb' . $time,
'sign' => strtolower(md5($this->accessToken->clientId . $time . $this->accessToken->apiKey)),
'id' => $this->accessToken->createUuid(),
'timestamp' => $time
];
try {
$request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $requestData);
$request = is_string($request) ? json_decode($request, true) : $request;
if (isset($request['error']) && $request['error'] == '18') {//token过期
$cacheServices = app()->make(CacheServices::class);
$cacheServices->delectDbCache('YLY_access_token_'. $this->accessToken->clientId);
$requestData['access_token'] = $this->accessToken->getAccessToken();
$request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $requestData);
}
} catch (\Exception $e) {
return $this->setError($e->getMessage());
}
$this->printerContent = null;
if ($request === false) {
return $this->setError('request was aborted');
}
$request = is_string($request) ? json_decode($request, true) : $request;
if (isset($request['error']) && $request['error'] != 0) {
return $this->setError(isset($request['error_description']) ? $request['error_description'] : 'Accesstoken has expired');
}
return $request;
}
/**
* 设置打印内容
* @param $content
* @param int $times
* @return YiLianYun
*/
public function setPrinterContent($content, $times = 1): self
{
$this->printerContent = $content;
return $this;
}
}