Attachment.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. MWF.require("MWF.widget.AttachmentController", null,false);
  2. MWF.xApplication.Forum.Attachment = new Class({
  3. Implements: [Options, Events],
  4. options: {
  5. "documentId" : "",
  6. "isNew": false,
  7. "isEdited" : true,
  8. "size" : "max",
  9. "isSizeChange" : true
  10. },
  11. initialize: function (node, app, actions, lp, options) {
  12. this.setOptions(options);
  13. this.app = app;
  14. this.node = $(node);
  15. this.actions = actions;
  16. this.lp = lp;
  17. },
  18. load: function () {
  19. this.loadAttachmentController();
  20. },
  21. loadAttachmentController: function () {
  22. var options = {
  23. "style": "cms",
  24. "title": "附件区域",
  25. "size": this.options.size ,
  26. "resize": true,
  27. //"attachmentCount": this.json.attachmentCount || 0,
  28. "isUpload": (this.options.isNew || this.options.isEdited),
  29. "isDelete": (this.options.isNew || this.options.isEdited),
  30. "isReplace": false,
  31. "isDownload": true,
  32. "isSizeChange": this.options.isSizeChange,
  33. "readonly": (!this.options.isNew && !this.options.isEdited )
  34. };
  35. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  36. this.attachmentController.load();
  37. //this.actions.listAttachmentInfo.each(function (att) {
  38. // this.attachmentController.addAttachment(att);
  39. //}.bind(this));
  40. if( this.data ){
  41. this.data.each(function (att) {
  42. this.attachmentController.addAttachment(att);
  43. }.bind(this));
  44. }else if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  45. this.listAttachment( function( json ){
  46. json.data.each(function (att) {
  47. this.attachmentController.addAttachment(att);
  48. }.bind(this));
  49. }.bind(this))
  50. }
  51. },
  52. transportData : function( json ){
  53. if( typeOf(json.data) == "array" ){
  54. json.data.each(function(d){
  55. d.person = d.creatorUid;
  56. d.lastUpdateTime = d.updateTime;
  57. })
  58. }else if( typeOf(json.data) == "object" ){
  59. var d = json.data;
  60. d.person = d.creatorUid;
  61. d.lastUpdateTime = d.updateTime;
  62. }else{
  63. json.each(function(d){
  64. d.person = d.creatorUid;
  65. d.lastUpdateTime = d.updateTime;
  66. })
  67. }
  68. return json;
  69. },
  70. listAttachment: function( callback ){
  71. if( !this.options.isNew && this.options.documentId && this.options.documentId!="" ){
  72. this.actions.listAttachment(this.options.documentId, function(json){
  73. if(callback)callback(this.transportData(json));
  74. }.bind(this))
  75. }
  76. },
  77. createUploadFileNode: function () {
  78. this.attachmentController.doUploadAttachment(
  79. {"site": this.options.documentId},
  80. this.actions.action,
  81. "uploadAttachment",
  82. {"id": this.options.documentId, "documentid":this.options.documentId},
  83. null,
  84. function(o){
  85. var j = o;
  86. if ( j.data ) {
  87. //j.userMessage
  88. var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  89. this.actions.getAttachment(aid, this.options.documentId, function (json) {
  90. json = this.transportData(json);
  91. if (json.data) {
  92. this.attachmentController.addAttachment(json.data, o.messageId);
  93. //this.attachmentList.push(json.data);
  94. }
  95. this.attachmentController.checkActions();
  96. this.fireEvent("upload", [json.data]);
  97. this.fireEvent("change");
  98. }.bind(this))
  99. }
  100. this.attachmentController.checkActions();
  101. }.bind(this),
  102. function(files){
  103. this.isQueryUploadSuccess = true;
  104. this.fireEvent( "queryUploadAttachment" );
  105. return this.isQueryUploadSuccess;
  106. }.bind(this),
  107. null, null, null,
  108. function (o) { //错误的回调
  109. if (o.messageId && this.attachmentController.messageItemList) {
  110. var message = this.attachmentController.messageItemList[o.messageId];
  111. if( message && message.node )message.node.destroy();
  112. }
  113. }.bind(this));
  114. // this.uploadFileAreaNode = new Element("div");
  115. // var html = "<input name=\"file\" type=\"file\" multiple/>";
  116. // this.uploadFileAreaNode.set("html", html);
  117. //
  118. // this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  119. // this.fileUploadNode.addEvent("change", function () {
  120. // this.isQueryUploadSuccess = true;
  121. // this.fireEvent( "queryUploadAttachment" );
  122. // if( this.isQueryUploadSuccess ){
  123. // var files = this.fileUploadNode.files;
  124. // if (files.length) {
  125. // for (var i = 0; i < files.length; i++) {
  126. // var file = files.item(i);
  127. //
  128. // var formData = new FormData();
  129. // formData.append('file', file);
  130. // formData.append('site', this.options.documentId);
  131. //
  132. // this.actions.uploadAttachment(this.options.documentId, function (o, text) {
  133. // j = JSON.decode(text);
  134. // if ( j.data ) {
  135. // //j.userMessage
  136. // var aid = typeOf( j.data ) == "object" ? j.data.id : j.data[0].id;
  137. // this.actions.getAttachment(aid, this.options.documentId, function (json) {
  138. // json = this.transportData(json);
  139. // if (json.data) {
  140. // this.attachmentController.addAttachment(json.data);
  141. // //this.attachmentList.push(json.data);
  142. // }
  143. // this.attachmentController.checkActions();
  144. //
  145. // this.fireEvent("upload", [json.data]);
  146. // }.bind(this))
  147. // }
  148. // this.attachmentController.checkActions();
  149. // }.bind(this), null, formData, file);
  150. // }
  151. // }
  152. // }else{
  153. // this.uploadFileAreaNode.destroy();
  154. // this.uploadFileAreaNode = false;
  155. // }
  156. // }.bind(this));
  157. },
  158. uploadAttachment: function (e, node) {
  159. //if (!this.uploadFileAreaNode) {
  160. this.createUploadFileNode();
  161. //}
  162. //this.fileUploadNode.click();
  163. },
  164. deleteAttachments: function (e, node, attachments) {
  165. var names = [];
  166. attachments.each(function (attachment) {
  167. names.push(attachment.data.name);
  168. }.bind(this));
  169. var _self = this;
  170. this.app.confirm("warn", e, this.lp.deleteAttachmentTitle, this.lp.deleteAttachment + "( " + names.join(", ") + " )", 300, 120, function () {
  171. while (attachments.length) {
  172. attachment = attachments.shift();
  173. _self.deleteAttachment(attachment);
  174. }
  175. this.close();
  176. }, function () {
  177. this.close();
  178. }, null);
  179. },
  180. deleteAttachment: function (attachment) {
  181. this.fireEvent("delete", [attachment.data]);
  182. this.actions.deleteAttachment(attachment.data.id, this.documentId, function (json) {
  183. this.attachmentController.removeAttachment(attachment);
  184. //this.form.businessData.attachmentList.erase( attachment.data )
  185. this.attachmentController.checkActions();
  186. }.bind(this));
  187. },
  188. replaceAttachment: function (e, node, attachment) {
  189. return false;
  190. var _self = this;
  191. this.form.confirm("warn", e, this.lp.replaceAttachmentTitle, this.lp.replaceAttachment + "( " + attachment.data.name + " )", 300, 120, function () {
  192. _self.replaceAttachmentFile(attachment);
  193. this.close();
  194. }, function () {
  195. this.close();
  196. }, null);
  197. },
  198. createReplaceFileNode: function (attachment) {
  199. this.replaceFileAreaNode = new Element("div");
  200. var html = "<input name=\"file\" type=\"file\" multiple/>";
  201. this.replaceFileAreaNode.set("html", html);
  202. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  203. this.fileReplaceNode.addEvent("change", function () {
  204. var files = this.fileReplaceNode.files;
  205. if (files.length) {
  206. for (var i = 0; i < files.length; i++) {
  207. var file = files.item(i);
  208. var formData = new FormData();
  209. formData.append('file', file);
  210. // formData.append('site', this.json.id);
  211. this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
  212. this.form.documentAction.getAttachment(attachment.data.id, this.opetions.documentId, function (json) {
  213. attachment.data = json.data;
  214. attachment.reload();
  215. if (o.messageId && this.attachmentController.messageItemList) {
  216. var message = this.attachmentController.messageItemList[o.messageId];
  217. if( message && message.node )message.node.destroy();
  218. }
  219. this.attachmentController.checkActions();
  220. }.bind(this))
  221. }.bind(this), null, formData, file);
  222. }
  223. }
  224. }.bind(this));
  225. },
  226. replaceAttachmentFile: function (attachment) {
  227. if (!this.replaceFileAreaNode) {
  228. this.createReplaceFileNode(attachment);
  229. }
  230. this.fileReplaceNode.click();
  231. },
  232. //小程序文件是否支持打开
  233. checkMiniProgramFile: function(ext) {
  234. var exts = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
  235. for(var i = 0; i < exts.length; i++){
  236. if(ext === exts[i]){
  237. return true;
  238. }
  239. }
  240. return false;
  241. },
  242. downloadAttachment: function (e, node, attachments) {
  243. //if( this.app.access.isAnonymousDynamic() ){
  244. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  245. //}else {
  246. attachments.each(function (att) {
  247. if (window.o2android && window.o2android.downloadAttachment) {
  248. window.o2android.downloadAttachment(att.data.id);
  249. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  250. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  251. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  252. wx.miniProgram.navigateTo({
  253. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  254. });
  255. } else {
  256. if (layout.mobile) {
  257. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  258. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  259. var xtoken = Cookie.read("x-token");
  260. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  261. });
  262. } else {
  263. this.actions.getAttachmentStream(att.data.id, this.options.documentId);
  264. }
  265. }
  266. }.bind(this));
  267. //}
  268. },
  269. openAttachment: function (e, node, attachments) {
  270. //if( this.app.access.isAnonymousDynamic() ){
  271. // this.app.openLoginForm( function(){ this.app.reload() }.bind(this) )
  272. //}else{
  273. attachments.each(function (att) {
  274. if (window.o2android && window.o2android.downloadAttachment) {
  275. window.o2android.downloadAttachment(att.data.id);
  276. } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.downloadAttachment) {
  277. window.webkit.messageHandlers.downloadAttachment.postMessage({ "id": att.data.id, "site": "bbs" });
  278. } else if (window.wx && window.__wxjs_environment === 'miniprogram' && this.checkMiniProgramFile(att.data.extension)) { //微信小程序
  279. wx.miniProgram.navigateTo({
  280. url: '../file/download?attId=' + att.data.id + '&type=bbs&subjectId=' + this.options.documentId
  281. });
  282. } else {
  283. if (layout.mobile) {
  284. //移动端 企业微信 钉钉 用本地打开 防止弹出自带浏览器 无权限问题
  285. this.actions.getAttachmentUrl(att.data.id, this.options.documentId, function (url) {
  286. var xtoken = Cookie.read("x-token");
  287. window.location = o2.filterUrl(url + "?x-token=" + xtoken);
  288. });
  289. } else {
  290. this.actions.getAttachmentData(att.data.id, this.options.documentId);
  291. }
  292. }
  293. }.bind(this));
  294. //}
  295. },
  296. getAttachmentUrl: function (attachment, callback) {
  297. this.actions.getAttachmentUrl(attachment.data.id, this.options.documentId, callback);
  298. },
  299. getAttachmentData : function(){
  300. var data = [];
  301. this.attachmentController.attachments.each(function( att ){
  302. data.push(att.data)
  303. });
  304. return data;
  305. },
  306. getAttachmentIds : function(){
  307. var ids = [];
  308. this.attachmentController.attachments.each(function( att ){
  309. ids.push(att.data.id)
  310. });
  311. return ids;
  312. },
  313. loadAttachmentSelecter: function( option, callback ){
  314. MWF.require("MWF.widget.AttachmentSelector", function() {
  315. var options = {
  316. "style" : "cms",
  317. "title": "选择附件",
  318. "listStyle": "icon",
  319. "selectType" : "all",
  320. "size": "max",
  321. "attachmentCount": 0,
  322. "isUpload": true,
  323. "isDelete": true,
  324. "isReplace": true,
  325. "isDownload": true,
  326. "toBase64" : true,
  327. "base64MaxSize" : 800,
  328. "readonly": false
  329. };
  330. options = Object.merge( options, option );
  331. if (this.readonly) options.readonly = true;
  332. this.attachmentController = new MWF.widget.AttachmentSelector(document.body, this, options);
  333. this.attachmentController.load();
  334. this.postSelect = callback;
  335. if( this.data ){
  336. this.data.each(function (att) {
  337. this.attachmentController.addAttachment(att);
  338. }.bind(this));
  339. }else{
  340. this.listAttachment( function( json ){
  341. json.data.each(function (att) {
  342. this.attachmentController.addAttachment(att);
  343. }.bind(this));
  344. }.bind(this))
  345. }
  346. }.bind(this));
  347. },
  348. selectAttachment: function(e, node, attachments){
  349. if( attachments.length > 0 ){
  350. var data = attachments[attachments.length-1].data;
  351. this.actions.getAttachmentUrl( data.id, this.options.documentId, function(url){
  352. if( this.attachmentController.options.toBase64 ){
  353. this.actions.getSubjectAttachmentBase64( data.id, this.attachmentController.options.base64MaxSize, function( json ){
  354. var base64Code = json.data ? "data:image/png;base64,"+json.data.value : null;
  355. if(this.postSelect)this.postSelect( url , data, base64Code )
  356. }.bind(this) )
  357. }else{
  358. if(this.postSelect)this.postSelect( url , data )
  359. }
  360. }.bind(this))
  361. }
  362. }
  363. });