publish.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="publish">
  3. <u-navbar leftIconColor="#000" :fixed="false" leftIconSize="48" :autoBack="true">
  4. <view class="" slot="left" style="color: #DC3C23;">
  5. <!-- Cancel -->
  6. {{ $t('news.lab9') }}
  7. </view>
  8. <view class="" slot="center" style="">
  9. <!-- Publish -->
  10. {{ $t('news.lab10') }}
  11. </view>
  12. <view class="flex-end" slot="right" @click="submit()">
  13. <text class="Confirm">
  14. <!-- Confirm -->
  15. {{ $t('news.confirm') }}
  16. </text>
  17. </view>
  18. </u-navbar>
  19. <!-- <image src="" mode="scaleToFill"></image> -->
  20. <u--textarea @focus="focus" @blur="blur" class="input" height="308" v-model="value2" confirmType="done"
  21. border="none" placeholder="Please enter your content" maxlength="-1"></u--textarea>
  22. <view class="upload-nav flex-start">
  23. <!-- <u-upload :fileList="fileList2" :uploadText="$t('common.lab')" @afterRead="afterRead" @delete="deletePic"
  24. imageMode="scaleToFill" name="2" width="200" height="200" multiple :maxCount="1">
  25. <image src="/static/image/videos/Upload@2x.png" mode="aspectFill" style="width: 200rpx;height: 200rpx;">
  26. </image>
  27. </u-upload> -->
  28. <u-upload :fileList="fileList1" :uploadText="$t('common.lab')" @afterRead="afterRead" @delete="deletePic"
  29. imageMode="scaleToFill" accept="video" name="1" width="200" height="200" multiple :maxCount="1">
  30. <image src="/static/image/videos/Uploadthevideo@2x.png" mode="aspectFill"
  31. style="width: 200rpx;height: 200rpx;"></image>
  32. </u-upload>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. type: 0,
  41. value2: '',
  42. showType: false,
  43. show: false,
  44. columns: [],
  45. select: {
  46. name: ''
  47. },
  48. CovrUrl:'',
  49. status: 'error',
  50. fileList2: [],
  51. fileList1: [],
  52. }
  53. },
  54. computed: {
  55. qiuNiu() {
  56. return this.$store.state.qiuNiu
  57. }
  58. },
  59. onLoad() {},
  60. methods: {
  61. /* 提交数据 */
  62. submit() {
  63. if (this.$store.state.isLogin != 1) {
  64. this.$toUrl('/pages/login/login')
  65. return
  66. }
  67. if (this.fileList1.length == 0) return this.$toast(this.$t('news.lab12'))
  68. if (!this.CovrUrl) return
  69. let data = {
  70. flie: [{
  71. img: this.CovrUrl,
  72. video: this.fileList1[0].url
  73. }],
  74. title: this.value2,
  75. }
  76. let str = this.fileList1[0].url.slice(0, 4)
  77. if (str == 'blob') {
  78. return this.$toast(this.$t('news.lab11'))
  79. }
  80. uni.$u.http.post('api/Video_user/insert', data).then(res => {
  81. this.$back()
  82. let pages = getCurrentPages();
  83. let prevPage = pages[pages.length - 2]; //上一页页面实例
  84. prevPage.$vm.ajax.page = 1;
  85. prevPage.$vm.ajax.load = true;
  86. prevPage.$vm.getList()
  87. }).catch(res => {})
  88. },
  89. close() {
  90. this.showType = false
  91. },
  92. // 获取焦点
  93. focus(e) {
  94. this.show = true
  95. },
  96. // 失去焦点
  97. blur(e) {
  98. // this.show = false
  99. },
  100. // 删除图片
  101. deletePic(event) {
  102. this[`fileList${event.name}`].splice(event.index, 1)
  103. },
  104. // 新增图片
  105. async afterRead(event) {
  106. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  107. let lists = [].concat(event.file)
  108. let fileListLen = this[`fileList${event.name}`].length
  109. lists.map((item) => {
  110. this[`fileList${event.name}`].push({
  111. ...item,
  112. status: 'uploading',
  113. message: this.$t('common.lab')
  114. })
  115. })
  116. for (let i = 0; i < lists.length; i++) {
  117. const result = await this.uploadFilePromise(lists[i]);
  118. // this.getVideoPic(result)
  119. this.parseBlob(result)
  120. let item = this[`fileList${event.name}`][fileListLen]
  121. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  122. status: 'success',
  123. message: '',
  124. url: result
  125. }))
  126. fileListLen++
  127. }
  128. },
  129. /* 已实现简单方式,废用 */
  130. getVideoPic(url) {
  131. let that = this;
  132. // return new Promise(function(resolve, reject) {
  133. let dataURL = '';
  134. let video = document.createElement("video");
  135. video.currentTime = 3;
  136. video.setAttribute('crossOrigin', 'anonymous'); //处理跨域
  137. video.setAttribute('src', url);
  138. video.setAttribute('width', 400);
  139. video.setAttribute('height', 240);
  140. video.setAttribute('preload', 'auto');
  141. video.addEventListener('loadeddata', function() {
  142. let canvas = document.createElement("canvas"),
  143. width = video.width, //canvas的尺寸和图片一样
  144. height = video.height;
  145. canvas.width = width;
  146. canvas.height = height;
  147. canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
  148. dataURL = canvas.toDataURL('image/jpeg'); //转换为base64
  149. that.parseBlob(dataURL)
  150. });
  151. // })
  152. },
  153. parseBlob(base64) {
  154. let key = new Date().getTime() + '.png';
  155. var blob = `${base64}?vframe/jpg/offset/0`
  156. let {
  157. token,
  158. domain,
  159. uploadHost,
  160. httpsdomain
  161. } = this.qiuNiu;
  162. uni.uploadFile({
  163. url: 'https://' + uploadHost, // 仅为示例,非真实的接口地址
  164. filePath: blob,
  165. name: 'file',
  166. formData: {
  167. key: key,
  168. token: token
  169. },
  170. success: (res) => {
  171. let data = JSON.parse(res.data)
  172. this.CovrUrl = httpsdomain + '/' + data.key
  173. },
  174. })
  175. },
  176. uploadFilePromise(item) {
  177. if (item.type == 'video') {
  178. this.status = 'loading'
  179. }
  180. let {
  181. token,
  182. domain,
  183. uploadHost,
  184. httpsdomain
  185. } = this.qiuNiu;
  186. let type = item.name.slice(-5);
  187. let key = new Date().getTime() + type;
  188. return new Promise((resolve, reject) => {
  189. let a = uni.uploadFile({
  190. url: 'https://' + uploadHost, // 仅为示例,非真实的接口地址
  191. filePath: item.url,
  192. name: 'file',
  193. formData: {
  194. key: key,
  195. token: token
  196. },
  197. success: (res) => {
  198. let data = JSON.parse(res.data)
  199. if (item.type == 'video') {
  200. this.status = 'success'
  201. }
  202. setTimeout(() => {
  203. resolve(httpsdomain + '/' + data.key);
  204. }, 200)
  205. }
  206. });
  207. })
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss">
  213. /deep/.u-upload__wrap__preview__other__text {
  214. display: none;
  215. }
  216. /deep/.u-upload__wrap__preview {
  217. width: 200rpx;
  218. height: 200rpx;
  219. margin: 0 !important;
  220. }
  221. page {
  222. background: #F3F3F7;
  223. }
  224. .upload-nav {
  225. padding: 24rpx;
  226. margin-top: 30rpx;
  227. }
  228. .select {
  229. color: #333;
  230. padding: 24rpx;
  231. }
  232. .input {
  233. margin: 20rpx 24rpx;
  234. }
  235. .Confirm {
  236. color: #fff;
  237. border-radius: 40rpx;
  238. padding: 6rpx 24rpx;
  239. background-color: $color2;
  240. }
  241. .operation {
  242. position: fixed;
  243. bottom: 0;
  244. left: 0;
  245. right: 0;
  246. padding: 32rpx;
  247. background-color: white;
  248. image {
  249. width: 48rpx;
  250. height: 48rpx;
  251. margin-right: 64rpx;
  252. }
  253. view {
  254. color: #fff;
  255. padding: 8rpx 30rpx;
  256. border-radius: 40px;
  257. background-color: $color2;
  258. }
  259. }
  260. </style>