Cache.php 9.7 KB

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