Cache.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/4/11
  6. * Time: 下午1:10
  7. */
  8. namespace common\helpers;
  9. use backendApi\modules\v1\models\AdminRole;
  10. use common\models\Config;
  11. use common\models\Countries;
  12. use common\models\Currency;
  13. use common\models\DealType;
  14. use common\models\DecRole;
  15. use common\models\Language;
  16. use common\models\OcrApi;
  17. use common\models\DeclarationLevel;
  18. use common\models\EmployLevel;
  19. use common\models\Region;
  20. use common\models\RegType;
  21. use common\models\SmsApi;
  22. use common\models\SmsTemplate;
  23. use common\models\StarCrownLevel;
  24. use common\models\User;
  25. use common\models\UserNetwork;
  26. use common\models\UserRelation;
  27. use common\models\WithdrawLevel;
  28. use Yii;
  29. use backendApi\modules\v1\models\Admin;
  30. use common\models\UserInfo;
  31. class Cache
  32. {
  33. const SYSTEM_CONFIG_KEY = 'sys:config';
  34. const DEC_LEVEL_CONFIG_KEY = 'sys:decLevel';
  35. const DEC_ROLE_CONFIG_KEY = 'sys:decRole';
  36. const EMP_LEVEL_CONFIG_KEY = 'sys:empLevel';
  37. const CROWN_LEVEL_CONFIG_KEY = 'sys:crownLevel';
  38. const ADMIN_ROLE_KEY = 'sys:adminRole';
  39. const SMS_TEMPLATE_DEC_KEY = 'sys:smsTemplateDec';
  40. const SMS_TEMPLATE_EMP_KEY = 'sys:smsTemplateEmp';
  41. const WITHDRAW_LEVEL_CONFIG_KEY = 'sys:withdrawLevel';
  42. const OCR_API_CONFIG_KEY = 'sys:ocrApi';
  43. const SMS_API_CONFIG_KEY = 'sys:smsApi';
  44. const REGION_CONFIG_KEY = 'sys:region';
  45. const DEAL_TYPE_CONFIG_KEY = 'sys:dealType';
  46. const REG_TYPE_CONFIG_KEY = 'sys:regType';
  47. const COUNTRIES_KEY = 'base:countries';
  48. const CURRENCIES_KEY = 'base:currencies';
  49. const CURRENCIES_CONVERSIONS_KEY = 'base:currencies_conversions';
  50. const LANGUAGE_KEY = 'base:languages';
  51. const FREE_TEMPLATE_KEY = 'base:freeTemplates';
  52. const ASYNC_KEY_PREFIX = 'async_';
  53. const SOCKET_KEY_PREFIX = 'socket_';
  54. const SOCKET_ADMIN = 'admin';
  55. const SOCKET_USER_PC = 'userPc';
  56. const SOCKET_USER_APP = 'userAPP';
  57. const USER_CLOSE_KEY_PREFIX = 'user:close_';
  58. const USER_INFO_KEY = 'user:baseInfo';
  59. const USER_NETWORK_PARENTS = 'user:networkParents';
  60. const USER_RELATION_PARENTS = 'user:relationParents';
  61. const USER_CREATED_AT_LIST = 'user:createdAtList_';
  62. /**
  63. * WebSocket的fd加入缓存
  64. * @param $app
  65. * @param $userId
  66. * @param $fd
  67. * @return mixed
  68. */
  69. public static function setWebSocketFd($app, $userId, $fd){
  70. if(isset($userId) && $userId != ''){
  71. if($app == self::SOCKET_ADMIN){
  72. $model = Admin::class;
  73. $field = 'ID';
  74. } elseif($app == self::SOCKET_USER_PC) {
  75. $model = UserInfo::class;
  76. $field = 'USER_ID';
  77. $app = self::SOCKET_USER_PC;
  78. } else {
  79. $model = UserInfo::class;
  80. $field = 'USER_ID';
  81. $app = self::SOCKET_USER_APP;
  82. }
  83. if($model::find()->where($field.'=:'.$field, [':'.$field=>$userId])->exists()){
  84. Yii::$app->redis->hset(self::SOCKET_KEY_PREFIX.$app, $userId, $fd);
  85. }
  86. }
  87. return $fd;
  88. }
  89. /**
  90. * 获取会员的WebSocket的fd
  91. * @param $app
  92. * @param $userId
  93. * @return mixed
  94. */
  95. public static function getWebSocketFd($app, $userId){
  96. return Yii::$app->redis->hget(self::SOCKET_KEY_PREFIX.$app, $userId);
  97. }
  98. /**
  99. * 获取系统配置信息
  100. * @return array|mixed|\yii\db\ActiveRecord[]
  101. */
  102. public static function getSystemConfig(){
  103. return Config::getFromCache();
  104. }
  105. /**
  106. * 更新系统配置
  107. * @return array|\yii\db\ActiveRecord[]
  108. */
  109. public static function updateSystemConfig(){
  110. return Config::updateToCache();
  111. }
  112. /**
  113. * 获取报单级别
  114. * @return array|mixed|\yii\db\ActiveRecord[]
  115. */
  116. public static function getDecLevelConfig(){
  117. return DeclarationLevel::getFromCache();
  118. }
  119. /**
  120. * 获取国家列表
  121. * @return array|mixed|\yii\db\ActiveRecord[]
  122. */
  123. public static function getCountries(){
  124. return Countries::getFromCache();
  125. }
  126. /**
  127. * 获取国家列表
  128. * @return array|mixed|\yii\db\ActiveRecord[]
  129. */
  130. public static function getLanguages(){
  131. return Language::getFromCache();
  132. }
  133. /**
  134. * 获取货币列表
  135. * @return array|mixed|\yii\db\ActiveRecord[]
  136. */
  137. public static function getCurrencies(){
  138. return Currency::getFromCache();
  139. }
  140. /**
  141. * 更新报单级别
  142. * @return array|\yii\db\ActiveRecord[]
  143. */
  144. public static function updateDecLevelConfig(){
  145. return DeclarationLevel::updateToCache();
  146. }
  147. /**
  148. * 获取报单级别
  149. * @return array|mixed|\yii\db\ActiveRecord[]
  150. */
  151. public static function getDecRoleConfig(){
  152. return DecRole::getFromCache();
  153. }
  154. /**
  155. * 更新报单级别
  156. * @return array|\yii\db\ActiveRecord[]
  157. */
  158. public static function updateDecRoleConfig(){
  159. return DecRole::updateToCache();
  160. }
  161. /**
  162. * 获取报单级别
  163. * @return array|mixed|\yii\db\ActiveRecord[]
  164. */
  165. public static function getDealType(){
  166. return DealType::getFromCache();
  167. }
  168. /**
  169. * 更新报单级别
  170. * @return array|\yii\db\ActiveRecord[]
  171. */
  172. public static function updateDealType(){
  173. return DealType::updateToCache();
  174. }
  175. /**
  176. * 获取地区
  177. * @return array|mixed|\yii\db\ActiveRecord[]
  178. */
  179. public static function getRegionConfig(){
  180. return Region::getFromCache();
  181. }
  182. /**
  183. * 更新地区
  184. * @return array|\yii\db\ActiveRecord[]
  185. */
  186. public static function updateRegionConfig(){
  187. return Region::updateToCache();
  188. }
  189. /**
  190. * 获取聘级
  191. * @return array|mixed|\yii\db\ActiveRecord[]
  192. */
  193. public static function getEmpLevelConfig(){
  194. return EmployLevel::getFromCache();
  195. }
  196. /**
  197. * 获取星级
  198. * @return array|mixed|\yii\db\ActiveRecord[]
  199. */
  200. public static function getStarCrownLevelConfig(){
  201. return StarCrownLevel::getFromCache();
  202. }
  203. /**
  204. * 更新管理员角色
  205. * @return array|\yii\db\ActiveRecord[]
  206. */
  207. public static function updateAdminRole(){
  208. return AdminRole::updateToCache();
  209. }
  210. /**
  211. * 获取管理员角色
  212. * @return array|mixed|\yii\db\ActiveRecord[]
  213. */
  214. public static function getAdminRole(){
  215. return AdminRole::getFromCache();
  216. }
  217. /**
  218. * 更新聘级
  219. * @return array|\yii\db\ActiveRecord[]
  220. */
  221. public static function updateEmpLevelConfig(){
  222. return EmployLevel::updateToCache();
  223. }
  224. /**
  225. * 获取报单级别
  226. * @return array|mixed|\yii\db\ActiveRecord[]
  227. */
  228. public static function getRegType(){
  229. return RegType::getFromCache();
  230. }
  231. /**
  232. * 更新报单级别
  233. * @return array|\yii\db\ActiveRecord[]
  234. */
  235. public static function updateRegType(){
  236. return RegType::updateToCache();
  237. }
  238. /**
  239. * 获取用户基本信息
  240. * @param $userId
  241. * @return array|null|\yii\db\ActiveRecord
  242. */
  243. public static function getUserBaseInfo($userId){
  244. return User::getBaseInfoFromRedis($userId);
  245. }
  246. /**
  247. * 执行异步任务时往缓存中把要传递的参数都存起来,方便在异步方法中直接调用
  248. * @param $params
  249. * @return string
  250. * @throws \yii\base\Exception
  251. */
  252. public static function setAsyncParams($params){
  253. // 生成一个随机key
  254. $taskKey = self::createRandomKey(self::ASYNC_KEY_PREFIX);
  255. Yii::$app->cache->set($taskKey, $params);
  256. return $taskKey;
  257. }
  258. /**
  259. * 获取异步参数
  260. * @param $taskKey
  261. * @return mixed
  262. */
  263. public static function getAsyncParams($taskKey){
  264. $result = Yii::$app->cache->get($taskKey);
  265. Yii::$app->cache->delete($taskKey);
  266. return $result;
  267. }
  268. /**
  269. * 获取异步参数但不删除异步参数缓存
  270. * (用于临时获取一下,但还不影响正式获取后删除的逻辑)
  271. * @param $taskKey
  272. * @return mixed
  273. */
  274. public static function getAsyncParamsWithoutDel($taskKey){
  275. return Yii::$app->cache->get($taskKey);
  276. }
  277. /**
  278. * @param $key
  279. * @return bool
  280. */
  281. public static function deleteAsyncParams($key){
  282. return Yii::$app->cache->delete($key);
  283. }
  284. /**
  285. * 生成一个随机不重复的key
  286. * @param $prefix
  287. * @return string
  288. * @throws \yii\base\Exception
  289. */
  290. public static function createRandomKey($prefix){
  291. $key = Yii::$app->security->generateRandomString(20);
  292. if(Yii::$app->cache->exists($prefix.$key)){
  293. self::createRandomKey($prefix);
  294. }
  295. return $prefix.$key;
  296. }
  297. /**
  298. * 获取短信模板通过报单级别
  299. * @return array|mixed
  300. */
  301. public static function getSmsTemplateByDec(){
  302. return SmsTemplate::getDecFromCache();
  303. }
  304. /**
  305. * 获取短信模板通过聘级
  306. * @return array|mixed
  307. */
  308. public static function getSmsTemplateByEmp(){
  309. return SmsTemplate::getEmpFromCache();
  310. }
  311. /**
  312. * 获取提现等级
  313. * @return array|mixed|\yii\db\ActiveRecord[]
  314. */
  315. public static function getWithdrawLevel(){
  316. return WithdrawLevel::getFromCache();
  317. }
  318. /**
  319. * 获取报单级别
  320. * @return array|mixed|\yii\db\ActiveRecord[]
  321. */
  322. public static function getOcrApiConfig(){
  323. return OcrApi::getFromCache();
  324. }
  325. /**
  326. * 更新报单级别
  327. * @return array|\yii\db\ActiveRecord[]
  328. */
  329. public static function updateOcrApiConfig(){
  330. return OcrApi::updateToCache();
  331. }
  332. /**
  333. * 获取短信接口设置
  334. * @return array|mixed|\yii\db\ActiveRecord[]
  335. */
  336. public static function getSmsApiConfig(){
  337. return SmsApi::getFromCache();
  338. }
  339. /**
  340. * 更新短信接口设置
  341. * @return array|\yii\db\ActiveRecord[]
  342. */
  343. public static function updateSmsApiConfig(){
  344. return SmsApi::updateToCache();
  345. }
  346. /**
  347. * 设置上传token
  348. * @return string
  349. * @throws \yii\base\Exception
  350. */
  351. public static function setUploadToken(){
  352. $token = Yii::$app->security->generateRandomString(32);
  353. Yii::$app->cache->set($token, 1, 5 * 60);
  354. return $token;
  355. }
  356. /**
  357. * 用上传token获取上传资格
  358. * @param $token
  359. * @return mixed
  360. */
  361. public static function getUploadToken($token){
  362. $result = Yii::$app->cache->get($token);
  363. Yii::$app->cache->delete($token);
  364. return $result;
  365. }
  366. /**
  367. * 获取全部安置网络上级
  368. * @param $userId
  369. * @return array|mixed
  370. */
  371. public static function getAllNetworkParents($userId){
  372. return UserNetwork::getAllParentsFromRedis($userId);
  373. }
  374. /**
  375. * 获取全部推荐网络的上级
  376. * @param $userId
  377. * @return array|mixed
  378. */
  379. public static function getAllRelationParents($userId){
  380. return UserRelation::getAllParentsFromRedis($userId);
  381. }
  382. /**
  383. * 设置 当前自动发放奖金的状态
  384. *
  385. *
  386. */
  387. public static function setWithdrawLock($status){
  388. Yii::$app->cache->set('withdrawLock', $status);
  389. }
  390. /**
  391. * 获取 当前自动发放奖金的状态
  392. *
  393. *
  394. */
  395. public static function getWithdrawLock(){
  396. return Yii::$app->cache->get('withdrawLock');
  397. }
  398. }