├── pages
└── index
│ ├── index.json
│ ├── index.wxml
│ ├── index.js
│ └── index.wxss
├── app.wxss
├── res
└── image
│ └── wechat-logo.png
├── app.json
├── utils
└── util.js
├── app.js
└── README.md
/pages/index/index.json:
--------------------------------------------------------------------------------
1 | {}
--------------------------------------------------------------------------------
/app.wxss:
--------------------------------------------------------------------------------
1 | /**app.wxss**/
2 |
3 |
--------------------------------------------------------------------------------
/res/image/wechat-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HowName/smart-robot/HEAD/res/image/wechat-logo.png
--------------------------------------------------------------------------------
/app.json:
--------------------------------------------------------------------------------
1 | {
2 | "pages":[
3 | "pages/index/index"
4 | ],
5 | "window":{
6 | "backgroundTextStyle":"light",
7 | "navigationBarBackgroundColor": "#fff",
8 | "navigationBarTitleText": "Smart Robot",
9 | "navigationBarTextStyle":"black"
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/utils/util.js:
--------------------------------------------------------------------------------
1 | function formatTime(date) {
2 | var year = date.getFullYear()
3 | var month = date.getMonth() + 1
4 | var day = date.getDate()
5 |
6 | var hour = date.getHours()
7 | var minute = date.getMinutes()
8 | var second = date.getSeconds()
9 |
10 |
11 | return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
12 | }
13 |
14 | function formatNumber(n) {
15 | n = n.toString()
16 | return n[1] ? n : '0' + n
17 | }
18 |
19 | module.exports = {
20 | formatTime: formatTime
21 | }
22 |
--------------------------------------------------------------------------------
/pages/index/index.wxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | {{item.text}}
10 |
11 |
12 | {{item.text}}{{item.url}}
13 |
14 |
15 |
16 |
17 |
18 |
24 |
--------------------------------------------------------------------------------
/pages/index/index.js:
--------------------------------------------------------------------------------
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 | })
58 |
--------------------------------------------------------------------------------
/pages/index/index.wxss:
--------------------------------------------------------------------------------
1 | /**index.wxss**/
2 |
3 | page {
4 | height: 100%;
5 | }
6 |
7 | .container {
8 | height: 100%;
9 | min-height: 100%;
10 | display: flex;
11 | flex-direction: column;
12 | flex-wrap: wrap;
13 | padding: 15rpx 15rpx;
14 | background-color: gainsboro;
15 | }
16 |
17 | .chat-list {
18 | flex: 1;
19 | padding-bottom: 80rpx;
20 | }
21 |
22 | .scrool-view {
23 | }
24 |
25 | .chat-left {
26 | display: flex;
27 | align-items: center;
28 | margin-top: 20rpx;
29 | }
30 |
31 | .chat-left text {
32 | flex: 1;
33 | margin-left: 10rpx;
34 | padding: 10rpx 5rpx;
35 | background-color: white;
36 | border-radius: 15rpx;
37 | word-break: break-all;
38 | }
39 |
40 | .chat-right {
41 | display: flex;
42 | align-items: center;
43 | margin-top: 20rpx;
44 | }
45 |
46 | .chat-right text {
47 | flex: 1;
48 | margin-right: 10rpx;
49 | padding: 10rpx 5rpx;
50 | background-color: yellow;
51 | border-radius: 15rpx;
52 | word-break: break-all;
53 | }
54 |
55 | .avatar-img {
56 | width: 70rpx;
57 | height: 70rpx;
58 | border-radius: 70rpx;
59 | }
60 |
61 | .ask-input-word {
62 | width: 96%;
63 | position: fixed;
64 | padding: 5rpx;
65 | bottom: 5rpx;
66 | display: flex;
67 | align-items: center;
68 | }
69 |
70 | .ask-input-word input {
71 | background: white;
72 | border-radius: 15rpx;
73 | flex: 1;
74 | margin-right: 10rpx;
75 | }
76 |
77 | .pos-rel {
78 | position: relative;
79 | }
80 |
81 | .pos-abs {
82 | position: absolute;
83 | }
84 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | //app.js
2 | App({
3 | onLaunch: function () {
4 | //调用API从本地缓存中获取数据
5 | var logs = wx.getStorageSync('logs') || []
6 | logs.unshift(Date.now())
7 | wx.setStorageSync('logs', logs)
8 | },
9 | getUserInfo: function (cb) {
10 | var that = this
11 | if (this.globalData.userInfo) {
12 | typeof cb == "function" && cb(this.globalData.userInfo)
13 | } else {
14 | //调用登录接口
15 | wx.login({
16 | success: function () {
17 | wx.getUserInfo({
18 | success: function (res) {
19 | that.globalData.userInfo = res.userInfo
20 | typeof cb == "function" && cb(that.globalData.userInfo)
21 | }
22 | })
23 | }
24 | })
25 | }
26 | },
27 | globalData: {
28 | userInfo: null
29 | },
30 | // Request function
31 | req: function (method, url, arg) {
32 | let domian = 'http://www.tuling123.com/', data = { 'key': '9d2ff29d44b54e55acadbf5643569584' }, dataType = 'json';//为方便广大群众,提供key
33 | let header = { 'content-type': 'application/x-www-form-urlencoded' };
34 |
35 | if (arg.data) {
36 | data = Object.assign(data, arg.data);
37 | }
38 | if (arg.header) {
39 | header = Object.assign(header, arg.header);
40 | }
41 | if (arg.dataType) {
42 | dataType = arg.dataType;
43 | }
44 |
45 | let request = {
46 | method: method.toUpperCase(),
47 | url: domian + url,
48 | data: data,
49 | dataType: dataType,
50 | header: header,
51 | success: function (resp) {
52 | console.log('response content:', resp.data);
53 |
54 | let data = resp.data;
55 |
56 | typeof arg.success == "function" && arg.success(data);
57 | },
58 | fail: function () {
59 | wx.showToast({
60 | title: '请求失败,请稍后再试',
61 | icon: 'success',
62 | duration: 2000
63 | });
64 |
65 | typeof arg.fail == "function" && arg.fail();
66 | },
67 | complete: function () {
68 | typeof arg.complete == "function" && arg.complete();
69 | }
70 | };
71 | wx.request(request);
72 | },
73 | })
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 微信小程序-智能机器人
2 | 项目为智能应答机器人,使用了图灵机器人接口,慢慢调戏吧
3 |
4 | 
5 |
6 | - 首页,主要处理页:
7 |
8 | ```
9 | //index.js
10 |
11 | var app = getApp();
12 | var that;
13 | var chatListData = [];
14 |
15 | Page({
16 | data: {
17 | askWord: '',
18 | userInfo: {},
19 | chatList: [],
20 | },
21 | onLoad: function () {
22 | that = this;
23 | //获取用户信息
24 | app.getUserInfo(function (userInfo) {
25 | that.setData({
26 | userInfo: userInfo
27 | });
28 | });
29 | },
30 | onReady: function () {
31 | //问候语
32 | setTimeout(function () {
33 | that.addChat('你好啊!', 'l');
34 | }, 1000);
35 | },
36 | sendChat: function (e) {
37 |
38 | let word = e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;//支持两种提交方式
39 | that.addChat(word, 'r');
40 |
41 | //请求api获取回答
42 | app.req('post', 'openapi/api', {
43 | 'data': { 'info': word, 'loc': '广州', 'userid': '123' },
44 | 'success': function (resp) {
45 | that.addChat(resp.text, 'l');
46 | if (resp.url) {
47 | that.addChat(resp.url, 'l');
48 | }
49 | },
50 | });
51 |
52 | //清空输入框
53 | that.setData({
54 | askWord: ''
55 | });
56 | },
57 | //新增聊天列表
58 | addChat: function (word, orientation) {
59 | let ch = { 'text': word, 'time': new Date().getTime(), 'orientation': orientation };
60 | chatListData.push(ch);
61 | that.setData({
62 | chatList: chatListData
63 | });
64 | }
65 | })
66 |
67 | ```
68 |
69 | - 页面:
70 |
71 | ```
72 | //index.wxml
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | {{item.text}}
81 |
82 |
83 | {{item.text}}{{item.url}}
84 |
85 |
86 |
87 |
88 |
89 |
95 |
96 |
97 | ```
98 |
99 |
100 |
101 | - 网络请求方法:
102 | ```
103 | //app.js
104 |
105 | req: function (method, url, arg) {
106 | let domian = 'http://www.tuling123.com/', data = { 'key': '9d2ff29d44b54e55acadbf5643569584' }, dataType = 'json';//为方便广大群众,提供key
107 | let header = { 'content-type': 'application/x-www-form-urlencoded' };
108 |
109 | if (arg.data) {
110 | data = Object.assign(data, arg.data);
111 | }
112 | if (arg.header) {
113 | header = Object.assign(header, arg.header);
114 | }
115 | if (arg.dataType) {
116 | dataType = arg.dataType;
117 | }
118 |
119 | let request = {
120 | method: method.toUpperCase(),
121 | url: domian + url,
122 | data: data,
123 | dataType: dataType,
124 | header: header,
125 | success: function (resp) {
126 | console.log('response content:', resp.data);
127 |
128 | let data = resp.data;
129 |
130 | typeof arg.success == "function" && arg.success(data);
131 | },
132 | fail: function () {
133 | wx.showToast({
134 | title: '请求失败,请稍后再试',
135 | icon: 'success',
136 | duration: 2000
137 | });
138 |
139 | typeof arg.fail == "function" && arg.fail();
140 | },
141 | complete: function () {
142 | typeof arg.complete == "function" && arg.complete();
143 | }
144 | };
145 | wx.request(request);
146 | }
147 | ```
148 | 完!
149 |
--------------------------------------------------------------------------------