Cache.php 8.5 KB

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