Cache.php 8.6 KB

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