published_works.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <view class="container" :style="{'padding-bottom': pageHeight+'px'}">
  3. <view class="main">
  4. <!-- 上传图片 -->
  5. <view class="uploader-container flex wrap">
  6. <u-upload @on-change="change" :action="action" :header="{'token': token,'version': version}" deletable
  7. :max-count="9" @on-remove="remove" multiple custom-btn :width="160" :height="160" ref="upload"
  8. :fileList="fileList" :show-progress="false">
  9. <view slot="addBtn" class="uplader-upload" hover-class="slot-btn__hover" hover-stay-time="150">
  10. <u-icon size="56" color="#b1b1b1" name="camera" />
  11. </view>
  12. </u-upload>
  13. </view>
  14. <!-- 评论内容 -->
  15. <view class="content">
  16. <textarea v-model="formData.content" id="" maxlength="999" placeholder="说说你的购物体验和心得,我们都很期待呢~"
  17. :disable-default-padding="true" @focus="handleFocus" @blur="pageHeight = 0" :show-confirm-bar="false"></textarea>
  18. <view class="muted text-right">
  19. {{ formData.content.length }}/999
  20. </view>
  21. </view>
  22. <!-- 宝贝推荐 || 店铺推荐-->
  23. <view class="item flex row-between recommend" @click="showRecommend">
  24. <view class="flex nr normal">
  25. <image src="/bundle_b/static/icon_recommend.png" class="image m-r-10"></image>
  26. <text v-if="formData.shop.length===0 && formData.goods.length===0">宝贝/店铺</text>
  27. <text v-if="formData.shop.length!==0">店铺</text>
  28. <text v-if="formData.goods.length!==0">宝贝</text>
  29. </view>
  30. <view class="nr flex">
  31. <text class="muted m-r-10"
  32. v-if="formData.shop.length===0 && formData.goods.length===0">选择购买过的商品</text>
  33. <template v-if="formData.goods.length!==0">
  34. <block v-for="(item, index) in formData.goods" :key="index">
  35. <u-image v-if="index<=2" :src="item.image" width="58" height="58" class="m-l-6">
  36. </u-image>
  37. </block>
  38. </template>
  39. <template v-if="formData.shop.length!==0">
  40. <block v-for="(item, index) in formData.shop" :key="index">
  41. <u-image v-if="index<=2" :src="item.logo" width="58" height="58" class="m-l-6">
  42. </u-image>
  43. </block>
  44. </template>
  45. <u-icon name="arrow-right" size="22" color="#707070" class="m-l-10" />
  46. </view>
  47. </view>
  48. <!-- 话题 -->
  49. <view class="item flex row-between topic" @click="showTopicPopup=true">
  50. <view class="flex nr normal">
  51. <image src="/bundle_b/static/icon_topic.png" class="image m-r-10"></image>
  52. <text>话题</text>
  53. </view>
  54. <view class="nr flex">
  55. <view class="tags primary-tags m-r-10" v-if="formData.topic_id">
  56. <text>#{{ formData.topic_id.name }}</text>
  57. </view>
  58. <u-icon name="arrow-right" size="22" color="#707070"></u-icon>
  59. </view>
  60. </view>
  61. <!-- 初始化热门标签 -->
  62. <view class="tags m-t-10 gary-tags" v-show="!formData.topic_id">
  63. <block v-for="topicItem in recommendTopic" :key="topicItem.id">
  64. <text @click="handleTopic(topicItem)">#{{ topicItem.name }}</text>
  65. </block>
  66. </view>
  67. </view>
  68. <!-- 底部 -->
  69. <view class="footer">
  70. <button class="br60 white btn lg" @click="onSubmit">{{ !formData.id ? '发布' : '编辑' }}</button>
  71. </view>
  72. <!-- 组件 选择宝贝推荐 || 店铺推荐 -->
  73. <recommend v-model="showRecommendPopup" :shop="formData.shop" :goods="formData.goods" @change="handleRecommend">
  74. </recommend>
  75. <!-- 组件 话题 -->
  76. <topic v-model="showTopicPopup" @change="handleTopic"></topic>
  77. </view>
  78. </template>
  79. <script>
  80. import store from '@/store'
  81. import {
  82. baseURL,
  83. version
  84. } from '@/config/app.js'
  85. import {
  86. uploadFile,
  87. trottle
  88. } from "@/utils/tools.js"
  89. import {
  90. apiCommunityAdd,
  91. apiCommunityEdit,
  92. getCommunityRecommendTopic,
  93. getCommunityDetail,
  94. } from "@/api/community.js"
  95. import Recommend from "./components/recommend.vue"
  96. import Topic from "./components/topic.vue"
  97. export default {
  98. components: {
  99. Recommend,
  100. Topic
  101. },
  102. data() {
  103. return {
  104. action: '',
  105. token: '',
  106. version: version,
  107. showRecommendPopup: false,
  108. showTopicPopup: false,
  109. pageHeight: '',
  110. fileList: [],
  111. formData: {
  112. id: '',
  113. image: [],
  114. content: '',
  115. shop: [],
  116. goods: [],
  117. topic_id: ''
  118. },
  119. recommendTopic: []
  120. }
  121. },
  122. mounted() {
  123. this.action = baseURL + '/api/file/formimage';
  124. this.token = store.getters.token
  125. this.$toast = trottle(this.$toast, 3000, this)
  126. },
  127. onLoad() {
  128. const options = this.$Route.query;
  129. if (options.id) {
  130. this.formData.id = options.id
  131. this.initCommunityDetail()
  132. }
  133. this.initRecommendTopic();
  134. },
  135. methods: {
  136. // 初始化文章详情
  137. initCommunityDetail() {
  138. try {
  139. getCommunityDetail({
  140. id: this.formData.id
  141. }).then(res => {
  142. if (res.code == 1) {
  143. const {
  144. id,
  145. content,
  146. shop_data,
  147. goods_data,
  148. topic,
  149. images
  150. } = res.data;
  151. this.formData.id = id
  152. this.formData.content = content
  153. this.formData.shop = shop_data
  154. this.formData.goods = goods_data
  155. this.formData.topic_id = topic
  156. this.formData.image = images.filter(item => {
  157. item.url = item.image
  158. return 1
  159. })
  160. this.fileList = JSON.parse(JSON.stringify(this.formData.image))
  161. }
  162. })
  163. } catch (err) {
  164. this.$nextTick(() => {
  165. this.isFirstLoading = false;
  166. });
  167. }
  168. },
  169. // 初始化获取推荐话题
  170. initRecommendTopic() {
  171. getCommunityRecommendTopic().then(res => {
  172. this.recommendTopic = res.data;
  173. })
  174. },
  175. // 上传,不管成不成功都返回数据|提示
  176. change(event) {
  177. this.$toast({
  178. title: JSON.parse(event.data).msg
  179. })
  180. if (JSON.parse(event.data).code == 1) {
  181. this.formData.image.push({
  182. url: JSON.parse(event.data).data.uri
  183. })
  184. }
  185. },
  186. // 删除一个图片
  187. remove(event) {
  188. this.formData.image.splice(event, 1);
  189. },
  190. showRecommend() {
  191. this.showRecommendPopup = true
  192. },
  193. // 处理宝贝或店铺推荐
  194. handleRecommend(event) {
  195. const {
  196. type,
  197. data
  198. } = event;
  199. if (type == 0) {
  200. this.formData.goods = data;
  201. this.formData.shop = []
  202. } else {
  203. this.formData.goods = [];
  204. this.formData.shop = data
  205. }
  206. },
  207. // 处理选择话题
  208. handleTopic(event) {
  209. this.formData.topic_id = event;
  210. },
  211. onSubmit() {
  212. let params = {
  213. ...this.formData
  214. }
  215. params.goods = params.goods.map(item => item.goods_id)
  216. params.shop = params.shop.map(item => item.id)
  217. if (params.topic_id != null) params.topic_id = this.formData.topic_id.id
  218. params.image = this.formData.image.map(item => item.url)
  219. if (params.id) {
  220. this.handleCommunityEdit(params)
  221. } else {
  222. this.handleCommunityAdd(params)
  223. }
  224. },
  225. // 新增
  226. handleCommunityAdd(params) {
  227. apiCommunityAdd({
  228. ...params
  229. }).then(res => {
  230. this.$toast({
  231. title: res.msg
  232. })
  233. if (res.code == 1) {
  234. setTimeout(() => this.$Router.back(), 500)
  235. }
  236. })
  237. },
  238. // 编辑
  239. handleCommunityEdit(params) {
  240. apiCommunityEdit({
  241. ...params
  242. }).then(res => {
  243. this.$toast({
  244. title: res.msg
  245. })
  246. if (res.code == 1) {
  247. setTimeout(() => this.$Router.back(), 500)
  248. }
  249. })
  250. },
  251. // 处理文字和图片多时滚动条问题
  252. handleFocus(event) {
  253. this.pageHeight = event.detail.height * 1.5
  254. setTimeout(() => {
  255. uni.pageScrollTo({
  256. scrollTop: 160,
  257. })
  258. }, 50)
  259. }
  260. }
  261. }
  262. </script>
  263. <style lang="scss">
  264. page {
  265. background-color: #FFFFFF;
  266. }
  267. .container {
  268. .main {
  269. padding: 20rpx;
  270. background-color: #FFFFFF;
  271. // 上传图片
  272. .uploader-container {
  273. .uplader-upload {
  274. position: relative;
  275. width: 160rpx;
  276. height: 160rpx;
  277. line-height: 160rpx;
  278. text-align: center;
  279. margin: 11rpx;
  280. border: 2px solid #f0f0f0;
  281. border-radius: 14rpx;
  282. background-color: #f0f0f0;
  283. >view {
  284. color: #b1b1b1;
  285. }
  286. }
  287. }
  288. // 文章内容
  289. .content {
  290. padding: 20rpx;
  291. margin: 10rpx 0;
  292. border-radius: 10rpx;
  293. // background: #f8f8f8;
  294. textarea {
  295. width: 100%;
  296. min-height: 230rpx;
  297. }
  298. }
  299. .item {
  300. height: 88rpx;
  301. .image {
  302. width: 40rpx;
  303. height: 40rpx;
  304. }
  305. }
  306. // 推荐 宝贝 | 店铺
  307. .recommend {
  308. border-bottom: 1px solid #F2F2F2;
  309. }
  310. // 标签
  311. .tags {
  312. // padding-bottom: 14rpx;
  313. // border-bottom: 1px solid #F2F2F2;
  314. }
  315. .tags text {
  316. display: inline-block;
  317. margin-bottom: 10rpx;
  318. margin-right: 20rpx;
  319. border-radius: 26rpx;
  320. padding: 8rpx 24rpx;
  321. }
  322. // 标签主题颜色
  323. .primary-tags text {
  324. color: $-color-primary;
  325. margin-bottom: 0;
  326. background: rgba($-color-primary, .1);
  327. }
  328. // 标签主题颜色
  329. .gary-tags text {
  330. color: $-color-lighter;
  331. background: #f4f4f4;
  332. }
  333. }
  334. .footer {
  335. left: 0%;
  336. bottom: 50rpx;
  337. width: 100%;
  338. padding: 0 24rpx;
  339. position: fixed;
  340. .btn {
  341. height: 84rpx;
  342. line-height: 84rpx;
  343. background-color: rgba($-color-primary, .8);
  344. }
  345. }
  346. }
  347. </style>