RedisTrait.php
10.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace App\Traits;
use App\Enums\Common\Common;
use App\Enums\Common\RedisKey;
use App\Utils\RedisUtils;
use Illuminate\Support\Facades\Redis;
/**
* @author:wlj
* @date: 2022/5/30 17:08
*/
trait RedisTrait
{
public $login_expire_time;
public $redis;
public function __construct()
{
$this->login_expire_time = config('app.login_expire_time');
$this->redis = RedisUtils::getClient();
}
/**
* @notes: 获取redis
* @return Redis
* @author:wlj
* @date: 2022/5/30 17:16
*/
public function isLock($key): bool
{
return (bool)$this->redis->get($key);
}
/**
* @notes: 加锁
* @param $key
* @param int $time 秒
* @return bool
* @author:wlj
* @date: 2022/5/30 17:22
*/
public function lock($key, $time = 600): bool
{
return $this->redis->setnx($key, 1) && $this->redis->expire($key, $time);
}
/**
* @notes: 解锁
* @return bool
* @author:wlj
* @date: 2022/5/30 17:22
*/
public function unLock($key): bool
{
return $this->redis->del($key);
}
public function blpop($key, $bltime = 10)
{
return $this->redis->blPop($key, $bltime);
}
/**
* @notes: 获取位图数据
* @return Redis
* @author:wlj
* @date: 2022/5/30 17:16
*/
public function getBit($key, $offset): bool
{
return (bool)$this->redis->getBit($key, $offset);
}
public function setBit($key, $offset, $value): bool
{
return (bool)$this->redis->setBit($key, $offset, $value);
}
/**
* @notes: 获取为1的数量
* @param $key
* @param null $start
* @param null $end
* @return int
* @author:wlj
* @date: 2022/6/9 18:29
*/
public function bitCount($key, $start = null, $end = null)
{
if ($start == null || $end == null) {
return $this->redis->bitCount($key);
}
return $this->redis->bitCount($key, $start, $end);
}
public function setData($key, $value, $timeout = null)
{
return $this->redis->set($key, $value, $timeout);
}
public function getData($key)
{
return $this->redis->get($key);
}
public function delData($key)
{
return $this->redis->del($key);
}
public function hGet($key, $hashKey)
{
return $this->redis->hGet($key, $hashKey);
}
public function hGetAll($key)
{
return $this->redis->hGetAll($key);
}
public function sadd($key, ...$value1)
{
return $this->redis->sadd($key, ...$value1);
}
public function smembers($key)
{
return $this->redis->sMembers($key);
}
public function sRem($key, ...$member1)
{
return $this->redis->sRem($key, ...$member1);
}
public function hSet($key, $hashKey, $value)
{
return $this->redis->hSet($key, $hashKey, $value);
}
public function hDel($key, $hashKey)
{
return $this->redis->hDel($key, $hashKey);
}
#region 登录相关
/**
* @notes: 用户登录设置
* @param array $userInfo
* @param $token
* @author:wlj
* @date: 2022/6/14 16:34
*/
public function loginset(array $userInfo, $token)
{
$this->setData(RedisKey::USER_LOGIN . $token, json_encode($userInfo, true), $this->login_expire_time);
$this->redis->sAdd(RedisKey::USER_TOKEN . $userInfo['account_id'], $token);
// $this->setData(RedisKey::USER_TOKEN . $userInfo['account_id'], $token, $this->login_expire_time);
$this->hSet(RedisKey::USER_LAST_LOGIN_COMPANY, $userInfo['account_id'], $userInfo['company_id']);
return true;
}
/**
* @notes: 获取登录信息
* @param $token
* @return mixed
* @author:wlj
* @date: 2022/7/8 17:15
*/
public function getloginset($token)
{
$singleSign = config('app.single_sign');
$userInfo = json_decode($this->getData(RedisKey::USER_LOGIN . $token), true);
$hasToken = false;
if ($userInfo) {
$hasToken = $this->redis->sIsMember(RedisKey::USER_TOKEN . $userInfo['account_id'], $token);
}
//单点登录
// if ($singleSign) {
// $accountId = $userInfo['account_id'] ?? 0;
// $tokenOri = $this->getData(RedisKey::USER_TOKEN . $accountId);
// if ($userInfo && ($token == $tokenOri)) {
// return $userInfo;
// } else {
// return [];
// }
// }
// else {
// return empty($userInfo) ? [] : $userInfo;
// }
return (!empty($userInfo) && $hasToken) ? $userInfo : [];
}
/**
* @notes: 用户退出登录设置
* @param $token
* @author:wlj
* @date: 2022/6/14 16:34
*/
public function logoutset($token)
{
$singleSign = config('app.single_sign');
$userInfo = json_decode($this->getData(RedisKey::USER_LOGIN . $token), true);
//清除redis tokren对应 user_info数据
$this->redis->del(RedisKey::USER_LOGIN . $token);
if ($userInfo) {
//清除redis token集合中的数据
$this->redis->sRem(RedisKey::USER_TOKEN . $userInfo['account_id'], $token);
//退出登录时 删除公司权限
$this->redis->hDel(RedisKey::USER_PERMISSION, $userInfo['id']);
}
// if ($singleSign && $userInfo) {
// $userInfo = json_decode($userInfo, true);
// $this->redis->del(RedisKey::USER_TOKEN . $userInfo['account_id']);
// }
return true;
}
/**
* @notes: 用户退出登录设置--通过accountId
* @param $accountId
* @return bool
* @author:wlj
* @date: 2023/3/2 17:08
*/
public function logoutsetByaccountId($accountId)
{
$tokens = $this->redis->sMembers(RedisKey::USER_TOKEN . $accountId);
$this->delData(RedisKey::USER_TOKEN . $accountId);
foreach ($tokens as $token) {
$this->redis->del(RedisKey::USER_LOGIN . $token);
}
return true;
}
#endregion 登录相关
/**
* 设置用户权限
* @param $userId
* @param array $permissions
* @return bool|int
*/
public function setUserPermission($userId, $permissions = [])
{
return $this->hSet(RedisKey::USER_PERMISSION, $userId, json_encode($permissions));
}
/**
* 获取用户权限
* @param $userId
* @return array
*/
public function getUserPermission($userId): array
{
$permissions = $this->hGet(RedisKey::USER_PERMISSION, $userId);
return empty($permissions) ? [] : json_decode($permissions, true);
}
/**
* 删除用户公司权限
* @param $userId
*/
public function deleteUserPermission($userId)
{
return $this->hDel(RedisKey::USER_PERMISSION, $userId);
}
/**
* 设置项目成员权限
* @param $userId
* @param array $permissions
* @return bool|int
*/
public function setProjectMemberPermission($memberId, $permissions = [])
{
return $this->hSet(RedisKey::MEMBER_PROJECT_PERMISSION, $memberId, json_encode($permissions));
}
/**
* 获取项目成员权限
* @param $userId
* @return array
*/
public function getProjectMemberPermission($memberId): array
{
$permissions = $this->hGet(RedisKey::MEMBER_PROJECT_PERMISSION, $memberId);
return empty($permissions) ? [] : json_decode($permissions, true);
}
/**
* 删除项目员工权限
* @param $memberId
*/
public function deleteProjectMemberPermission($memberId)
{
return $this->hDel(RedisKey::MEMBER_PROJECT_PERMISSION, $memberId);
}
/**
* @notes: 删除数据
* @param $key
* @return int
* @author:wlj
* @date: 2022/10/31 15:58
*/
public function deleteKey($key)
{
return $this->redis->del($key);
}
/**
* @notes: 获取用户的fd 空为未上线 无需推送
* @param $token
* @return bool|mixed|string
* @author:wlj
* @date: 2022/10/31 15:59
*/
public function getUserFd($token)
{
return $this->getData(RedisKey::USER_FD_DATA . $token);
}
/**
* @notes: 给用户们添加新的未读的需求
* @param array $userIds
* @param int $storyId
* @author:wlj
* @date: 2022/11/2 16:00
*/
public function addUserAbeyanceStory($userIds = [], $storyId = 0)
{
foreach ($userIds as $userId) {
$this->hSet(RedisKey::USER_ABEYANCE_STORY . $userId, $storyId, Common::YES);
}
return true;
}
public function readUserAbeyanceStory($userIds = [], $storyId = 0)
{
foreach ($userIds as $userId) {
$this->hDel(RedisKey::USER_ABEYANCE_STORY . $userId, $storyId);
}
return true;
}
public function getKeys($patten)
{
return $this->redis->keys($patten);
}
/**
* @notes: 插入用户最近查看数据 默认10条 多的会trim掉
* @author:wlj
* @date: 2023/2/20 16:57
*/
public function setUserRecentActions($companId, $userId, $actionableType, $actionableId)
{
$user_recent_limit = config('app.user_recent_limit');
$start = 0;
$stop = -$user_recent_limit - 1;
$this->redis->zAdd(RedisKey::USER_RECENT_ACTIONS . $companId . '_' . $userId . '_' . $actionableType, [], time(), $actionableId);
$this->redis->zRemRangeByRank(RedisKey::USER_RECENT_ACTIONS . $companId . '_' . $userId . '_' . $actionableType, $start, $stop);
return true;
}
/**
* @notes: 获取用户最近查看数据 默认10条
* @param $companId
* @param $userId
* @param $actionableType
* @return array
* @author:wlj
* @date: 2023/2/21 10:05
*/
public function getUserRecentActions($companId, $userId, $actionableType)
{
return $this->redis->zRevRangeByScore(RedisKey::USER_RECENT_ACTIONS . $companId . '_' . $userId . '_' . $actionableType, +INF, -INF, ['withscores' => true]);
}
}