github地址: https://github.com/HowName/smart-robot
项目为智能应答机器人,使用了图灵机器人接口,慢慢调戏吧
- 首页,主要处理页:
1 //index.js 2 3 var app = getApp(); 4 var that; 5 var chatListData = []; 6 7 Page({ 8 data: { 9 askWord: ‘‘, 10 userInfo: {}, 11 chatList: [], 12 }, 13 onLoad: function () { 14 that = this; 15 //获取用户信息 16 app.getUserInfo(function (userInfo) { 17 that.setData({ 18 userInfo: userInfo 19 }); 20 }); 21 }, 22 onReady: function () { 23 //问候语 24 setTimeout(function () { 25 that.addChat(‘你好啊!‘, ‘l‘); 26 }, 1000); 27 }, 28 sendChat: function (e) { 29 30 let word = e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;//支持两种提交方式 31 that.addChat(word, ‘r‘); 32 33 //请求api获取回答 34 app.req(‘post‘, ‘openapi/api‘, { 35 ‘data‘: { ‘info‘: word, ‘loc‘: ‘广州‘, ‘userid‘: ‘123‘ }, 36 ‘success‘: function (resp) { 37 that.addChat(resp.text, ‘l‘); 38 if (resp.url) { 39 that.addChat(resp.url, ‘l‘); 40 } 41 }, 42 }); 43 44 //清空输入框 45 that.setData({ 46 askWord: ‘‘ 47 }); 48 }, 49 //新增聊天列表 50 addChat: function (word, orientation) { 51 let ch = { ‘text‘: word, ‘time‘: new Date().getTime(), ‘orientation‘: orientation }; 52 chatListData.push(ch); 53 that.setData({ 54 chatList: chatListData 55 }); 56 } 57 })
- 页面:
1 //index.wxml 2 3 <view class="container"> 4 <scroll-view class="scrool-view" scroll-y="true"> 5 <view class="chat-list"> 6 <block wx:for="{{chatList}}" wx:key="time"> 7 <view class="chat-left" wx:if="{{item.orientation == ‘l‘}}"> 8 <image class="avatar-img" src="../../res/image/wechat-logo.png"></image> 9 <text>{{item.text}}</text> 10 </view> 11 <view class="chat-right" wx:if="{{item.orientation == ‘r‘}}"> 12 <text>{{item.text}}{{item.url}}</text> 13 <image class="avatar-img" src="{{userInfo.avatarUrl}}"></image> 14 </view> 15 </block> 16 </view> 17 </scroll-view> 18 <form bindsubmit="sendChat"> 19 <view class="ask-input-word"> 20 <input placeholder="" name="ask_word" type="text" bindconfirm="sendChat" value="{{askWord}}" /> 21 <button formType="submit" size="mini">发送</button> 22 </view> 23 </form> 24 </view>
- 网络请求方法:
1 //app.js 2 3 req: function (method, url, arg) { 4 let domian = ‘http://www.tuling123.com/‘, data = { ‘key‘: ‘9d2ff29d44b54e55acadbf5643569584‘ }, dataType = ‘json‘;//为方便广大群众,提供key 5 let header = { ‘content-type‘: ‘application/x-www-form-urlencoded‘ }; 6 7 if (arg.data) { 8 data = Object.assign(data, arg.data); 9 } 10 if (arg.header) { 11 header = Object.assign(header, arg.header); 12 } 13 if (arg.dataType) { 14 dataType = arg.dataType; 15 } 16 17 let request = { 18 method: method.toUpperCase(), 19 url: domian + url, 20 data: data, 21 dataType: dataType, 22 header: header, 23 success: function (resp) { 24 console.log(‘response content:‘, resp.data); 25 26 let data = resp.data; 27 28 typeof arg.success == "function" && arg.success(data); 29 }, 30 fail: function () { 31 wx.showToast({ 32 title: ‘请求失败,请稍后再试‘, 33 icon: ‘success‘, 34 duration: 2000 35 }); 36 37 typeof arg.fail == "function" && arg.fail(); 38 }, 39 complete: function () { 40 typeof arg.complete == "function" && arg.complete(); 41 } 42 }; 43 wx.request(request); 44 }
完!