0


【扣子coze+微信开发者工具】实现ai自定义对话02:微信小程序对话页面简单设计


摘要

本系列文章实现了运用国内扣子Coze与微信开发者工具实现一个简单的微信小程序页面,一个与自定义AI语言交互问答的功能。本系列文章详细讲解的从创建bot到微信小程序页面还有接口的编写。后续可能会加入后端Java+SpringBoot的逻辑功能来丰富项目。


成果预览:

专栏相关操作 ** **传送==>(^_^)

1、创建自定义Bot

2、微信小程序对话页面简单设计

3、逻辑和接口实现

一、前言

在这之前要先拥有一个微信公众平台的账号,如果没有的话要去微信公众平台注册,然后创建一个小程序,记得更改成自己的AppID。

在pages里创建一个文件夹我这里命名为AIchat,在这个文件夹里再新建page。

我在小程序编译的时候出现了工具未校验合法域名的问题,这是我们在学习或者测试的时候,要把这方面的设置给选中,要不然是不可以编译的。

二、.wxml 文件编写

我们的主要目的是功能的实现,页面后期可以再慢慢修改,因此这里是一个比较简洁的页面。

  1. <!-- pages/AIchat/AIchat.wxml -->
  2. <view class="chat-container">
  3. <scroll-view scroll-y="true" style="height: 100vh;">
  4. <block wx:for="{{messages}}" wx:key="*this">
  5. <view class="chat-message {{item.sender === 'user' ? 'user' : 'assistant'}}">
  6. <!-- 文本消息 -->
  7. <view wx:if="{{item.type === 'text'}}" class="bubble text-bubble">
  8. {{item.content}}
  9. </view>
  10. <!-- 图片消息 -->
  11. <view wx:if="{{item.type === 'image'}}" class="bubble image-bubble">
  12. <image src="{{item.content}}" /></view>
  13. </view>
  14. </block>
  15. </scroll-view>
  16. <!-- 输入框区域 -->
  17. <view class="input-container">
  18. <input class="message-input" placeholder="请输入消息" bindinput="onInputMessage" value="{{inputValue}}" />
  19. <view class="send-button-container">
  20. <button class="message-send" bindtap="onSendMessage">发送</button>
  21. <!-- 假设图标用于触发图片或文件选择 -->
  22. <image class="add-icon" src="/images/ai.png" mode="widthFix" bindtap="onAddAction" />
  23. </view>
  24. </view>
  25. </view>

三、.wxss文件编写

  1. /* pages/AIchat/AIchat.wxss */
  2. .chat-container {
  3. margin-left: 3%;
  4. display: flex;
  5. flex-direction: column;
  6. /* justify-content: space-between; */
  7. /* overflow-y: auto; */
  8. height: 100%;
  9. }
  10. /* 对话气泡基础样式 */
  11. .chat-message {
  12. display: flex;
  13. align-items: flex-end; /* 文本垂直底部对齐 */
  14. margin-bottom: 10px;
  15. max-width: 90%; /* 宽度最大80% */
  16. }
  17. /* 用户气泡 */
  18. .user {
  19. align-self: flex-end; /* 用户消息靠右 */
  20. }
  21. /* 助手气泡 */
  22. .assistant {
  23. align-self: flex-start; /* 助手消息靠左 */
  24. }
  25. /* 文本气泡 */
  26. .bubble.text-bubble {
  27. background-color: white; /* 默认背景色 */
  28. border-radius: 10px;
  29. padding: 10px;
  30. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  31. max-width: calc(80% - 20px); /* 减去边距以适应80%宽度限制 */
  32. }
  33. /* 用户文本气泡颜色 */
  34. .user .text-bubble {
  35. background-color: lightblue;
  36. }
  37. /* 助手文本气泡颜色 */
  38. .assistant .text-bubble {
  39. background-color: #f0f0f0;
  40. }
  41. /* 图片气泡基础样式 */
  42. .bubble.image-bubble {
  43. position: relative; /* 为上传文本提供定位上下文 */
  44. }
  45. /* 图片气泡中的图片样式 */
  46. .bubble.image-bubble image {
  47. width: 150px;
  48. height: 150px;
  49. object-fit: cover;
  50. border-radius:5px ;
  51. /* max-width: 100%; */
  52. /* height: auto; */
  53. }
  54. /* 上传文本提示 */
  55. .input-container {
  56. background-color: rgb(241, 231, 231);
  57. position: fixed;
  58. height: 8%;
  59. bottom: 0;
  60. left: 0;
  61. right: 0;
  62. padding: 10px;
  63. display: flex;
  64. flex-direction: row; /* 改为列布局 */
  65. justify-content: center; /* 垂直居中对齐 */
  66. align-items: center; /* 水平居中对齐,如果需要 */
  67. flex-grow: 1;
  68. }
  69. .message-input {
  70. background-color: rgb(236, 231, 231);
  71. border: 1px solid #000;
  72. border-radius: 4px;
  73. padding-left: 5px;
  74. padding-right: 5px;
  75. height: 70%;
  76. /* flex-grow: 1; */
  77. font-size: 20px;
  78. letter-spacing: 2px;
  79. /* 设置字间距 */
  80. width: 80%;
  81. margin-top: auto;
  82. margin-bottom: auto;
  83. /* opacity: 1; */
  84. margin-right: 10px;
  85. }
  86. .send-button-container {
  87. width: 30%;
  88. height: 100%;
  89. display: flex; /* 使按钮和图片水平排列 */
  90. justify-content: space-between;
  91. align-items: center; /* 垂直居中 */
  92. }
  93. .message-send{
  94. background-color: deepskyblue;
  95. height: 70%;
  96. width: 50%;
  97. letter-spacing: 4px;
  98. /* text-align: center; */
  99. padding: 0 0;
  100. margin-right: 5px;
  101. }
  102. .add-icon {
  103. width: 100%; /* 根据实际图片大小调整 */
  104. margin-left: 10px; /* 调整与发送按钮的间距 */
  105. }

四、总结

本文主要讲的是微信小程序页面的创建与设计。

作者为正在学习的在读研究生,希望可以与大家交流学习,欢迎指正!!


本文转载自: https://blog.csdn.net/qq_54413078/article/details/140448271
版权归原作者 @千千结 所有, 如有侵权,请联系我们删除。

“【扣子coze+微信开发者工具】实现ai自定义对话02:微信小程序对话页面简单设计”的评论:

还没有评论