Cache.php 10 KB

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