app端图片上传接口(tp5)

xiaoxiao2021-02-28  50

//视图层

 <div class="approve_NI_3">        <div class="approve_NI_3A">            <div class="approve_NI_3top">                <img src="../images/sp_xing.png"/>                <div>图片</div>            </div>            <div class="approve_NI_3bottom">                <input type="file" name="img" id="file" accept="image/*;capture=camera" style="display: none;"/>                <!--<img src="../images/wd_mrtx.png"  />-->  <div>  <img src="../images/sp_tupian.png" id="ui_avatar"/>  </div>                        </div>        </div>

    </div>

//js部分

$(function(){    $('#submit_btn').click(function(){    //alert(123);        mui.toast('添加审批成功');        setTimeout(function(){                     window.location.href='./approve_sponsor.html';                 },1000)  });}) //上传图片    mui.plusReady(function(){    document.getElementById('ui_avatar').addEventListener('tap',function(){        if(mui.os.plus){            var a=[{                title:'拍照'            },{                title:'从手机相册选择'            }];            plus.nativeUI.actionSheet({                title:'修改头像',                cancel:'取消',                buttons:a            },function(b){                switch(b.index){                    case 0:                        break;                    case 1:                        //拍照                        getImages();                        break;                    case 2:                        //打开相册                        galleryImages();                        break;                    default:                        break;                }            },false);           }    });    //拍照    function getImages(){        var mobileCamera=plus.camera.getCamera();        mobileCamera.captureImage(function(e){            plus.io.resolveLocalFileSystemURL(e,function(entry){                var path=entry.toLocalURL()+'?version='+new Date().getTime();                uploadHeadImg(path);            },function(err){                console.log("读取拍照文件错误");            });        },function(e){            console.log("er",err);        },function(){            filename:'_doc/head.png';        });    }    //从本地相册选择    function galleryImages(){        //console.log("你选择了从相册选择");        plus.gallery.pick(function(a){            plus.io.resolveLocalFileSystemURL(a,function(entry){                plus.io.resolveLocalFileSystemURL('_doc/',function(root){                    root.getFile('head.png',{},function(file){                        //文件已经存在                        file.remove(function(){                            //console.log("文件移除成功");                            entry.copyTo(root,'head.png',function(e){                                var path=e.fullPath+'?version='+new Date().getTime();                                //console.log(path);                                uploadHeadImg(e.fullPath);                            },function(err){                                console.log("copy image fail: ",err);                            });                        },function(err){                            console.log("删除图片失败:("+JSON.stringify(err)+")");                        });                    },function(err){                        //打开文件失败                        entry.copyTo(root,'head.png',function(e){                            var path=e.fullPath+'?version='+new Date().getTime();                            uploadHeadImg(e.fullPath);                        },function(err){                            console.log("上传图片失败:("+JSON.stringify(err)+")");                        });                    });                },function(e){                    console.log("读取文件夹失败:("+JSON.stringify(err)+")");                });            });        },function(err){            console.log("读取拍照文件失败: ",err);        },{            filter:'image'        });    };    //上传图片    function uploadHeadImg(imgPath){        //选中图片之后,头像当前的照片变为选择的照片        var mainImg=document.getElementById('ui_avatar');        mainImg.src=imgPath; mainImg.onload =function() { var ordercode = $('#ordercode').val();//订单号            var goodsname = $('#goodsname').val();//商品名称            var reason = $('#reason').val();//异常原因            var ui_gid = $('#ui_gid').val();//审批人            var s_id = localStorage.getItem("s_id");            var time = getTime();            var sign = getSign([s_id,time]);            var han = /^[\u4e00-\u9fa5]+$/;            if(ordercode==""){            mui.toast('订单编号不能为空');            return false;            }            if(goodsname==""){                mui.toast('商品名称不能为空');                return false;            }            if (!han.test(goodsname)) {            mui.toast("商品名称必须为汉字")            return false;        }            if(!reason){                mui.toast('异常原因不能为空');                return false;            }            if (!han.test(reason)) {            mui.toast("异常原因必须为汉字")            return false;        }            if(!ui_gid){                mui.toast('审批人不能为空');                return false;            }            var imgData=getBase64Image(mainImg);//         var data = getBase64Image(img);         console.log(imgData);         var s_id = localStorage.getItem("s_id");         var time = getTime();         var sign = getSign([s_id,time]);         mui.ajax(api_url+"Power/updateImageware",{             data:{                 'img':imgData,                 's_id':s_id,                 'time':time,                 'sign':sign,                 'ordercode': ordercode,            'goodsname': goodsname,            'reason': reason,            'ui_gid': ui_gid,             },             dataType:'json',//服务器返回json格式数据             type:'post',//HTTP请求类型             timeout:10000,//超时时间设置为10秒;             success:function(data){                 if(data.code == 1){                     mui.toast(data.message);                 }else{                 console.log(data.message);                     mui.toast(data.message);                 }             },             error:function(xhr,type,errorThrown){                 if(type=='timeout'){                     mui.alert('服务器连接超时,请稍后再试');                 }                }         });       }        var images=new Image();        images.src=imgPath;        var imgData=getBase64Image(mainImg);//      var aaa = getBase64Image(mainImg);        console.log(imgData);//      var s_id = localStorage.getItem("s_id");//      var time = getTime();//      var sign = getSign([s_id,time]);//      mui.ajax(api_url+"Kjds/updateImage",{//          data:{//              'img':imgData,//              's_id':s_id,//              'time':time,//              'sign':sign//          },//          dataType:'json',//服务器返回json格式数据//          type:'post',//HTTP请求类型//          timeout:10000,//超时时间设置为10秒;//          success:function(data){//              if(data.code == 1){//                  mui.toast(data.message);//              }else{//              console.log(data.message);//                  mui.toast(data.message);//              }//          },//          error:function(xhr,type,errorThrown){//              if(type=='timeout'){//                  mui.alert('服务器连接超时,请稍后再试');//              }   //          }//      });    } //压缩图片转成base64    function getBase64Image(img){        var canvas = document.createElement("canvas");      canvas.width = img.width;      canvas.height = img.height;      var ctx = canvas.getContext("2d");      ctx.drawImage(img, 0, 0, img.width, img.height);      var dataURL = canvas.toDataURL("image/png");      return dataURL    }   

});

//后台接口部分

public function updateImageware(){ if(request()->isPost()){ $s_id=input('s_id'); $time = input('time');     $sign_temp = input('sign');     $ordercode = input('ordercode');     $goodsname = input('goodsname');     $reason = input('reason');     $ui_gid = input('ui_gid'); $sign = check_sign(array($s_id,$time));     //图片     $file = input('img');     if($sign == $sign_temp){     if($file){     $new_file = $this->base64_image_content($file,'public/uploads');     if($new_file){     $url = "http://".$_SERVER['HTTP_HOST'].$new_file;       $orderdata=Db::table('kjds_tb_order')->where(array("to_order_id"=>$ordercode))->find();             if($orderdata){                  session_id($s_id);             $uid=Session::get('login_id'); $o_id=$orderdata['to_id']; $o_gid=$orderdata['o_g_id']; $datas['sp_oid']= $o_id; //审批订单id $datas['sp_gid']= $o_gid; //审批商品id $datas['sp_go_uid']=$uid;//发布用户ID $datas['sp_to_uid']=$ui_gid;//审批人ID $datas['sp_con']=$reason;//审批内容 $datas['sp_sptid']=1;//审批类型 $datas['sp_status']=0;//审批状态 $datas['sp_create']=time();//提交时间 $datas['sp_delete']=0; $datas['sp_goodsname']=$goodsname;             $datas['sp_image']=$url; $res = Db::table('kjds_approval')->insert($datas); if($res){             //调用父类记录日志方法,添加操作记录             $whe['u_id']=$uid;         $userdata=Db::table('kjds_user')->where($whe)->find();         $uname=$userdata['u_username'];         $log_con="添加审批";//操作的内容         $log_type="添加";//日志分类         $oid=$o_id;//订单id         $gid=$o_gid;//商品id         $log=parent::setLog($uid,$uname,$log_con,$log_type,$oid,$gid);         if($log){         $result = array(     'code' => 1,     'message' => '图片获取成功',     'filePath'=>$url     );         }else{         $result = array(     'code' => 0,     'message' => '图片获取失败',     );         }                         }else{             $result = array(     'code' => 0,     'message' => '添加审批失败',     );             }             }else{             $result = array(     'code' => 0,     'message' => '订单编号不存在!'     );             }             }else{     $result = array(     'code' => 0,     'message' => '上传失败2!'     );     }                      }else{     $result = array(     'code' => 0,     'message' => '上传失败1!'     );     }     }else{     $result = array(     'code' => 0,     'message' => '请求错误!'     );     }     return json($result); } }

转载请注明原文地址: https://www.6miu.com/read-2624089.html

最新回复(0)