Commit b2bb0ad6 authored by gxx's avatar gxx

用户头像编辑

parent d0977de5
......@@ -11,9 +11,11 @@ namespace App\Contracts;
interface UserServiceContract
{
public function getAll();
public function getAll($page);
public function get($id);
public function update($id,$phone,$user_name,$real_name,$gender,$email);
public function update($id,$phone,$user_name,$real_name,$gender,$email,$head_icon);
public function simpleSearch();
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Contracts\UserServiceContract;
use App\Util\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
......@@ -34,18 +35,26 @@ class UserController extends Controller
public function update(Request $request)
{
$id = $request->id;
$user_name = $request->user_name;
$real_name = $request->real_name;
$phone = $request->phone;
$gender = $request->gender;
$email = $request->email;
$result = $this->mUser->update($id,$phone,$user_name,$real_name,$gender,$email);
$head_icon = $request->file('head_icon');
if ($head_icon == null){
$head_icon = null;
$result = $this->mUser->update($id,$phone,$user_name,$real_name,$gender,$email,$head_icon);
}else{
$result = $this->mUser->update($id,$phone,$user_name,$real_name,$gender,$email,$head_icon);
}
if ($result){
return $this->getAll();
return redirect(route('user.getAll'));
}else{
return Message::makeError('保存失败');
return Message::makeError('修改失败');
}
}
......@@ -53,7 +62,7 @@ class UserController extends Controller
public function getAll()
{
$result = $this->mUser->getAll();
$result = $this->mUser->getAll(2);
if ($result){
return view('admin/usertables',compact('result'));
}else{
......
......@@ -11,15 +11,17 @@ namespace App\Services;
use App\Contracts\UserServiceContract;
use App\Models\User;
use App\Util\FileUploader;
use App\Util\UploadPhoto;
class UserService implements UserServiceContract
{
public function getAll()
public function getAll($page)
{
$users = User::paginate(2);
$users = User::paginate($page);
if (count($users ) < 0){
return null;
}
......@@ -39,19 +41,33 @@ class UserService implements UserServiceContract
public function update($id, $phone, $user_name, $real_name, $gender, $email)
public function update($id, $phone, $user_name, $real_name, $gender, $email,$head_icon)
{
$user = User::find($id);
if ($user == null ){
return null;
}
if ($head_icon == null){
$user->phone = $phone;
$user->user_name = $user_name;
$user->real_name = $real_name;
$user->gender = $gender== '女'?'1':'0';
$user->email = $email;
$result = $user->update();
}else{
$user->phone = $phone;
$user->user_name = $user_name;
$user->real_name = $real_name;
$user->gender = $gender== '女'?'1':'0';
$user->email = $email;
$result = $user->update();
$upload = new UploadPhoto();
$imgPath = $upload->upload($head_icon);
$user->phone = $phone;
$user->user_name = $user_name;
$user->real_name = $real_name;
$user->gender = $gender== '女'?'1':'0';
$user->email = $email;
$user->head_icon = $imgPath;
$result = $user->update();
}
if ($result){
return $result;
}else{
......
......@@ -121,4 +121,59 @@ class FileUploader
}
/**
* @return mixed
*/
public function getFileInfo()
{
return $this->fileInfo;
}
// public function update()
// {
//
// // 获取上传文件的大小
// $size = $request->file('head_icon')->getSize();
//
// if ($size > 2*1024*1024){
// return back()->with('上传文件不能超过2M');
// }
// // 文件类型
// $mimeType = $request->file('head_icon')->getMimeType();
// // 扩展文件名
// $ext = $request->file('head_icon')->getClientOriginalExtension();
// // 判断是否通过HTTP POST 上传
// $realPath = $request->file('head_icon')->getRealPath();
// if (!$realPath){
// return back()-with('非法操作');
// }
// $today = date('Y-m-d');
// $dir = storage_path().'\app\public\uploads\\'.$today;
// if (!$dir){
// mkdir($dir);
// }
// // 新文件名
// $newFilename = uniqid().'.'.$ext;
// if (Storage::disk('uploads')->put($today.'/'.$newFilename,file_get_contents($realPath))){
// echo '上传成功';
// }else{
// echo 'error';
// }
// $head_icon = 'storage/'.$today.'/'.$newFilename;
// }
//
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Aladin
* Date: 2017/9/27
* Time: 17:28
*/
namespace App\Util;
use Illuminate\Support\Facades\Storage;
class UploadPhoto
{
public function upload($head_icon)
{
// 获取上传文件的大小
$size = $head_icon->getSize();
if ($size > 2*1024*1024){
return back()->with('上传文件不能超过2M');
}
// 文件类型
$mimeType = $head_icon->getMimeType();
// 扩展文件名
$ext = $head_icon->getClientOriginalExtension();
// 判断是否通过HTTP POST 上传
$realPath = $head_icon->getRealPath();
if (!$realPath){
return '操作非法';
}
$today = date('Y-m-d');
$dir = storage_path().'\app\public\uploads\\'.$today;
if (!$dir){
mkdir($dir);
}
// 新文件名
$newFilename = uniqid().'.'.$ext;
if (Storage::disk('uploads')->put($today.'/'.$newFilename,file_get_contents($realPath))){
$head_icon = 'storage/'.$today.'/'.$newFilename;
return $head_icon;
}else{
return false;
}
}
}
\ No newline at end of file
......@@ -55,6 +55,13 @@ return [
'visibility' => 'public',
],
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
......
......@@ -9,8 +9,19 @@
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{url('user/update')}}">
<form role="form" action="{{url('user/update')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">头像</label>
<p>
<img src="{{asset($result['head_icon'])}}" alt="" width="100px" height="80px" style="border-radius: 80%">
</p>
</div>
<div class="form-group">
<label for="exampleInputEmail1">用户名</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="User_email" name="user_name" value="{{$result['user_name']}}">
......@@ -34,10 +45,8 @@
</div>
<div class="form-group">
<label for="exampleInputFile">头像</label>
<label for="exampleInputFile">更换头像</label>
<input type="file" id="exampleInputFile" name="head_icon" >
<img src="" alt="" id="Preview" style="border-radius: 80%;">
</div>
......@@ -52,73 +61,74 @@
<script src="{{url('js/jquery-3.2.1.min.js')}}"> </script>
<script>
{{--<script>--}}
{{--$("#exampleInputFile").change(function(){--}}
$("#exampleInputFile").change(function(){
{{--var file = this.files[0];--}}
{{--// 判断文件大小--}}
{{--if(file.size > 5*1024*1024){--}}
{{--alert('文件过大!');--}}
{{--}--}}
{{--// 使用FileReader 接口--}}
{{--var reader = new FileReader();--}}
var file = this.files[0];
// 判断文件大小
if(file.size > 5*1024*1024){
alert('文件过大!');
}
// 使用FileReader 接口
var reader = new FileReader();
{{--// 成功读取--}}
{{--reader.onload = function(){--}}
// 成功读取
reader.onload = function(){
{{--// 通过reader.result 属性 来访问生成的 base 64 DataURL--}}
{{--var fileInfo = reader.result;--}}
// 通过reader.result 属性 来访问生成的 base 64 DataURL
var fileInfo = reader.result;
//console.log(fileInfo);
{{--$('#Preview').attr('src',fileInfo).css({width:100,height:90});--}}
$('#Preview').attr('src',fileInfo).css({width:100,height:90});
{{--//console.log(fileInfo);--}}
{{--var name = $('#exampleInputFile').val();--}}
{{--// 分割字符串--}}
{{--var arr = name.split('\\');--}}
{{--var fileName = arr[arr.length-1];--}}
//console.log(fileInfo);
var name = $('#exampleInputFile').val();
// 分割字符串
var arr = name.split('\\');
var fileName = arr[arr.length-1];
{{--base64_uploading(fileInfo,fileName);--}}
{{--}--}}
//base64_uploading(fileInfo,fileName);
}
{{--reader.readAsDataURL(file);--}}
reader.readAsDataURL(file);
{{--})--}}
})
{{--//AJAX 上传base 64--}}
{{--function base64_uploading(base64Data,fileName){--}}
//AJAX 上传base 64
function base64_uploading(base64Data,fileName){
{{--$.ajax({--}}
{{--type: 'post',--}}
{{--url: "{{url('user/update')}}",--}}
{{--data: {--}}
{{--'base64':base64Data,--}}
{{--'name':fileName--}}
{{--},--}}
{{--dataType:"json",--}}
{{--headers: {--}}
{{--'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')--}}
{{--},--}}
{{--success:function(data){--}}
$.ajax({
type: 'post',
url: "{{url('ajaxupload')}}",
data: {
'base64':base64Data,
'name':fileName
},
dataType:"json",
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
},
success:function(data){
{{--console.log(data);--}}
{{--if (data ==1 ){--}}
{{--alert('上传成功')--}}
{{--}else {--}}
{{--alert('上传失败')--}}
{{--}--}}
{{--},--}}
{{--error:function (data) {--}}
console.log(data);
if (data ==1 ){
alert('上传成功')
}else {
alert('上传失败')
}
},
error:function (data) {
console.log(data);
}
})
}
{{--alert(1);--}}
{{--console.log(data);--}}
{{--}--}}
{{--})--}}
{{--}--}}
</script>
{{--</script>--}}
@endsection
\ No newline at end of file
......@@ -23,7 +23,7 @@
<div class="box-body box-profile">
<img class="profile-user-img img-responsive img-circle" src="{{asset('/bower_components/AdminLTE/dist/img/user4-128x128.jpg')}}" alt="User profile picture">
<img class="profile-user-img img-responsive img-circle" src="{{asset($result['head_icon'] == ''?:$result['head_icon'])}}" alt="User profile picture">
<h3 class="profile-username text-center">{{$result['user_name']}}</h3>
......
......@@ -39,6 +39,9 @@
<th>操作</th>
</tr>
</thead>
@foreach($result as $value)
<tbody>
<tr>
......@@ -46,9 +49,9 @@
<td>{{$value['phone']}}</td>
<td>{{$value['user_name']}}</td>
<td>{{$value['real_name']}}</td>
<td>{{$value['head_icon']}}</td>
<td><img src="{{asset($value['head_icon'] ==''?'':$value['head_icon'])}}" alt="" width="120px" height="50px" > </td>
<td> {{$value['gender'] == 0 ?'男':'女'}}</td>
<td>{{$value['eamil']}}</td>
<td>{{$value['email']}}</td>
<td>{{$value['status']}}</td>
<td><a href="{{url('user/get')}}/{{$value['id']}}" class="btn btn-info"> 查看详情 </a>
<a href="{{url('user/updatePage')}}/{{$value['id']}}" class="btn btn-info"> 编辑 </a>
......@@ -57,7 +60,7 @@
</tr>
</tbody>
@endforeach
@endforeach
</table>
{{ $result->links() }}
......
......@@ -42,7 +42,7 @@ Route::group(['prefix'=> 'user'],function (){
Route::get('getAll','UserController@getAll')->name('user.getAll');
Route::get('get/{id}','UserController@get')->name('user.get');
Route::get('updatePage/{id}','UserController@updatePage')->name('user.updatePage');
Route::get('update','UserController@update')->name('user.update');
Route::post('update','UserController@update')->name('user.update');
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment