index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // pages/exampleDetail/index.js
  2. Page({
  3. data: {
  4. type: "",
  5. envId: "",
  6. showTip: false,
  7. title: "",
  8. content: "",
  9. haveGetOpenId: false,
  10. openId: "",
  11. haveGetCodeSrc: false,
  12. codeSrc: "",
  13. haveGetRecord: false,
  14. record: [],
  15. haveGetImgSrc: false,
  16. imgSrc: "",
  17. // ai
  18. modelConfig: {
  19. modelProvider: "deepseek", // 大模型服务厂商
  20. quickResponseModel: "deepseek-v3", // 快速响应模型 (混元 turbo, gpt4 turbo版,deepseek v3等)
  21. logo: "https://cloudcache.tencent-cloud.com/qcloud/ui/static/static_source_business/2339414f-2c0d-4537-9618-1812bd14f4af.svg", // model 头像
  22. welcomeMsg: "我是deepseek-v3,很高兴见到你!", // model 欢迎语
  23. },
  24. callcbrCode: "",
  25. initEnvCode: "",
  26. callOpenIdCode: "",
  27. callMiniProgramCode: "",
  28. callFunctionCode: "",
  29. callCreateCollectionCode: "",
  30. callUploadFileCode: "",
  31. showInsertModal: false,
  32. insertRegion: "",
  33. insertCity: "",
  34. insertSales: "",
  35. haveGetCallContainerRes: false,
  36. callContainerResStr: "",
  37. ai_page_config: `{
  38. "usingComponents": {
  39. "agent-ui":"/components/agent-ui/index"
  40. },
  41. }`,
  42. ai_wxml_config: `<agent-ui agentConfig="{{agentConfig}}" showBotAvatar="{{showBotAvatar}}" chatMode="{{chatMode}}" modelConfig="{{modelConfig}}""></agent-ui>`,
  43. ai_data_config: `data: {
  44. chatMode: "bot", // bot 表示使用agent,model 表示使用大模型
  45. showBotAvatar: true, // 是否在对话框左侧显示头像
  46. agentConfig: {
  47. botId: "your agent id", // agent id,
  48. allowWebSearch: true, // 允许客户端选择展示联网搜索按钮
  49. allowUploadFile: true, // 允许客户端展示上传文件按钮
  50. allowPullRefresh: true, // 允许客户端展示下拉刷新
  51. allowUploadImage: true, // 允许客户端展示上传图片按钮
  52. allowMultiConversation: true, // 允许客户端展示查看会话列表/新建会话按钮
  53. showToolCallDetail: true, // 是否展示 mcp server toolCall 细节
  54. allowVoice: true, // 允许客户端展示语音按钮
  55. showBotName: true, // 允许展示bot名称
  56. },
  57. modelConfig: {
  58. modelProvider: "hunyuan-open", // 大模型服务厂商
  59. quickResponseModel: "hunyuan-lite", // 大模型名称
  60. logo: "", // model 头像
  61. welcomeMsg: "欢迎语", // model 欢迎语
  62. },
  63. }`,
  64. },
  65. onLoad(options) {
  66. console.log("options", options);
  67. if (
  68. options.type === "cloudbaserunfunction" ||
  69. options.type === "cloudbaserun"
  70. ) {
  71. this.getCallcbrCode();
  72. }
  73. if (options.type === "getOpenId") {
  74. this.getOpenIdCode();
  75. }
  76. if (options.type === "getMiniProgramCode") {
  77. this.getMiniProgramCode();
  78. }
  79. if (options.type === "createCollection") {
  80. this.getCreateCollectionCode();
  81. }
  82. if (options.type === "uploadFile") {
  83. this.getUploadFileCode();
  84. }
  85. this.setData({ type: options?.type, envId: options?.envId });
  86. },
  87. copyUrl() {
  88. wx.setClipboardData({
  89. data: "https://gitee.com/TencentCloudBase/cloudbase-agent-ui/tree/main/apps/miniprogram-agent-ui/miniprogram/components/agent-ui",
  90. success: function (res) {
  91. wx.showToast({
  92. title: "复制成功",
  93. icon: "success",
  94. });
  95. },
  96. });
  97. },
  98. insertRecord() {
  99. this.setData({
  100. showInsertModal: true,
  101. insertRegion: "",
  102. insertCity: "",
  103. insertSales: "",
  104. });
  105. },
  106. deleteRecord(e) {
  107. console.log("deleteRecord e", e);
  108. // 调用云函数删除记录
  109. wx.showLoading({
  110. title: "删除中...",
  111. });
  112. wx.cloud
  113. .callFunction({
  114. name: "quickstartFunctions",
  115. data: {
  116. type: "deleteRecord",
  117. data: {
  118. _id: e.currentTarget.dataset.id,
  119. },
  120. },
  121. })
  122. .then((resp) => {
  123. wx.showToast({
  124. title: "删除成功",
  125. });
  126. this.getRecord(); // 刷新列表
  127. wx.hideLoading();
  128. })
  129. .catch((e) => {
  130. wx.showToast({
  131. title: "删除失败",
  132. icon: "none",
  133. });
  134. wx.hideLoading();
  135. });
  136. // const index = e.currentTarget.dataset.index;
  137. // const record = this.data.record;
  138. // record.splice(index, 1);
  139. // this.setData({
  140. // record,
  141. // });
  142. },
  143. // 输入框事件
  144. onInsertRegionInput(e) {
  145. this.setData({ insertRegion: e.detail.value });
  146. },
  147. onInsertCityInput(e) {
  148. this.setData({ insertCity: e.detail.value });
  149. },
  150. onInsertSalesInput(e) {
  151. this.setData({ insertSales: e.detail.value });
  152. },
  153. // 取消弹窗
  154. onInsertCancel() {
  155. this.setData({ showInsertModal: false });
  156. },
  157. // 确认插入
  158. async onInsertConfirm() {
  159. const { insertRegion, insertCity, insertSales } = this.data;
  160. if (!insertRegion || !insertCity || !insertSales) {
  161. wx.showToast({ title: "请填写完整信息", icon: "none" });
  162. return;
  163. }
  164. wx.showLoading({ title: "插入中..." });
  165. try {
  166. await wx.cloud.callFunction({
  167. name: "quickstartFunctions",
  168. data: {
  169. type: "insertRecord",
  170. data: {
  171. region: insertRegion,
  172. city: insertCity,
  173. sales: Number(insertSales),
  174. },
  175. },
  176. });
  177. wx.showToast({ title: "插入成功" });
  178. this.setData({ showInsertModal: false });
  179. this.getRecord(); // 刷新列表
  180. } catch (e) {
  181. wx.showToast({ title: "插入失败", icon: "none" });
  182. } finally {
  183. wx.hideLoading();
  184. }
  185. },
  186. getOpenId() {
  187. wx.showLoading({
  188. title: "",
  189. });
  190. wx.cloud
  191. .callFunction({
  192. name: "quickstartFunctions",
  193. data: {
  194. type: "getOpenId",
  195. },
  196. })
  197. .then((resp) => {
  198. this.setData({
  199. haveGetOpenId: true,
  200. openId: resp.result.openid,
  201. });
  202. wx.hideLoading();
  203. })
  204. .catch((e) => {
  205. wx.hideLoading();
  206. const { errCode, errMsg } = e;
  207. if (errMsg.includes("Environment not found")) {
  208. this.setData({
  209. showTip: true,
  210. title: "云开发环境未找到",
  211. content:
  212. "如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。",
  213. });
  214. return;
  215. }
  216. if (errMsg.includes("FunctionName parameter could not be found")) {
  217. this.setData({
  218. showTip: true,
  219. title: "请上传云函数",
  220. content:
  221. "在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。",
  222. });
  223. return;
  224. }
  225. });
  226. },
  227. clearOpenId() {
  228. this.setData({
  229. haveGetOpenId: false,
  230. openId: "",
  231. });
  232. },
  233. clearCallContainerRes() {
  234. this.setData({
  235. haveGetCallContainerRes: false,
  236. callContainerResStr: "",
  237. });
  238. },
  239. getCodeSrc() {
  240. wx.showLoading({
  241. title: "",
  242. });
  243. wx.cloud
  244. .callFunction({
  245. name: "quickstartFunctions",
  246. data: {
  247. type: "getMiniProgramCode",
  248. },
  249. })
  250. .then((resp) => {
  251. this.setData({
  252. haveGetCodeSrc: true,
  253. codeSrc: resp.result,
  254. });
  255. wx.hideLoading();
  256. })
  257. .catch((e) => {
  258. wx.hideLoading();
  259. const { errCode, errMsg } = e;
  260. if (errMsg.includes("Environment not found")) {
  261. this.setData({
  262. showTip: true,
  263. title: "云开发环境未找到",
  264. content:
  265. "如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。",
  266. });
  267. return;
  268. }
  269. if (errMsg.includes("FunctionName parameter could not be found")) {
  270. this.setData({
  271. showTip: true,
  272. title: "请上传云函数",
  273. content:
  274. "在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。",
  275. });
  276. return;
  277. }
  278. });
  279. },
  280. clearCodeSrc() {
  281. this.setData({
  282. haveGetCodeSrc: false,
  283. codeSrc: "",
  284. });
  285. },
  286. bindInput(e) {
  287. const index = e.currentTarget.dataset.index;
  288. const record = this.data.record;
  289. record[index].sales = Number(e.detail.value);
  290. this.setData({
  291. record,
  292. });
  293. },
  294. getRecord() {
  295. wx.showLoading({
  296. title: "",
  297. });
  298. wx.cloud
  299. .callFunction({
  300. name: "quickstartFunctions",
  301. data: {
  302. type: "selectRecord",
  303. },
  304. })
  305. .then((resp) => {
  306. this.setData({
  307. haveGetRecord: true,
  308. record: resp.result.data,
  309. });
  310. wx.hideLoading();
  311. })
  312. .catch((e) => {
  313. this.setData({
  314. showTip: true,
  315. });
  316. wx.hideLoading();
  317. });
  318. },
  319. clearRecord() {
  320. this.setData({
  321. haveGetRecord: false,
  322. record: [],
  323. });
  324. },
  325. updateRecord() {
  326. wx.showLoading({
  327. title: "",
  328. });
  329. wx.cloud
  330. .callFunction({
  331. name: "quickstartFunctions",
  332. data: {
  333. type: "updateRecord",
  334. data: this.data.record,
  335. },
  336. })
  337. .then((resp) => {
  338. wx.showToast({
  339. title: "更新成功",
  340. });
  341. wx.hideLoading();
  342. })
  343. .catch((e) => {
  344. console.log(e);
  345. this.setData({
  346. showUploadTip: true,
  347. });
  348. wx.hideLoading();
  349. });
  350. },
  351. uploadImg() {
  352. wx.showLoading({
  353. title: "",
  354. });
  355. // 让用户选择一张图片
  356. wx.chooseMedia({
  357. count: 1,
  358. success: (chooseResult) => {
  359. // 将图片上传至云存储空间
  360. wx.cloud
  361. .uploadFile({
  362. // 指定上传到的云路径
  363. cloudPath: `my-photo-${new Date().getTime()}.png`,
  364. // 指定要上传的文件的小程序临时文件路径
  365. filePath: chooseResult.tempFiles[0].tempFilePath,
  366. })
  367. .then((res) => {
  368. console.log("upload res", res);
  369. this.setData({
  370. haveGetImgSrc: true,
  371. imgSrc: res.fileID,
  372. });
  373. })
  374. .catch((e) => {
  375. console.log("e", e);
  376. });
  377. },
  378. complete: () => {
  379. wx.hideLoading();
  380. },
  381. });
  382. },
  383. clearImgSrc() {
  384. this.setData({
  385. haveGetImgSrc: false,
  386. imgSrc: "",
  387. });
  388. },
  389. goOfficialWebsite() {
  390. const url = "https://docs.cloudbase.net/toolbox/quick-start";
  391. wx.navigateTo({
  392. url: `../web/index?url=${url}`,
  393. });
  394. },
  395. runCallContainer: async function () {
  396. const app = getApp();
  397. console.log("globalData", app.globalData);
  398. const c1 = new wx.cloud.Cloud({
  399. resourceEnv: app.globalData.env,
  400. });
  401. await c1.init();
  402. const r = await c1.callContainer({
  403. path: "/api/users", // 填入业务自定义路径
  404. header: {
  405. "X-WX-SERVICE": "express-test", // 填入服务名称
  406. },
  407. // 其余参数同 wx.request
  408. method: "GET",
  409. });
  410. console.log(r);
  411. this.setData({
  412. haveGetCallContainerRes: true,
  413. callContainerResStr: `${JSON.stringify(r.data.items, null, 2)}`,
  414. });
  415. },
  416. getCallcbrCode: function () {
  417. const app = getApp();
  418. this.setData({
  419. callcbrCode: `const c1 = new wx.cloud.Cloud({
  420. resourceEnv: ${app.globalData.env}
  421. })
  422. await c1.init()
  423. const r = await c1.callContainer({
  424. path: '/api/users', // 此处填入业务自定义路径, /api/users 为示例路径
  425. header: {
  426. 'X-WX-SERVICE': 'express-test', // 填入业务服务名称,express-test 为示例服务
  427. },
  428. // 其余参数同 wx.request
  429. method: 'GET',
  430. })`,
  431. });
  432. },
  433. getInitEnvCode: function () {
  434. const app = getApp();
  435. this.setData({
  436. initEnvCode: `wx.cloud.init({
  437. env: ${app.globalData.env},
  438. traceUser: true,
  439. });`,
  440. });
  441. },
  442. getCreateCollectionCode: function () {
  443. this.setData({
  444. callCreateCollectionCode: `const cloud = require('wx-server-sdk');
  445. cloud.init({
  446. env: cloud.DYNAMIC_CURRENT_ENV
  447. });
  448. const db = cloud.database();
  449. // 创建集合云函数入口函数
  450. exports.main = async (event, context) => {
  451. try {
  452. // 创建集合
  453. await db.createCollection('sales');
  454. return {
  455. success: true
  456. };
  457. } catch (e) {
  458. return {
  459. success: true,
  460. data: 'create collection success'
  461. };
  462. }
  463. };`,
  464. });
  465. },
  466. getOpenIdCode: function () {
  467. this.setData({
  468. callOpenIdCode: `const cloud = require('wx-server-sdk');
  469. cloud.init({
  470. env: cloud.DYNAMIC_CURRENT_ENV
  471. });
  472. // 获取openId云函数入口函数
  473. exports.main = async (event, context) => {
  474. // 获取基础信息
  475. const wxContext = cloud.getWXContext();
  476. return {
  477. openid: wxContext.OPENID,
  478. appid: wxContext.APPID,
  479. unionid: wxContext.UNIONID,
  480. };
  481. };`,
  482. callFunctionCode: `wx.cloud.callFunction({
  483. name: 'quickstartFunctions',
  484. data: {
  485. type: 'getOpenId'
  486. }
  487. }).then((resp) => console.log(resp))`,
  488. });
  489. },
  490. getMiniProgramCode: function () {
  491. this.setData({
  492. callMiniProgramCode: `const cloud = require('wx-server-sdk');
  493. cloud.init({
  494. env: cloud.DYNAMIC_CURRENT_ENV
  495. });
  496. // 获取小程序二维码云函数入口函数
  497. exports.main = async (event, context) => {
  498. // 获取小程序二维码的buffer
  499. const resp = await cloud.openapi.wxacode.get({
  500. path: 'pages/index/index'
  501. });
  502. const { buffer } = resp;
  503. // 将图片上传云存储空间
  504. const upload = await cloud.uploadFile({
  505. cloudPath: 'code.png',
  506. fileContent: buffer
  507. });
  508. return upload.fileID;
  509. };
  510. `,
  511. callFunctionCode: `wx.cloud.callFunction({
  512. name: 'quickstartFunctions',
  513. data: {
  514. type: 'getMiniProgramCode'
  515. }
  516. }).then((resp) => console.log(resp))`,
  517. });
  518. },
  519. getUploadFileCode: function () {
  520. this.setData({
  521. callUploadFileCode: `wx.chooseMedia({
  522. count: 1,
  523. success: (chooseResult) => {
  524. // 将图片上传至云存储空间
  525. wx.cloud
  526. .uploadFile({
  527. // 指定上传到的云路径
  528. cloudPath: "my-photo.png",
  529. // 指定要上传的文件的小程序临时文件路径
  530. filePath: chooseResult.tempFiles[0].tempFilePath,
  531. })
  532. .then((res) => {
  533. console.log(res)
  534. })
  535. .catch((e) => {
  536. console.log('e', e)
  537. });
  538. }
  539. });`,
  540. });
  541. },
  542. });