-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserTrait.php
72 lines (66 loc) · 1.7 KB
/
UserTrait.php
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
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\user;
use Yii;
use yii\helpers\FileHelper;
/**
* Class UserTrait
* @package yuncms\user
*/
trait UserTrait
{
/**
* 获取模块配置
* @param string $key
* @param null $default
* @return bool|mixed|string
*/
public function getSetting($key, $default = null)
{
$value = Yii::$app->settings->get($key, 'user', $default);
if ($key == 'avatarUrl' || $key == 'avatarPath') {
return Yii::getAlias($value);
}
return $value;
}
/**
* 获取头像的存储路径
* @param int $userId
* @return string
*/
public function getAvatarPath($userId)
{
$avatarPath = $this->getSetting('avatarPath') . '/' . $this->getAvatarSubPath($userId);
if (!is_dir($avatarPath)) {
FileHelper::createDirectory($avatarPath);
}
return $avatarPath . substr($userId, -2);
}
/**
* 获取指定用户头像访问Url
* @param int $userId 用户ID
* @return string
*/
public function getAvatarUrl($userId)
{
return $this->getSetting('avatarUrl') . '/' . $this->getAvatarSubPath($userId) . substr($userId, -2);
}
/**
* 计算用户头像子路径
*
* @param int $userId 用户ID
* @return string
*/
public function getAvatarSubPath($userId)
{
$id = sprintf("%09d", $userId);
$dir1 = substr($id, 0, 3);
$dir2 = substr($id, 3, 2);
$dir3 = substr($id, 5, 2);
return $dir1 . '/' . $dir2 . '/' . $dir3 . '/';
}
}