App.vue 2.8 KB
Newer Older
6
UPDATE  
64104061f23fda247c679fa8 已提交
1
<template>
6
update  
64104061f23fda247c679fa8 已提交
2 3 4 5 6 7 8 9
  <div class="container ivu-p">
    <div class="dialog">
      <template v-for="(item, index) in dialogs" :key="index">
        <div class="dialog-item" :class="{ 'dialog-item-me': item.role === 'me', 'dialog-item-ai': item.role === 'ai' }">
          <div class="dialog-item-main">{{ item.text }}</div>
        </div>
      </template>
    </div>
10
    <div class="question ivu-mt">
6
update  
64104061f23fda247c679fa8 已提交
11
      <Input
12
       v-model="question" 
6
update  
64104061f23fda247c679fa8 已提交
13 14 15 16
       type="textarea" 
       :autosize="{ minRows: 4, maxRows: 6 }" 
       placeholder="输入你的问题" 
      />
6
update  
64104061f23fda247c679fa8 已提交
17 18 19 20 21 22 23 24
      <Row class="ivu-mt">
        <Col>
          <Button type="primary" size="large" icon="md-send" :loading="loading" @click="handleSend">发送</Button>
        </Col>
        <Col>
          <Button size="large" class="ivu-ml" icon="md-add" :disabled="loading" @click="handleNewChat">新对话</Button>
        </Col>
      </Row>
6
update  
64104061f23fda247c679fa8 已提交
25
    </div>
6
622eda98dfef6c4fdb84ccca 已提交
26
  </div>
6
UPDATE  
64104061f23fda247c679fa8 已提交
27
</template>
6
update  
64104061f23fda247c679fa8 已提交
28
<script>
6
update  
64104061f23fda247c679fa8 已提交
29 30
  import axios from 'axios';

6
UPDATE  
64104061f23fda247c679fa8 已提交
31
  const token = 'Bearer ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVWlMQ0pzZFNJNklrbHVjME52WkdVaUxDSmxlSEFpT2pFMk9EVTFORGczT1Rrc0luVnlJam95TENKcWRHa2lPaUpCVUVsZlZFOUxSVTVmWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVXROQ0o5LmlyLTJYa1A4dFhNaFVldnlzTFhkUlJsY1VBV0ZiaWE5em9ZdGN6VlpleFk=';
32 33
  const api = 'https://api.ai100.ai/ai/api/ai/chat';

6
update  
64104061f23fda247c679fa8 已提交
34 35 36
  export default {
    data () {
      return {
37
        question: '',
6
update  
64104061f23fda247c679fa8 已提交
38
        loading: false,
6
update  
64104061f23fda247c679fa8 已提交
39
        dialogs: []
6
update  
64104061f23fda247c679fa8 已提交
40 41 42
      }
    },
    methods: {
6
update  
64104061f23fda247c679fa8 已提交
43
      handleSend () {
44
        if (this.loading || this.question === '') return;
6
update  
64104061f23fda247c679fa8 已提交
45
        this.loading = true;
46

6
update  
64104061f23fda247c679fa8 已提交
47 48
        const question = this.question;
        this.question = '';
49

6
update  
64104061f23fda247c679fa8 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        this.dialogs.push({
          role: 'me',
          text: question
        });

        axios.post(api, 
          {
            prompt: '',
            question,
            stream: false
          },
          {
            headers: {
              Accept: '*/*',
              Authorization: token
            }
          }
        )
        .then(({ data: res }) => {
          console.log(res);
        })
        .finally(() => {
          this.loading = false;
        });
6
update  
64104061f23fda247c679fa8 已提交
74 75 76
      },
      handleNewChat () {
        this.dialogs = [];
6
update  
64104061f23fda247c679fa8 已提交
77 78 79
      }
    }
  }
6
622eda98dfef6c4fdb84ccca 已提交
80
</script>
6
update  
64104061f23fda247c679fa8 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<style>
  .container{
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .dialog{
    flex: 1;
    overflow: auto;
  }
  .dialog-item{
    display: flex;
  }
  .dialog-item-main{
    max-width: 80%;
    padding: 8px;
    word-wrap: break-word;
    margin-top: 16px;
    border-radius: 4px;
  }
  .dialog-item-me{
    justify-content: flex-end;
  }
  .dialog-item-me .dialog-item-main{
    background-color: antiquewhite;
  }
  .dialog-item-ai .dialog-item-main{
    background-color: #eee;
  }
</style>