upload.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. var upload_pic = [];
  2. var upload_pic_info = [];
  3. var upload_file = [];
  4. var upload = [];
  5. var showUploadE = false;
  6. var showUploadFilesUploadE = false;
  7. var showUploadFilesSearchE = false;
  8. var showUploadFilesSearchState = 1;
  9. var showUploadFilesSearchAll = false;
  10. var upload_search_default = '';
  11. var showUploadUrl;
  12. var showUploadType = 1;
  13. var showUploadCall = false;
  14. var showUploadThis = false;
  15. var showUploadIndex = false;
  16. var picManageE = false;
  17. var picManageI = false;
  18. var picManageV = false;
  19. var picManageLayer = false;
  20. var showUploadFilesValue = [];
  21. //创建监听函数
  22. var xhrOnProgress=function(fun) {
  23. xhrOnProgress.onprogress = fun; //绑定监听
  24. //使用闭包实现监听绑
  25. return function() {
  26. //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
  27. var xhr = $.ajaxSettings.xhr();
  28. //判断监听函数是否为函数
  29. if (typeof xhrOnProgress.onprogress !== 'function')
  30. return xhr;
  31. //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
  32. if (xhrOnProgress.onprogress && xhr.upload) {
  33. xhr.upload.onprogress = xhrOnProgress.onprogress;
  34. }
  35. return xhr;
  36. }
  37. }
  38. function showUploadCover(i,e,key,url,assets,type)
  39. {
  40. layui.use(['upload','layer','element'], function() {
  41. var html = '';
  42. var layer = layui.layer;
  43. var cover = e.attr('data-cover');
  44. var layerConfig = {
  45. type: 1,
  46. title: e.attr('value'),
  47. shade: 0.1,
  48. shadeClose : true,
  49. content: html,
  50. area: ['800px', '500px'],
  51. btn: ['保存', '关闭'],
  52. btnAlign: 'c',
  53. yes: function(index, layero){
  54. var pic = $('#cover_pic').attr('src');
  55. var num = $('#cover_num').val();
  56. var key = $('#cover_key').val();
  57. var video = $('#cover_cur').val();
  58. var copy = url.replace('save.videoCover', 'view.copy');
  59. var i = layer.load(0, {shade: false});
  60. $.getJSON(copy, {json:1,file:pic,num:num,key:key,video:video}, function(t)
  61. {
  62. pic = t.data;
  63. $('#update_' + cover + '_' + cover).val(pic);
  64. $('#show_update_' + cover + '_' + cover).attr('src', pic);
  65. layer.close(i);
  66. layer.close(index);
  67. })
  68. },
  69. btn2: function(index, layero){
  70. layer.close(index);
  71. },
  72. };
  73. e.unbind('click').bind('click', function()
  74. {
  75. var view = url.replace('save', 'view');
  76. showUploadE = e;
  77. if (!type) {
  78. type = 1;
  79. }
  80. showUploadType = type;
  81. showUploadUrl = url;
  82. var cur = $('#' + showUploadE.attr('v')).val();
  83. $.getJSON(view, {json:1,key:key,cur:cur,cover:cover}, function(t)
  84. {
  85. if (showUploadIndex) {
  86. layer.close(showUploadIndex);
  87. }
  88. //layer.closeAll();
  89. layerConfig.content = t.data;
  90. layerConfig.end = function() {
  91. if (showUploadFilesUploadE) {
  92. showUploadFilesUploadE = false;
  93. }
  94. }
  95. showUploadIndex = layer.open(layerConfig);
  96. })
  97. })
  98. });
  99. }
  100. function showUpload(i,e,key,url,assets,type)
  101. {
  102. layui.use(['upload','layer','element'], function() {
  103. var html = '';
  104. var layer = layui.layer;
  105. var layerConfig = {
  106. type: 1,
  107. title: e.attr('value'),
  108. shade: 0.1,
  109. shadeClose : true,
  110. content: html,
  111. area: ['800px', '500px']
  112. };
  113. e.unbind('click').bind('click', function()
  114. {
  115. url = url.replace('save', 'view');
  116. showUploadE = e;
  117. if (!type) {
  118. type = 1;
  119. }
  120. showUploadType = type;
  121. showUploadUrl = url;
  122. var cur = $('#' + showUploadE.attr('v')).val();
  123. var value = '';
  124. if (e.attr('data-value')) {
  125. value = e.attr('data-value');
  126. }
  127. var upload_search = '';
  128. if (e.attr('upload_search')) {
  129. upload_search = e.attr('upload_search');
  130. }
  131. $.getJSON(url, {json:1,key:key,cur:cur,value:value,upload_search:upload_search}, function(t)
  132. {
  133. if (showUploadIndex) {
  134. layer.close(showUploadIndex);
  135. }
  136. //layer.closeAll();
  137. layerConfig.content = t.data;
  138. layerConfig.end = function() {
  139. if (showUploadFilesUploadE) {
  140. showUploadFilesUploadE = false;
  141. }
  142. }
  143. showUploadIndex = layer.open(layerConfig);
  144. })
  145. })
  146. });
  147. }
  148. function editorShowUpload(cur, url, title, func, self)
  149. {
  150. showUploadType = 2;
  151. showUploadCall = func;
  152. showUploadThis = self;
  153. layui.use(['upload','layer','element'], function() {
  154. var html = '';
  155. var layer = layui.layer;
  156. var layerConfig = {
  157. type: 1,
  158. title: title,
  159. shade: 0.1,
  160. shadeClose : true,
  161. content: html,
  162. area: '800px'
  163. };
  164. url = url.replace('save', 'view');
  165. showUploadUrl = url;
  166. $.getJSON(url, {json:1,cur:cur}, function(t)
  167. {
  168. layerConfig.content = t.data;
  169. showUploadIndex = layer.open(layerConfig);
  170. })
  171. });
  172. }
  173. //删除
  174. function showUploadFilesSetState(e, url)
  175. {
  176. if (confirm('确定进行此项操作吗?')) {
  177. $.getJSON(url, {json:1}, function(t)
  178. {
  179. if (!showUploadFilesSearchE) {
  180. showUploadFilesSearchE = $('#showUploadFilesSearch');
  181. }
  182. showUploadFilesSearch(showUploadFilesSearchE, showUploadFilesSearchState,showUploadFilesSearchAll);
  183. })
  184. }
  185. }
  186. function showUploadFilesShowValue(e)
  187. {
  188. var html = e.html();
  189. layer.alert(html);
  190. }
  191. function showUploadFilesSetValue(e, url, id, value)
  192. {
  193. if (showUploadFilesValue[id]) {
  194. value = showUploadFilesValue[id];
  195. }
  196. layer.prompt({
  197. formType: 2,
  198. value: value,
  199. title: '请输入附加值,例子:key1=value1&key2=value2',
  200. area: ['300px', '150px'] //自定义文本域宽高
  201. }, function(value, index, elem){
  202. showUploadFilesValue[id] = value;
  203. $.getJSON(url, {value:value}, function(t){});
  204. layer.close(index);
  205. });
  206. }
  207. function showUploadFilesSet(e, name, url, id)
  208. {
  209. $('.layui-anim-yes').addClass('layui-anim-no');
  210. $('.layui-anim-yes').removeClass('layui-anim-yes');
  211. e.parent().removeClass('layui-anim-no');
  212. e.parent().addClass('layui-anim-yes');
  213. $('.file_cur').val(url);
  214. if (showUploadType == 1) {
  215. var value = showUploadE.attr('v');
  216. var key = showUploadE.attr('key');
  217. loadUploadSet(showUploadE, value, {name:name,url:url}, key);
  218. } else if (showUploadType == 2) {
  219. showUploadCall.call(showUploadThis, url, name, id);
  220. } else if (showUploadType == 3) {
  221. var value = showUploadE.attr('editor_id');
  222. var key = showUploadE.attr('key');
  223. //KindEditor.insertHtml('#' + value, '<div class="dever-video"><embed src="'+url+'" type="video/x-ms-asf-plugin" width="450" height="300" autostart="false" loop="true" data-id="'+id+'" data-key="'+key+'"/><div style="width:450px;height:20px;">'+name+':'+id+'(请勿修改)</div></div>');
  224. KindEditor.insertHtml('#' + value, '<img src="'+url+'" styles="width:450px;height:300px;" style="width:350px;" data-id="'+id+'" data-key="'+key+'"/>');
  225. }
  226. if (showUploadIndex) {
  227. layer.close(showUploadIndex);
  228. showUploadIndex = false;
  229. } else {
  230. layer.closeAll();
  231. }
  232. }
  233. function showUploadFilesUpload(e)
  234. {
  235. showUploadFilesUploadE = e;
  236. showUploadE.parent().find('.image_upload').click();
  237. }
  238. function showUploadFilesSearch(e, state, all, pg)
  239. {
  240. showUploadFilesSearchE = e;
  241. showUploadFilesSearchState = state;
  242. var p = e.parent();
  243. if (!pg) {
  244. var pg = p.find('.file_pg').val();
  245. }
  246. var key = p.find('.file_key').val();
  247. var name = p.find('.file_filename').val();
  248. var tag = p.find('.file_tag').val();
  249. var cate = p.find('.file_cate').val();
  250. var cur = p.find('.file_cur').val();
  251. var upload_search = p.find('.upload_search').val();
  252. var state = state;
  253. var url = showUploadUrl;
  254. if (upload_search) {
  255. upload_search_default = upload_search;
  256. }
  257. upload_search = upload_search_default;
  258. if (all) {
  259. upload_search = '';
  260. showUploadFilesSearchAll = true;
  261. } else {
  262. showUploadFilesSearchAll = false;
  263. }
  264. showUploadFiles(url, name, key, cate, tag, cur, pg, upload_search, state);
  265. }
  266. function showUploadFiles(url, name, key, cate, tag, cur, pg, upload_search, state)
  267. {
  268. if (!pg) {
  269. pg = 1;
  270. }
  271. $.getJSON(url, {json:1,key:key, name:name,cate:cate, tag:tag, pg:pg, cur:cur, upload_search:upload_search,state:state}, function(t)
  272. {
  273. var t = '<div>' + t.data + '</div>';
  274. html = $(t).find('#component-anim').html();
  275. $("#component-anim").html(html);
  276. })
  277. }
  278. function loadUploadL(i,e,key,url,assets)
  279. {
  280. var value = e.attr('v');
  281. var call = e.attr('call');
  282. if (typeof(upload[value]) != "undefined") {
  283. //return;
  284. }
  285. upload[value] = true;
  286. upload_pic[value] = [];
  287. upload_pic_info[value] = [];
  288. upload_file[value] = [];
  289. var id = e.attr('id');
  290. var token = e.attr('token');
  291. var host = e.attr('host');
  292. var domain = e.attr('domain');
  293. var bucket = e.attr('bucket');
  294. var path = e.attr('path');
  295. var cover = e.attr('cover');
  296. var type = e.attr('m');
  297. var search = '';
  298. if (e.attr('upload_search_value')) {
  299. search = e.attr('upload_search_value');
  300. }
  301. if (!cover) {
  302. cover = 1;
  303. }
  304. var uploadUrl = url;
  305. url = domain;
  306. layui.use(['layer','element'], function() {
  307. var layer = layui.layer;
  308. var element = layui.element;
  309. $("#" + id).unbind("change").bind("change",function() {
  310. var file = this.files[0];
  311. if (file) {
  312. element.progress(id + '_progress', '0%');
  313. layer.load();
  314. var filename = file.name;
  315. var temp = filename.split('.');
  316. var name = temp[0];
  317. filename = path + hex_md5(file.name) + '.' + temp[temp.length-1];
  318. //不覆盖
  319. if (cover == 2) {
  320. var timestamp = Date.parse(new Date());
  321. filename = path + hex_md5(file.name + '_' + timestamp) + '.' + temp[temp.length-1];
  322. }
  323. // 设置next,error,complete对应的操作,分别处理相应的进度信息,错误信息,以及完成后的操作
  324. var error = function(err) {
  325. //请求异常回调
  326. layer.alert('文件上传错误');
  327. element.progress(id + '_progress', '0%')
  328. $('#' + id + '_progress').hide();
  329. layer.closeAll('loading');
  330. };
  331. var complete = function(res) {
  332. //&& res.key.match(/\.(jpg|jpeg|png|gif)$/)
  333. if (res.key) {
  334. element.progress(id + '_progress', '0%')
  335. $('#' + id + '_progress').hide();
  336. layer.closeAll('loading');
  337. var data = {};
  338. if (res.hash) {
  339. if (search.indexOf('#') != -1) {
  340. var temp = search.split('#');
  341. search = temp[0];
  342. if (temp[1]) {
  343. temp[1] = $('#update_'+temp[1]+'_value').val();
  344. if (temp[1] > 0) {
  345. search = search + '_' + temp[1];
  346. }
  347. }
  348. }
  349. $.getJSON(uploadUrl.replace('save.start', 'yun.addFile'), {source:file.name,file:res.key, key:key, search:search, call:call});
  350. data.status = 1;
  351. data.name = res.key;
  352. data.url = host + res.key;
  353. loadUploadSet(e,value,data,key);
  354. } else {
  355. layer.alert('文件上传错误');
  356. return false;
  357. }
  358. }
  359. };
  360. if (type == 'qiniu') {
  361. qiniuHandle(id, name, token, domain, bucket, file, filename, element, error, complete);
  362. } else if(type == 'oss') {
  363. ossHandle(id, name, token, domain, bucket, file, filename, element, error, complete);
  364. }
  365. }
  366. });
  367. });
  368. }
  369. function ossHandle(id, name, token, domain, bucket, file, filename, element, error, complete)
  370. {
  371. var temp = token.split('||');
  372. if (temp[0]) {
  373. token = temp[0];
  374. } else {
  375. token = '';
  376. }
  377. let ossConfig = {
  378. // region以杭州为例(oss-cn-hangzhou),其他region按实际情况填写。
  379. region: 'oss-' + domain,
  380. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  381. accessKeyId: temp[1],
  382. accessKeySecret: temp[2],
  383. bucket: bucket,
  384. }
  385. if (token) {
  386. ossConfig.stsToken = token;
  387. }
  388. let client = new OSS(ossConfig);
  389. let tempCheckpoint;
  390. // 定义上传方法。
  391. async function multipartUpload () {
  392. try {
  393. // object-key可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
  394. let result = await client.multipartUpload(filename, file, {
  395. progress: function (p, checkpoint) {
  396. $('#' + id + '_progress').show();
  397. element.progress(id + '_progress', parseInt(p*100)+'%')
  398. // 断点记录点。浏览器重启后无法直接继续上传,您需要手动触发上传操作。
  399. tempCheckpoint = checkpoint;
  400. if (p >= 1) {
  401. complete({key:filename,hash:true});
  402. }
  403. },
  404. //meta: { year: 2020, people: 'test' },
  405. //mime: 'image/jpeg'
  406. })
  407. } catch(e){
  408. console.log(e);
  409. error();
  410. }
  411. }
  412. // 开始分片上传。
  413. multipartUpload();
  414. }
  415. function qiniuHandle(id, name, token, domain, bucket, file, filename, element, error, complete)
  416. {
  417. // eslint-disable-next-line
  418. var finishedAttr = [];
  419. // eslint-disable-next-line
  420. var compareChunks = [];
  421. var observable;
  422. var subscription;
  423. var next = function(response) {
  424. var chunks = response.chunks||[];
  425. var total = response.total;
  426. // 这里对每个chunk更新进度,并记录已经更新好的避免重复更新,同时对未开始更新的跳过
  427. for (var i = 0; i < chunks.length; i++) {
  428. if (chunks[i].percent === 0 || finishedAttr[i]){
  429. continue;
  430. }
  431. if (compareChunks[i].percent === chunks[i].percent){
  432. continue;
  433. }
  434. if (chunks[i].percent === 100){
  435. finishedAttr[i] = true;
  436. }
  437. }
  438. $('#' + id + '_progress').show();
  439. element.progress(id + '_progress', parseInt(total.percent)+'%')
  440. compareChunks = chunks;
  441. };
  442. var subObject = {
  443. next: next,
  444. error: error,
  445. complete: complete
  446. };
  447. // 调用sdk上传接口获得相应的observable,控制上传和暂停
  448. var layerConfig = {
  449. useCdnDomain: true,
  450. disableStatisticsReport: false,
  451. retryCount: 6
  452. };
  453. var putExtra = {
  454. fname: "",
  455. params: {"x:name":name},
  456. mimeType: null
  457. };
  458. observable = qiniu.upload(file, filename, token, putExtra, layerConfig);
  459. subscription = observable.subscribe(subObject);
  460. }
  461. function loadUpload(i,e,key,url,assets)
  462. {
  463. var value = e.attr('v');
  464. var call = e.attr('call');
  465. if (typeof(upload[value]) != "undefined") {
  466. //return;
  467. }
  468. upload[value] = true;
  469. upload_pic[value] = [];
  470. upload_pic_info[value] = [];
  471. upload_file[value] = [];
  472. var data = {};
  473. data.key = key;
  474. data.call = call;
  475. var token = e.attr('token');
  476. var host = e.attr('host');
  477. var domain = e.attr('domain');
  478. var path = e.attr('path');
  479. var cover = e.attr('cover');
  480. data.search = '';
  481. if (e.attr('upload_search_value')) {
  482. data.search = e.attr('upload_search_value');
  483. }
  484. if (!cover) {
  485. cover = 1;
  486. }
  487. if (token) {
  488. data.token = token;
  489. }
  490. var uploadUrl = url;
  491. if (domain) {
  492. url = domain;
  493. delete data.key;
  494. //data.key = 2;
  495. }
  496. var id = e.attr('id');
  497. layui.use(['upload','layer','element'], function() {
  498. var layer = layui.layer;
  499. var layuiUpload = layui.upload;
  500. var element = layui.element;
  501. var uploadInst = layuiUpload.render({
  502. elem: '#' + id
  503. ,data: data
  504. ,field: 'file'
  505. ,url: url
  506. ,multiple: true
  507. ,accept:'file'
  508. //,auto:false
  509. ,xhr:xhrOnProgress
  510. ,progress:function(value){//上传进度回调 value进度值
  511. $('#' + id + '_progress').show();
  512. element.progress(id + '_progress', value+'%')//设置页面进度条
  513. }
  514. ,before: function(obj){
  515. if (this.data.search.indexOf('#') != -1) {
  516. var temp = this.data.search.split('#');
  517. this.data.search = temp[0];
  518. if (temp[1]) {
  519. temp[1] = $('#update_'+temp[1]+'_value').val();
  520. if (temp[1] > 0) {
  521. this.data.search = this.data.search + '_' + temp[1];
  522. }
  523. }
  524. }
  525.             }
  526. ,choose: function(obj, self){
  527. if (domain) {
  528. obj.editFile(path, cover);
  529. }
  530. element.progress(id + '_progress', '0%')
  531. //layer.load();
  532. }
  533. ,done: function(data, file) {
  534. if (data.hash) {
  535. data.status = 1;
  536. data.name = data.key;
  537. data.url = host + data.key;
  538. $.getJSON(uploadUrl.replace('save.start', 'yun.addFile'), {source:file.name,file:data.key, key:key, call:call});
  539. }
  540. element.progress(id + '_progress', '0%')
  541. $('#' + id + '_progress').hide();
  542. layer.closeAll('loading');
  543. if (data.status == 1) {
  544. loadUploadSet(e,value,data,key);
  545. } else {
  546. element.progress(id + '_progress', '0%')
  547. layer.alert(data.message);
  548. return false;
  549. }
  550. }
  551. ,error: function(){
  552. //请求异常回调
  553. layer.alert('文件上传错误');
  554. element.progress(id + '_progress', '0%')
  555. $('#' + id + '_progress').hide();
  556. layer.closeAll('loading');
  557. }
  558. });
  559. });
  560. }
  561. function loadUploadSet(e,value,data,key)
  562. {
  563. if (showUploadFilesUploadE) {
  564. showUploadFilesSearch(showUploadFilesUploadE, showUploadFilesSearchState, showUploadFilesSearchAll);
  565. return;
  566. }
  567. var mul = e.attr('mul');
  568. var place = e.attr('place');
  569. if (mul == 'yes') {
  570. if (e.attr('t') == 'file') {
  571. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  572. upload_file[value] = $("#" + value).val().split(',');
  573. }
  574. var m = $('.' +value+'_mul');
  575. var i = m.find('li').eq(upload_file[value].length);
  576. if (i.length) {
  577. i.find('a').eq(0).html(data.name);
  578. } else {
  579. m.append('<li><a href="'+data.url+'" target="_blank" title="点此下载" >'+data.name+'</a>&nbsp;&nbsp;<a href="javascript:;" title="点此删除" style="color:red;" onclick="fileDel($(this), \''+value+'\')">[删除]</a></li>');
  580. }
  581. upload_file[value].push(data.url);
  582. $("#" + value).val(upload_file.join(','));
  583. } else {
  584. var i = e.attr('i');
  585. var info = '';
  586. if (data.width) {
  587. info = data.width + '_' + data.height + '_' +data.size + '_' + data.fid;
  588. }
  589. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  590. upload_pic[value] = $("#" + value).val().split(',');
  591. }
  592. if ($('#'+i).length) {
  593. upload_pic_info[value] = $("#" + i).val().split(',');
  594. }
  595. var m = $('.' +value+'_mul');
  596. var index = m.find('img').eq(upload_pic[value].length);
  597. var set = index.length;
  598. var place = m.find('.place');
  599. if (place.length) {
  600. set = false;
  601. }
  602. if (set) {
  603. index.attr('src', data.url);
  604. index.attr('data-src', data.url);
  605. index.attr('info', info);
  606. } else {
  607. m.append('<a href="javascript:;" title="打开图片管理工具" onclick="picManage($(this), \''+value+'\', \''+key+'\', 2, \''+i+'\', \''+place+'\')"><img src="'+data.url+'" data-src="'+data.url+'" info="'+info+'" width="150" class="cropper_img" style="margin-left:5px;" alt="打开图片管理工具"/></a>');
  608. }
  609. upload_pic[value].push(data.url);
  610. $("#" + value).val(upload_pic[value].join(','));
  611. if ($('#'+i).length && info) {
  612. upload_pic_info[value].push(info);
  613. $('#'+i).val(upload_pic_info[value].join(','));
  614. }
  615. }
  616. } else {
  617. if (e.attr('t') == 'file') {
  618. } else {
  619. var i = e.attr('i');
  620. if ($('#show_'+value).length) {
  621. $('#show_'+value).parent().show();
  622. $('#show_'+value).attr('src',data.url).show();
  623. $('#show_'+value).attr('data-src',data.url);
  624. if (!checkClick($('#show_'+value))) {
  625. $('#show_'+value).unbind('click').bind('click', function()
  626. {
  627. picManage($(this), value, key, 1, i, place);
  628. })
  629. }
  630. }
  631. }
  632. $('#'+value).val(data.url);
  633. if ($('#'+i).length && data.width) {
  634. var info = data.width + '_' + data.height + '_' +data.size + '_' + data.fid;
  635. $('#'+i).val(info);
  636. }
  637. }
  638. }
  639. function picSet(pic, s, info)
  640. {
  641. if (picManageE) {
  642. //$('#'+value).val(data.url);
  643. var img = picManageE.find('img');
  644. var old = img.attr('src');
  645. img.attr('src', pic);
  646. var value = $("#" + picManageV);
  647. value.val(value.val().replace(old, pic));
  648. if (s != 1) {
  649. upload_pic[picManageV].replaceOne(old,pic);
  650. }
  651. var i = picManageI;
  652. if ($('#'+i).length) {
  653. if (s == 1) {
  654. $('#'+i).val(info);
  655. } else {
  656. var old = img.attr('info');
  657. if (old) {
  658. upload_pic_info[picManageV].replaceOne(old,info);
  659. $("#" + i).val(upload_pic_info[picManageV].join(','));
  660. }
  661. }
  662. }
  663. }
  664. layer.close(picManageLayer);
  665. }
  666. function picDel(s)
  667. {
  668. if (!picManageE) {
  669. return;
  670. }
  671. if (confirm('确定要删除图片吗?')) {
  672. var v = picManageV;
  673. var e = picManageE;
  674. var i = picManageI;
  675. if (s == 1) {
  676. e.hide();
  677. $("#" + v).val('');
  678. if ($('#'+i).length) {
  679. $('#'+i).val('');
  680. }
  681. } else {
  682. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  683. upload_pic[v] = $("#" + v).val().split(',');
  684. }
  685. if (upload_pic_info[v].length <= 0 && $('#'+i).length && $("#" + i).val()) {
  686. upload_pic_info[v] = $("#" + i).val().split(',');
  687. }
  688. var p = e.find('img').attr('src');
  689. e.remove();
  690. upload_pic[v].remove(p);
  691. $("#" + v).val(upload_pic[v].join(','));
  692. if ($('#'+i).length) {
  693. var info = e.find('img').attr('info');
  694. upload_pic_info[v].remove(info);
  695. $("#" + i).val(upload_pic_info[v].join(','));
  696. }
  697. }
  698. }
  699. layer.close(picManageLayer);
  700. }
  701. function picManage(e,v,k,s,i,wh)
  702. {
  703. if (!s) {
  704. s = 2;
  705. }
  706. if (!wh) {
  707. wh = '160*90';
  708. }
  709. picManageE = e;
  710. picManageV = v;
  711. picManageI = i;
  712. var param = '';
  713. var img = e.find('img');
  714. if (img.attr('data-src')) {
  715. var pic = img.attr('data-src');
  716. param = img.attr('src');
  717. } else {
  718. var pic = img.attr('src');
  719. img.attr('data-src', pic);
  720. }
  721. if (pic.indexOf('temp.im') != -1) {
  722. return;
  723. }
  724. if (pic.indexOf('?t=') != -1) {
  725. var temp = pic.split('?');
  726. pic = temp[0];
  727. } else if (pic.indexOf('?') != -1) {
  728. return;
  729. }
  730. var url = config.upload.replace('save', 'view') + '.manage&state='+s+'&key='+k+'&pic=' + pic + '&param=' + param+'&wh='+wh;
  731. var layerConfig = {
  732. type: 2,
  733. title: '图片裁剪工具',
  734. content : [url, 'no'],
  735. shade: 0.1,
  736. shadeClose : true,
  737. area: ['1000px','550px']
  738. };
  739. picManageLayer = layer.open(layerConfig);
  740. }
  741. function fileDel(e,v)
  742. {
  743. if(confirm('确定要删除文件吗?'))
  744. {
  745. if(upload_file[v].length <= 0 && $("#" + v).val())
  746. {
  747. upload_file[v] = $("#" + v).val().split(',');
  748. }
  749. var parent = e.parent();
  750. var p = parent.find('a').eq(0).attr('href');
  751. parent.remove();
  752. upload_file[v].remove(p);
  753. //console.info(pic);
  754. $("#" + v).val(upload_file[v].join(','));
  755. }
  756. }