健康同学微信公众号h5项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
9.3KB

  1. <!-- -->
  2. <template>
  3. <div class="expert-chat">
  4. <NavBar :title="title" @on-click-left="onNavBack"> </NavBar>
  5. <div class="chat-container" ref="chatBox">
  6. <van-pull-refresh v-model="refreshing" @refresh="onRefresh" v-if="false">
  7. <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
  8. <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
  9. <div v-for="(item, index) in chatList" :key="index" :class="item.class">
  10. <!-- 左边 -->
  11. <div :class="item.leftClass">
  12. <!-- 专家部分 -->
  13. <div class="avatar" v-if="item.class !== 'mine'">
  14. <img :src="defaultImg" alt="">
  15. </div>
  16. <!-- 用户部分 -->
  17. <div class="mine-con" v-else>
  18. <p>{{ item.time }}</p>
  19. <div class="text">
  20. <span>{{ item.text }}</span>
  21. </div>
  22. </div>
  23. </div>
  24. <div :class="item.rightClass">
  25. <!-- 用户部分 -->
  26. <div class="avatar" v-if="item.class === 'mine'">
  27. <img :src="defaultImg" alt="">
  28. </div>
  29. <!-- 专家部分 -->
  30. <div class="mine-con" v-else>
  31. <p>{{ item.time }}</p>
  32. <div class="text">
  33. <span>{{ item.text }}</span>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </van-list>
  39. </van-pull-refresh>
  40. <!-- 短语 -->
  41. <div class="short-text">
  42. <div class="btn" v-for="(item, index) in shortList" :key="index" :style="{ backgroundColor: item.bgColor }"
  43. @click="onAddChat(item)">
  44. <p>{{ item.name }}</p>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="send">
  49. <div class="left">
  50. <van-cell-group>
  51. <van-field v-model="chat" placeholder="说点什么" maxlength="50" />
  52. </van-cell-group>
  53. </div>
  54. <div class="right" @click="onSend">
  55. <div class="circle">发送</div>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import NavBar from '@/components/NavBar';
  62. import { isNull } from '@/services/utils-service';
  63. import APIOptimize from '@/api/optimize';
  64. export default {
  65. components: {
  66. NavBar,
  67. },
  68. data() {
  69. return {
  70. chat: '',
  71. list: [],
  72. loading: false,
  73. finished: false,
  74. refreshing: false,
  75. defaultImg: require('../../assets/myself/4_62.png'),
  76. chatList: [
  77. {
  78. time: '2024-02-24',
  79. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  80. class: 'cell-chat',
  81. leftClass: 'left',
  82. rightClass: 'right'
  83. }, {
  84. time: '2024-02-24',
  85. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  86. class: 'cell-chat',
  87. leftClass: 'left',
  88. rightClass: 'right'
  89. },
  90. {
  91. time: '2024-02-24',
  92. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  93. class: 'mine',
  94. leftClass: 'left',
  95. rightClass: 'right'
  96. },
  97. {
  98. time: '2024-02-24',
  99. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  100. class: 'cell-chat',
  101. leftClass: 'left',
  102. rightClass: 'right'
  103. },
  104. /* {
  105. time: '2024-02-24',
  106. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  107. class: 'mine',
  108. leftClass: 'left',
  109. rightClass: 'right'
  110. },
  111. {
  112. time: '2024-02-24',
  113. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  114. class: 'cell-chat',
  115. leftClass: 'left',
  116. rightClass: 'right'
  117. },
  118. {
  119. time: '2024-02-24',
  120. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  121. class: 'cell-chat',
  122. leftClass: 'left',
  123. rightClass: 'right'
  124. },
  125. {
  126. time: '2024-02-24',
  127. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  128. class: 'mine',
  129. leftClass: 'left',
  130. rightClass: 'right'
  131. },
  132. {
  133. time: '2024-02-24',
  134. text: '也可以与 CellGroup 搭配使用,CellGroup 可以为 Cell 提供上下外边框。',
  135. class: 'mine',
  136. leftClass: 'left',
  137. rightClass: 'right'
  138. }, */
  139. ],
  140. shortList: [
  141. { name: '赞', bgColor: 'red' },
  142. { name: '有道理', bgColor: 'green' },
  143. { name: '说的透彻', bgColor: 'red' },
  144. { name: '是这样的', bgColor: 'green' },
  145. { name: '有道理', bgColor: 'green' },
  146. { name: '有道理', bgColor: 'red' },
  147. { name: '学习了', bgColor: 'green' },
  148. ]
  149. }
  150. },
  151. created() {
  152. },
  153. computed: {
  154. title() {
  155. return this.$route.query.title || ''
  156. }
  157. },
  158. mounted() {
  159. this.scrollToBottom(); // 页面加载时也滚动到底部
  160. },
  161. methods: {
  162. scrollToBottom() {
  163. this.$refs.chatBox.scrollTop = this.$refs.chatBox.scrollHeight;
  164. },
  165. onNavBack() {
  166. this.$router.push({
  167. name: 'trainingcamp'
  168. })
  169. },
  170. onLoad() {
  171. setTimeout(() => {
  172. if (this.refreshing) {
  173. this.list = [];
  174. this.refreshing = false;
  175. }
  176. for (let i = 0; i < 10; i++) {
  177. this.list.push(this.list.length + 1);
  178. }
  179. this.loading = false;
  180. if (this.list.length >= 40) {
  181. this.finished = true;
  182. }
  183. }, 1000);
  184. },
  185. onRefresh() {
  186. // 清空列表数据
  187. this.finished = false;
  188. // 重新加载数据
  189. // 将 loading 设置为 true,表示处于加载状态
  190. this.loading = true;
  191. this.onLoad();
  192. },
  193. onAddChat(item) {
  194. this.chat = item.name;
  195. },
  196. onSend() {
  197. if (isNull(this.chat)) {
  198. return this.$toast.fail({
  199. message: '评论不能为空'
  200. })
  201. }
  202. this.onComment();
  203. /* let message = {
  204. time: '2024-02-24',
  205. text: this.chat,
  206. class: 'mine',
  207. leftClass: 'left',
  208. rightClass: 'right'
  209. };
  210. this.chatList.push(message); */
  211. // TODO,调取接口
  212. },
  213. // 发送评论
  214. onComment() {
  215. this.$toast.loading();
  216. let reqBody = {
  217. lessonId: Number(this.$route.query.lessonId),
  218. content: this.chat,
  219. shortcutCode: ""
  220. };
  221. APIOptimize.comment(reqBody).then(res => {
  222. console.log("评论详情", res);
  223. const data = res.data;
  224. if (data.stateCode === 1) {
  225. this.$toast.success({
  226. message: '评论成功'
  227. });
  228. setTimeout(() => {
  229. this.$router.push({
  230. name: 'trainingcamp',
  231. });
  232. })
  233. } else {
  234. this.$dialog.confirm({
  235. message: `${data.message}`,
  236. showCancelButton: false
  237. })
  238. }
  239. }).catch((e) => {
  240. this.$dialog.confirm({
  241. message: `${e.message}`
  242. })
  243. }).finally(() => {
  244. /* this.$toast.clear() */
  245. })
  246. }
  247. },
  248. }
  249. </script>
  250. <style scoped lang="scss">
  251. @import './scss/expertChat.scss';
  252. /* @import url(); 引入css类 */
  253. </style>