Cache.php 9.8 KB

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