Cache.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\OcrApi;
  12. use common\models\DeclarationLevel;
  13. use common\models\EmployLevel;
  14. use common\models\Region;
  15. use common\models\SmsApi;
  16. use common\models\SmsTemplate;
  17. use common\models\StarCrownLevel;
  18. use common\models\User;
  19. use common\models\UserNetwork;
  20. use common\models\UserRelation;
  21. use common\models\WithdrawLevel;
  22. use Yii;
  23. use backendApi\modules\v1\models\Admin;
  24. use common\models\UserInfo;
  25. class Cache
  26. {
  27. const SYSTEM_CONFIG_KEY = 'sys:config';
  28. const DEC_LEVEL_CONFIG_KEY = 'sys:decLevel';
  29. const DEC_ROLE_CONFIG_KEY = 'sys:decRole';
  30. const EMP_LEVEL_CONFIG_KEY = 'sys:empLevel';
  31. const CROWN_LEVEL_CONFIG_KEY = 'sys:crownLevel';
  32. const ASYNC_KEY_PREFIX = 'async_';
  33. const SOCKET_KEY_PREFIX = 'socket_';
  34. const SOCKET_ADMIN = 'admin';
  35. const SOCKET_USER_PC = 'userPc';
  36. const SOCKET_USER_APP = 'userAPP';
  37. const USER_CLOSE_KEY_PREFIX = 'user:close_';
  38. const USER_INFO_KEY = 'user:baseInfo';
  39. const USER_NETWORK_PARENTS = 'user:networkParents';
  40. const USER_RELATION_PARENTS = 'user:relationParents';
  41. const USER_CREATED_AT_LIST = 'user:createdAtList_';
  42. /**
  43. * WebSocket的fd加入缓存
  44. * @param $app
  45. * @param $userId
  46. * @param $fd
  47. * @return mixed
  48. */
  49. public static function setWebSocketFd($app, $userId, $fd){
  50. if(isset($userId) && $userId != ''){
  51. if($app == self::SOCKET_ADMIN){
  52. $model = Admin::class;
  53. $field = 'ID';
  54. } elseif($app == self::SOCKET_USER_PC) {
  55. $model = UserInfo::class;
  56. $field = 'USER_ID';
  57. $app = self::SOCKET_USER_PC;
  58. } else {
  59. $model = UserInfo::class;
  60. $field = 'USER_ID';
  61. $app = self::SOCKET_USER_APP;
  62. }
  63. if($model::find()->where($field.'=:'.$field, [':'.$field=>$userId])->exists()){
  64. Yii::$app->redis->hset(self::SOCKET_KEY_PREFIX.$app, $userId, $fd);
  65. }
  66. }
  67. return $fd;
  68. }
  69. /**
  70. * 获取会员的WebSocket的fd
  71. * @param $app
  72. * @param $userId
  73. * @return mixed
  74. */
  75. public static function getWebSocketFd($app, $userId){
  76. return Yii::$app->redis->hget(self::SOCKET_KEY_PREFIX.$app, $userId);
  77. }
  78. /**
  79. * 获取系统配置信息
  80. * @return array|mixed|\yii\db\ActiveRecord[]
  81. */
  82. public static function getSystemConfig(){
  83. return Config::getFromCache();
  84. }
  85. /**
  86. * 更新系统配置
  87. * @return array|\yii\db\ActiveRecord[]
  88. */
  89. public static function updateSystemConfig(){
  90. return Config::updateToCache();
  91. }
  92. /**
  93. * 获取报单级别
  94. * @return array|mixed|\yii\db\ActiveRecord[]
  95. */
  96. public static function getDecLevelConfig(){
  97. return DeclarationLevel::getFromCache();
  98. }
  99. /**
  100. * 更新报单级别
  101. * @return array|\yii\db\ActiveRecord[]
  102. */
  103. public static function updateDecLevelConfig(){
  104. return DeclarationLevel::updateToCache();
  105. }
  106. /**
  107. * 获取地区
  108. * @return array|mixed|\yii\db\ActiveRecord[]
  109. */
  110. public static function getRegionConfig(){
  111. return Region::getFromCache();
  112. }
  113. /**
  114. * 更新地区
  115. * @return array|\yii\db\ActiveRecord[]
  116. */
  117. public static function updateRegionConfig(){
  118. return Region::updateToCache();
  119. }
  120. /**
  121. * 获取聘级
  122. * @return array|mixed|\yii\db\ActiveRecord[]
  123. */
  124. public static function getEmpLevelConfig(){
  125. return EmployLevel::getFromCache();
  126. }
  127. /**
  128. * 获取星级
  129. * @return array|mixed|\yii\db\ActiveRecord[]
  130. */
  131. public static function getStarCrownLevelConfig(){
  132. return StarCrownLevel::getFromCache();
  133. }
  134. /**
  135. * 更新聘级
  136. * @return array|\yii\db\ActiveRecord[]
  137. */
  138. public static function updateEmpLevelConfig(){
  139. return EmployLevel::updateToCache();
  140. }
  141. /**
  142. * 获取用户基本信息
  143. * @param $userId
  144. * @return array|null|\yii\db\ActiveRecord
  145. */
  146. public static function getUserBaseInfo($userId){
  147. return User::getBaseInfoFromRedis($userId);
  148. }
  149. /**
  150. * 执行异步任务时往缓存中把要传递的参数都存起来,方便在异步方法中直接调用
  151. * @param $params
  152. * @return string
  153. * @throws \yii\base\Exception
  154. */
  155. public static function setAsyncParams($params){
  156. // 生成一个随机key
  157. $taskKey = self::createRandomKey(self::ASYNC_KEY_PREFIX);
  158. Yii::$app->cache->set($taskKey, $params);
  159. return $taskKey;
  160. }
  161. /**
  162. * 获取异步参数
  163. * @param $taskKey
  164. * @return mixed
  165. */
  166. public static function getAsyncParams($taskKey){
  167. $result = Yii::$app->cache->get($taskKey);
  168. Yii::$app->cache->delete($taskKey);
  169. return $result;
  170. }
  171. /**
  172. * 获取异步参数但不删除异步参数缓存
  173. * (用于临时获取一下,但还不影响正式获取后删除的逻辑)
  174. * @param $taskKey
  175. * @return mixed
  176. */
  177. public static function getAsyncParamsWithoutDel($taskKey){
  178. return Yii::$app->cache->get($taskKey);
  179. }
  180. /**
  181. * @param $key
  182. * @return bool
  183. */
  184. public static function deleteAsyncParams($key){
  185. return Yii::$app->cache->delete($key);
  186. }
  187. /**
  188. * 生成一个随机不重复的key
  189. * @param $prefix
  190. * @return string
  191. * @throws \yii\base\Exception
  192. */
  193. public static function createRandomKey($prefix){
  194. $key = Yii::$app->security->generateRandomString(20);
  195. if(Yii::$app->cache->exists($prefix.$key)){
  196. self::createRandomKey($prefix);
  197. }
  198. return $prefix.$key;
  199. }
  200. /**
  201. * 获取全部安置网络上级
  202. * @param $userId
  203. * @return array|mixed
  204. */
  205. public static function getAllNetworkParents($userId){
  206. return UserNetwork::getAllParentsFromRedis($userId);
  207. }
  208. /**
  209. * 获取全部推荐网络的上级
  210. * @param $userId
  211. * @return array|mixed
  212. */
  213. public static function getAllRelationParents($userId){
  214. return UserRelation::getAllParentsFromRedis($userId);
  215. }
  216. }