Cache.php 9.2 KB

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