作者 Karson

新增会员首字母头像功能

新增文本框挺拽上传和粘贴上传
优化注册和登录的跳转
修复后台默认皮肤保存失效的BUG
... ... @@ -13,8 +13,9 @@ if (!function_exists('__')) {
*/
function __($name, $vars = [], $lang = '')
{
if (is_numeric($name) || !$name)
if (is_numeric($name) || !$name) {
return $name;
}
if (!is_array($vars)) {
$vars = func_get_args();
array_shift($vars);
... ... @@ -22,7 +23,6 @@ if (!function_exists('__')) {
}
return \think\Lang::get($name, $vars, $lang);
}
}
if (!function_exists('format_bytes')) {
... ... @@ -36,11 +36,11 @@ if (!function_exists('format_bytes')) {
function format_bytes($size, $delimiter = '')
{
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for ($i = 0; $size >= 1024 && $i < 6; $i++)
for ($i = 0; $size >= 1024 && $i < 6; $i++) {
$size /= 1024;
}
return round($size, 2) . $delimiter . $units[$i];
}
}
if (!function_exists('datetime')) {
... ... @@ -56,7 +56,6 @@ if (!function_exists('datetime')) {
$time = is_numeric($time) ? $time : strtotime($time);
return date($format, $time);
}
}
if (!function_exists('human_date')) {
... ... @@ -71,7 +70,6 @@ if (!function_exists('human_date')) {
{
return \fast\Date::human($time, $local);
}
}
if (!function_exists('cdnurl')) {
... ... @@ -84,14 +82,14 @@ if (!function_exists('cdnurl')) {
*/
function cdnurl($url, $domain = false)
{
$url = preg_match("/^https?:\/\/(.*)/i", $url) ? $url : \think\Config::get('upload.cdnurl') . $url;
if ($domain && !preg_match("/^(http:\/\/|https:\/\/)/i", $url)) {
$regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
$url = preg_match($regex, $url) ? $url : \think\Config::get('upload.cdnurl') . $url;
if ($domain && !preg_match($regex, $url)) {
$domain = is_bool($domain) ? request()->domain() : $domain;
$url = $domain . $url;
}
return $url;
}
}
... ... @@ -109,20 +107,19 @@ if (!function_exists('is_really_writable')) {
}
if (is_dir($file)) {
$file = rtrim($file, '/') . '/' . md5(mt_rand());
if (($fp = @fopen($file, 'ab')) === FALSE) {
return FALSE;
if (($fp = @fopen($file, 'ab')) === false) {
return false;
}
fclose($fp);
@chmod($file, 0777);
@unlink($file);
return TRUE;
} elseif (!is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE) {
return FALSE;
return true;
} elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
return false;
}
fclose($fp);
return TRUE;
return true;
}
}
if (!function_exists('rmdirs')) {
... ... @@ -135,10 +132,12 @@ if (!function_exists('rmdirs')) {
*/
function rmdirs($dirname, $withself = true)
{
if (!is_dir($dirname))
if (!is_dir($dirname)) {
return false;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
... ... @@ -150,7 +149,6 @@ if (!function_exists('rmdirs')) {
}
return true;
}
}
if (!function_exists('copydirs')) {
... ... @@ -167,7 +165,9 @@ if (!function_exists('copydirs')) {
}
foreach (
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
) as $item
) {
if ($item->isDir()) {
$sontDir = $dest . DS . $iterator->getSubPathName();
... ... @@ -179,16 +179,13 @@ if (!function_exists('copydirs')) {
}
}
}
}
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($string)
{
return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
}
}
if (!function_exists('addtion')) {
... ... @@ -201,8 +198,9 @@ if (!function_exists('addtion')) {
*/
function addtion($items, $fields)
{
if (!$items || !$fields)
if (!$items || !$fields) {
return $items;
}
$fieldsArr = [];
if (!is_array($fields)) {
$arr = explode(',', $fields);
... ... @@ -260,7 +258,6 @@ if (!function_exists('addtion')) {
}
return $items;
}
}
if (!function_exists('var_export_short')) {
... ... @@ -288,8 +285,80 @@ if (!function_exists('var_export_short')) {
case "boolean":
return $var ? "TRUE" : "FALSE";
default:
return var_export($var, TRUE);
return var_export($var, true);
}
}
}
if (!function_exists('letter_avatar')) {
/**
* 首字母头像
* @param $text
* @return string
*/
function letter_avatar($text)
{
$total = unpack('L', hash('adler32', $text, true))[1];
$hue = $total % 360;
list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
$bg = "rgb({$r},{$g},{$b})";
$color = "#ffffff";
$first = mb_strtoupper(mb_substr($text, 0, 1));
$src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" fill="' . $color . '" text-anchor="middle" alignment-baseline="central">' . $first . '</text></svg>');
$value = 'data:image/svg+xml;base64,' . $src;
return $value;
}
}
if (!function_exists('hsv2rgb')) {
function hsv2rgb($h, $s, $v)
{
$r = $g = $b = 0;
$i = floor($h * 6);
$f = $h * 6 - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i % 6) {
case 0:
$r = $v;
$g = $t;
$b = $p;
break;
case 1:
$r = $q;
$g = $v;
$b = $p;
break;
case 2:
$r = $p;
$g = $v;
$b = $t;
break;
case 3:
$r = $p;
$g = $q;
$b = $v;
break;
case 4:
$r = $t;
$g = $p;
$b = $v;
break;
case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
return [
floor($r * 255),
floor($g * 255),
floor($b * 255)
];
}
}
... ...
... ... @@ -39,7 +39,12 @@ class User extends Model
*/
public function getAvatarAttr($value, $data)
{
return $value ? $value : '/assets/img/avatar.png';
if (!$value) {
//如果不需要启用首字母头像,请使用
//$value = '/assets/img/avatar.png';
$value = letter_avatar($data['nickname']);
}
return $value;
}
/**
... ...
... ... @@ -27,11 +27,6 @@ class User extends Frontend
$this->error(__('User center already closed'));
}
$ucenter = get_addon_info('ucenter');
if ($ucenter && $ucenter['state']) {
include ADDON_PATH . 'ucenter' . DS . 'uc.php';
}
//监听注册登录注销的事件
Hook::add('user_login_successed', function ($user) use ($auth) {
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
... ... @@ -82,7 +77,7 @@ class User extends Frontend
{
$url = $this->request->request('url');
if ($this->auth->id) {
$this->success(__('You\'ve logged in, do not login again'), $url);
$this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
}
if ($this->request->isPost()) {
$username = $this->request->post('username');
... ... @@ -124,13 +119,7 @@ class User extends Frontend
$this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
}
if ($this->auth->register($username, $password, $email, $mobile)) {
$synchtml = '';
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS) {
$uc = new \addons\ucenter\library\client\Client();
$synchtml = $uc->uc_user_synregister($this->auth->id, $password);
}
$this->success(__('Sign up successful') . $synchtml, $url ? $url : url('user/index'));
$this->success(__('Sign up successful'), $url ? $url : url('user/index'));
} else {
$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
}
... ... @@ -153,7 +142,7 @@ class User extends Frontend
{
$url = $this->request->request('url');
if ($this->auth->id) {
$this->success(__('You\'ve logged in, do not login again'), $url);
$this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
}
if ($this->request->isPost()) {
$account = $this->request->post('account');
... ... @@ -184,13 +173,7 @@ class User extends Frontend
return false;
}
if ($this->auth->login($account, $password)) {
$synchtml = '';
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS) {
$uc = new \addons\ucenter\library\client\Client();
$synchtml = $uc->uc_user_synlogin($this->auth->id);
}
$this->success(__('Logged in successful') . $synchtml, $url ? $url : url('user/index'));
$this->success(__('Logged in successful'), $url ? $url : url('user/index'));
} else {
$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
}
... ... @@ -213,13 +196,7 @@ class User extends Frontend
{
//注销本站
$this->auth->logout();
$synchtml = '';
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS) {
$uc = new \addons\ucenter\library\client\Client();
$synchtml = $uc->uc_user_synlogout();
}
$this->success(__('Logout successful') . $synchtml, url('user/index'));
$this->success(__('Logout successful'), url('user/index'));
}
/**
... ... @@ -270,13 +247,7 @@ class User extends Frontend
$ret = $this->auth->changepwd($newpassword, $oldpassword);
if ($ret) {
$synchtml = '';
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS) {
$uc = new \addons\ucenter\library\client\Client();
$synchtml = $uc->uc_user_synlogout();
}
$this->success(__('Reset password successful') . $synchtml, url('user/login'));
$this->success(__('Reset password successful'), url('user/login'));
} else {
$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
}
... ...
... ... @@ -354,7 +354,7 @@ define(['jquery', 'bootstrap', 'backend', 'addtabs', 'adminlte', 'form'], functi
function setup() {
var tmp = localStorage.getItem('skin');
if (tmp && $.inArray(tmp, my_skins))
if (tmp && $.inArray(tmp, my_skins) != -1)
change_skin(tmp);
// 皮肤切换
... ...
... ... @@ -95,8 +95,14 @@ define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang'], function ($, undefine
return url;
},
//获取修复后可访问的cdn链接
cdnurl: function (url) {
return /^(?:[a-z]+:)?\/\//i.test(url) ? url : Config.upload.cdnurl + url;
cdnurl: function (url, domain) {
var rule = new RegExp("^((?:[a-z]+:)?\\/\\/|data:image\\/)", "i");
var url = rule.test(url) ? url : Config.upload.cdnurl + url;
if (domain && !rule.test(url)) {
domain = typeof domain === 'string' ? domain : location.origin;
url = domain + url;
}
return url;
},
//查询Url参数
query: function (name, url) {
... ...
... ... @@ -294,6 +294,52 @@ define(['jquery', 'bootstrap', 'plupload', 'template'], function ($, undefined,
$("#" + preview_id).trigger("fa.preview.change");
});
}
if (input_id) {
//粘贴上传
$("body").on('paste', "#" + input_id, function (event) {
var that = this;
var image, pasteEvent;
pasteEvent = event.originalEvent;
if (pasteEvent.clipboardData && pasteEvent.clipboardData.items) {
image = Upload.api.getImageFromClipboard(pasteEvent);
if (image) {
event.preventDefault();
var button = $(".plupload[data-input-id='" + $(that).attr("id") + "']");
Upload.api.send(image, function (data) {
var urlArr = [];
if (button && button.data("multiple") && $(that).val() !== '') {
urlArr.push($(that).val());
}
urlArr.push(data.url);
$(that).val(urlArr.join(",")).trigger("change");
});
}
}
});
//拖拽上传
$("body").on('drop', "#" + input_id, function (event) {
var that = this;
var images, pasteEvent;
pasteEvent = event.originalEvent;
if (pasteEvent.dataTransfer && pasteEvent.dataTransfer.files) {
images = Upload.api.getImageFromDrop(pasteEvent);
if (images.length > 0) {
event.preventDefault();
var button = $(".plupload[data-input-id='" + $(that).attr("id") + "']");
$.each(images, function (i, image) {
Upload.api.send(image, function (data) {
var urlArr = [];
if (button && button.data("multiple") && $(that).val() !== '') {
urlArr.push($(that).val());
}
urlArr.push(data.url);
$(that).val(urlArr.join(",")).trigger("change");
});
});
}
}
});
}
Upload.list[id].init();
});
},
... ... @@ -321,8 +367,34 @@ define(['jquery', 'bootstrap', 'plupload', 'template'], function ($, undefined,
alert("Custom Callback,Response URL:" + response.url);
},
},
getImageFromClipboard: function (data) {
var i, item;
i = 0;
while (i < data.clipboardData.items.length) {
item = data.clipboardData.items[i];
if (item.type.indexOf("image") !== -1) {
return item.getAsFile() || false;
}
};
i++;
}
return false;
},
getImageFromDrop: function (data) {
var i, item, images;
i = 0;
images = [];
while (i < data.dataTransfer.files.length) {
item = data.dataTransfer.files[i];
if (item.type.indexOf("image") !== -1) {
images.push(item);
}
i++;
}
return images;
}
}
}
;
return Upload;
});
\ No newline at end of file
... ...