Cache.php 9.0 KB

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