images.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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">
  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.startRecord();
  207. },
  208. playVoice() {
  209. console.log('播放录音');
  210. this.play = true;
  211. if (this.voicePath) {
  212. innerAudioContext.src = this.voicePath;
  213. innerAudioContext.play();
  214. innerAudioContext.onEnded((res) => {
  215. console.log('播放完成');
  216. this.play = false;
  217. });
  218. }
  219. },
  220. playStop() {
  221. console.log('停止播放录音');
  222. this.play = false;
  223. if (this.voicePath) {
  224. innerAudioContext.src = this.voicePath;
  225. innerAudioContext.stop();
  226. }
  227. },
  228. preview(item) {
  229. let urls = [];
  230. this.value.forEach((item) => {
  231. urls.push(this.ip + item);
  232. });
  233. // 预览图片
  234. uni.previewImage({
  235. urls: urls,
  236. current: this.ip + item,
  237. success: (res) => {}
  238. });
  239. },
  240. del(item) {
  241. this.value.splice(this.value.indexOf(item), 1);
  242. this.$emit('input', this.value);
  243. this.$forceUpdate();
  244. }
  245. }
  246. };
  247. </script>
  248. <style lang="scss" scoped>
  249. .imgs {
  250. overflow: hidden;
  251. .uploads {
  252. float: left;
  253. width: 80px;
  254. height: 80px;
  255. text-align: center;
  256. border-radius: 10px;
  257. margin: 5px 5px 5px 5px;
  258. overflow: hidden;
  259. background-color: #f8f8f8;
  260. .bw {
  261. padding-top: 10px;
  262. .icon {
  263. font-size: 30px;
  264. display: block;
  265. float: none;
  266. }
  267. .text {
  268. font-size: 13px;
  269. color: #444444;
  270. padding-top: 5px;
  271. }
  272. }
  273. }
  274. .photo {
  275. float: left;
  276. margin: 5px 5px 5px 5px;
  277. position: relative;
  278. overflow: hidden;
  279. border-radius: 8px;
  280. image {
  281. border-radius: 5px;
  282. }
  283. .video {
  284. border-radius: 5px;
  285. }
  286. .audio {
  287. }
  288. .del {
  289. position: absolute;
  290. top: 0px;
  291. right: 0px;
  292. background-color: #00000078;
  293. color: white;
  294. padding: 3px 8px;
  295. font-size: 12px;
  296. border-radius: 0px 0px 0px 10px;
  297. }
  298. }
  299. .audio {
  300. background-color: #eeeeee;
  301. border-radius: 20px;
  302. overflow: hidden;
  303. padding: 5px;
  304. button {
  305. float: left;
  306. background-color: #eeeeee;
  307. font-size: 14px;
  308. .icon {
  309. padding-right: 3px;
  310. }
  311. }
  312. }
  313. }
  314. </style>