更新记录

1.6.5(2023-10-11)

1、新增预设提示词页面 2、UI界面美化 3、修改了一些bug 4、支持uni-ai

1.5.3(2023-09-22)

1、更新加密云函数列表

1.5.2(2023-09-22)

1、首页新增prompt示例 2、更新充值页面,充值金额 3、更新Logo 4、更新页面UI

查看更多

平台兼容性

阿里云 腾讯云 支付宝云
× ×
Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.7.9 × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

云函数类插件通用教程

使用云函数类插件的前提是:使用HBuilderX 2.9+


云函数类插件通用教程

使用云函数类插件的前提是:使用HBuilderX 2.9+


插件介绍

  1. 支持最新的GPT接口,支持智能聊天、文字翻译。
  2. 支持微信广告和支付功能
  3. 支持直接使用云函数调用。
  4. 代码简单、易懂,注释完整。
  5. 支持SSE(Server-Send Events)可以流式返回数据
  6. 支持预设提示词
  7. 界面美观

体验小程序

gh_6e15288a2a6b_258

广告

广告的配置文件在/config/index.js下:

复制代码复制代码/*  注意开通微信小程序广告,需要用户数量大于1000 */
const Config = {
    // 微信插屏广告id
    interstitialAdId: "adunit-73dbdc26382b4954",
    // 激励视频广告id
    rewardedVideoAdId: "adunit-7a7e6a3a39740d03",
    // banner广告id
    bannerAdId: "adunit-a0f929d26231b85f",
    // 观看广告获得积分
    adprice: 3,
    // 分享好友获得积分
    inviteprice: 2
}
export default Config

当符合小程序流量主条件(1000用户数)后,把id替换成自己的id即可。

支付

支付需要企业资质,然后还需要认证。可以注册个公司或者个体户,这里不多讲哈。

支付用到的是uni-pay,文档在这里:https://uniapp.dcloud.net.cn/uniCloud/uni-pay.html

当你开通了微信支付后,需要修改的主要是这个地方:

复制代码/uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center/uni-pay/config.js

需要改这个地方:

复制代码复制代码"wxpay": {
        "enable": true, // 是否启用微信支付
        // 微信 - 小程序支付
        "mp": {
            "appId": "", // 小程序的appid
            "secret": "", // 小程序的secret
            "mchId": "", // 商户id
            "key": "", // v2的api key
            "pfx": fs.readFileSync(__dirname + '/wxpay/wxpay.p12'), // v2需要用到的证书
            "v3Key": "", // v3的api key
            "appCertPath": path.join(__dirname, 'wxpay/apiclient_cert.pem'), // v3需要用到的证书
            "appPrivateKeyPath": path.join(__dirname, 'wxpay/apiclient_key.pem'), // v3需要用到的证书
            "version": 2, // 启用支付的版本 2代表v2版本 3 代表v3版本
        },
        ...

把appId、secret、mchId、key等改成自己的。

然后找到/pages/charge/charge.vue页面,把toPay函数中注释的代码放开即可。

复制代码复制代码        toPay(item) {
                // 支付前,请确认已经开通了微信支付,并且修改了配置文件:/uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center/uni-pay/config.js

                this.addpoint = item.point;
                this.total_fee = item.price * 100 //改成*100

                let rand = (Math.random() * 10000).toFixed(0)
                let timestamp = Date.now()
                this.order_no = `recharge${timestamp}${rand}`;
                this.out_trade_no = `recharge${timestamp}${rand-1}`;

                /* --------------------如果已经开通了微信支付,把下面的代码放开-------------------- */
                // 发起支付
                // this.$refs.uniPay.createOrder({
                //  provider: "wxpay", // 支付供应商
                //  total_fee: this.total_fee, // 支付金额,单位分 100 = 1元
                //  order_no: this.order_no, // 业务系统订单号(即你自己业务系统的订单表的订单号)
                //  out_trade_no: this.out_trade_no, // 插件支付单号
                //  description: this.description, // 支付描述
                //  type: this.type, // 支付回调类型
                //  qr_code: this.qr_code, // 是否强制使用扫码支付
                //  openid: this.openid, // 微信公众号需要
                //  custom: {
                //      user_id: this.userInfo._id,
                //      recharge_balance: this.total_fee
                //  }
                // });
                /* --------------------如果已经开通了微信支付,把上面的代码放开-------------------- */

            },

云函数

修改uniCloud/cloudfunctions/get-openid/index.js

把appid和secret 换成自己的

复制代码复制代码const apiUrl = 'https://api.weixin.qq.com/sns/jscode2session'
// 改成自己的
const appid = ""
const secret = ""
const request = async (params) => {
    const res = await uniCloud.httpclient.request(apiUrl, {
        method: 'GET',
        data: {
            "appid": appid,
            "secret": secret,
            "js_code": params.code,
            "grant_type": "authorization_code"
        },
        contentType: 'json', // 指定以application/json发送data内的数据
        dataType: 'json' // 指定返回值为json格式,自动进行parse
    })
    return res

}

AI接口

现已接入uni-ai,更快捷,更方便!

云函数: /uniCloud/cloudfunctions/sse-chat/index.js

        const llmManager = uniCloud.ai.getLLMManager({
        // 换成自己的,使用uni-ai计费网关则不需要填写
        provider: provider,
        // 换成自己的,使用uni-ai计费网关则不需要填写
        apiKey: apiKey,
        // 使用openai接口需要配置此代理
        // proxy: 'openai.wndbac.cn'
        groupId: groupId
    })

我这里用的商汤的minimax大模型,你可以去这里注册:https://api.minimax.chat/,然后把上面的 apiKey 和groupId换成自己的。

如果你使用uni-ai的计费网关,则不要填写这两个参数。具体可以参考uni-ai的文档:https://zh.uniapp.dcloud.io/uniCloud/uni-ai.html

参数 类型 必填 默认值 说明
provider string minimax llm服务商,目前支持openaibaiduminimaxazure(azure新增于HBuilderX 3.8.3)、ifly(azure新增于HBuilderX 3.8.10)。
apiKey string 使用uni-ai计费网关时不填。使用自己的key时必填 - llm服务商的apiKey。
endpoint string 使用uni-ai计费网关时不填。使用自己的azure账户时必填 - azure服务端点,在azure创建ai服务时获取
groupId string 使用uni-ai计费网关时不填。使用自己的minimax账户时必填 - minimax的groupId参数。
accessToken string baidu必填 - llm服务商的accessToken。如何获取请参考:百度AI鉴权认证机制,需确保已开通相关接口的调用权限
proxy string 使用国外openai时必填,其他情况不填 - 可有效连接openai服务器的、可被uniCloud云函数连接的代理服务器地址。格式为IP或域名,域名不包含http前缀,协议层面仅支持https。配置为openai时必填
appId string 使用uni-ai计费网关时,部分情况必填(见后),使用讯飞账户时必填 - 客户端manifest.json内的appId,部分场景下(云函数url化、定时触发)云函数/云对象无法获取客户端appId,需要通过此参数传递
apiSecret string 讯飞必填 - llm服务商的apiSecret。

您可以自行去大模型厂商处注册并填写相应的apiKey的参数。也可以通过DCloud来购买,即使用uni-ai计费网关

uni-ai计费网关使用门槛低,并且可以一处充值,多模型体验。您无需在多个大模型厂商处申请,只需向DCloud购买token套餐,即可体验各种大模型的效果。

调用云函数

在/pages/chat/chat.vue中有调用代码:

async sseMessage(inputText) {

  const channel = new uniCloud.SSEChannel() // 创建消息通道
  channel.on('message', (message) => { // 监听message事件
    // console.log('>>>>>>>>>收到消息:on message', message);
    this.replyMessage(message)
  })
  channel.on('end', (message) => { // 监听end事件,如果云端执行end时传了message,会在客户端end事件内收到传递的消息
    this.updateMessage()
    channel.close()
  })
  await channel.open() // 等待通道开启

  const res = await uniCloud.callFunction({
    name: 'sse-chat',
    data: {
      channel: channel, // 调用云函数时传入通道对象
      inputText: inputText
    }
  })
},

采用了SSE(Server-Send Events),可以像ChatGPT一样流式的返回数据

隐私、权限声明

1. 本插件需要申请的系统权限列表:
  1. 开通流量主 2. 微信支付
2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

插件已经接入微信小程序广告,包括banner广告、插屏广告和封面广告

隐私、权限声明

1. 本插件需要申请的系统权限列表:

1. 开通流量主 2. 微信支付 3.开通uni-ai或者其他大模型接口

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

插件已经接入微信小程序广告,包括banner广告、插屏广告和封面广告

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问