// pages/future/future.js const app = getApp(); const baseUrl = app.remoteConfig.baseUrl; Component({ /** * 继承父级样式 */ options: { addGlobalClass: true, }, // 属性定义 properties: { // 用户信息 userInfo: { type: Object, value: ()=>{ return { } }, observer: function(newVal, oldVal) { // 属性值变化时执行 console.log(newVal,oldVal,'change') } } }, /** *组件的初始数据 */ data: { mode:'introduce', article:{}, articleId:44, baseUrl:'https://yongma16.xyz/api/', 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 } }, lifetimes: { // 生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 attached: function () { this.getRemoteArticle(this.data.articleId) if(wx.getStorageSync('openAiOptions')){ this.setData( { chatObjConfig:wx.getStorageSync('openAiOptions') } ) } if(wx.getStorageSync('currentUserInfo')){ console.log('currentUserInfo',wx.getStorageSync('currentUserInfo')) this.setData( { currentUserInfo:wx.getStorageSync('currentUserInfo') } ) } }, moved: function () { }, detached: function () { wx.setStorageSync('openAiOptions', this.data.chatObjConfig) }, }, methods: { 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) 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) option.some((item,index)=>{ if(currentIndex===index){ item.answer=choices?choices.map(choicesItem=>{return choicesItem.text}).join('\n'):'。。。未知' item.isEdit=false return true } return false }) const chatObjConfig={ option:option, currentIndex:currentIndex+1 } option.push({ question:'', answer:'', isEdit:true }) thisBack.setData( { isLoading: false, searchOpenAiText:'', chatObjConfig:chatObjConfig } ) wx.hideLoading() resolve(res) }, fail: error => { thisBack.setData({ isLoading: false }) wx.hideLoading() } }); }) } } })