Cache.php 12 KB

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