Cache.php 11 KB

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