send.vue 15 KB

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