| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /**
- * 增加或编辑
- * */
- var proimglist = []; //保存附件的数组
- var uploader; //上传实例
- var limitNum = 1; //限制数量
- $(function() {
- autosize($('textarea'));
- var n_id = helper.request.queryString("edit_id");
- if(n_id) { //修改
- getDetail(n_id);
- } else {
- uploaderImages(); //初始化附件上传
- }
- //添加编辑保存按钮点击
- $('#module_save').on('click', btn_save);
- });
- //获取详情
- function getDetail(ids) {
- $.getJSON(huayi.config.callcenter_url + "configurationapi/api/appmenuInfo/getmodule", {
- mid: ids,
- }, function(data) {
- if(data.state == "success") {
- var res = data.data;
- $('#type').find('input[name="type_option"][value="'+ res.type +'"]').prop('checked', 'checked');// 否 int 20181228添加,不传的话默认值是1 ,1表示APP,2表示微信
- $('#module_code').val(res.code); // 是 string 编码
- $('#module_title').val(res.name); // 是 string 名称
- $('#flag').find('input[name="flag_option"][value="'+ res.enable +'"]').prop('checked', 'checked');// 是 int 1为启用,0为不启用,2为删除
- $('#module_content').val(res.remark); // 是 string 备注信息
- $('#module_sort').val(res.sort); // 是 int 排序字段默认为1
- //附件
- proimglist = res.imgUrl;
- proimglist = proimglist == null ? [] : proimglist;
- limitNum -= proimglist.length;
- uploaderImages(); //附件上传
- if(proimglist.length > 0) {
- $('#fileNum').text(proimglist.length);
- var $list = $("#fileList");
- $.each(proimglist, function(i, v) {
- if(v.filesmallurl) {
- //原来的图片
- var $li = $(
- '<li><span class="img_mask"><i class="img_del"></i></span>' +
- '<img src="' + v.filesmallurl + '" title="原来的图片"/>' +
- '<span class="file_name">' + v.filename + '</span>' +
- '</li>'
- );
- $list.append($li);
- }
- });
- $list.find('li').off('click');
- $list.find('li').on('click', '.img_del', function() {
- $(this).parent().parent().remove();
- var itemFileName = $(this).parent().parent().find('.file_name').text();
- $.each(proimglist, function(i, v) {
- if(v && v.filename == itemFileName) {
- proimglist.splice(i, 1);
- // helper.methods.delImgs(v.filesmallurl, v.fileurl);
- }
- });
- $('#fileNum').text(proimglist.length);
- limitNum = 1 - proimglist.length;
- uploaderImages();
- });
- }
- }
- });
- }
- //保存
- function btn_save() {
- var n_id = helper.request.queryString("edit_id");
- var wUrl, loadIndex;
- if(!$.trim($('#module_code').val())) {
- layer.confirm('编码不能为空!', {
- icon: 2,
- btn: ['确定'] //按钮
- });
- return;
- }
-
- if(!$.trim($('#module_title').val())) {
- layer.confirm('模块名称不能为空!', {
- icon: 2,
- btn: ['确定'] //按钮
- });
- return;
- }
- if(!$.trim($('#module_content').val())) {
- layer.confirm('备注信息不能为空!', {
- icon: 2,
- btn: ['确定'] //按钮
- });
- return;
- }
- if(!regexs.integerReg.test($.trim($('#module_sort').val()))) {
- layer.confirm('请输入有效的排序编号(正整数、负整数、0)!', {
- icon: 2,
- btn: ['确定'] //按钮
- });
- return;
- }
-
- if(proimglist && proimglist.length === 0) {
- layer.confirm('请上传模块图片!', {
- icon: 2,
- btn: ['确定'] //按钮
- });
- return;
- }
- if(n_id) {
- wURL = "configurationapi/api/appmenuInfo/editmodule";
- } else {
- wURL = "configurationapi/api/appmenuInfo/addmodule";
- }
- $.ajax({
- type: "post",
- url: huayi.config.callcenter_url + wURL,
- dataType: 'json',
- async: true,
- beforeSend: function() { //触发ajax请求开始时执行
- $('#module_save').attr("disabled", true);
- $('#module_save').text('保存中...');
- loadIndex = layer.load();
- },
- data: {
- id: n_id, // 是 string 节点ID
- type: $('#type').find('input[name="type_option"]:checked').val(),// 否 int 20181228添加,不传的话默认值是1 ,1表示APP,2表示微信
- code: $('#module_code').val(), // 是 string 编码
- name: $('#module_title').val(), // 是 string 名称
- flag: $('#flag').find('input[name="flag_option"]:checked').val(),// 是 int 1为启用,0为不启用,2为删除
- remark: helper.filter.delHtmlTag($('#module_content').val()), // 是 string 备注信息
- sort: $('#module_sort').val(), // 是 int 排序字段默认为1
- imgurl: proimglist // 是 string 图片
- },
- success: function(data) {
- layer.close(loadIndex);
- if(data.state == "success") {
- var index = parent.layer.getFrameIndex(window.name);
- parent.layer.close(index);
- parent.$('#table_all').bootstrapTable('refresh');
- parent.layer.msg("保存成功");
- }
- },
- error: function(textStatus) {
- layer.close(loadIndex);
- layer.confirm('网络繁忙,请稍后再试...', {
- closeBtn: 0,
- btn: ['确定'] //按钮
- });
- $('#module_save').text('保存');
- $('#module_save').attr("disabled", false);
- },
- complete: function(XMLHttpRequest, textStatus) {
- layer.close(loadIndex);
- if(textStatus == 'timeout') {
- var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
- xmlhttp.abort();
- layer.confirm('网络超时,请稍后再试...', {
- closeBtn: 0,
- btn: ['确定'] //按钮
- });
- }
- $('#module_save').text('保存');
- $('#module_save').attr("disabled", false);
- },
- });
- }
- //上传附件
- function uploaderImages() {
- if(uploader) {
- uploader.destroy();
- }
- uploader = WebUploader.create({
- auto: true, // 选完文件后,是否自动上传
- swf: '../plugins/webuploader/Uploader.swf', // swf文件路径
- server: huayi.config.callcenter_url + "fileserverapi/Api/Upload", // 文件接收服务端
- pick: '#imgPicker', // 选择文件的按钮。可选
- formData: {
- uploadtype: 'appModule'
- },
- // 只允许选择图片文件。
- accept: {
- title: 'Images',
- extensions: 'gif,jpg,jpeg,bmp,png',
- mimeTypes: 'image/*'
- },
- compress: false, //webuploader默认压缩图片,设置compress:false,可以按照原始比例上传图片
- //fileVal: "upFile",//设置文件上传域的name。
- threads: limitNum, //上传并发数。允许同时最大上传进程数,为了保证文件上传顺序
- fileNumLimit: limitNum,
- fileSizeLimit: 50 * 1024 * 1024, // 50 M
- fileSingleSizeLimit: 20 * 1024 * 1024 // 20 M
- });
- uploader.on('fileQueued', function(file) {
- var $list = $("#fileList"),
- $li = $(
- '<li id="' + file.id + '"><span class="img_mask"><i class="img_del"></i></span>' +
- '<img/>' +
- '<span class="img_state"><i></i></span>' +
- '<span class="file_name">' + file.name + '</span>' +
- '</li>'
- ),
- $img = $li.find('img');
- // $list为容器jQuery实例
- $list.append($li);
- // 创建缩略图
- uploader.makeThumb(file, function(error, src) {
- if(error) {
- $img.replaceWith('<div class="noThumb"><i class="glyphicon glyphicon-paperclip"></i><p>无法预览</p></div>');
- return;
- }
- $img.attr('src', src);
- }, 109, 109); //109*109为缩略图尺寸
- //绑定删除
- $('#' + file.id).on('click', '.img_del', function() {
- uploader.removeFile(file);
- var $fileLi = $('#' + file.id);
- $fileLi.off().find('.img_del').off().end().remove();
- var itemFileName = $(this).parent().parent().find('.file_name').text();
- $.each(proimglist, function(i, v) {
- if(v && v.filename == itemFileName) {
- proimglist.splice(i, 1);
- helper.methods.delImgs(v.filesmallurl, v.fileurl);
- }
- });
- $('#fileNum').text(proimglist.length);
- });
- });
- // 文件上传过程中创建进度条实时显示。
- uploader.on('uploadProgress', function(file, percentage) {
- var $li = $('#' + file.id),
- $percent = $li.find('.progress span');
- // 避免重复创建
- if(!$percent.length) {
- $percent = $('<div class="progress" style="">' +
- '<span class="text">0%</span>' +
- '<span class="percentage"></span>' +
- '</div>')
- .appendTo($li)
- .find('span');
- }
- $percent.eq(0).text(Math.round(percentage * 100) + '%');
- $percent.eq(1).css('width', Math.round(percentage * 100) + '%');
- });
- // 文件上传成功,给item添加成功class, 用样式标记上传成功。
- uploader.on('uploadSuccess', function(file, res) {
- proimglist.push(res.data[0]);
- $('#fileNum').text(proimglist.length);
- $('#' + file.id).addClass('upload-state-done');
- });
- // 文件上传失败,显示上传出错。
- uploader.on('uploadError', function(file) {
- var $li = $('#' + file.id),
- $error = $li.find('div.error');
- // 避免重复创建
- if(!$error.length) {
- $error = $('<div class="error"></div>').appendTo($li);
- }
- $error.text('上传失败');
- });
- // 完成上传完了,成功或者失败,先删除进度条。
- uploader.on('uploadComplete', function(file) {
- $('#' + file.id).find('.progress').remove();
- });
- uploader.onError = function(code) {
- //console.log('Error:' + code);
- switch(code) {
- case "Q_EXCEED_NUM_LIMIT":
- layer.msg('只能上传一张图片。');
- break;
- case "Q_TYPE_DENIED":
- layer.msg('图片类型不正确。');
- break;
- case "F_DUPLICATE":
- layer.msg('该文件已上传,请选择其它文件。');
- break;
- case "F_EXCEED_SIZE":
- layer.msg('单文件大小不能超过20M。');
- break;
- case "Q_EXCEED_SIZE_LIMIT":
- layer.msg('总文件大小不能超过50M。');
- break;
- default:
- break;
- }
- };
- }
|