From a01b47fd94a90da56bd13a012d49bf5053a74940 Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Sat, 21 Dec 2019 14:32:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=93=E5=AD=98=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E8=8E=B7=E5=8F=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/app/models/system/Cache.php | 49 +++++++++++++---- crmeb/crmeb/services/GroupDataService.php | 57 +++++++++++--------- crmeb/crmeb/services/SystemConfigService.php | 21 +++++--- crmeb/crmeb/services/UploadService.php | 45 ++++++++++++++-- crmeb/crmeb/services/YLYService.php | 28 ++++++---- crmeb/public/install/crmeb.sql | 4 +- 6 files changed, 150 insertions(+), 54 deletions(-) diff --git a/crmeb/app/models/system/Cache.php b/crmeb/app/models/system/Cache.php index ce332246..dc29c49c 100644 --- a/crmeb/app/models/system/Cache.php +++ b/crmeb/app/models/system/Cache.php @@ -1,4 +1,5 @@ value('result'); - return json_decode($result,true); + $result = self::where('key', $key)->value('result'); + if ($result) { + return json_decode($result, true); + } else { + if ($default instanceof \Closure) { + // 获取缓存数据 + $value = $default(); + if ($value) { + self::setDbCache($key, $value, $expire); + return $value; + } + } else { + self::setDbCache($key, $default, $expire); + return $default; + } + return null; + } } /** @@ -39,14 +56,23 @@ class Cache extends BaseModel * @param int $expire * @return void */ - public static function setDbCache(string $key,$result,$expire = self::EXPIRE) + public static function setDbCache(string $key, $result, $expire = self::EXPIRE) { self::delectDeOverdueDbCache(); $addTime = $expire ? time() + $expire : 0; - if(self::be(['key'=>$key])){ - return self::where(['key'=>$key])->update(['result'=>json_encode($result),'add_time'=>$addTime]); - }else{ - return self::create(['key'=>$key,'result'=>json_encode($result),'add_time'=>$addTime]); + if (self::be(['key' => $key])) { + return self::where(['key' => $key])->update([ + 'result' => json_encode($result), + 'expire_time' => $addTime, + 'add_time' => time() + ]); + } else { + return self::create([ + 'key' => $key, + 'result' => json_encode($result), + 'expire_time' => $addTime, + 'add_time' => time() + ]); } } @@ -55,6 +81,7 @@ class Cache extends BaseModel */ public static function delectDeOverdueDbCache() { + self::where('expire_time', '<>', 0)->where('expire_time', '<', time())->delete(); } /** @@ -63,8 +90,8 @@ class Cache extends BaseModel */ public static function delectDbCache(string $key = '') { - if($key) - return self::where('key',$key)->delete(); + if ($key) + return self::where('key', $key)->delete(); else return self::delete(); } diff --git a/crmeb/crmeb/services/GroupDataService.php b/crmeb/crmeb/services/GroupDataService.php index 65d58d97..3705670d 100644 --- a/crmeb/crmeb/services/GroupDataService.php +++ b/crmeb/crmeb/services/GroupDataService.php @@ -23,23 +23,26 @@ class GroupDataService */ public static function getGroupData(string $config_name, $limit = 0, bool $isCaChe = false): array { + $callable = function () use ($config_name, $limit) { + $data = SystemGroupData::getGroupData($config_name, $limit); + if (is_object($data)) + $data = $data->toArray(); + return $data; + }; try { $cacheName = $limit ? "group_data_{$config_name}_{$limit}" : "data_{$config_name}"; - $callable = function () use ($config_name, $limit) { - $data = SystemGroupData::getGroupData($config_name, $limit); - if (is_object($data)) - $data = $data->toArray(); - return $data; - }; - if ($isCaChe) return $callable(); return CacheService::get($cacheName, $callable); } catch (\Throwable $e) { - return []; + try { + return $callable(); + } catch (\Exception $e) { + return []; + } } } @@ -52,23 +55,26 @@ class GroupDataService */ public static function getData(string $config_name, int $limit = 0, bool $isCaChe = false): array { + $callable = function () use ($config_name, $limit) { + $data = SystemGroupData::getAllValue($config_name, $limit); + if (is_object($data)) + $data = $data->toArray(); + return $data; + }; try { $cacheName = $limit ? "data_{$config_name}_{$limit}" : "data_{$config_name}"; - $callable = function () use ($config_name, $limit) { - $data = SystemGroupData::getAllValue($config_name, $limit); - if (is_object($data)) - $data = $data->toArray(); - return $data; - }; - if ($isCaChe) return $callable(); return CacheService::get($cacheName, $callable); } catch (\Throwable $e) { - return []; + try { + return $callable(); + } catch (\Exception $e) { + return []; + } } } @@ -80,23 +86,26 @@ class GroupDataService */ public static function getDataNumber(int $id, bool $isCaChe = false): array { + $callable = function () use ($id) { + $data = SystemGroupData::getDateValue($id); + if (is_object($data)) + $data = $data->toArray(); + return $data; + }; try { $cacheName = "data_number_{$id}"; - $callable = function () use ($id) { - $data = SystemGroupData::getDateValue($id); - if (is_object($data)) - $data = $data->toArray(); - return $data; - }; - if ($isCaChe) return $callable(); return CacheService::get($cacheName, $callable); } catch (\Throwable $e) { - return []; + try { + return $callable(); + } catch (\Exception $e) { + return []; + } } } } \ No newline at end of file diff --git a/crmeb/crmeb/services/SystemConfigService.php b/crmeb/crmeb/services/SystemConfigService.php index a6b03a69..9f46e90e 100644 --- a/crmeb/crmeb/services/SystemConfigService.php +++ b/crmeb/crmeb/services/SystemConfigService.php @@ -25,9 +25,18 @@ class SystemConfigService */ protected static function init() { - self::$configList = CacheService::get(self::CACHE_SYSTEM, function () { + $confingFun = function () { return self::getAll(); - }); + }; + try { + self::$configList = CacheService::get(self::CACHE_SYSTEM, $confingFun); + } catch (\Throwable $e) { + try { + self::$configList = $confingFun(); + } catch (\Exception $e) { + self::$configList = []; + } + } } /**获取系统配置 @@ -48,12 +57,12 @@ class SystemConfigService * @param bool $isCaChe 是否获取缓存配置 * @return bool|mixed|string */ - public static function get($key,$default = '',bool $isCaChe = false) + public static function get($key, $default = '', bool $isCaChe = false) { - if($isCaChe){ - try{ + if ($isCaChe) { + try { return SystemConfig::getConfigValue($key); - }catch (\Throwable $e){ + } catch (\Throwable $e) { return $default; } } diff --git a/crmeb/crmeb/services/UploadService.php b/crmeb/crmeb/services/UploadService.php index c8856c9d..d81f4b5e 100644 --- a/crmeb/crmeb/services/UploadService.php +++ b/crmeb/crmeb/services/UploadService.php @@ -75,7 +75,17 @@ class UploadService * 上传图片的大小 2MB 单位字节 * @var string */ - protected $imageValidate = 'filesize:2097152|fileExt:jpg,jpeg,png,gif,pem|fileMime:image/jpeg,image/gif,image/png,text/plain'; + protected $imageValidate = null; + + /** + * 上传规则 + * @var array + */ + protected $imageValidateArray = [ + 'filesize' => 2097152, + 'fileExt' => ['jpg', 'jpeg', 'png', 'gif', 'pem', 'mp3', 'wma', 'wav', 'amr', 'mp4'], + 'fileMime' => ['image/jpeg', 'image/gif', 'image/png', 'text/plain', 'audio/mpeg'], + ]; protected $propsRule = [ 'returnErr' => false, @@ -86,15 +96,44 @@ class UploadService protected function __construct() { - self::init(); + $this->init(); } /** * 初始化 */ - private static function init() + protected function init() { self::$uploadStatus = new \StdClass(); + $this->extractValidate(); + } + + /** + * 提取上传验证 + */ + protected function extractValidate() + { + $imageValidate = []; + foreach ($this->imageValidateArray as $key => $value) { + $imageValidate[] = $key . ':' . (is_array($value) ? implode(',', $value) : $value); + } + $this->imageValidate = implode('|', $imageValidate); + unset($imageValidate); + } + + /** + * 设置上传验证 + * @param array $imageValidateArray + * @return $this + */ + public function setImageValidateArray(array $imageValidateArray) + { + if (isset($imageValidateArray['filesize']) && !is_int($imageValidateArray['filesize'])) { + $imageValidateArray['filesize'] = 2097152; + } + $this->imageValidateArray = array_merge($this->imageValidateArray, $imageValidateArray); + $this->extractValidate(); + return $this; } /** diff --git a/crmeb/crmeb/services/YLYService.php b/crmeb/crmeb/services/YLYService.php index 487248ac..3d7de1b5 100644 --- a/crmeb/crmeb/services/YLYService.php +++ b/crmeb/crmeb/services/YLYService.php @@ -86,8 +86,7 @@ class YLYService extends HttpService implements ProviderInterface * */ protected function getAccessToken() { - $token = CacheModel::getDbCache('YLY_access_token'); - if (!$token) { + $this->access_token = CacheModel::getDbCache('YLY_access_token', function () { $request = self::postRequest($this->apiUrl . 'oauth/oauth', [ 'client_id' => $this->client_id, 'grant_type' => 'client_credentials', @@ -100,11 +99,12 @@ class YLYService extends HttpService implements ProviderInterface $request['error'] = $request['error'] ?? 0; $request['error_description'] = $request['error_description'] ?? ''; if ($request['error'] == 0 && $request['error_description'] == 'success') { - $token = $request['body']['access_token'] ?? ''; - CacheModel::setDbCache('YLY_access_token', $token); + return $request['body']['access_token'] ?? ''; } - } - $this->access_token = $token; + return ''; + }, 20 * 86400); + if (!$this->access_token) + throw new AuthException('获取access_token获取失败'); } /** @@ -123,7 +123,7 @@ class YLYService extends HttpService implements ProviderInterface * @param string $order_id * @return bool|mixed */ - public function orderPrinting(string $order_id = '') + public function orderPrinting(string $order_id = '', int $errorCount = 0) { $request = self::postRequest($this->apiUrl . 'print/index', [ 'client_id' => $this->client_id, @@ -136,7 +136,13 @@ class YLYService extends HttpService implements ProviderInterface 'timestamp' => time() ]); if ($request === false) return false; - return json_decode($request, true); + $request = json_decode($request, true); + if (isset($request['error']) && in_array($request['error'], [18, 14]) && $errorCount == 0) { + CacheModel::delectDbCache('YLY_access_token'); + $this->getAccessToken(); + return $this->orderPrinting($order_id, 1); + } + return $request; } /** @@ -172,7 +178,11 @@ class YLYService extends HttpService implements ProviderInterface $goodsStr = ''; foreach ($product as $item) { $goodsStr .= ''; - $goodsStr .= ""; + if (isset($item['productInfo']['attrInfo'])) { + $price = $item['productInfo']['attrInfo']['price'] ?? $item['productInfo']['price'] ?? 0; + $goodsStr .= ""; + } else + $goodsStr .= ""; $goodsStr .= ''; } $goodsStr .= '
商品名称数量单价金额
{$item['productInfo']['store_name']}{$item['cart_num']}{$item['productInfo']['price']}{$item['truePrice']}{$item['productInfo']['store_name']}{$item['cart_num']}{$price}{$item['truePrice']}{$item['productInfo']['store_name']}{$item['cart_num']}{$item['productInfo']['price']}{$item['truePrice']}
'; diff --git a/crmeb/public/install/crmeb.sql b/crmeb/public/install/crmeb.sql index f0136261..bc714992 100644 --- a/crmeb/public/install/crmeb.sql +++ b/crmeb/public/install/crmeb.sql @@ -90,8 +90,10 @@ CREATE TABLE IF NOT EXISTS `eb_article_content` ( CREATE TABLE IF NOT EXISTS `eb_cache` ( `key` varchar(32) NOT NULL, `result` text COMMENT '缓存数据', + `expire_time` int(10) NOT NULL DEFAULT '0' COMMENT '失效时间0=永久', `add_time` int(10) DEFAULT NULL COMMENT '缓存时间', - PRIMARY KEY (`key`) USING BTREE + PRIMARY KEY (`key`) USING BTREE, + KEY `key` (`key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='微信缓存表'; -- --------------------------------------------------------