Cache.php 9.9 KB

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