send.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <template>
  2. <view class="container">
  3. <!-- 页面标题区域 -->
  4. <view v-if="!headerType" class="header" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }">
  5. <img src="../../static/index/left2.png" :style="{ paddingTop: statusBarH * 2 + 'rpx' }" @click="handleBack" />
  6. <view class="header_text">发推文</view>
  7. </view>
  8. <view v-else class="header2" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }">
  9. <img src="../../static/index/left2.png" :style="{ paddingTop: statusBarH * 2 + 'rpx' }" @click="handleBack" />
  10. <view class="header_text">发推文</view>
  11. </view>
  12. <!-- 图片视频上传展示区域 -->
  13. <view class="upload" :style="{ paddingTop: statusBarH * 2 + customBarH * 2 + 'rpx' }">
  14. <view v-if="subImgList.length < maxImgCount && !subVideo" class="upload_img" @click="handleImage">
  15. <img src="../../static/index/upload-img.png" />
  16. 添加图片
  17. </view>
  18. <view v-if="!subImgList.length && !subVideo" class="upload_video" @click="handleVideo">
  19. <img src="../../static/index/upload-video.png" />
  20. 添加视频
  21. </view>
  22. <!-- 图片展示区域 -->
  23. <view class="upload_imgBox" v-for="(item, index) in subImgList" :key="index">
  24. <img mode="aspectFill" :src="item" @click="handleLookImg(subImgList, index)" />
  25. <view class="icon" @click="handleDelete(index)">
  26. <img src="../../static/index/close2.png" />
  27. </view>
  28. </view>
  29. <!-- 视频展示区域 -->
  30. <view v-if="subVideo" class="upload_VideoBox">
  31. <video
  32. id="myVideo"
  33. :show-fullscreen-btn="false"
  34. :show-play-btn="false"
  35. :src="subVideo"
  36. @fullscreenchange="fullscreenchange"
  37. @click="handleClickVideo('myVideo')"
  38. ></video>
  39. <view class="icon" @click="handleDeleteVideo">
  40. <img src="../../static/index/close2.png" />
  41. </view>
  42. </view>
  43. </view>
  44. <!-- 标题输入区域 -->
  45. <view class="title">
  46. <input class="input" type="text" placeholder-style="color:#B3B3B3" placeholder="请输入标题" v-model="titleValue" />
  47. </view>
  48. <!-- 推文内容输入区域 -->
  49. <view class="content">
  50. <textarea class="textarea" placeholder-style="color:#B3B3B3" maxlength="3000" placeholder="请分享你的故事吧~" v-model="contentValue"></textarea>
  51. <!-- 内容长度 -->
  52. <view class="textarea_length">{{ contentValue.length }}/3000</view>
  53. </view>
  54. <!-- 添加地点区域 -->
  55. <view class="add_place" @click="handleGoPage">
  56. <img class="img" src="../../static/index/send-address.png" />
  57. 添加地点
  58. <img class="img2" src="../../static/index/send-right.png" />
  59. </view>
  60. <!-- 关联民宿区域 -->
  61. <view class="and" v-if="andList.length">
  62. <img class="img" src="../../static/index/send-binding.png" />
  63. 关联民宿
  64. </view>
  65. <!-- 关联民宿列表区域 -->
  66. <view class="and_list">
  67. <!-- 每一个民宿区域 -->
  68. <view class="and_box" :class="{ active: item.is_collect_hotel }" v-for="item in andList" :key="item.id" @click="handleChange(item)">{{ item.hotel_name }}</view>
  69. </view>
  70. <!-- 发布按钮区域 -->
  71. <view class="sub_btn" @click="handleClickSub">发布</view>
  72. <!-- 状态弹窗区域 -->
  73. <uni-popup ref="popup" :is-mask-click="false" type="center">
  74. <view class="popup_body">
  75. <img src="../../static/index/success.png" />
  76. <view class="msg">发布成功</view>
  77. <view class="btn" @click="handleClickLook">查看推文</view>
  78. </view>
  79. </uni-popup>
  80. <!-- 用于图片压缩的canvas画布 -->
  81. <canvas
  82. :style="{
  83. width: cw + 'px',
  84. height: cw + 'px',
  85. position: 'absolute',
  86. zIndex: -1,
  87. left: '-10000rpx',
  88. top: '-10000rpx'
  89. }"
  90. canvas-id="zipCanvas3"
  91. ></canvas>
  92. <!--画布结束-->
  93. </view>
  94. </template>
  95. <script>
  96. // 图片压缩方法
  97. import getLessLimitSizeImage from '@/util/imageCompress.js'
  98. export default {
  99. data() {
  100. return {
  101. // 顶部页面标题栏显示隐藏控制
  102. headerType: false,
  103. // 状态栏高度
  104. statusBarH: 0,
  105. // 胶囊按钮栏高度
  106. customBarH: 0,
  107. // 标题数据
  108. titleValue: '',
  109. // 推文内容区域
  110. contentValue: '',
  111. // activeIndex: null,
  112. // 关联民宿列表
  113. andList: [],
  114. // 上传的图片数据
  115. subImgList: [],
  116. // 最大上传图片张图
  117. maxImgCount: 9,
  118. // 上传的视频数据
  119. subVideo: '',
  120. // video 上下文 videoContext 对象
  121. videoContext: null,
  122. // 是否是全屏状态
  123. isFullScreen: false
  124. }
  125. },
  126. created() {
  127. // 获取系统信息
  128. uni.getSystemInfo({
  129. success: (e) => {
  130. // 获取状态栏高度
  131. this.statusBarH = e.statusBarHeight + 10
  132. // // 获取菜单按钮栏高度
  133. let custom = uni.getMenuButtonBoundingClientRect()
  134. this.customBarH = custom.height + 10
  135. }
  136. })
  137. },
  138. onLoad() {
  139. uni.$on('add', this.add)
  140. },
  141. onPageScroll(e) {
  142. if (e.scrollTop > 50) {
  143. this.headerType = true
  144. } else {
  145. this.headerType = false
  146. }
  147. },
  148. methods: {
  149. // 切换关联民宿回调
  150. handleChange(item) {
  151. item.is_collect_hotel = !item.is_collect_hotel
  152. },
  153. // 点击添加图片回调
  154. handleImage() {
  155. uni.chooseMedia({
  156. count: this.maxImgCount - this.subImgList.length,
  157. mediaType: ['image'],
  158. sizeType: ['compressed'],
  159. success: (res) => {
  160. res.tempFiles.forEach((ele) => {
  161. //这里的id和页面中写的html代码的canvas的id要一致
  162. let canvasId = 'zipCanvas3'
  163. //原图的路径
  164. let imagePath = ele.tempFilePath
  165. //大小限制1024kb
  166. let limitSize = 1024 * 3
  167. //初始绘画区域是画布自身的宽度也就是屏幕宽度
  168. let drawWidth = uni.getSystemInfoSync().windowWidth
  169. // 图片过大压缩
  170. getLessLimitSizeImage(canvasId, imagePath, limitSize, drawWidth, (resPath) => {
  171. this.handleUploadMini(resPath, 1)
  172. })
  173. })
  174. }
  175. })
  176. },
  177. // 点击添加视频回调
  178. handleVideo() {
  179. uni.chooseMedia({
  180. count: 1,
  181. mediaType: ['video'],
  182. success: (res) => {
  183. res.tempFiles.forEach((ele) => {
  184. if (ele.size > 1024 * 1024 * 5) {
  185. // 视频过大压缩
  186. uni.compressVideo({
  187. src: ele.tempFilePath,
  188. quality: 'low',
  189. success: (res) => {
  190. // console.log('压缩后', res)
  191. this.handleUploadMini(res.tempFilePath, 2)
  192. },
  193. fail: (err) => {
  194. // console.log(err)
  195. uni.showToast(
  196. {
  197. title: '视频压缩失败',
  198. icon: 'none'
  199. },
  200. 2000
  201. )
  202. }
  203. })
  204. } else {
  205. this.handleUploadMini(ele.tempFilePath, 2)
  206. }
  207. })
  208. }
  209. })
  210. },
  211. // 上传请求
  212. handleUploadMini(ele, type) {
  213. // 开始上传
  214. uni.showLoading({
  215. title: '上传中'
  216. })
  217. uni.uploadFile({
  218. url: `https://chtech.ncjti.edu.cn/homestay/file/cos/upload`,
  219. filePath: ele,
  220. name: 'files',
  221. success: (uploadFileRes) => {
  222. // console.log(JSON.parse(uploadFileRes.data))
  223. let temRes = JSON.parse(uploadFileRes.data)
  224. if (temRes.code === 200 || temRes.code === 1) {
  225. if (type === 1) {
  226. this.subImgList.push(temRes.data)
  227. } else if (type === 2) {
  228. this.subVideo = temRes.data
  229. }
  230. // console.log(this.subImgList)
  231. // console.log(this.subVideo)
  232. } else {
  233. uni.showToast({
  234. title: temRes.message || '上传失败',
  235. icon: 'none'
  236. })
  237. }
  238. },
  239. fail: () => {
  240. uni.showToast({
  241. title: '上传失败',
  242. icon: 'error'
  243. })
  244. },
  245. complete: (res) => {
  246. uni.hideLoading({
  247. complete: (hide) => {}
  248. })
  249. }
  250. })
  251. },
  252. // 删除图片回调
  253. handleDelete(index) {
  254. this.subImgList.splice(index, 1)
  255. },
  256. // 删除视频回调
  257. handleDeleteVideo() {
  258. this.subVideo = ''
  259. },
  260. // 进入全屏和退出全屏时触发的回调
  261. fullscreenchange(e) {
  262. this.isFullScreen = e.detail.fullScreen
  263. },
  264. // 点击视频控件时触发的回调
  265. handleClickVideo(id) {
  266. this.videoContext = uni.createVideoContext(id)
  267. if (this.isFullScreen) {
  268. this.videoContext.stop()
  269. this.videoContext.exitFullScreen()
  270. } else {
  271. this.videoContext.requestFullScreen()
  272. this.videoContext.play()
  273. }
  274. },
  275. // 点击添加地点回调
  276. handleGoPage() {
  277. this.andList = this.andList.filter((ele) => ele.is_collect_hotel)
  278. const list = JSON.stringify(this.andList)
  279. uni.navigateTo({
  280. url: `/pages/addPlace/addPlace?list=${list}`
  281. })
  282. },
  283. // 全局自定义事件
  284. add(e) {
  285. this.andList = e.list
  286. },
  287. // 点击发布按钮回调
  288. handleClickSub() {
  289. if (!this.subImgList.length && !this.subVideo) {
  290. uni.showToast({
  291. title: '请上传图片或者视频',
  292. icon: 'none',
  293. mask: true
  294. })
  295. return
  296. }
  297. if (!this.titleValue) {
  298. uni.showToast({
  299. title: '请输入标题',
  300. icon: 'none',
  301. mask: true
  302. })
  303. return
  304. }
  305. if (!this.contentValue) {
  306. uni.showToast({
  307. title: '请分享你的故事吧~',
  308. icon: 'none',
  309. mask: true
  310. })
  311. return
  312. }
  313. let temList = this.andList.filter((ele) => ele.is_collect_hotel)
  314. console.log(temList)
  315. console.log(this.subImgList)
  316. console.log(this.subVideo)
  317. console.log(this.titleValue)
  318. console.log(this.contentValue)
  319. this.$refs.popup.open()
  320. },
  321. // 点击弹窗查看推文按钮回调
  322. handleClickLook() {
  323. this.$refs.popup.close()
  324. },
  325. // 点击图片回调
  326. handleLookImg(urls, current) {
  327. uni.previewImage({
  328. urls,
  329. current
  330. })
  331. },
  332. handleBack() {
  333. uni.navigateBack(1)
  334. }
  335. }
  336. }
  337. </script>
  338. <style lang="scss">
  339. .container {
  340. padding-bottom: 80rpx;
  341. min-height: 100vh;
  342. background-color: #fff;
  343. .header {
  344. z-index: 1;
  345. position: fixed;
  346. top: 0;
  347. left: 0;
  348. right: 0;
  349. display: flex;
  350. justify-content: center;
  351. // position: relative;
  352. background-color: #fff;
  353. img {
  354. position: absolute;
  355. top: 0;
  356. left: 0;
  357. width: 47rpx;
  358. height: 47rpx;
  359. }
  360. .header_text {
  361. font-size: 34rpx;
  362. }
  363. }
  364. .header2 {
  365. z-index: 1;
  366. position: fixed;
  367. top: 0;
  368. left: 0;
  369. right: 0;
  370. display: flex;
  371. justify-content: center;
  372. background-color: #fff;
  373. img {
  374. position: absolute;
  375. top: 0;
  376. left: 0;
  377. width: 47rpx;
  378. height: 47rpx;
  379. }
  380. .header_text {
  381. font-size: 34rpx;
  382. }
  383. }
  384. .upload {
  385. margin-top: 30rpx;
  386. padding-bottom: 10rpx;
  387. display: flex;
  388. flex-wrap: wrap;
  389. .upload_img {
  390. display: flex;
  391. flex-direction: column;
  392. justify-content: center;
  393. align-items: center;
  394. margin-left: 20rpx;
  395. width: 186rpx;
  396. height: 283rpx;
  397. color: #999999;
  398. font-size: 28rpx;
  399. border-radius: 8rpx;
  400. background-color: #e6e6e6;
  401. img {
  402. margin-top: 10rpx;
  403. width: 68rpx;
  404. height: 68rpx;
  405. }
  406. }
  407. .upload_video {
  408. display: flex;
  409. flex-direction: column;
  410. justify-content: center;
  411. align-items: center;
  412. margin-left: 26rpx;
  413. width: 186rpx;
  414. height: 283rpx;
  415. color: #999999;
  416. font-size: 28rpx;
  417. border-radius: 8rpx;
  418. background-color: #e6e6e6;
  419. img {
  420. margin-top: 12rpx;
  421. width: 68rpx;
  422. height: 68rpx;
  423. }
  424. }
  425. .upload_imgBox {
  426. position: relative;
  427. margin-left: 20rpx;
  428. margin-bottom: 20rpx;
  429. width: 186rpx;
  430. height: 283rpx;
  431. img {
  432. width: 100%;
  433. height: 100%;
  434. }
  435. .icon {
  436. position: absolute;
  437. top: 0;
  438. right: 0;
  439. display: flex;
  440. justify-content: center;
  441. align-items: center;
  442. width: 30rpx;
  443. height: 30rpx;
  444. border-radius: 7rpx;
  445. background-color: rgba(000, 000, 000, 0.5);
  446. img {
  447. width: 30rpx;
  448. height: 30rpx;
  449. }
  450. }
  451. }
  452. .upload_VideoBox {
  453. position: relative;
  454. margin-left: 20rpx;
  455. width: 186rpx;
  456. height: 283rpx;
  457. video {
  458. width: 100%;
  459. height: 100%;
  460. }
  461. .icon {
  462. position: absolute;
  463. top: 0;
  464. right: 0;
  465. display: flex;
  466. justify-content: center;
  467. align-items: center;
  468. width: 30rpx;
  469. height: 30rpx;
  470. border-radius: 7rpx;
  471. background-color: rgba(000, 000, 000, 0.5);
  472. img {
  473. width: 30rpx;
  474. height: 30rpx;
  475. }
  476. }
  477. }
  478. }
  479. .title {
  480. margin: auto;
  481. width: 710rpx;
  482. height: 100rpx;
  483. border-bottom: 1rpx solid #e5e5e5;
  484. .input {
  485. box-sizing: border-box;
  486. padding-right: 20rpx;
  487. width: 100%;
  488. height: 100%;
  489. font-size: 32rpx;
  490. }
  491. }
  492. .content {
  493. margin: auto;
  494. width: 710rpx;
  495. height: 290rpx;
  496. font-size: 28rpx;
  497. border-bottom: 1rpx solid #e5e5e5;
  498. .textarea {
  499. margin-top: 25rpx;
  500. width: 100%;
  501. height: 240rpx;
  502. }
  503. .textarea_length {
  504. margin-top: 5rpx;
  505. text-align: end;
  506. }
  507. }
  508. .add_place {
  509. display: flex;
  510. align-items: center;
  511. margin: auto;
  512. width: 710rpx;
  513. height: 100rpx;
  514. font-size: 28rpx;
  515. border-bottom: 1rpx solid #e5e5e5;
  516. .img {
  517. margin-right: 10rpx;
  518. width: 55rpx;
  519. height: 55rpx;
  520. }
  521. .img2 {
  522. margin-left: auto;
  523. width: 40rpx;
  524. height: 40rpx;
  525. }
  526. }
  527. .and {
  528. display: flex;
  529. align-items: center;
  530. margin: auto;
  531. width: 710rpx;
  532. height: 88rpx;
  533. font-size: 28rpx;
  534. .img {
  535. margin-left: 10rpx;
  536. margin-right: 8rpx;
  537. width: 42rpx;
  538. height: 42rpx;
  539. }
  540. }
  541. .and_list {
  542. display: flex;
  543. flex-wrap: wrap;
  544. margin: auto;
  545. width: 710rpx;
  546. .and_box {
  547. margin-bottom: 20rpx;
  548. margin-right: 16rpx;
  549. padding: 0 44rpx;
  550. display: flex;
  551. justify-content: center;
  552. align-items: center;
  553. height: 55rpx;
  554. color: #999999;
  555. font-size: 24rpx;
  556. border-radius: 66rpx;
  557. background-color: #f2f2f2;
  558. }
  559. .active {
  560. color: #fff;
  561. background-color: #096562;
  562. }
  563. }
  564. .sub_btn {
  565. display: flex;
  566. justify-content: center;
  567. align-items: center;
  568. margin: 310rpx auto 0;
  569. width: 710rpx;
  570. height: 100rpx;
  571. color: #fff;
  572. font-size: 32rpx;
  573. border-radius: 64rpx;
  574. background-color: #096562;
  575. }
  576. .popup_body {
  577. display: flex;
  578. flex-direction: column;
  579. justify-content: space-evenly;
  580. align-items: center;
  581. width: 481rpx;
  582. height: 404rpx;
  583. border-radius: 23rpx;
  584. background-color: #fff;
  585. img {
  586. width: 112rpx;
  587. height: 112rpx;
  588. }
  589. .msg {
  590. color: #0f194d;
  591. font-size: 28rpx;
  592. }
  593. .btn {
  594. display: flex;
  595. justify-content: center;
  596. align-items: center;
  597. width: 396rpx;
  598. height: 76rpx;
  599. font-size: 26rpx;
  600. color: #fff;
  601. border-radius: 43rpx;
  602. background: linear-gradient(90deg, rgba(11, 193, 150, 1) 0%, rgba(9, 101, 98, 1) 100%);
  603. }
  604. }
  605. }
  606. </style>