微信小程序课设(基于云开发)

微信小程序通过Laf云平台接入ChatGPT实现聊天,回答方式采用流式回答。

以下是图片展示其页面

回答次数通过卡密兑换

以下是对话页面的代码

html 复制代码
<!--pages/content/content.wxml-->

<view class="container">




<view class="div" hidden="{{AnswerList.length>0}}">

  <view>

    <image src="../../images/editor.png" mode=""/>

    <text class="one-text">AI创作</text>

  </view>

  <view>

    <image src="../../images/tip.png" mode=""/>

    <text class="one-text">有趣的提问</text>

  </view>

  <view>

    <image src="../../images/book.png" mode=""/>

    <text class="one-text">AI百科</text>

  </view>

</view>

<view class="div-two" hidden="{{AnswerList.length>0}}">

  <view class="div-two-one">

    <text bindtap="handleTextClick" data-message="写一首赞美祖国的诗">写一首赞美祖国的诗→</text>

    <text bindtap="handleTextClick" data-message="写一篇科幻小说" >写一篇科幻小说→</text>

    <text bindtap="handleTextClick" data-message="安排一场发布会流程" >安排一场发布会流程→</text>

  </view>

  <view class="div-two-one">

    <view class="div-two-one">

      <text bindtap="handleTextClick" data-message="有哪些有趣的科学实验" >有哪些有趣的科学实验→</text>

      <text bindtap="handleTextClick" data-message="一个AI也回答不出来的问题" >问一个AI也回答不出来的问题→</text>

      <text bindtap="handleTextClick" data-message="AI会替代人类工作吗?" >AI会替代人类工作吗?→</text>

    </view>

  </view>

  <view class="div-two-one">

      <text bindtap="handleTextClick" data-message="用简单的术语来解释人工智能" >用简单的术语来解释人工智能→</text>

      <text bindtap="handleTextClick" data-message="红烧牛肉的做法" >红烧牛肉的做法→</text>

      <text bindtap="handleTextClick" data-message="请介绍一下你自己" >请介绍一下你自己→</text>

  </view>

</view>



<scroll-view scroll-y="true" hidden="{{AnswerList.length==0}}">

<view class="div-answer" wx:for="{{AnswerList}}" wx:key="index">

  <view class="answer">

    <image src="../../images/49.webp"></image>

    <rich-text>{{item.message}}</rich-text>

  </view>



  <view class="answer answer-one">

    <image src="../../images/chatgpt.jpeg"></image>

    <rich-text>{{item.answer || '...'}}</rich-text>

  </view>



  </view>

</scroll-view>




<view class="question">

  <view class="question-one"><textarea disabled="{{isDisable}}" class="texta" value="{{question}}" placeholder="请输入内容" style="width: 80%;" bindinput="inputChange" ></textarea>

  <image src="../../images/send.png" bind:tap="submitQuestion"></image></view>

</view>






</view>
javascript 复制代码
//流式响应格式 
const requestTask=wx.request({
        url: ,
        method:'POST',
        data:{
          message:this.data.AnswerList[this.data.AnswerList.length-1].message,
          parentMessageId:this.data.conversationId
        },
        enableChunked:true,
        success:(res)=>{
          //流式输出完成后,修改并保存balance
          wx.request({
            url: ,
            method:'POST',
            data:{
              balance:this.data.balance,
              _id:this.data._id
            },
            success:(res)=>{
              wx.setStorageSync('user', res.data)
              console.log(res.data);
            }
          })
        }
      })
      //通过流式输出响应到页面上,并保存对话id
      requestTask.onChunkReceived((response)=>{
        const arrayBuffer = response.data;
        const uint8Array = new Uint8Array(arrayBuffer);
        let text = wx.arrayBufferToBase64(uint8Array);
        text = Base64.decode(text);
       
        const t1=text.split("--!")
        if (t1.length>1) {
          this.setData({
            parentMessageId:t1[1]
          })
        }
        const len=this.data.AnswerList.length
          this.data.AnswerList[len-1]={
            message:this.data.AnswerList[len-1].message,
            answer:this.data.AnswerList[len-1].answer+t1[0]
          }
          this.setData({
            AnswerList:this.data.AnswerList,
            isDisable:false
          })
          console.log(t1);
      })

详细代码通过加企鹅获取

493305086

相关推荐
软件开发技术深度爱好者1 小时前
小学英语单词魔法学园HTML5实现
javascript·html5·英语学习
勉灬之1 小时前
Next.js + Prisma 跨平台部署踩坑记
开发语言·javascript·ecmascript
糖果店的幽灵1 小时前
langgraph分支之 - 动态分支(Dynamic Branch)
java·前端·javascript·人工智能·langgraph
这是个栗子1 小时前
前端开发中的常用工具函数(九)
开发语言·javascript·ecmascript·at
小码哥0682 小时前
从三甲综合医院到连锁诊所:医院管理小程序的场景化突围逻辑(陪诊系统-陪诊APP-陪诊小程序)
小程序·医院管理系统·陪诊小程序·陪诊系统·陪诊app
2501_916007473 小时前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
Revolution613 小时前
一段 JavaScript 代码执行时,到底发生了什么
前端·javascript
正在走向自律4 小时前
用豆包Seed Evolving打造全功能【AI智能记账】小程序,开源可落地
人工智能·小程序·开源·智能记账
To_OC12 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
To_OC14 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode