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
  const token = 'Bearer ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVWlMQ0pzZFNJNklrbHVjME52WkdVaUxDSmxlSEFpT2pFMk9EVTFORGczT1Rrc0luVnlJam95TENKcWRHa2lPaUpCVUVsZlZFOUxSVTVmWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVXROQ0o5LmlyLTJYa1A4dFhNaFVldnlzTFhkUlJsY1VBV0ZiaWE5em9ZdGN6VlpleFk=';
30 31
  const api = 'https://api.ai100.ai/ai/api/ai/chat';

6
update  
64104061f23fda247c679fa8 已提交
32 33 34
  export default {
    data () {
      return {
35
        question: '',
6
update  
64104061f23fda247c679fa8 已提交
36 37 38 39
        loading: false,
        dialogs: [
          {
            role: 'me',
6
update  
64104061f23fda247c679fa8 已提交
40
            text: '你好'
6
update  
64104061f23fda247c679fa8 已提交
41 42 43
          },
          {
            role: 'ai',
6
update  
64104061f23fda247c679fa8 已提交
44
            text: '你也好'
6
update  
64104061f23fda247c679fa8 已提交
45 46 47 48 49
          }
        ]
      }
    },
    methods: {
50 51
      async handleSend () {
        if (this.loading || this.question === '') return;
6
update  
64104061f23fda247c679fa8 已提交
52
        this.loading = true;
6
update  
64104061f23fda247c679fa8 已提交
53
        // todo
54 55 56 57 58 59 60 61 62 63 64
        const response = await fetch(api, this.getParams());
        console.log(response)
      },
      getParams () {
        const headers = {
          "Accept": "*/*",
				  "Authorization": token,
				  "Content-Type": "application/json"
        }

        const payload = {
6
UPDATE  
64104061f23fda247c679fa8 已提交
65
          prompt: ' ',
66 67 68 69 70 71 72 73 74
          question: this.question,
          stream: false
        }

        return {
          method: 'POST',
          headers,
          body: JSON.stringify(payload)
        }
6
update  
64104061f23fda247c679fa8 已提交
75 76 77
      },
      handleNewChat () {
        this.dialogs = [];
6
update  
64104061f23fda247c679fa8 已提交
78 79 80
      }
    }
  }
6
622eda98dfef6c4fdb84ccca 已提交
81
</script>
6
update  
64104061f23fda247c679fa8 已提交
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 111
<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>