当前位置:   article > 正文

uniapp H5端模拟机器人回复

uniapp h5端模拟机器人回复

 

模拟机器人聊天并回复

1.html部分,通过user字段区分机器人或用户发送的信息进行渲染。

  1. <template>
  2. <view class="vw1">
  3. <view class="center">
  4. <view class="message_box">
  5. <view class="vw3" v-for="(item, index) in msglist" :key="index">
  6. <view v-if="item.user != 'my'">
  7. <view class="icon_box">
  8. <img class="icon" src="../../static/jqr.png" alt="" />
  9. <p>机器人</p>
  10. </view>
  11. <text class="chat-bubble">{{ item.text }}</text>
  12. </view>
  13. <view class="my" v-else>
  14. <text class="my_p">{{ item.text }}</text>
  15. <view class="my_box">
  16. <img class="my_icon" src="../../static/tx.png" alt="" />
  17. <p></p>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="vw2">
  23. <input class="ip1" placeholder="请输入您想咨询的信息" v-model="text" />
  24. <button class="bt1" type="primary" @click="click1">发送</button>
  25. </view>
  26. <view class="vw2">
  27. <button type="primary" @click="click2">模拟回复</button>
  28. </view>
  29. </view>
  30. </view>
  31. </template>

2.JS部分

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. text: "", //用户输入框中输入的内容
  6. msglist: [], //用于存放对话内容的列表
  7. };
  8. },
  9. methods: {
  10. // 此处为写死数据模拟机器人回复,后期通过接口返回信息添加到数组里即可。
  11. click2() {
  12. this.msglist.push({
  13. text: "你好,您想咨询什么问题",
  14. user: "",
  15. });
  16. },
  17. // 用户发送信息,此处为写死数据。后期通过接口方式将text传送即可。
  18. click1() {
  19. if (this.text == "") {
  20. uni.showToast({
  21. title: "请输入内容",
  22. icon: "none",
  23. });
  24. return;
  25. }
  26. //定义一个click1函数来实现存入对话及api请求
  27. this.msglist.push({
  28. text: this.text,
  29. user: "my",
  30. }); //将输入框中内容传入存放对话的列表
  31. this.text = "";
  32. uni.request({
  33. //发起api请求
  34. url: "xxx",
  35. //将用户输入框中的内容放入api的url中
  36. success: (res) => {
  37. console.log(res.data);
  38. this.text = ""; //清空输入框,方便用户下次输入
  39. this.msglist.push(res.data.text); //将机器人回复内容传入列表
  40. },
  41. });
  42. },
  43. },
  44. };
  45. </script>

3.css部分

  1. <style lang="less" scoped>
  2. .vw1 {
  3. width: 100%;
  4. height: 100%;
  5. flex: ;
  6. display: flex;
  7. flex-direction: column;
  8. justify-content: column;
  9. align-items: center;
  10. }
  11. .center {
  12. width: 84%;
  13. height: 80vh;
  14. border: 2rpx solid #ccc;
  15. border-radius: 5rpx;
  16. padding: 15rpx 20rpx;
  17. .message_box {
  18. height: 70vh;
  19. margin-bottom: 20rpx;
  20. overflow-y: auto;
  21. }
  22. .vw3 {
  23. margin-top: 7px;
  24. .icon_box {
  25. display: inline-block;
  26. text-align: center;
  27. .icon {
  28. width: 65rpx;
  29. height: 65rpx;
  30. vertical-align: bottom;
  31. }
  32. p {
  33. font-size: 26rpx;
  34. }
  35. }
  36. .chat-bubble {
  37. position: relative;
  38. width: 200px;
  39. padding: 30rpx;
  40. border-radius: 10px;
  41. border: 2rpx solid #fff;
  42. background-color: #ed6b7a;
  43. display: inline-block;
  44. margin-left: 10px;
  45. color: #fff;
  46. vertical-align: bottom;
  47. }
  48. .chat-bubble:before {
  49. content: "";
  50. position: absolute;
  51. left: -10px;
  52. top: 50%;
  53. transform: translateY(-50%) rotate(45deg);
  54. width: 20px;
  55. height: 20px;
  56. background-color: #ed6b7a;
  57. border: inherit;
  58. border-top: none;
  59. border-right: none;
  60. }
  61. .chat-bubble:after {
  62. content: "";
  63. position: absolute;
  64. left: -9px;
  65. top: 50%;
  66. transform: translateY(-50%) rotate(45deg);
  67. width: 18px;
  68. height: 18px;
  69. background-color: #ed6b7a;
  70. border: inherit;
  71. border-top: none;
  72. border-right: none;
  73. }
  74. .my {
  75. text-align: right;
  76. .my_box {
  77. display: inline-block;
  78. text-align: center;
  79. .my_icon {
  80. width: 65rpx;
  81. height: 65rpx;
  82. vertical-align: bottom;
  83. }
  84. p {
  85. font-size: 26rpx;
  86. }
  87. }
  88. }
  89. .my_p {
  90. position: relative;
  91. width: 200px;
  92. padding: 30rpx;
  93. border-radius: 10px;
  94. border: 2rpx solid #fff;
  95. background-color: #22ac38;
  96. display: inline-block;
  97. margin-right: 15px;
  98. color: #fff;
  99. vertical-align: bottom;
  100. }
  101. .my_p:before {
  102. content: "";
  103. position: absolute;
  104. right: -10px;
  105. top: 50%;
  106. -webkit-transform: translateY(-50%) rotate(45deg);
  107. transform: translateY(-49%) rotate(-136deg);
  108. width: 18px;
  109. height: 22px;
  110. background-color: #22ac38;
  111. border: inherit;
  112. border-top: none;
  113. border-right: none;
  114. }
  115. .my_p:after {
  116. content: "";
  117. position: absolute;
  118. right: -10px;
  119. top: 50%;
  120. -webkit-transform: translateY(-50%) rotate(45deg);
  121. transform: translateY(-49%) rotate(-136deg);
  122. width: 18px;
  123. height: 22px;
  124. background-color: #22ac38;
  125. border: inherit;
  126. border-top: none;
  127. border-right: none;
  128. }
  129. }
  130. .vw2 {
  131. margin-bottom: 20px;
  132. .ip1 {
  133. display: inline-block !important;
  134. width: 78%;
  135. caret-color: #4e6ef2;
  136. box-sizing: border-box;
  137. display: block;
  138. padding: 10px 20px;
  139. margin: 0;
  140. height: 48px;
  141. line-height: 20px;
  142. border: 1px solid #b8b8b8;
  143. font-size: 18px;
  144. color: #1f1f1f;
  145. transition: 0.3s;
  146. font-family: PingFangSC-Regular, Tahoma, Helvetica, "Microsoft Yahei",
  147. "微软雅黑", Arial, STHeiti;
  148. border-radius: 8px;
  149. }
  150. .bt1 {
  151. display: inline-block;
  152. width: 20%;
  153. margin-left: 2%;
  154. height: 48px;
  155. line-height: 48px;
  156. background: #4e6ef2;
  157. border-radius: 10px;
  158. box-shadow: 0 6px 16px 0 rgb(78 111 242 / 30%);
  159. font-size: 17px;
  160. font-weight: 800;
  161. border: 0;
  162. color: #fff;
  163. cursor: pointer;
  164. }
  165. }
  166. }
  167. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/971205
推荐阅读
相关标签
  

闽ICP备14008679号