Browse Source

修改聊天记录

zs 1 year ago
parent
commit
14b5293135
4 changed files with 330 additions and 2 deletions
  1. 5 0
      src/router/index.ts
  2. 57 0
      src/stores/users/chat.ts
  3. 258 0
      src/views/user/user/chat.vue
  4. 10 2
      src/views/user/user/index.vue

+ 5 - 0
src/router/index.ts

@@ -49,6 +49,11 @@ const user = [
     path: '/user/user',
     meta: { title: '用户管理' },
     component: () => import('@/views/user/user/index.vue')
+  },
+  {
+    path: '/user/chat',
+    meta: { title: '聊天记录' },
+    component: () => import('@/views/user/user/chat.vue')
   }
 ];
 const core = [

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

@@ -0,0 +1,57 @@
+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: `/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 read = async (payload: any): Promise<IQueryResult> => {
+    const res = await axios.$get(`${api.url}/read`, payload);
+    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,
+    read,
+    fetch,
+    create,
+    update,
+    del
+  };
+});

+ 258 - 0
src/views/user/user/chat.vue

@@ -0,0 +1,258 @@
+<template>
+  <div id="detail">
+    <el-row>
+      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
+        <el-col :span="24" class="one">
+          <cSearch :is_back="true" @toBack="toBack"></cSearch>
+        </el-col>
+        <el-col :span="14" class="two">
+          <div style="overflow: auto" v-scrollBottom>
+            <ul v-infinite-scroll="load" class="infinite-list">
+              <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>
+          </div>
+        </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/notarization/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 { ChatStore } from '@/stores/users/chat';
+import { ConfigStore } from '@/stores/system/config';
+import { UserStore } from '@/stores/users/user';
+import type { IQueryResult } from '@/util/types.util';
+const { proxy } = getCurrentInstance() as any;
+
+const chatAxios = ChatStore();
+const configAxios = ConfigStore();
+const userAxios = UserStore();
+let admin: Ref<any> = ref(store.state.user);
+// 路由
+const route = useRoute();
+// 加载中
+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 () => {
+  loading.value = true;
+  await searchOther();
+  await search({ skip, limit });
+  loading.value = false;
+});
+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: any = await chatAxios.query(info);
+    if (res.errcode == '0') {
+      list.value = res.data.reverse();
+      total.value = res.total;
+    }
+  }
+};
+// 查询其他信息
+const searchOther = async () => {
+  let id = route.query.id;
+  let res: IQueryResult;
+  if (id) {
+    res = await userAxios.fetch(id);
+    if (res.errcode == '0') user.value = res.data;
+    // 全部已读
+    await chatAxios.read({ user: id });
+  }
+  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">
+.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>

+ 10 - 2
src/views/user/user/index.vue

@@ -8,7 +8,7 @@
         <cButton :isAdd="false"> </cButton>
       </el-col>
       <el-col :span="24" class="thr">
-        <cTable :fields="fields" :opera="[]" :list="list" @query="search" :total="total"> </cTable>
+        <cTable :fields="fields" :opera="opera" :list="list" @query="search" @chat="toChat" :total="total"> </cTable>
       </el-col>
     </el-col>
   </el-row>
@@ -19,13 +19,15 @@ import { ref, Ref, onMounted, inject } from 'vue';
 // NeedChange
 import { UserStore } from '@/stores/users/user';
 import type { IQueryResult } from '@/util/types.util';
+import { useRouter } from 'vue-router';
 
 onMounted(async () => {
   loading.value = true;
   await search({ skip, limit });
   loading.value = false;
 });
-
+// 路由
+const router = useRouter();
 const loading: Ref<any> = ref(false);
 // NeedChange
 const store = UserStore();
@@ -60,6 +62,12 @@ let fields: Ref<any[]> = ref([
   { label: '身份证号', model: 'card', isSearch: true },
   { label: '电话', model: 'phone', isSearch: true }
 ]);
+// 操作
+let opera: Ref<any[]> = ref([{ label: '聊天记录', method: 'chat' }]);
+// 聊天记录
+const toChat = async (data: any) => {
+  router.push({ path: '/user/chat', query: { id: data._id } });
+};
 </script>
 
 <style scoped></style>