InteractIssue.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <div class="InteractIssue">
  3. <div class="txt1">
  4. <div class="txtBs">{{ txt1.length }}/20</div>
  5. <van-field
  6. v-model.trim="txt1"
  7. maxlength="20"
  8. placeholder="在此处填写标题"
  9. />
  10. </div>
  11. <div class="txt2">
  12. <div class="txtBs">{{ txt2.length }}/500</div>
  13. <van-field
  14. v-model="txt2"
  15. type="textarea"
  16. maxlength="500"
  17. placeholder="在此处填写正文"
  18. />
  19. </div>
  20. <div class="imgTit">
  21. <img src="../../../assets/img/interact/topic.png" alt="" />
  22. 话题:
  23. </div>
  24. <!-- 话题标签 -->
  25. <div class="topicBox">
  26. <div
  27. class="topic"
  28. v-for="item in topicData"
  29. :key="item.id"
  30. :class="{ topicAc: topic === item.id }"
  31. @click="topic = item.id"
  32. >
  33. #{{ item.name }}
  34. </div>
  35. </div>
  36. <div class="imgTit" @click="LocationShow = true">
  37. <div class="rightInco">
  38. <van-icon name="arrow" />
  39. </div>
  40. <img src="../../../assets/img/interact/add.png" alt="" />
  41. 位置:
  42. </div>
  43. <!-- 定位标签 -->
  44. <div class="locationBox" v-show="locationInfo.id">
  45. <div class="location locationAc">{{ locationInfo.name }}</div>
  46. </div>
  47. <!-- 上传图片 -->
  48. <div class="upFile">
  49. <div>
  50. <input
  51. type="file"
  52. accept=".png,.jpg,.gif"
  53. ref="myInput"
  54. @input="handeUpPhoto"
  55. v-show="0"
  56. />
  57. <div
  58. class="upImg"
  59. @click="$refs.myInput.click()"
  60. v-show="imgList.length < 5"
  61. ></div>
  62. </div>
  63. <Draggable
  64. class="moveDraggable"
  65. :class="{ moveDraggableAll: imgList.length === 5 }"
  66. v-model="imgList"
  67. group="itxst"
  68. @start="start"
  69. animation="300"
  70. >
  71. <transition-group>
  72. <div class="imgRow" v-for="(item, index) in imgList" :key="item.id">
  73. <img
  74. :src="baseURL + item.filePath"
  75. alt=""
  76. @click="lookImg(baseURL + item.filePath)"
  77. />
  78. <!-- 删除 -->
  79. <div class="delImg" @click="delImg(item.id)"></div>
  80. <!-- 封面 -->
  81. <div class="imgCover" v-show="index === 0">封面</div>
  82. </div>
  83. </transition-group>
  84. </Draggable>
  85. </div>
  86. <div class="imgUpTit">
  87. 可拖动图片调整顺序,首张图将作为封面。<br />最大支持5M,最多可上传5张图片。
  88. </div>
  89. <!-- 发布按钮 -->
  90. <div class="issueBtn" @click="issueDone">发 布</div>
  91. <!-- 点击定位之后出来的页面 -->
  92. <Location
  93. v-show="LocationShow"
  94. @closeLoc="LocationShow = false"
  95. @setLocationInfo="setLocationInfo"
  96. />
  97. </div>
  98. </template>
  99. <script>
  100. import { baseURL } from "@/utils/request";
  101. import {
  102. uploadImgAPI,
  103. shareSaveAPI,
  104. delImgAPI,
  105. getDictAPI,
  106. } from "@/api/interact.js";
  107. import Location from "./interactLocation.vue";
  108. import { Toast } from "vant";
  109. //导入draggable组件
  110. import Draggable from "vuedraggable";
  111. import { Dialog, ImagePreview } from "vant";
  112. export default {
  113. name: "InteractIssue",
  114. components: { Draggable, Location },
  115. data() {
  116. return {
  117. baseURL,
  118. txt1: "",
  119. txt2: "",
  120. topicData: [
  121. // { id: 1, name: "景点" },
  122. // { id: 2, name: "美食" },
  123. // { id: 3, name: "游玩" },
  124. // { id: 4, name: "酒店" },
  125. ],
  126. topic: 1,
  127. locationInfo: {},
  128. LocationShow: false,
  129. // 上传相关
  130. imgList: [
  131. // {
  132. // id: 65,
  133. // fileName: "22222.jpg",
  134. // filePath: "/comment/img/20221125_1113069452.jpg",
  135. // },
  136. ],
  137. };
  138. },
  139. computed: {},
  140. watch: {},
  141. methods: {
  142. // -------------关于上传
  143. async handeUpPhoto(e) {
  144. if (e.target.files) {
  145. // 拿到files信息
  146. const filesInfo = e.target.files[0];
  147. // 校验格式
  148. const type = ["image/jpeg", "image/png", "image/gif"];
  149. if (!type.includes(filesInfo.type)) {
  150. e.target.value = "";
  151. return Toast.fail("只支持jpg、png、gif格式!");
  152. }
  153. // 校验大小
  154. if (filesInfo.size > 5 * 1024 * 1024) {
  155. e.target.value = "";
  156. return Toast.fail("最大支持5M!");
  157. }
  158. // 创建FormData对象
  159. const fd = new FormData();
  160. // 把files添加进FormData对象(‘photo’为后端需要的字段)
  161. fd.append("file", filesInfo);
  162. fd.append("type", "img");
  163. const res = await uploadImgAPI(fd);
  164. if (res.code === 0) {
  165. Toast.success("上传成功!");
  166. } else Toast.fail(res.msg);
  167. this.imgList.push(res.data);
  168. }
  169. },
  170. // 设置位置
  171. setLocationInfo(item) {
  172. this.locationInfo = item;
  173. this.LocationShow = false;
  174. },
  175. // 点击发布
  176. async issueDone() {
  177. if (this.txt1 === "") return Toast.fail("标题不能为空!");
  178. if (this.txt2.replaceAll("\n", "").replaceAll(" ", "") === "")
  179. return Toast.fail("正文不能为空!");
  180. if (!this.locationInfo.id) return Toast.fail("请选择位置!");
  181. if (this.imgList.length === 0) return Toast.fail("至少上传一张图片!");
  182. // 图片id数组
  183. const imgArr = this.imgList.map((v) => {
  184. return v.id;
  185. });
  186. const obj = {
  187. description: this.txt2
  188. .replaceAll("\n", "<br/>")
  189. .replaceAll(" ", "&ensp;"),
  190. positionId: this.locationInfo.id,
  191. dictTopicId: this.topic,
  192. fileIds: imgArr.join(","),
  193. name: this.txt1,
  194. thumb: this.imgList[0].filePath,
  195. };
  196. const res = await shareSaveAPI(obj);
  197. if (res.code === 0) {
  198. Toast.success("发布成功!");
  199. this.$router.push("/layout/interact");
  200. } else Toast.fail(res.msg);
  201. },
  202. // 拖拽图片
  203. start(e) {},
  204. // 查看大图
  205. lookImg(url) {
  206. ImagePreview({
  207. images: [url],
  208. showIndex: false,
  209. });
  210. },
  211. // 点击删除图片
  212. delImg(id) {
  213. Dialog.confirm({
  214. title: "确定删除吗?",
  215. beforeClose: async (action, done) => {
  216. if (action === "confirm") {
  217. await delImgAPI(id);
  218. // 点击了确定
  219. this.imgList = this.imgList.filter((v) => v.id !== id);
  220. Toast.success("删除成功!");
  221. }
  222. done();
  223. },
  224. });
  225. },
  226. },
  227. async created() {
  228. // 获取话题列表
  229. const res = await getDictAPI("topic");
  230. this.topicData = res.data;
  231. },
  232. mounted() {},
  233. beforeCreate() {}, //生命周期 - 创建之前
  234. beforeMount() {}, //生命周期 - 挂载之前
  235. beforeUpdate() {}, //生命周期 - 更新之前
  236. updated() {}, //生命周期 - 更新之后
  237. beforeDestroy() {}, //生命周期 - 销毁之前
  238. destroyed() {}, //生命周期 - 销毁完成
  239. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  240. };
  241. </script>
  242. <style lang='less' scoped>
  243. .InteractIssue {
  244. width: 100%;
  245. height: 100%;
  246. padding: 20px 15px;
  247. overflow-y: auto;
  248. /deep/.van-cell {
  249. width: 100%;
  250. height: 36px;
  251. line-height: 36px;
  252. padding: 0 40px 0 5px;
  253. background-color: #f4f4f4;
  254. }
  255. .txt1 {
  256. width: 100%;
  257. height: 36px;
  258. position: relative;
  259. .txtBs {
  260. z-index: 3;
  261. color: #9e9a9a;
  262. font-size: 14px;
  263. position: absolute;
  264. right: 3px;
  265. top: 50%;
  266. transform: translateY(-50%);
  267. }
  268. }
  269. .txt2 {
  270. margin: 20px 0;
  271. position: relative;
  272. .txtBs {
  273. z-index: 3;
  274. color: #9e9a9a;
  275. font-size: 14px;
  276. position: absolute;
  277. right: 3px;
  278. bottom: 5px;
  279. }
  280. /deep/.van-field__body {
  281. height: calc(100% - 30px);
  282. }
  283. /deep/textarea {
  284. height: 100%;
  285. }
  286. /deep/.van-cell {
  287. line-height: 21px;
  288. width: 100%;
  289. height: 200px;
  290. padding: 0 5px;
  291. background-color: #f4f4f4;
  292. }
  293. }
  294. .imgTit {
  295. margin: 20px 0;
  296. font-size: 18px;
  297. font-weight: 700;
  298. display: flex;
  299. align-items: center;
  300. position: relative;
  301. .rightInco {
  302. position: absolute;
  303. right: 0;
  304. top: 50%;
  305. transform: translateY(-50%);
  306. color: #c2c2c2;
  307. font-size: 22px;
  308. }
  309. & > img {
  310. width: 20px;
  311. margin-right: 8px;
  312. }
  313. }
  314. .topicBox {
  315. display: flex;
  316. flex-wrap: wrap;
  317. .topic {
  318. font-size: 14px;
  319. border-radius: 17px;
  320. border: 1px solid #393939;
  321. min-width: 60px;
  322. padding: 0 10px;
  323. height: 34px;
  324. line-height: 32px;
  325. margin-right: 16px;
  326. margin-bottom: 12px;
  327. }
  328. .topicAc {
  329. border: none;
  330. line-height: 34px;
  331. background-color: #ff7e89;
  332. color: #fff;
  333. }
  334. }
  335. .locationBox {
  336. display: flex;
  337. flex-wrap: wrap;
  338. .location {
  339. font-size: 14px;
  340. border-radius: 17px;
  341. border: 1px solid #393939;
  342. min-width: 60px;
  343. padding: 0 10px;
  344. height: 34px;
  345. line-height: 32px;
  346. margin-right: 16px;
  347. margin-bottom: 12px;
  348. text-align: center;
  349. }
  350. .locationAc {
  351. border: none;
  352. line-height: 34px;
  353. background-color: #b5d4ff;
  354. color: #fff;
  355. }
  356. }
  357. .upFile {
  358. margin: 20px 0 0px;
  359. display: flex;
  360. flex-wrap: wrap;
  361. .upImg {
  362. width: 80px;
  363. height: 80px;
  364. background-image: url("../../../assets/img/interact/upload.png");
  365. background-size: 100% 100%;
  366. margin-right: 10px;
  367. margin-bottom: 10px;
  368. }
  369. .moveDraggable {
  370. width: calc(100% - 90px);
  371. }
  372. .moveDraggableAll {
  373. width: 100%;
  374. }
  375. .moveDraggable span {
  376. display: flex;
  377. flex-wrap: wrap;
  378. }
  379. .imgRow {
  380. position: relative;
  381. margin-right: 10px;
  382. margin-bottom: 10px;
  383. width: 80px;
  384. height: 80px;
  385. border-radius: 6px;
  386. & > img {
  387. width: 100%;
  388. height: 100%;
  389. object-fit: cover;
  390. }
  391. .delImg {
  392. position: absolute;
  393. width: 16px;
  394. height: 16px;
  395. top: -6px;
  396. right: -6px;
  397. background-image: url("../../../assets/img/interact/del.png");
  398. background-size: 100% 100%;
  399. }
  400. .imgCover {
  401. pointer-events: none;
  402. position: absolute;
  403. bottom: 0;
  404. left: 0;
  405. width: 100%;
  406. height: 40px;
  407. line-height: 50px;
  408. text-align: center;
  409. font-size: 14px;
  410. color: #fff;
  411. background-image: url("../../../assets/img/interact/bg.png");
  412. background-size: 100% 100%;
  413. }
  414. }
  415. }
  416. .imgUpTit {
  417. font-size: 12px;
  418. color: #a6a6a6;
  419. margin: 10px 0 40px;
  420. }
  421. .issueBtn {
  422. width: 80%;
  423. height: 50px;
  424. max-width: 300px;
  425. margin: 0 auto 30px;
  426. line-height: 50px;
  427. background-color: #ff7e89;
  428. border-radius: 25px;
  429. text-align: center;
  430. color: #fff;
  431. font-size: 18px;
  432. }
  433. }
  434. </style>