apply.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <view :style="vuex_skin">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <u-navbar title-color="#000000" :is-back="true" title="我要报名"></u-navbar>
  5. <!-- #endif -->
  6. <view class="add-media" @click="handleUploadWork">
  7. <view class="flex-direction flex justify-center" style="align-items: center;" v-show="!showUrl">
  8. <u-icon name="plus-circle-fill" :color="vuex_theme.bgColor" size="90"></u-icon>
  9. <text v-text="activeType == 'VIDEO_TEXT'?'上传视频作品':'上传图片作品'"
  10. style="margin-top: 26rpx; color: #010101; font-size: 24rpx; font-weight: bold;">上传作品(图片或视频)</text>
  11. </view>
  12. <video v-if="activeType == 'VIDEO_TEXT' && showUrl" :src="showUrl"></video>
  13. <image v-if="activeType == 'IMAGE_TEXT' && showUrl" :src="showUrl" mode="aspectFill"></image>
  14. </view>
  15. <view class="fill-info">
  16. <text class="info-title" style="font-weight: bold; color: #010101; font-size: 34rpx;">填写信息</text>
  17. <view v-if="activeType == 'VIDEO_TEXT'" style="margin-bottom: 64rpx;">
  18. <text style="color: #010101; font-size: 28rpx; font-weight: bold;"><text style="color: #f00;">*</text> 作品封面</text>
  19. <view style="padding: 5rpx 0 20rpx; border-bottom: 1rpx solid #E3E3E3;">
  20. <upload-img ref="uploadImg" :count="1" col="4" @click="uploadCover"></upload-img>
  21. </view>
  22. </view>
  23. <view style="margin-bottom: 64rpx;">
  24. <text style="color: #010101; font-size: 28rpx; font-weight: bold;"><text style="color: #f00;">*</text> 标题</text>
  25. <view style="padding: 32rpx 0 0; border-bottom: 1rpx solid #E3E3E3;">
  26. <u-input placeholder="请输入标题内容" v-model="applyInfo.title"></u-input>
  27. </view>
  28. </view>
  29. <view style="margin-bottom: 64rpx;">
  30. <text style="color: #010101; font-size: 28rpx; font-weight: bold;"><text style="color: #f00;">*</text> 详细介绍</text>
  31. <view style="padding: 32rpx 0 0; border-bottom: 1rpx solid #E3E3E3;">
  32. <u-input placeholder="请输入详细介绍" v-model="applyInfo.content"></u-input>
  33. <!-- <u-input placeholder="请输入详细介绍" v-model="applyInfo.content" type="textarea" height="60" auto-height></u-input> -->
  34. </view>
  35. </view>
  36. <view style="margin-bottom: 64rpx;">
  37. <text style="color: #010101; font-size: 28rpx; font-weight: bold;"><text style="color: #f00;">*</text> 联系方式</text>
  38. <view style="padding: 32rpx 0 0; border-bottom: 1rpx solid #E3E3E3;">
  39. <u-input placeholder="请输入手机号码" v-model="applyInfo.phone"></u-input>
  40. </view>
  41. </view>
  42. <u-button shape="circle" :custom-style="customStyle" @click="submit">确定提交</u-button>
  43. </view>
  44. <compress ref="compress"></compress>
  45. <toast ref="toast"></toast>
  46. <uploadAlert ref="uploadAlert" :updateProgress="updateProgress" @cancelUpload="cancelUpload"></uploadAlert>
  47. </view>
  48. </template>
  49. <script>
  50. import compress from "@/components/compress.vue"
  51. import uploadImg from "@/components/uploadImg/uploadImg.vue"
  52. import uploadAlert from "@/components/alert/uploadAlert.vue"
  53. import config from "@/assets/http/config.js"
  54. let uploadTask
  55. export default {
  56. name: '',
  57. components: {
  58. compress,
  59. uploadImg,
  60. uploadAlert
  61. },
  62. data() {
  63. return {
  64. //压缩参数
  65. compressParams: {
  66. src: '',
  67. maxSize: 2048,
  68. fileType: 'jpg',
  69. quality: 1,
  70. minSize: 800 //最小压缩尺寸,图片尺寸小于该时值不压缩,非H5平台有效。若需要忽略该设置,可设置为一个极小的值,比如负数。
  71. },
  72. customStyle: {
  73. color: 'white',
  74. background: "#E72226",
  75. fontSize: '32rpx',
  76. width: '340rpx',
  77. margin: '25rpx auto 0'
  78. },
  79. //显示的url,有可能是图文,也有可能是视频
  80. showUrl: '',
  81. applyInfo: {},
  82. activeType: '', //活动类型: VIDEO_TEXT视频图文 IMAGE_TEXT图片文本
  83. updateProgress: '', //上传进度
  84. };
  85. },
  86. onLoad(options) {
  87. if (this.$isEmpty(this.vuex_userId) ||
  88. this.$isEmpty(options.activeType) ||
  89. this.$isEmpty(options.activeId)) {
  90. uni.switchTab({
  91. url: "/pages/index/home"
  92. })
  93. return
  94. }
  95. this.initData(options)
  96. },
  97. methods: {
  98. /**
  99. * 初始化数据
  100. */
  101. initData(options) {
  102. this.customStyle.background = this.vuex_theme.bgColor
  103. this.activeType = options.activeType;
  104. // this.activeType = 'IMAGE_TEXT';
  105. this.applyInfo = {
  106. userId: this.vuex_userId,
  107. phone: this.vuex_phone,
  108. activeId: options.activeId,
  109. title: "",
  110. imgUrl: "",
  111. videoUrl: "",
  112. content: "",
  113. }
  114. },
  115. /**
  116. * 上传封面
  117. */
  118. uploadCover(e) {
  119. this.applyInfo.imgUrl = e[0]
  120. },
  121. /**
  122. * 上传文件
  123. * 图文存进imgUrl
  124. */
  125. async uploadFile(params) {
  126. let res = await this.$api.uploadFile(params)
  127. if (this.activeType == 'VIDEO_TEXT') {
  128. this.applyInfo.videoUrl = res.data.data.link;
  129. } else {
  130. this.applyInfo.imgUrl = res.data.data.link
  131. }
  132. },
  133. /**
  134. * 上传作品
  135. */
  136. handleUploadWork() {
  137. if (this.activeType == 'VIDEO_TEXT') {
  138. //上传视频作品
  139. this.uploadVideoWork()
  140. } else {
  141. //上传图片作品
  142. this.uploadImageWork()
  143. }
  144. },
  145. /**
  146. * 上传图文作品
  147. */
  148. async uploadImageWork() {
  149. let res = await this.$mpi.chooseImage()
  150. this.compressParams.src = res[0]
  151. this.$dialog.showLoading('作品上传中..')
  152. let src= await this.$refs.compress.compress(this.compressParams)
  153. let resp = await this.$api.uploadFile(src)
  154. this.applyInfo.imgUrl = resp.data.data.link
  155. //显示的是图文链接
  156. this.showUrl = this.applyInfo.imgUrl
  157. uni.hideLoading()
  158. },
  159. /**
  160. * 上传视频作品
  161. */
  162. async uploadVideoWork() {
  163. let resp = await this.chooseVideo()
  164. // this.$dialog.showLoading('正在上传中..')
  165. this.$refs.uploadAlert.show = true;
  166. //上传视频链接
  167. let videoRes = await this.uploadVideo(resp.tempFilePath)
  168. this.applyInfo.videoUrl = videoRes.data.link;
  169. //上传视频封面链接
  170. // let coverRes = await this.$api.uploadFile(resp.thumbTempFilePath)
  171. // this.applyInfo.imgUrl = coverRes.data.data.link
  172. // this.$refs.uploadImg.imgList = [this.applyInfo.imgUrl]
  173. //显示的是视频链接
  174. this.showUrl = this.applyInfo.videoUrl
  175. uni.hideLoading()
  176. },
  177. /**
  178. * 取消视频作品
  179. */
  180. cancelUpload(){
  181. uploadTask.abort();
  182. },
  183. /**
  184. * 选择视频
  185. */
  186. chooseVideo() {
  187. var that = this;
  188. return new Promise((resolve, reject) => {
  189. uni.chooseVideo({
  190. count: 1,
  191. sourceType: ['album'],
  192. success: function(res) {
  193. let fileSize = (res.size/1024/1024).toFixed(2)
  194. console.log("视频大小", fileSize)
  195. if(fileSize > 50){
  196. that.$refs.toast.error('上传视频不能大于50M')
  197. return
  198. }
  199. resolve(res)
  200. },
  201. fail() {
  202. uni.hideLoading()
  203. }
  204. });
  205. })
  206. },
  207. /**
  208. * 上传视频
  209. */
  210. uploadVideo(tempFilePath) {
  211. let that = this;
  212. return new Promise((resolve, reject) => {
  213. uploadTask = uni.uploadFile({
  214. url: `${config.baseURL}/blade-resource/oss/endpoint/put-file`,
  215. filePath: tempFilePath,
  216. name: 'file',
  217. formData: { user: 'test' },
  218. header: { "Blade-Auth": uni.getStorageSync('token') },
  219. success(res) {
  220. resolve(JSON.parse(res.data))
  221. that.$refs.uploadAlert.show = false;
  222. that.updateProgress = '';
  223. },
  224. fail(err) {
  225. //用户取消上传
  226. if(err.errMsg === 'uploadFile:fail abort'){
  227. that.$refs.uploadAlert.show = false;
  228. that.updateProgress = '';
  229. that.$refs.toast.warn('取消上传')
  230. }
  231. }
  232. });
  233. uploadTask.onProgressUpdate(function(res) {
  234. that.updateProgress = res;
  235. console.log('上传进度' + res.progress);
  236. console.log('已经上传的数据长度' + res.totalBytesSent);
  237. console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  238. });
  239. });
  240. },
  241. /**
  242. * 压缩视频
  243. */
  244. compressVideo() {
  245. uni.compressVideo({
  246. src: tempFilePath,
  247. quality: 'high', //'low':低,'medium':中,'high':高
  248. bitrate: 0,
  249. fps: 0,
  250. resolution: 0,
  251. success: function(csRes) {
  252. // 未进入
  253. console.log('压缩后大小')
  254. console.log(csRes.size)
  255. },
  256. fail: function(cfRes) {
  257. console.log(cfRes)
  258. uni.showToast({
  259. title: '视频压缩失败',
  260. icon: 'none'
  261. })
  262. }
  263. });
  264. },
  265. submit() {
  266. if (this.activeType == 'VIDEO_TEXT') {
  267. //视频
  268. if (this.$isEmpty(this.applyInfo.videoUrl)) {
  269. this.$refs.toast.warn('请上传视频作品')
  270. return
  271. }
  272. if (this.$isEmpty(this.applyInfo.imgUrl)) {
  273. this.$refs.toast.warn('请上传作品封面')
  274. return
  275. }
  276. } else {
  277. //图文
  278. if (this.$isEmpty(this.applyInfo.imgUrl)) {
  279. this.$refs.toast.warn('请上传图文作品')
  280. return
  281. }
  282. }
  283. if (this.$isEmpty(this.applyInfo.title)) {
  284. this.$refs.toast.warn('请输入标题内容')
  285. return
  286. }
  287. if (this.$isEmpty(this.applyInfo.content)) {
  288. this.$refs.toast.warn('请上传作品封面')
  289. return
  290. }
  291. if (this.$isEmpty(this.applyInfo.phone)) {
  292. this.$refs.toast.warn('请输入手机号')
  293. return
  294. }
  295. this.$api.activity.submitForm(this.applyInfo).then(res => {
  296. if (res.data.success) {
  297. this.$dialog.showModalAndBack('添加成功', this.vuex_theme.bgColor)
  298. }
  299. })
  300. }
  301. }
  302. };
  303. </script>
  304. <style lang="scss" scoped>
  305. .add-media {
  306. width: 700rpx;
  307. height: 428rpx;
  308. border-radius: 14rpx;
  309. background: #FFFFFF;
  310. margin: 40rpx auto;
  311. display: flex;
  312. flex-direction: column;
  313. justify-content: center;
  314. align-items: center;
  315. }
  316. .fill-info {
  317. width: 700rpx;
  318. border-radius: 14rpx;
  319. background: #FFFFFF;
  320. margin: 0 auto;
  321. display: flex;
  322. flex-direction: column;
  323. padding: 40rpx 54rpx 50rpx;
  324. margin-bottom: 88rpx;
  325. .info-title {
  326. position: relative;
  327. margin-bottom: 60rpx;
  328. &:before {
  329. content: '';
  330. width: 8rpx;
  331. height: 32rpx;
  332. background: var(--bgColor);
  333. position: absolute;
  334. left: -18rpx;
  335. top: 50%;
  336. transform: translateY(-50%);
  337. }
  338. }
  339. }
  340. </style>