Browse Source

修改聊天记录

zs 1 year ago
parent
commit
760c1504db
3 changed files with 243 additions and 8 deletions
  1. 2 1
      src/components/frame/c-upload.vue
  2. 52 0
      src/stores/users/chat.ts
  3. 189 7
      src/views/user/user/chat.vue

+ 2 - 1
src/components/frame/c-upload.vue

@@ -11,7 +11,8 @@
       :on-exceed="outLimit"
       :on-preview="filePreview"
       :on-success="onSuccess"
-      :before-remove="onRemove">
+      :before-remove="onRemove"
+    >
       <el-button type="primary">选择文件</el-button>
       <template #tip v-if="tip">
         <p style="color: #ff0000">{{ tip }}</p>

+ 52 - 0
src/stores/users/chat.ts

@@ -0,0 +1,52 @@
+import { ref, computed } from 'vue';
+import { defineStore } from 'pinia';
+import { AxiosWrapper } from '@/util/axios-wrapper';
+import _ from 'lodash';
+
+import type { IQueryType, IQueryResult, IQueryParams } from '@/util/types.util';
+const axios = new AxiosWrapper();
+const api = {
+  url: `/travel/v1/api/chat`
+};
+export const ChatStore = defineStore('chat', () => {
+  const count = ref(0);
+  const doubleCount = computed(() => count.value * 2);
+  function increment() {
+    count.value++;
+  }
+  const query = async ({ skip = 0, limit = undefined, ...info }: IQueryParams = {}): Promise<IQueryResult> => {
+    let cond: IQueryType = {};
+    if (skip) cond.skip = skip;
+    if (limit) cond.limit = limit;
+    cond = { ...cond, ...info };
+    const res = await axios.$get(`${api.url}`, cond);
+    return res;
+  };
+  const fetch = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$get(`${api.url}/${payload}`);
+    return res;
+  };
+  const create = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$post(`${api.url}`, payload);
+    return res;
+  };
+  const update = async (payload: any): Promise<IQueryResult> => {
+    const id = _.get(payload, 'id', _.get(payload, '_id'));
+    const res = await axios.$post(`${api.url}/${id}`, payload);
+    return res;
+  };
+  const del = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$delete(`${api.url}/${payload}`);
+    return res;
+  };
+  return {
+    count,
+    doubleCount,
+    increment,
+    query,
+    fetch,
+    create,
+    update,
+    del
+  };
+});

+ 189 - 7
src/views/user/user/chat.vue

@@ -5,23 +5,75 @@
         <el-col :span="24" class="one">
           <cSearch :is_back="true" @toBack="toBack"></cSearch>
         </el-col>
-        <el-col :span="24" class="two"> 聊天记录{{ list }} </el-col>
+        <el-col :span="14" class="two">
+          <ul v-infinite-scroll="load" class="infinite-list" style="overflow: auto">
+            <el-col :span="24" class="list" v-for="item in list" :key="item._id">
+              <el-col :span="24" class="two_1" v-if="item.speaker == user._id">
+                <el-col class="time" v-if="item.time != ''">{{ item.time }}</el-col>
+                <el-col :span="24" class="content">
+                  <el-col :span="2" class="logo">
+                    <el-image v-if="user && user.logo.length != 0" :src="user.logo[0].url || '../../assets/bglogin.jpg'"
+                      style="width: 60px; height: 60px; border-radius: 60px" mode="widthFix"></el-image>
+                  </el-col>
+                  <el-col :span="8" v-if="item.msg_type == '0'">
+                    <text class="text">{{ item.content }}</text>
+                  </el-col>
+                  <el-col :span="8" class="image" v-else>
+                    <el-image :src="item.content" style="width: 15vw" mode="widthFix"></el-image>
+                  </el-col>
+                </el-col>
+              </el-col>
+              <el-col :span="24" class="two_2" v-else>
+                <el-col class="time" v-if="item.time != ''">{{ item.time }}</el-col>
+                <el-col :span="24" class="content">
+                  <el-col :span="8" v-if="item.msg_type == '0'">
+                    <text class="text">{{ item.content }}</text>
+                  </el-col>
+                  <el-col :span="8" class="image" v-else>
+                    <el-image :src="item.content" style="width: 15vw" mode="widthFix"></el-image>
+                  </el-col>
+                  <el-col :span="2" class="logo">
+                    <el-image v-if="config && config.file.length != 0" :src="config.file[0].url"
+                      style="width: 60px; height: 60px; border-radius: 60px" mode="widthFix"></el-image>
+                  </el-col>
+                </el-col>
+              </el-col>
+            </el-col>
+          </ul>
+        </el-col>
+        <el-col :span="14" class="bottom">
+          <el-col :span="20" class="input">
+            <el-input v-model="input" type="textarea" placeholder="请输入" />
+          </el-col>
+          <el-col :span="4" class="button">
+            <el-button type="primary" @click="toSend">发送</el-button>
+            <cUpload class="upload" :limit="1" accept="*" url="/files/travel/chat/upload" :list="file" @change="onUpload">
+            </cUpload>
+          </el-col>
+        </el-col>
       </el-col>
     </el-row>
   </div>
 </template>
 <script setup lang="ts">
 // 基础
+import moment from 'moment';
 import type { Ref } from 'vue';
 import { onMounted, ref, getCurrentInstance } from 'vue';
 import { useRoute } from 'vue-router';
+import store from '@/stores/counter';
+import { ElMessage } from 'element-plus';
 // 接口
-import { ProblemStore } from '@/stores/customer/problem';
+import { ChatStore } from '@/stores/users/chat';
+import { ConfigStore } from '@/stores/basic/config';
 import { UserStore } from '@/stores/users/user';
 import type { IQueryResult } from '@/util/types.util';
 const { proxy } = getCurrentInstance() as any;
-const problemAxios = ProblemStore();
+
+const chatAxios = ChatStore();
+const configAxios = ConfigStore();
 const userAxios = UserStore();
+let admin: Ref<any> = ref(store.state.user);
 // 路由
 const route = useRoute();
 // 加载中
@@ -29,8 +81,12 @@ const loading: Ref<any> = ref(false);
 let list: Ref<any> = ref([]);
 let total: Ref<number> = ref(0);
 let skip = 0;
+let page = 0;
 let limit: number = proxy.$limit;
 const user: Ref<any> = ref({ logo: [] });
+const config: Ref<any> = ref({ file: [] });
+const file: Ref<any> = ref([]);
+const input: Ref<any> = ref('');
 // 字典表
 // 请求
 onMounted(async () => {
@@ -42,9 +98,9 @@ onMounted(async () => {
 const search = async (e: { skip: number; limit: number }) => {
   if (user.value._id) {
     const info = { skip: e.skip, limit: e.limit, user: user.value._id };
-    const res: IQueryResult = await problemAxios.query(info);
+    const res: any = await chatAxios.query(info);
     if (res.errcode == '0') {
-      list.value = res.data;
+      list.value = res.data.reverse();
       total.value = res.total;
     }
   }
@@ -52,14 +108,140 @@ const search = async (e: { skip: number; limit: number }) => {
 // 查询其他信息
 const searchOther = async () => {
   let id = route.query.id;
+  let res: IQueryResult;
   if (id) {
-    let res: IQueryResult = await userAxios.fetch(id);
+    res = await userAxios.fetch(id);
     if (res.errcode == '0') user.value = res.data;
   }
+  res = await configAxios.query();
+  if (res.errcode == '0') {
+    config.value = res.data;
+  }
+};
+// 发送
+const toSend = async () => {
+  let res: IQueryResult;
+  let form: any = { user: user.value._id, speaker: admin.value._id, content: input.value, msg_type: '0', time: moment().format('YYYY-MM-DD HH:mm:ss') };
+  // 发送给服务器消息
+  res = await chatAxios.create(form);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `发送消息信息成功` });
+    input.value = '';
+    search({ skip, limit });
+  }
+};
+// 上传
+const onUpload = async (e: { value: Array<any> }) => {
+  const { value } = e;
+  let res: IQueryResult;
+  let form: any = { user: user.value._id, speaker: admin.value._id, content: value[0].url, msg_type: '1', time: moment().format('YYYY-MM-DD HH:mm:ss') };
+  // 发送给服务器消息
+  res = await chatAxios.create(form);
+  if (res.errcode == 0) {
+    ElMessage({ type: `success`, message: `发送消息信息成功` });
+    file.value = [];
+    search({ skip, limit });
+  }
 };
+// 下拉查看聊天记录
+const load = () => {
+  if (total.value != 0) {
+    if (total.value > limit) {
+      page = page + 1;
+      limit = limit * page;
+      search({ skip, limit });
+    } else ElMessage({ type: `warning`, message: `已经到底了!` });
+  }
+}
 // 返回上一页
 const toBack = () => {
   window.history.go(-1);
 };
 </script>
-<style scoped lang="scss"></style>
+<style scoped lang="scss">
+.main {
+  .two {
+    padding: 20px;
+    border: 1px solid #d5d5da;
+    background: #f1f1f1;
+    border-radius: 5px;
+
+    .infinite-list {
+      height: 550px;
+      padding: 0;
+      margin: 0;
+      list-style: none;
+    }
+
+    .list {
+      display: flex;
+      flex-direction: column;
+
+      .two_1 {
+        .time {
+          text-align: center;
+          color: #858585;
+          font-size: 12px;
+        }
+
+        .content {
+          display: flex;
+          align-items: center;
+
+          .logo {
+            height: 60px;
+          }
+
+          .text {
+            padding: 12px;
+            background: #ffffff;
+            border-radius: 0px 12px 12px 12px;
+          }
+        }
+      }
+
+      .two_2 {
+        text-align: right;
+
+        .time {
+          text-align: center;
+          color: #858585;
+          font-size: 12px;
+        }
+
+        .content {
+          display: flex;
+          align-items: center;
+          justify-content: right;
+
+          .logo {
+            height: 60px;
+          }
+
+          .text {
+            padding: 12px;
+            border-radius: 12px 0px 12px 12px;
+            background: #16f80f;
+          }
+        }
+      }
+    }
+  }
+
+  .bottom {
+    display: flex;
+    align-items: center;
+    padding: 5px 0 0 0;
+
+    .button {
+      display: flex;
+      margin: 10px 0 0 5px;
+
+      .upload {
+        text-align: right;
+        padding: 0 0 0 5px;
+      }
+    }
+  }
+}
+</style>