使用框架:laravel+layui
插件地址:ajaxfileupload.js若失效自行百度
laravel图片处理插件:【扩展推荐】Intervention/image 图片处理
JS代码
//添加汽修厂
$(".addShop").click(function () {
var params = {};
params.shop_fileimg = $('#shopImg').val();
params.shop_fileimg = $(".shopImgNull").attr("src");
params.shop_name = $('#shopName').val().trim();
if(params.shop_name==""){
if(params.shop_fileimg==""){
layer.msg('请上传门头照');
return false;
}
}
params.shop_name = $("#shopName").val().trim();
if(params.shop_name==""){
layer.msg('请输入店铺名称');
return false;
}
var shop_tel = $("#shopTel").val().trim();
if(shop_tel==""){
layer.msg('请输入手机号');
return false;
}else{
if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(shop_tel))&&!(/^\d{4}-\d{7,8}$/.test(shop_tel))){
layer.msg('号码有误,重新输入');
return false;
}else {
params.shop_tel = shop_tel;
}
}
params.shop_city = $('#provinces option:selected').text();
if(params.shop_city==""){
layer.msg('请选择地区');
return false;
}else{
params.shop_area = $('#citys option:selected').text();
}
params.shop_address = $("#shopAddr").val().trim();
if(params.shop_address==""){
layer.msg('请输入详细地址');
return false;
}
layer.load(2);
$.ajaxFileUpload({
type:'POST',
data:params,
fileElementId : ['shopImg'], //文件上传域的ID
url: '/addShopImg', //用于文件上传的服务器端请求地址
secureuri: false, //是否需要安全协议,一般设置为false
dataType: 'json', //返回值类型 一般设置为json
success: function (status){
if (status==1){
layer.closeAll('loading');
layer.msg('添加成功',{time:2000,icon: 1},function(){
window.location.href ="/shopList";
});
} else if (status==2) {
layer.closeAll('loading');
layer.msg('该店铺已被添加过,将直接修改对应店铺信息!',{time:2000,icon: 1},function(){
window.location.href ="/shopList";
});
}else if(status==3){
layer.closeAll('loading');
layer.msg('修改成功',{time:2000,icon: 1},function(){
window.location.href ="/shopList";
});
}else{
layer.closeAll('loading');
layer.msg('添加失败',{time:2000,icon: 1},function(){
/*window.location.href ="/factory_list";*/
});
}
},
});
});
controller代码
use Intervention\Image\ImageManagerStatic as Image;
//添加商店
public function addShopImg(Request $request){
$file =$request->all();
// 获取上传的文件对象
$pic = $request->file('shopImg');
if($pic){
//上传文件夹的路径
$dirname = "/public/static/img/";
$entension=$pic->getClientOriginalExtension();//上传文件的后缀
$newName=date('YmdHis').mt_rand(100,900).'.'.$entension;//设置图片上传之后的名字。
$fileName = "static/img/".$newName;//文件路径,存放数据库
$newPath=base_path().$dirname.$newName;//存储文件绝对路径
Image::make($pic)->resize(270, 162)->save($newPath);//移动图片到指定地址,并设置大小
/*move_uploaded_file($pic,$newPath);*///PHP原生函数,存储文件到指定位置
$DataInsert=[
"shopname" =>$file['shop_name'], //'店铺名称',
"shopimg" =>$fileName, //'店铺头像',
"shoptel" =>$file['shop_tel'], //'店铺电话',
"shopcity" =>$file['shop_city'], //'店铺城市',
"shoparea" =>$file['shop_area'], //'店铺区域',
"shopaddr" =>$file['shop_address'], //'店铺地址',
"shopflag" =>1, //'1:店铺运行中 0:店铺暂时关闭',
"addtime" =>time(), //'添加时间',
];
$num = JSDAreaShop::where('shopname',$file['shop_name'])->get()->count();
if ($num==1){
$deleteImgPath = JSDAreaShop::where('shopname',$file['shop_name'])->value("shopimg");
$deleteImg = unlink(public_path($deleteImgPath));
if($deleteImg){
JSDAreaShop::where('shopname',$file['shop_name'])->update($DataInsert);
$status=2;
}
}else{
$insert = JSDAreaShop::insert($DataInsert);
if ($insert){
$status=1;
}else{
$status=0;
}
}
}else{
$DataInsert=[
"shopname" =>$file['shop_name'], //'店铺名称',
"shoptel" =>$file['shop_tel'], //'店铺电话',
"shopcity" =>$file['shop_city'], //'店铺城市',
"shoparea" =>$file['shop_area'], //'店铺区域',
"shopaddr" =>$file['shop_address'], //'店铺地址',
"shopflag" =>1, //'1:店铺运行中 0:店铺暂时关闭',
"addtime" =>time(), //'添加时间',
];
JSDAreaShop::where('shopname',$file['shop_name'])->update($DataInsert);
$status=3;
}
return $status;
}