|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+(function(mui, window, document, undefined) {
|
|
|
2
|
+ mui.init();
|
|
|
3
|
+ var get = function(id) {
|
|
|
4
|
+ return document.getElementById(id);
|
|
|
5
|
+ };
|
|
|
6
|
+ var qsa = function(sel) {
|
|
|
7
|
+ return [].slice.call(document.querySelectorAll(sel));
|
|
|
8
|
+ };
|
|
|
9
|
+ var ui = {
|
|
|
10
|
+ question: get('question'),
|
|
|
11
|
+ contact: get('contact'),
|
|
|
12
|
+ imageList: get('image-list'),
|
|
|
13
|
+ submit: get('submit')
|
|
|
14
|
+ };
|
|
|
15
|
+ ui.clearForm = function() {
|
|
|
16
|
+ ui.question.value = '';
|
|
|
17
|
+ ui.contact.value = '';
|
|
|
18
|
+ ui.imageList.innerHTML = '';
|
|
|
19
|
+ ui.newPlaceholder();
|
|
|
20
|
+ };
|
|
|
21
|
+ ui.getFileInputArray = function() {
|
|
|
22
|
+ return [].slice.call(ui.imageList.querySelectorAll('input[type="file"]'));
|
|
|
23
|
+ };
|
|
|
24
|
+ ui.getFileInputIdArray = function() {
|
|
|
25
|
+ var fileInputArray = ui.getFileInputArray();
|
|
|
26
|
+ var idArray = [];
|
|
|
27
|
+ fileInputArray.forEach(function(fileInput) {
|
|
|
28
|
+ if(fileInput.value != '') {
|
|
|
29
|
+ idArray.push(fileInput.getAttribute('id'));
|
|
|
30
|
+ }
|
|
|
31
|
+ });
|
|
|
32
|
+ return idArray;
|
|
|
33
|
+ };
|
|
|
34
|
+ var imageIndexIdNum = 0;
|
|
|
35
|
+ ui.newPlaceholder = function() {
|
|
|
36
|
+ var fileInputArray = ui.getFileInputArray();
|
|
|
37
|
+ if(fileInputArray &&
|
|
|
38
|
+ fileInputArray.length > 0 &&
|
|
|
39
|
+ fileInputArray[fileInputArray.length - 1].parentNode.classList.contains('space')) {
|
|
|
40
|
+ return;
|
|
|
41
|
+ }
|
|
|
42
|
+ imageIndexIdNum++;
|
|
|
43
|
+ var placeholder = document.createElement('div');
|
|
|
44
|
+ placeholder.setAttribute('class', 'image-item space');
|
|
|
45
|
+ var closeButton = document.createElement('div');
|
|
|
46
|
+ closeButton.setAttribute('class', 'image-close');
|
|
|
47
|
+ closeButton.innerHTML = 'X';
|
|
|
48
|
+ closeButton.addEventListener('click', function(event) {
|
|
|
49
|
+ event.stopPropagation();
|
|
|
50
|
+ event.cancelBubble = true;
|
|
|
51
|
+ setTimeout(function() {
|
|
|
52
|
+ ui.imageList.removeChild(placeholder);
|
|
|
53
|
+ }, 0);
|
|
|
54
|
+ return false;
|
|
|
55
|
+ }, false);
|
|
|
56
|
+ var fileInput = document.createElement('input');
|
|
|
57
|
+ fileInput.setAttribute('type', 'file');
|
|
|
58
|
+ fileInput.setAttribute('class', 'image-box ');
|
|
|
59
|
+ fileInput.setAttribute('accept', 'image/*');
|
|
|
60
|
+ fileInput.setAttribute('id', 'image-' + imageIndexIdNum);
|
|
|
61
|
+
|
|
|
62
|
+ fileInput.addEventListener('change', function(event) {
|
|
|
63
|
+ var file = fileInput.files[0];
|
|
|
64
|
+ var lengh = document.getElementsByClassName("image-item").length;
|
|
|
65
|
+ if(lengh <6) {
|
|
|
66
|
+ if(file) {
|
|
|
67
|
+ var filename = $("#image-" + imageIndexIdNum).val();
|
|
|
68
|
+ fileName = filename.split("\\")[2];
|
|
|
69
|
+// alert(fileName);
|
|
|
70
|
+ var reader = new FileReader();
|
|
|
71
|
+ reader.onload = function() {
|
|
|
72
|
+ //处理 android 4.1 兼容问题
|
|
|
73
|
+ var base64 = reader.result.split(',')[1];
|
|
|
74
|
+ var dataUrl = 'data:image/png;base64,' + base64;
|
|
|
75
|
+ var usercode = localStorage.getItem("user"); //获取本地存储
|
|
|
76
|
+ //image-box
|
|
|
77
|
+ //
|
|
|
78
|
+ placeholder.style.backgroundImage = 'url(' + dataUrl + ')';
|
|
|
79
|
+ $.ajax({
|
|
|
80
|
+ type: "post",
|
|
|
81
|
+// http://192.168.4.18:4010/ 测试
|
|
|
82
|
+// http://12345.shangqiu.gov.cn:8819/ 正式服务器
|
|
|
83
|
+// http://121.196.219.217:8002/ 阿里云服务器
|
|
|
84
|
+ url: 'http://117.158.196.116:4010/Web/Upload64',
|
|
|
85
|
+ async: true,
|
|
|
86
|
+ dataType: 'json',
|
|
|
87
|
+ data: {
|
|
|
88
|
+ dataurl: dataUrl,
|
|
|
89
|
+ usercode: usercode,
|
|
|
90
|
+ filename:fileName,
|
|
|
91
|
+ },
|
|
|
92
|
+
|
|
|
93
|
+ success: function(data) {
|
|
|
94
|
+ if(data.state == "success") {
|
|
|
95
|
+ fileInput.setAttribute('indexs', data.data.F_FileId);
|
|
|
96
|
+ mui.toast('上传成功');
|
|
|
97
|
+
|
|
|
98
|
+ } else {
|
|
|
99
|
+ mui.alert(data.message);
|
|
|
100
|
+ }
|
|
|
101
|
+ }
|
|
|
102
|
+ });
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ reader.readAsDataURL(file);
|
|
|
106
|
+ placeholder.classList.remove('space');
|
|
|
107
|
+ ui.newPlaceholder();
|
|
|
108
|
+ }
|
|
|
109
|
+ }else{
|
|
|
110
|
+ mui.toast('最多上传五张');
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ }, false);
|
|
|
114
|
+ placeholder.appendChild(closeButton);
|
|
|
115
|
+ placeholder.appendChild(fileInput);
|
|
|
116
|
+ ui.imageList.appendChild(placeholder);
|
|
|
117
|
+ };
|
|
|
118
|
+ ui.newPlaceholder();
|
|
|
119
|
+})(mui, window, document, undefined);
|