// pages/future/future.js const app = getApp(); const baseUrl = app.remoteConfig.baseUrl; const baseArticleId = app.wxProgramConfig.apiPageConfig.articleId; const baseMode = app.wxProgramConfig.apiPageConfig.mode; Component({ /** * 继承父级样式 */ options: { addGlobalClass: true, }, // 属性定义 properties: { // 用户信息 userInfo: { type: Object, value: () => { return { } }, } }, /** *组件的初始数据 */ data: { scrollTop: 0, mode: baseMode, article: {}, articleId: baseArticleId, isShowOenAi: false, baseUrl: 'https://yongma16.xyz/api/', baseCloudUrl: 'https://yongma16.xyz/cloudApi/', baseCloudPath: 'openAiRecord/add', path: 'article/blog/', currentUserInfo: { nickName: '', avatarUrl: '' }, aiConfig: { avatarUrl: 'https://yongma16.xyz/staticFile/common/img/aiTop.jpg', bgUrl: 'https://yongma16.xyz/staticFile/common/img/aiBg.jpg' }, searchOpenAiText: '', responseText: '', // questions,answer,index chatObjConfig: { option: [{ question: '', answer: '', isEdit: true }], currentIndex: 0 }, layoutConfig:{ introduceText:'api介绍', useText:'使用', returnText:'返回介绍', sendText:'发送', searchText:'关键词查询' } }, observers: { mode: function (newVal, oldVal) { // 属性值变化时执行 console.log(newVal, oldVal, 'change') wx.showLoading({ title: '加载中', }) // 在当前同步流程结束后,下一个时间片执行 wx.nextTick(() => { wx.hideLoading({ success: (res) => {}, }) // 触发父组件事件 this.triggerEvent('updateBottomNavigation', newVal === 'introduce') }) } }, lifetimes: { // 生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 attached: function () { this.getRemoteArticle(this.data.articleId) const openAiOptions = wx.getStorageSync('openAiOptions') if (openAiOptions) { this.setData( { chatObjConfig: openAiOptions, mode: app.wxProgramConfig.apiPageConfig.mode, layoutConfig:app.wxProgramConfig.apiPageConfig.layoutConfig, } ) } const currentUserInfo = wx.getStorageSync('currentUserInfo') if (currentUserInfo && currentUserInfo.nickName) { console.log('currentUserInfo', currentUserInfo) this.setData( { currentUserInfo: currentUserInfo } ) this.setData({ isShowOenAi: true }) } }, moved: function () { }, detached: function () { wx.setStorageSync('openAiOptions', this.data.chatObjConfig) }, }, methods: { upper(e) { console.log('upper e', e) }, lower(e) { console.log('lower e', e) }, scroll(e) { console.log('scroll e', e) }, createOpenRecord(params) { const headers = { 'Content-Type': 'application/json;charset=UTF-8' } wx.request({ url: this.data.baseCloudUrl + this.data.baseCloudPath, headers: headers, data: params, method: 'POST', success: (res => { console.log('cloud res', res) }), fail: r => { console.log('cloud r', r) } }) }, scrollToBottom() { const index=this.data.chatObjConfig.option.length-1 this.setData({ toView:`chat-mode${index}` }) }, getRemoteArticle: function (id) { this.setData({ isLoading: true }) const baseUrl = this.data.baseUrl const path = this.data.path const headers = { 'Content-Type': 'application/json;charset=UTF-8' } const that = this const params = { id: id } wx.request({ url: baseUrl + path, headers: headers, data: params, method: 'POST', success: (res) => { const data = res.data const articleStr = data && data.article && data.article const markdownText = app.towxml(articleStr, 'markdown', { theme: 'light', //主题 dark 黑色,light白色,不填默认light base: baseUrl + path, events: { //为元素绑定的事件方法 tap: e => { console.log('tap', e); }, change: e => { console.log('todo', e); } } }); that.setData({ isLoading: false, article: markdownText }) } }); }, changeMode(e) { const { mode } = e.currentTarget.dataset console.log(mode, 'mode') this.setData({ mode: mode }) }, bindKeyInput(e) { const { columnIndex } = e.currentTarget.dataset console.log('this.data.chatObjConfig', this.data.chatObjConfig) const option = this.data.chatObjConfig.option option.some((item, index) => { if (columnIndex === index) { item.question = e.detail.value item.isEdit = true return true } return false }) this.setData({ searchOpenAiText: e.detail.value, chatObjConfig: { option: option, currentIndex: columnIndex } }) }, search(e) { console.log(this.data.searchOpenAiText) this.scrollToBottom() if (!this.data.searchOpenAiText) { wx.showModal({ cancelColor: 'cancelColor', title: '请输入!' }) return } wx.showLoading({ title: '加载中', }) this.setData({ isLoading: true }) console.log(e) const path = '/common-api/searchOpenAiText/' const headers = { 'Content-Type': 'application/json;charset=UTF-8' } const params = { "text": this.data.searchOpenAiText } const thisBack = this return new Promise((resolve, reject) => { wx.request({ url: baseUrl + path, headers: headers, data: params, method: 'GET', success: (res) => { console.log(res, 'res') const data = res.data.data const option = thisBack.data.chatObjConfig.option const currentIndex = thisBack.data.chatObjConfig.currentIndex const choices = data.choices console.log('choices', choices) const answer = choices ? choices.map(choicesItem => { return choicesItem.text }).join('\n') : '。。。未知' option.some((item, index) => { if (currentIndex === index) { item.answer = answer item.isEdit = false return true } return false }) thisBack.createOpenRecord({ wx_name: thisBack.data.currentUserInfo.nickName, wx_img: thisBack.data.currentUserInfo.avatarUrl, search_text: params.text, search_response: answer }) const chatObjConfig = { option: option, currentIndex: currentIndex + 1 } option.push({ question: '', answer: '', isEdit: true }) thisBack.setData( { isLoading: false, searchOpenAiText: '', chatObjConfig: chatObjConfig } ) wx.hideLoading() thisBack.scrollToBottom() resolve(res) }, fail: error => { thisBack.setData({ isLoading: false }) wx.hideLoading() } }); }) } } })