send.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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="请输入标题" maxlength="30" 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.isCheck }" v-for="item in andList" :key="item.id" @click="handleChange(item)">{{ item.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. townId: null
  125. }
  126. },
  127. created() {
  128. // 获取系统信息
  129. uni.getSystemInfo({
  130. success: (e) => {
  131. // 获取状态栏高度
  132. this.statusBarH = e.statusBarHeight + 10
  133. // // 获取菜单按钮栏高度
  134. let custom = uni.getMenuButtonBoundingClientRect()
  135. this.customBarH = custom.height + 10
  136. }
  137. })
  138. },
  139. onLoad() {
  140. uni.$on('add', this.add)
  141. },
  142. onPageScroll(e) {
  143. if (e.scrollTop > 50) {
  144. this.headerType = true
  145. } else {
  146. this.headerType = false
  147. }
  148. },
  149. methods: {
  150. // 切换关联民宿回调
  151. handleChange(item) {
  152. item.isCheck = !item.isCheck
  153. },
  154. // 点击添加图片回调
  155. handleImage() {
  156. uni.chooseMedia({
  157. count: this.maxImgCount - this.subImgList.length,
  158. mediaType: ['image'],
  159. sizeType: ['compressed'],
  160. success: (res) => {
  161. res.tempFiles.forEach((ele) => {
  162. //这里的id和页面中写的html代码的canvas的id要一致
  163. let canvasId = 'zipCanvas3'
  164. //原图的路径
  165. let imagePath = ele.tempFilePath
  166. //大小限制1024kb
  167. let limitSize = 1024 * 3
  168. //初始绘画区域是画布自身的宽度也就是屏幕宽度
  169. let drawWidth = uni.getSystemInfoSync().windowWidth
  170. // 图片过大压缩
  171. getLessLimitSizeImage(canvasId, imagePath, limitSize, drawWidth, (resPath) => {
  172. this.handleUploadMini(resPath, 1)
  173. })
  174. })
  175. }
  176. })
  177. },
  178. // 点击添加视频回调
  179. handleVideo() {
  180. uni.chooseMedia({
  181. count: 1,
  182. mediaType: ['video'],
  183. success: (res) => {
  184. res.tempFiles.forEach((ele) => {
  185. if (ele.size > 1024 * 1024 * 5) {
  186. // 视频过大压缩
  187. uni.compressVideo({
  188. src: ele.tempFilePath,
  189. quality: 'low',
  190. success: (res) => {
  191. // console.log('压缩后', res)
  192. this.handleUploadMini(res.tempFilePath, 2)
  193. },
  194. fail: (err) => {
  195. // console.log(err)
  196. uni.showToast(
  197. {
  198. title: '视频压缩失败',
  199. icon: 'none'
  200. },
  201. 2000
  202. )
  203. }
  204. })
  205. } else {
  206. this.handleUploadMini(ele.tempFilePath, 2)
  207. }
  208. })
  209. }
  210. })
  211. },
  212. // 上传请求
  213. handleUploadMini(ele, type) {
  214. // 开始上传
  215. uni.showLoading({
  216. title: '上传中'
  217. })
  218. uni.uploadFile({
  219. url: `https://chtech.ncjti.edu.cn/homestay/file/cos/upload`,
  220. filePath: ele,
  221. name: 'files',
  222. success: (uploadFileRes) => {
  223. // console.log(JSON.parse(uploadFileRes.data))
  224. let temRes = JSON.parse(uploadFileRes.data)
  225. if (temRes.code === 200 || temRes.code === 1) {
  226. if (type === 1) {
  227. this.subImgList.push(temRes.data)
  228. } else if (type === 2) {
  229. this.subVideo = temRes.data
  230. }
  231. // console.log(this.subImgList)
  232. // console.log(this.subVideo)
  233. } else {
  234. uni.showToast({
  235. title: temRes.message || '上传失败',
  236. icon: 'none'
  237. })
  238. }
  239. },
  240. fail: () => {
  241. uni.showToast({
  242. title: '上传失败',
  243. icon: 'error'
  244. })
  245. },
  246. complete: (res) => {
  247. uni.hideLoading({
  248. complete: (hide) => {}
  249. })
  250. }
  251. })
  252. },
  253. // 删除图片回调
  254. handleDelete(index) {
  255. this.subImgList.splice(index, 1)
  256. },
  257. // 删除视频回调
  258. handleDeleteVideo() {
  259. this.subVideo = ''
  260. },
  261. // 进入全屏和退出全屏时触发的回调
  262. fullscreenchange(e) {
  263. this.isFullScreen = e.detail.fullScreen
  264. },
  265. // 点击视频控件时触发的回调
  266. handleClickVideo(id) {
  267. this.videoContext = uni.createVideoContext(id)
  268. if (this.isFullScreen) {
  269. this.videoContext.stop()
  270. this.videoContext.exitFullScreen()
  271. } else {
  272. this.videoContext.requestFullScreen()
  273. this.videoContext.play()
  274. }
  275. },
  276. // 点击添加地点回调
  277. handleGoPage() {
  278. this.andList = this.andList.filter((ele) => ele.isCheck)
  279. const list = JSON.stringify(this.andList)
  280. uni.navigateTo({
  281. url: `/pages/addPlace/addPlace?list=${list}&townId=${this.townId}`
  282. })
  283. },
  284. // 全局自定义事件
  285. add(e) {
  286. this.andList = e.list
  287. this.townId = e.townId
  288. },
  289. // 点击发布按钮回调
  290. async handleClickSub() {
  291. if (!this.subImgList.length && !this.subVideo) {
  292. uni.showToast({
  293. title: '请上传图片或者视频',
  294. icon: 'none',
  295. mask: true
  296. })
  297. return
  298. }
  299. if (!this.titleValue) {
  300. uni.showToast({
  301. title: '请输入标题',
  302. icon: 'none',
  303. mask: true
  304. })
  305. return
  306. }
  307. if (!this.contentValue) {
  308. uni.showToast({
  309. title: '请分享你的故事吧~',
  310. icon: 'none',
  311. mask: true
  312. })
  313. return
  314. }
  315. if (!this.andList.length) {
  316. uni.showToast({
  317. title: '请添加地点关联相关民宿',
  318. icon: 'none',
  319. mask: true
  320. })
  321. return
  322. }
  323. if (!this.andList.some((ele) => ele.isCheck)) {
  324. uni.showToast({
  325. title: '请关联至少一个民宿',
  326. icon: 'none',
  327. mask: true
  328. })
  329. return
  330. }
  331. let temList = []
  332. this.andList.forEach((ele) => {
  333. if (ele.isCheck) {
  334. temList.push(ele.id)
  335. }
  336. })
  337. const res = await this.$myRequest({
  338. url: '/mhotel/articleinsertArticleTweet.action',
  339. method: 'post',
  340. data: {
  341. images: this.subImgList,
  342. video: this.subVideo,
  343. townId: this.townId,
  344. hotelIds: temList,
  345. title: this.titleValue,
  346. content: this.contentValue,
  347. createId: uni.getStorageSync('userInfo').id
  348. }
  349. })
  350. // console.log(res)
  351. if (res.code === 200) {
  352. this.$refs.popup.open()
  353. }
  354. },
  355. // 点击弹窗查看推文按钮回调
  356. handleClickLook() {
  357. this.$refs.popup.close()
  358. let userId = uni.getStorageSync('userInfo').id
  359. uni.redirectTo({
  360. url: `/pages/myHome/myHome?userId=${userId}`
  361. })
  362. },
  363. // 点击图片回调
  364. handleLookImg(urls, current) {
  365. uni.previewImage({
  366. urls,
  367. current
  368. })
  369. },
  370. handleBack() {
  371. uni.showModal({
  372. content: '确认退出吗,已编辑的内容将不保存',
  373. cancelText: '退出',
  374. cancelColor: '#808080',
  375. confirmText: '继续发布',
  376. confirmColor: '#000000',
  377. success: (res) => {
  378. if (res.confirm) {
  379. } else if (res.cancel) {
  380. uni.navigateBack(1)
  381. }
  382. }
  383. })
  384. }
  385. }
  386. }
  387. </script>
  388. <style lang="scss">
  389. .container {
  390. padding-bottom: 80rpx;
  391. min-height: 100vh;
  392. background-color: #fff;
  393. .header {
  394. z-index: 1;
  395. position: fixed;
  396. top: 0;
  397. left: 0;
  398. right: 0;
  399. display: flex;
  400. justify-content: center;
  401. // position: relative;
  402. background-color: #fff;
  403. img {
  404. position: absolute;
  405. top: 0;
  406. left: 0;
  407. width: 47rpx;
  408. height: 47rpx;
  409. }
  410. .header_text {
  411. font-size: 34rpx;
  412. }
  413. }
  414. .header2 {
  415. z-index: 1;
  416. position: fixed;
  417. top: 0;
  418. left: 0;
  419. right: 0;
  420. display: flex;
  421. justify-content: center;
  422. background-color: #fff;
  423. img {
  424. position: absolute;
  425. top: 0;
  426. left: 0;
  427. width: 47rpx;
  428. height: 47rpx;
  429. }
  430. .header_text {
  431. font-size: 34rpx;
  432. }
  433. }
  434. .upload {
  435. margin-top: 30rpx;
  436. padding-bottom: 10rpx;
  437. display: flex;
  438. flex-wrap: wrap;
  439. .upload_img {
  440. display: flex;
  441. flex-direction: column;
  442. justify-content: center;
  443. align-items: center;
  444. margin-left: 20rpx;
  445. width: 186rpx;
  446. height: 283rpx;
  447. color: #999999;
  448. font-size: 28rpx;
  449. border-radius: 8rpx;
  450. background-color: #e6e6e6;
  451. img {
  452. margin-top: 10rpx;
  453. width: 68rpx;
  454. height: 68rpx;
  455. }
  456. }
  457. .upload_video {
  458. display: flex;
  459. flex-direction: column;
  460. justify-content: center;
  461. align-items: center;
  462. margin-left: 26rpx;
  463. width: 186rpx;
  464. height: 283rpx;
  465. color: #999999;
  466. font-size: 28rpx;
  467. border-radius: 8rpx;
  468. background-color: #e6e6e6;
  469. img {
  470. margin-top: 12rpx;
  471. width: 68rpx;
  472. height: 68rpx;
  473. }
  474. }
  475. .upload_imgBox {
  476. position: relative;
  477. margin-left: 20rpx;
  478. margin-bottom: 20rpx;
  479. width: 186rpx;
  480. height: 283rpx;
  481. img {
  482. width: 100%;
  483. height: 100%;
  484. }
  485. .icon {
  486. position: absolute;
  487. top: 0;
  488. right: 0;
  489. display: flex;
  490. justify-content: center;
  491. align-items: center;
  492. width: 30rpx;
  493. height: 30rpx;
  494. border-radius: 7rpx;
  495. background-color: rgba(000, 000, 000, 0.5);
  496. img {
  497. width: 30rpx;
  498. height: 30rpx;
  499. }
  500. }
  501. }
  502. .upload_VideoBox {
  503. position: relative;
  504. margin-left: 20rpx;
  505. width: 186rpx;
  506. height: 283rpx;
  507. video {
  508. width: 100%;
  509. height: 100%;
  510. }
  511. .icon {
  512. position: absolute;
  513. top: 0;
  514. right: 0;
  515. display: flex;
  516. justify-content: center;
  517. align-items: center;
  518. width: 30rpx;
  519. height: 30rpx;
  520. border-radius: 7rpx;
  521. background-color: rgba(000, 000, 000, 0.5);
  522. img {
  523. width: 30rpx;
  524. height: 30rpx;
  525. }
  526. }
  527. }
  528. }
  529. .title {
  530. margin: auto;
  531. width: 710rpx;
  532. height: 100rpx;
  533. border-bottom: 1rpx solid #e5e5e5;
  534. .input {
  535. box-sizing: border-box;
  536. padding-right: 20rpx;
  537. width: 100%;
  538. height: 100%;
  539. font-size: 32rpx;
  540. }
  541. }
  542. .content {
  543. margin: auto;
  544. width: 710rpx;
  545. height: 290rpx;
  546. font-size: 28rpx;
  547. border-bottom: 1rpx solid #e5e5e5;
  548. .textarea {
  549. margin-top: 25rpx;
  550. width: 100%;
  551. height: 240rpx;
  552. }
  553. .textarea_length {
  554. margin-top: 5rpx;
  555. text-align: end;
  556. }
  557. }
  558. .add_place {
  559. display: flex;
  560. align-items: center;
  561. margin: auto;
  562. width: 710rpx;
  563. height: 100rpx;
  564. font-size: 28rpx;
  565. border-bottom: 1rpx solid #e5e5e5;
  566. .img {
  567. margin-right: 10rpx;
  568. width: 55rpx;
  569. height: 55rpx;
  570. }
  571. .img2 {
  572. margin-left: auto;
  573. width: 40rpx;
  574. height: 40rpx;
  575. }
  576. }
  577. .and {
  578. display: flex;
  579. align-items: center;
  580. margin: auto;
  581. width: 710rpx;
  582. height: 88rpx;
  583. font-size: 28rpx;
  584. .img {
  585. margin-left: 10rpx;
  586. margin-right: 8rpx;
  587. width: 42rpx;
  588. height: 42rpx;
  589. }
  590. }
  591. .and_list {
  592. display: flex;
  593. flex-wrap: wrap;
  594. margin: auto;
  595. width: 710rpx;
  596. .and_box {
  597. margin-bottom: 20rpx;
  598. margin-right: 16rpx;
  599. padding: 0 44rpx;
  600. display: flex;
  601. justify-content: center;
  602. align-items: center;
  603. height: 55rpx;
  604. color: #999999;
  605. font-size: 24rpx;
  606. border-radius: 66rpx;
  607. background-color: #f2f2f2;
  608. }
  609. .active {
  610. color: #fff;
  611. background-color: #096562;
  612. }
  613. }
  614. .sub_btn {
  615. display: flex;
  616. justify-content: center;
  617. align-items: center;
  618. margin: 310rpx auto 0;
  619. width: 710rpx;
  620. height: 100rpx;
  621. color: #fff;
  622. font-size: 32rpx;
  623. border-radius: 64rpx;
  624. background-color: #096562;
  625. }
  626. .popup_body {
  627. display: flex;
  628. flex-direction: column;
  629. justify-content: space-evenly;
  630. align-items: center;
  631. width: 481rpx;
  632. height: 404rpx;
  633. border-radius: 23rpx;
  634. background-color: #fff;
  635. img {
  636. width: 112rpx;
  637. height: 112rpx;
  638. }
  639. .msg {
  640. color: #0f194d;
  641. font-size: 28rpx;
  642. }
  643. .btn {
  644. display: flex;
  645. justify-content: center;
  646. align-items: center;
  647. width: 396rpx;
  648. height: 76rpx;
  649. font-size: 26rpx;
  650. color: #fff;
  651. border-radius: 43rpx;
  652. background: linear-gradient(90deg, rgba(11, 193, 150, 1) 0%, rgba(9, 101, 98, 1) 100%);
  653. }
  654. }
  655. }
  656. </style>