images.vue 8.0 KB

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