| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- var fileArr = [],
- fileSrcLength, imgUpLength = 0,imgsLength=0;
- mui.init({
- swipeBack: true //启用右滑关闭功能
- });
- document.getElementById('headImage').addEventListener('tap', function() {
- if (mui.os.plus) {
- var buttonTit = [{
- title: "拍照"
- }, {
- title: "从手机相册选择"
- }];
- plus.nativeUI.actionSheet({
- title: "上传图片",
- cancel: "取消",
- buttons: buttonTit
- }, function(b) { /*actionSheet 按钮点击事件*/
- switch (b.index) {
- case 0:
- break;
- case 1:
- getImage(); /*拍照*/
- break;
- case 2:
- galleryImg(); /*打开相册*/
- break;
- default:
- break;
- }
- })
- }
- }, false);
- // 拍照获取图片
- function getImage() {
- var c = plus.camera.getCamera();
- fileArr = [];
- imgUpLength = 0;
- c.captureImage(function(e) {
- plus.io.resolveLocalFileSystemURL(e, function(entry) {
- plus.nativeUI.showWaiting
- var imgSrc = entry.toLocalURL() + "?version=" + new Date().getTime(); //拿到图片路径
- imgUpLength++;
- appendFile(imgSrc);
- }, function(e) {
- console.log("读取拍照文件错误:" + e.message);
- });
- }, function(s) {
- console.log("error" + s.message);
- }, {
- filename: "_doc/camera/"
- })
- }
- // 从相册中选择图片
- function galleryImg() {
- // 从相册中选择图片
- fileArr = [];
- imgUpLength = 0;
- plus.gallery.pick(function(e) {
- for (var i in e.files) {
- var fileSrc = e.files[i];
- fileSrcLength = e.files.length;
- imgUpLength++;
- appendFile(fileSrc);
- }
- plus.nativeUI.showWaiting();
- }, function(e) {
- console.log("取消选择图片");
- }, {
- filter: "image",
- multiple: true,
- //maximum: 5,
- system: false,
- onmaxed: function() {
- plus.nativeUI.alert('最多只能选择5张图片');
- }
- });
- }
- function setHtml(path, fl) {
- var str = '';
- str = '<li class="mui-table-view-cell" imgId=""><div class="img_div">' +
- '<img src="' + path + '" base64="' + f1 + '">' +
- '<div class="mui-icon mui-icon-trash deleteBtn"></div>' +
- '</div></li>';
- $("#imgs").append(str);
- }
- $("#imgs").on("tap", ".deleteBtn", function() {
- var btnArray = ['取消', '确定'];
- var index = $(this).parent().parent();
- mui.confirm('确认删除', '提示', btnArray, function(e) {
- if (e.index == 1) {
- index.remove();
- imgsLength--;
- } else {
- plus.nativeUI.toast('取消');
- }
- })
- })
- function upFile() {
- mui.ajax(huayi.config.callcenter_url + '/ApplicationsVersion/UploadBase64', {
- data: {
- "dataurl": fileArr
- },
- dataType: 'json', //服务器返回json格式数据
- type: 'post', //HTTP请求类型
- timeout: 10000, //超时时间设置为10秒
- success: function(data) {
- if (data.state == "success") {
- for (var i in data.acs) {
- $("#imgs li").eq(Number(imgsLength)).attr("imgId", data.acs[i].F_Id)
- imgsLength++;
- }
- mui.alert("图片上传成功");
- plus.nativeUI.closeWaiting();
- } else {
- mui.alert("上传失败");
- plus.nativeUI.closeWaiting();
- }
- },
- error: function(xhr, type, errorThrown) {
- mui.alert(errorThrown);
- plus.nativeUI.closeWaiting();
- }
- });
- }
- // 添加文件
- var f1 = null;
- function appendFile(path) {
- var bitmap = new plus.nativeObj.Bitmap("test");
- // 从本地加载Bitmap图片
- bitmap.load(path,function(){
- var base64 = bitmap.toBase64Data()
- f1 = base64; // 把base64数据丢过去,上传要用。
- setHtml(path, f1);
- fileArr.push(f1);
- if (imgUpLength == fileArr.length) {
- upFile()
- }
- },function(e){
- console.log('加载图片失败:'+JSON.stringify(e));
- });
-
- }
|