From 56fcda9ea84ce5cb7a6ba1d2f8f71ffb68f3cd90 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9E=97=E4=B8=80=E5=B3=B0?= <1feng.0595@gmail.com>
Date: Mon, 28 May 2018 00:27:27 +0800
Subject: [PATCH] image upload
---
app/Http/Controllers/Api/ImageController.php | 130 ++----------------
app/Http/Controllers/Api/UserController.php | 2 +-
app/Http/Logic/UserLogic.php | 2 +
app/Http/Requests/UserRequest.php | 2 +-
.../views/weixin/user/userinfo.blade.php | 2 +-
5 files changed, 15 insertions(+), 123 deletions(-)
diff --git a/app/Http/Controllers/Api/ImageController.php b/app/Http/Controllers/Api/ImageController.php
index 7ee42a7..258f07f 100644
--- a/app/Http/Controllers/Api/ImageController.php
+++ b/app/Http/Controllers/Api/ImageController.php
@@ -16,57 +16,6 @@ class ImageController extends CommonController
$this->path = '/uploads/'.date('Y/m',time());
}
- //单文件/图片上传,成功返回路径,不含域名
- public function imageUpload(Request $request)
- {
- $file = $_FILES['file'];//得到传输的数据
-
- $type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
-
- $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
- $uploads_path = $this->path; //存储路径
-
- $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
-
- //判断文件类型是否被允许上传
- if(!in_array($type, $allow_type))
- {
- //如果不被允许,则直接停止程序运行
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
- }
-
- //判断是否是通过HTTP POST上传的
- if(!is_uploaded_file($file['tmp_name']))
- {
- //如果不是通过HTTP POST上传的
- return ReturnData::create(ReturnData::SYSTEM_FAIL);
- }
-
- //文件小于1M
- if ($file["size"] < 2048000)
- {
- if ($file["error"] > 0)
- {
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]);
- }
- else
- {
- if(!file_exists(base_path('public').$uploads_path))
- {
- Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
- }
-
- move_uploaded_file($file["tmp_name"], base_path('public').$image_path);
- }
- }
- else
- {
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
- }
-
- return ReturnData::create(ReturnData::SUCCESS,$image_path);
- }
-
//阿里云OSS图片上传
public function ossImageUpload()
{
@@ -82,84 +31,25 @@ class ImageController extends CommonController
/**
* 多文件上传,成功返回路径,不含域名
- * 多文件上传格式:
- *
- *
- *
- */
- public function multipleImageUpload(Request $request)
- {
- $res = [];
- $file = $_FILES['file'];//得到传输的数据
-
- if($file)
- {
- foreach($file['name'] as $key=>$value)
- {
- $type = strtolower(substr(strrchr($file["name"][$key], '.'), 1)); //文件后缀
-
- $image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type;
- $uploads_path = $this->path; //存储路径
-
- $allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
-
- //判断文件类型是否被允许上传
- if(!in_array($type, $allow_type))
- {
- //如果不被允许,则直接停止程序运行
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确');
- }
-
- //判断是否是通过HTTP POST上传的
- if(!is_uploaded_file($file['tmp_name'][$key]))
- {
- //如果不是通过HTTP POST上传的
- return ReturnData::create(ReturnData::SYSTEM_FAIL);
- }
-
- //文件小于2M
- if ($file["size"][$key] < 2048000)
- {
- if ($file["error"][$key] > 0)
- {
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"][$key]);
- }
- else
- {
- if(!file_exists(base_path('public').$uploads_path))
- {
- Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
- }
-
- move_uploaded_file($file["tmp_name"][$key], base_path('public').$image_path);
- }
- }
- else
- {
- return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M');
- }
-
- $res[] = $image_path;
- }
- }
-
- return ReturnData::create(ReturnData::SUCCESS,$res);
- }
-
- /**
- * 多文件上传,成功返回路径,不含域名
- * 多文件上传格式:
+ * 格式1:
*
*
*
+ * 格式2:
+ *
+ *
+ *
*/
- public function multipleFileUpload(Request $request)
+ public function imageUpload(Request $request)
{
$res = [];
$files = $_FILES;//得到传输的数据
if($files)
{
+ // 对上传文件数组信息处理
+ $files = $this->dealFiles($files);
+
foreach($files as $key=>$file)
{
$type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
@@ -291,7 +181,7 @@ class ImageController extends CommonController
/**
* 转换上传文件数组变量为正确的方式
* @access public
- * @param array $files 上传的文件变量
+ * @param array $files 上传的文件变量
* @return array
*/
public function dealFiles($files)
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index a1fac02..feee12d 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -85,7 +85,7 @@ class UserController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
}
}
-
+ $data = [];
if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}
diff --git a/app/Http/Logic/UserLogic.php b/app/Http/Logic/UserLogic.php
index 71efaf5..5e96bf5 100644
--- a/app/Http/Logic/UserLogic.php
+++ b/app/Http/Logic/UserLogic.php
@@ -104,6 +104,8 @@ class UserLogic extends BaseLogic
$validator = $this->getValidate($data, 'edit');
if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
+ $data['updated_at'] =time();
+
$res = $this->getModel()->edit($data,$where);
if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php
index b864169..bf7ace8 100644
--- a/app/Http/Requests/UserRequest.php
+++ b/app/Http/Requests/UserRequest.php
@@ -79,7 +79,7 @@ class UserRequest extends BaseRequest
//场景验证规则
protected $scene = [
'add' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'sex', 'birthday', 'commission', 'money', 'frozen_money', 'point', 'rank_points', 'address_id', 'add_time', 'user_rank', 'parent_id', 'nickname', 'mobile', 'status', 'group_id', 'updated_at', 'signin_time', 'openid', 'unionid', 'push_id', 'refund_account', 'refund_name', 'consumption_money'],
- 'edit' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'sex', 'birthday', 'commission', 'money', 'frozen_money', 'point', 'rank_points', 'address_id', 'add_time', 'user_rank', 'parent_id', 'nickname', 'mobile', 'status', 'group_id', 'updated_at', 'signin_time', 'openid', 'unionid', 'push_id', 'refund_account', 'refund_name', 'consumption_money'],
+ 'edit' => ['email', 'head_img', 'sex', 'birthday', 'address_id', 'nickname', 'updated_at', 'refund_account', 'refund_name'],
'wx_register' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'add_time', 'parent_id', 'nickname', 'mobile', 'group_id', 'openid', 'unionid', 'push_id'],
'del' => ['id'],
];
diff --git a/resources/views/weixin/user/userinfo.blade.php b/resources/views/weixin/user/userinfo.blade.php
index 3254806..3bbd647 100644
--- a/resources/views/weixin/user/userinfo.blade.php
+++ b/resources/views/weixin/user/userinfo.blade.php
@@ -43,7 +43,7 @@ $(function(){
$("#head_img").ajaxSubmit({
dataType: 'json',
success: function(res) {
- var img = res.data;
+ var img = res.data[0];
if(res.code==0)
{
$("#avator").attr("src",img);