images.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="imgs">
  3. <view v-if="type === '图片' || type === '视频'">
  4. <view class="photo" v-for="(item, index) in value" :key="index" :style="{ width: size, height: size }">
  5. <image :src="ip + item" mode="aspectFill" :style="{ width: size, height: size }" v-if="type === '图片'" @click.stop="preview(item)"></image>
  6. <video :src="ip + item" v-if="type === '视频'" controls class="video" :style="{ width: size, height: size }"></video>
  7. <text class="icon del" @click.stop="del(item)" v-if="!read">&#xe8b6;</text>
  8. </view>
  9. </view>
  10. <audio :src="ip + value[0]" v-if="type === '录音' && read" name="录音文件" controls></audio>
  11. <view class="uploads" v-if="type != '录音' && value.length < 3 && !read" @click.stop="chooseImage()">
  12. <view class="bw" v-if="type == '图片'">
  13. <view class="icon">&#xe696;</view>
  14. <view class="text">上传图片</view>
  15. </view>
  16. <view class="bw" v-if="type == '视频'">
  17. <view class="icon">&#xe622;</view>
  18. <view class="text">上传视频</view>
  19. </view>
  20. </view>
  21. <view v-if="type == '录音' && !read" class="audio">
  22. <button @click="startRecord()" :disabled="stop">
  23. <text class="icon">&#xe618;</text>
  24. <text>开始</text>
  25. </button>
  26. <button @click="endRecord()" :disabled="!stop">
  27. <text class="icon">&#xe611;</text>
  28. <text>停止</text>
  29. </button>
  30. <button @click="playVoice()" :disabled="stop">
  31. <text class="icon">&#xe628;</text>
  32. <text>播放</text>
  33. </button>
  34. <button @click="again()">
  35. <text class="icon">&#xe607;</text>
  36. <text>重新</text>
  37. </button>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. const recorderManager = uni.getRecorderManager();
  43. const innerAudioContext = uni.createInnerAudioContext();
  44. export default {
  45. name: 'images',
  46. props: {
  47. value: {
  48. type: Array
  49. },
  50. read: {
  51. type: Boolean,
  52. default: false
  53. },
  54. type: {
  55. type: String,
  56. default: 'img'
  57. },
  58. size: {
  59. type: String,
  60. default: '80px'
  61. }
  62. },
  63. data() {
  64. return {
  65. stop: false,
  66. ip: this.http.ip,
  67. voicePath: ''
  68. };
  69. },
  70. mounted() {
  71. if (this.type === '录音') {
  72. recorderManager.onStop((res) => {
  73. this.voicePath = res.tempFilePath;
  74. uni.uploadFile({
  75. url: this.ip + '/app/common/upload',
  76. filePath: res.tempFilePath,
  77. name: 'file',
  78. header: { Authorization: this.getUser().token },
  79. success: (res) => {
  80. let data = JSON.parse(res.data);
  81. if (data.code === 200) {
  82. this.value.push(data.fileName);
  83. this.$emit('input', this.value);
  84. this.$forceUpdate();
  85. } else {
  86. uni.showModal({ content: data.msg, showCancel: false });
  87. }
  88. uni.hideLoading();
  89. },
  90. fail: (res) => {
  91. uni.hideLoading();
  92. uni.showModal({ content: '上传失败', showCancel: false });
  93. }
  94. });
  95. });
  96. }
  97. },
  98. methods: {
  99. chooseImage() {
  100. //照片选择
  101. if (this.type === '图片') {
  102. uni.chooseImage({
  103. count: 3, //默认9
  104. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  105. success: (res) => {
  106. res.tempFilePaths.forEach((path) => {
  107. uni.showLoading({ title: '正在上传图片', mask: true });
  108. uni.uploadFile({
  109. url: this.ip + '/app/common/upload',
  110. filePath: path,
  111. name: 'file',
  112. header: { Authorization: this.getUser().token },
  113. success: (res) => {
  114. let data = JSON.parse(res.data);
  115. if (data.code === 200) {
  116. if (this.value.length < 3) {
  117. this.value.push(data.fileName);
  118. this.$emit('input', this.value);
  119. this.$forceUpdate();
  120. } else {
  121. uni.showModal({ content: '最多只能上传5个附件', showCancel: false });
  122. }
  123. } else {
  124. uni.showModal({ content: data.msg, showCancel: false });
  125. }
  126. uni.hideLoading();
  127. },
  128. fail: (res) => {
  129. uni.hideLoading();
  130. uni.showModal({ content: '图片上传失败', showCancel: false });
  131. }
  132. });
  133. });
  134. }
  135. });
  136. } else if (this.type === '视频') {
  137. uni.chooseVideo({
  138. count: 3,
  139. sourceType: ['camera', 'album'],
  140. maxDuration: 30,
  141. success: (path) => {
  142. uni.showLoading({ title: '正在上传...', mask: true });
  143. uni.uploadFile({
  144. url: this.ip + '/app/common/upload',
  145. filePath: path.tempFilePath,
  146. name: 'file',
  147. header: { Authorization: this.getUser().token },
  148. success: (res) => {
  149. uni.hideLoading();
  150. let data = JSON.parse(res.data);
  151. if (data.code === 200) {
  152. if (this.value.length < 3) {
  153. this.value.push(data.fileName);
  154. this.$emit('input', this.value);
  155. this.$forceUpdate();
  156. } else {
  157. uni.showModal({ content: '最多只能上传5个附件', showCancel: false });
  158. }
  159. } else {
  160. uni.showModal({ content: data.msg, showCancel: false });
  161. }
  162. uni.hideLoading();
  163. },
  164. fail: (res) => {
  165. uni.hideLoading();
  166. uni.showModal({ content: '图片上传失败', showCancel: false });
  167. }
  168. });
  169. }
  170. });
  171. } else if (this.type === '录音') {
  172. }
  173. },
  174. startRecord() {
  175. console.log('开始录音');
  176. this.stop = true;
  177. recorderManager.start();
  178. },
  179. endRecord() {
  180. console.log('录音结束');
  181. this.stop = false;
  182. recorderManager.stop();
  183. },
  184. again() {
  185. this.voicePath = '';
  186. this.startRecord();
  187. },
  188. playVoice() {
  189. console.log('播放录音');
  190. if (this.voicePath) {
  191. innerAudioContext.src = this.voicePath;
  192. innerAudioContext.play();
  193. }
  194. },
  195. preview(item) {
  196. let urls = [];
  197. this.value.forEach((item) => {
  198. urls.push(this.ip + item);
  199. });
  200. // 预览图片
  201. uni.previewImage({
  202. urls: urls,
  203. current: this.ip + item,
  204. success: (res) => {}
  205. });
  206. },
  207. del(item) {
  208. this.value.splice(this.value.indexOf(item), 1);
  209. this.$emit('input', this.value);
  210. this.$forceUpdate();
  211. }
  212. }
  213. };
  214. </script>
  215. <style lang="scss" scoped>
  216. .imgs {
  217. overflow: hidden;
  218. .uploads {
  219. float: left;
  220. width: 80px;
  221. height: 80px;
  222. text-align: center;
  223. border-radius: 10px;
  224. margin: 5px 5px 5px 5px;
  225. overflow: hidden;
  226. background-color: #f8f8f8;
  227. .bw {
  228. padding-top: 10px;
  229. .icon {
  230. font-size: 30px;
  231. display: block;
  232. float: none;
  233. }
  234. .text {
  235. font-size: 13px;
  236. color: #444444;
  237. padding-top: 5px;
  238. }
  239. }
  240. }
  241. .photo {
  242. float: left;
  243. margin: 5px 5px 5px 5px;
  244. position: relative;
  245. overflow: hidden;
  246. border-radius: 8px;
  247. image {
  248. border-radius: 5px;
  249. }
  250. .video {
  251. border-radius: 5px;
  252. }
  253. .audio {
  254. }
  255. .del {
  256. position: absolute;
  257. top: 0px;
  258. right: 0px;
  259. background-color: #00000078;
  260. color: white;
  261. padding: 3px 8px;
  262. font-size: 12px;
  263. border-radius: 0px 0px 0px 10px;
  264. }
  265. }
  266. .audio {
  267. background-color: #eeeeee;
  268. border-radius: 20px;
  269. overflow: hidden;
  270. padding: 5px;
  271. button {
  272. float: left;
  273. background-color: #eeeeee;
  274. font-size: 14px;
  275. .icon {
  276. padding-right: 3px;
  277. }
  278. }
  279. }
  280. }
  281. </style>