import lrz from "lrz"
import {generateMixed} from "../common"
import {add2Cache,getFromCache,removeFromCache} from "../cache"
// Created by quanzaiyu on 2017/10/25 0025.
var state = {
img_upload_cache: [],//上传文件缓存
img_paths: [],//上传状态,包括 ready selected uploading finished
img_status:'ready',// # 上传后的路径反馈数组(数据结构为Set集合)
items:new Map(),
orderno:"",
headerimg:""
}
const actions = {
choiceImgdate({commit}){//选择拍照或上传
if (!window.plus){
actions. choiceDanpic({commit})//如果不支持plus,就用本地相册上传即可
return;
}
let title = "选择照片"
let btns = ['拍照','相册']
var func = function(e){
var index = e.index;
if(index == 1) actions.choiceCamera({commit});
if(index == 2) actions. choiceDanpic({commit});
}
if(title && btns && btns.length > 0){
var btnArray = [];
for(var i=0; i btnArray.push({title:btns[i]}); } plus.nativeUI.actionSheet({ title : title, cancel : '取消', buttons : btnArray }, function(e){ if(func) func(e); }) } }, choiceCamera({commit}){//相机 var cmr = plus.camera.getCamera(); cmr.captureImage(function (path){ plus.io.resolveLocalFileSystemURL(path, function(entry){ let localurl = entry.toLocalURL(); actions.insertPhoto({commit},localurl); }, function(e){plus.nativeUI.toast("读取拍照文件错误:" + e.message); }); }, function(e){},{index:1,filename:"_doc/camera/"}); }, choicePic({commit}){//多图选择 plus.gallery.pick(function(e){ for(var i in e.files){ if (i < 9) { actions.insertPhoto({commit},e.files[i]); } } }, function ( e ) { console.log( "取消选择图片" ); },{filter:"image",multiple:true, maximum:9, });//过滤器只要图片,多选 }, choiceDanpic({commit}){//丹徒选择 plus.gallery.pick(function(path){ actions.insertPhoto({commit},path); }, function ( e ) { console.log( "取消选择图片" ); },{filter:"image",filename:'_documents/siyuan/' });//过滤器只要图片,多选 }, insertPhoto({commit},data){//选图 lrz(data, { width: 480 }).then(function(rst) { let reader = new FileReader() reader.readAsDataURL(rst.file); reader.onload = function(e) { rst.imgshow=e.target.result; rst.guid=generateMixed(6); commit('headerimg_set',rst) } return rst }).always(function() { // 清空文件上传控件的值 e.target.value = null }); }, delate({commit},guid){//删除图片 commit('set_img_paths',guid) }, getdata({commit},orderno){//获取单个编号相册情况 commit('get_img_data',orderno) }, clearupload({commit}){//清除照片 commit('chu_set') }, getdanchuan({commit},obj){ lrz(obj.files[0], {width: 350}, function (results) {import lrz from "lrz" import {generateMixed} from "../common" import {add2Cache,getFromCache,removeFromCache} from "../cache" // Created by quanzaiyu on 2017/10/25 0025. var state = { img_upload_cache: [],//上传文件缓存 img_paths: [],//上传状态,包括 ready selected uploading finished img_status:'ready',// # 上传后的路径反馈数组(数据结构为Set集合) items:new Map(), orderno:"", headerimg:"" } const actions = { choiceImgdate({commit}){//选择拍照或上传 if (!window.plus){ actions. choiceDanpic({commit})//如果不支持plus,就用本地相册上传即可 return; } let title = "选择照片" let btns = ['拍照','相册'] var func = function(e){ var index = e.index; if(index == 1) actions.choiceCamera({commit}); if(index == 2) actions. choiceDanpic({commit}); } if(title && btns && btns.length > 0){ var btnArray = []; for(var i=0; i btnArray.push({title:btns[i]}); } plus.nativeUI.actionSheet({ title : title, cancel : '取消', buttons : btnArray }, function(e){ if(func) func(e); }) } }, choiceCamera({commit}){//相机 var cmr = plus.camera.getCamera(); cmr.captureImage(function (path){ plus.io.resolveLocalFileSystemURL(path, function(entry){ let localurl = entry.toLocalURL(); actions.insertPhoto({commit},localurl); }, function(e){plus.nativeUI.toast("读取拍照文件错误:" + e.message); }); }, function(e){},{index:1,filename:"_doc/camera/"}); }, choicePic({commit}){//多图选择 plus.gallery.pick(function(e){ for(var i in e.files){ if (i < 9) { actions.insertPhoto({commit},e.files[i]); } } }, function ( e ) { console.log( "取消选择图片" ); },{filter:"image",multiple:true, maximum:9, });//过滤器只要图片,多选 }, choiceDanpic({commit}){//丹徒选择 plus.gallery.pick(function(path){ actions.insertPhoto({commit},path); }, function ( e ) { console.log( "取消选择图片" ); },{filter:"image",filename:'_documents/siyuan/' });//过滤器只要图片,多选 }, insertPhoto({commit},data){//选图 lrz(data, { width: 480 }).then(function(rst) { let reader = new FileReader() reader.readAsDataURL(rst.file); reader.onload = function(e) { rst.imgshow=e.target.result; rst.guid=generateMixed(6); commit('headerimg_set',rst) } return rst }).always(function() { // 清空文件上传控件的值 e.target.value = null }); }, delate({commit},guid){//删除图片 commit('set_img_paths',guid) }, getdata({commit},orderno){//获取单个编号相册情况 commit('get_img_data',orderno) }, clearupload({commit}){//清除照片 commit('chu_set') }, getdanchuan({commit},obj){ lrz(obj.files[0], {width: 350}, function (results) { // 你需要的数据都在这里,可以以字符串的形式传送base64给服务端转存为图片。 commit('headerimg_set',results) }); } } const getters = {} const mutations = { set_img_upload_cache (state,rst) { let newItem=[]; console.log(state.orderno); if (state.items.has(state.orderno)) { newItem=state.items.get(state.orderno); newItem.push(rst) }else{ newItem.push(rst) } state.items.set(state.orderno,newItem); let goods=state.items.get(state.orderno); state.img_upload_cache= goods }, set_img_paths(state,guid){ let newItem=[]; if (state.items.has(state.orderno)) { newItem=state.items.get(state.orderno) for(let i=0;i< newItem.length;i++){ let pitem=newItem[i]; if (pitem.guid==guid) { newItem.splice(i,1) } } } state.items.set(state.orderno,newItem); let goods=state.items.get(state.orderno); state.img_upload_cache= goods; }, get_img_data(state,orderno){ state.img_upload_cache=[]; state.items.clear(); state.orderno=orderno; console.log("获取传递过来的"+orderno) }, chu_set(state){//上传成功清除 state.img_upload_cache=[]; state.items.delete(state.orderno); state.orderno=""; }, headerimg_set(state,rst){//单图头像base64 state.headerimg=rst.base64 } } export default { namespaced: true, state, mutations, actions, getters } // 你需要的数据都在这里,可以以字符串的形式传送base64给服务端转存为图片。 commit('headerimg_set',results) }); } } const getters = {} const mutations = { set_img_upload_cache (state,rst) { let newItem=[]; console.log(state.orderno); if (state.items.has(state.orderno)) { newItem=state.items.get(state.orderno); newItem.push(rst) }else{ newItem.push(rst) } state.items.set(state.orderno,newItem); let goods=state.items.get(state.orderno); state.img_upload_cache= goods }, set_img_paths(state,guid){ let newItem=[]; if (state.items.has(state.orderno)) { newItem=state.items.get(state.orderno) for(let i=0;i< newItem.length;i++){ let pitem=newItem[i]; if (pitem.guid==guid) { newItem.splice(i,1) } } } state.items.set(state.orderno,newItem); let goods=state.items.get(state.orderno); state.img_upload_cache= goods; }, get_img_data(state,orderno){ state.img_upload_cache=[]; state.items.clear(); state.orderno=orderno; console.log("获取传递过来的"+orderno) }, chu_set(state){//上传成功清除 state.img_upload_cache=[]; state.items.delete(state.orderno); state.orderno=""; }, headerimg_set(state,rst){//单图头像base64 state.headerimg=rst.base64 } } export default { namespaced: true, state, mutations, actions, getters }