├── README.md ├── app.js ├── app.json ├── app.wxss ├── emoji ├── bishi.png ├── bugaoxing.png ├── chijing.png ├── deyi.png ├── exin.png ├── guai.png ├── haha.png ├── han.png ├── hehe.png ├── heixian.png ├── hu.png ├── huaji.png ├── jingku.png ├── jingya.png ├── kaixin.png ├── ku.png ├── kuanghan.png ├── lengmo.png ├── mengmengda.png ├── mianqiang.png ├── nu.png ├── pen.png ├── qie.png ├── qinqin.png ├── 吐舌.png ├── 咦.png ├── 委屈.png ├── 生气.png ├── 疑问.png ├── 真棒.png ├── 睡觉.png ├── 笑眼.png └── 阴险.png ├── images ├── emotion.png ├── keyinput.png ├── robot.jpg ├── speech0.png ├── speech1.png ├── speech2.png └── voice.png ├── pages └── index │ ├── index.js │ ├── index.json │ ├── index.wxml │ └── index.wxss ├── utils └── util.js └── wa-ui └── wa-ui.wxss /README.md: -------------------------------------------------------------------------------- 1 | # partner-robot 2 | 微信小程序-语音聊天机器人,与机器人一对一聊天,支持语音、表情、文字等。 3 | 服务端项目地址 4 | + nodejs服务端 [partner-nodejs-server](https://github.com/zhukai-git/partner-nodejs-server) 5 | ### 已发布小程序:聊天机器人小Q 6 | #### 服务端被窝关掉了,所以发送消息没有回复了。。 7 | ![](https://novicezk.github.io/partner-code.jpg) 8 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | App({ 2 | globalData: { 3 | host: 'https://homolo.top', 4 | emojisEn: ['bugaoxing', 'guai', 'qinqin', 'lengmo', 'qie', 'mianqiang', 'chijing', 'tushe', 'hehe', 'hu', 'yi', 'haha', 'ku', 'pen', 'weiqu', 'kaixin', 'deyi', 'nu', 'exin', 'jingku', 'jingya', 'han', 'huaji', 'kuanghan', 'shengqi', 'yiwen', 'zhenbang', 'shuijue', 'xiaoyan', 'mengmengda', 'bishi', 'yinxian', 'heixian'], 5 | emojis: ['不高兴', '乖', '亲亲', '冷漠', '切~', '勉强', '吃惊', '吐舌', '呵呵', '呼~', '咦', '哈哈', '哭', '喷', '委屈', '开心', '得意', '怒', '恶心', '惊哭', '惊讶', '汗', '滑稽', '狂汗', '生气', '疑问', '真棒', '睡觉', '笑眼', '萌萌哒', '鄙视', '阴险', '黑线'] 6 | }, 7 | onLaunch: function () { 8 | var that = this; 9 | wx.getSetting({ 10 | success(res) { 11 | if (!res['scope.record']) { 12 | wx.authorize({ 13 | scope: 'scope.record' 14 | }) 15 | } 16 | if (!res['scope.userInfo']) { 17 | wx.authorize({ 18 | scope: 'scope.userInfo' 19 | }) 20 | } 21 | } 22 | }) 23 | wx.login({ 24 | success: function (data) { 25 | wx.getUserInfo({ 26 | success: function (res) { 27 | wx.setStorage({ 28 | key: 'userInfo', 29 | data: res.userInfo, 30 | }) 31 | } 32 | }) 33 | wx.request({ 34 | method: 'GET', 35 | url: that.globalData.host + '/wx/getOpenid/' + data.code, 36 | success: function (res) { 37 | wx.setStorage({ 38 | key: 'openid', 39 | data: res.data, 40 | }) 41 | } 42 | }); 43 | } 44 | }) 45 | } 46 | }) -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ 3 | "pages/index/index" 4 | ], 5 | "window": { 6 | "backgroundTextStyle": "light", 7 | "navigationBarBackgroundColor": "#000", 8 | "navigationBarTitleText": "小Q", 9 | "navigationBarTextStyle": "#fff" 10 | }, 11 | "networkTimeout": { 12 | "request": 10000, 13 | "downloadFile": 10000 14 | } 15 | } -------------------------------------------------------------------------------- /app.wxss: -------------------------------------------------------------------------------- 1 | /**app.wxss**/ 2 | @import './wa-ui/wa-ui.wxss'; 3 | page{ 4 | height: 100% 5 | } 6 | .container { 7 | height: 100%; 8 | display: flex; 9 | flex-direction: column; 10 | align-items: center; 11 | justify-content: space-between; 12 | padding: 200rpx 0; 13 | box-sizing: border-box; 14 | } 15 | .padding{ 16 | padding:20rpx; 17 | } 18 | .padding_half{ 19 | padding: 10rpx; 20 | } 21 | .graySpace{ 22 | padding-left: 40rpx; 23 | background: #eee; 24 | font-size: 12px; 25 | min-height: 30rpx; 26 | } 27 | .item-avatar .text_box,.item-avatar-lg .text_box{ 28 | display: flex; 29 | flex-direction: column 30 | } 31 | .flex-row{ 32 | display: flex; 33 | flex-direction: row; 34 | } 35 | .flex-column{ 36 | display: flex; 37 | flex-direction: column 38 | } 39 | .flex-center{ 40 | display: flex; 41 | justify-content: center; 42 | } 43 | .flex{ 44 | display: flex; 45 | } 46 | .title{ 47 | font-family: "微软雅黑"; 48 | font-size: 32rpx; 49 | } 50 | .message{ 51 | font-size: 30rpx; 52 | } 53 | 54 | .item-avatar-lg,.item-avatar-md,.item-avatar-sm,.item-avatar-xs{ 55 | display: flex; 56 | padding: 20rpx 30rpx; 57 | border-bottom: 1rpx solid #eee; 58 | cursor: pointer; 59 | position:relative; 60 | } 61 | .item-avatar-lg image{ 62 | float: left; 63 | width: 125rpx; 64 | height: 125rpx; 65 | box-sizing: border-box 66 | } 67 | .item-avatar-md image{ 68 | float: left; 69 | width: 85rpx; 70 | height: 85rpx; 71 | box-sizing: border-box 72 | } 73 | .item-avatar-sm image{ 74 | float: left; 75 | width: 65rpx; 76 | height: 65rpx; 77 | box-sizing: border-box 78 | } 79 | .item-avatar-xs image{ 80 | float: left; 81 | width: 45rpx; 82 | height: 45rpx; 83 | box-sizing: border-box 84 | } 85 | 86 | .item-avatar-md .marks{ 87 | position: absolute; 88 | top: 0px; 89 | left: 90rpx; 90 | width: 40rpx; 91 | height: 40rpx; 92 | line-height: 40rpx; 93 | background: red; 94 | border-radius: 50%; 95 | color: white; 96 | text-align: center; 97 | font-size: 28rpx; 98 | } 99 | .item-avatar-lg .item-text,.item-avatar-md .item-text,.item-avatar-sm .item-text,.item-avatar-xs .item-text{ 100 | padding-left: 10px; 101 | } 102 | .item-avatar-md .item-time{ 103 | position: absolute; 104 | right: 20rpx; 105 | } 106 | .clearfix{ 107 | overflow: hidden; 108 | } 109 | .icon::before{ 110 | font-size: 70rpx 111 | } 112 | .floatR{ 113 | float: right; 114 | } 115 | -------------------------------------------------------------------------------- /emoji/bishi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/bishi.png -------------------------------------------------------------------------------- /emoji/bugaoxing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/bugaoxing.png -------------------------------------------------------------------------------- /emoji/chijing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/chijing.png -------------------------------------------------------------------------------- /emoji/deyi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/deyi.png -------------------------------------------------------------------------------- /emoji/exin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/exin.png -------------------------------------------------------------------------------- /emoji/guai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/guai.png -------------------------------------------------------------------------------- /emoji/haha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/haha.png -------------------------------------------------------------------------------- /emoji/han.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/han.png -------------------------------------------------------------------------------- /emoji/hehe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/hehe.png -------------------------------------------------------------------------------- /emoji/heixian.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/heixian.png -------------------------------------------------------------------------------- /emoji/hu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/hu.png -------------------------------------------------------------------------------- /emoji/huaji.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/huaji.png -------------------------------------------------------------------------------- /emoji/jingku.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/jingku.png -------------------------------------------------------------------------------- /emoji/jingya.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/jingya.png -------------------------------------------------------------------------------- /emoji/kaixin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/kaixin.png -------------------------------------------------------------------------------- /emoji/ku.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/ku.png -------------------------------------------------------------------------------- /emoji/kuanghan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/kuanghan.png -------------------------------------------------------------------------------- /emoji/lengmo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/lengmo.png -------------------------------------------------------------------------------- /emoji/mengmengda.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/mengmengda.png -------------------------------------------------------------------------------- /emoji/mianqiang.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/mianqiang.png -------------------------------------------------------------------------------- /emoji/nu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/nu.png -------------------------------------------------------------------------------- /emoji/pen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/pen.png -------------------------------------------------------------------------------- /emoji/qie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/qie.png -------------------------------------------------------------------------------- /emoji/qinqin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/qinqin.png -------------------------------------------------------------------------------- /emoji/吐舌.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/吐舌.png -------------------------------------------------------------------------------- /emoji/咦.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/咦.png -------------------------------------------------------------------------------- /emoji/委屈.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/委屈.png -------------------------------------------------------------------------------- /emoji/生气.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/生气.png -------------------------------------------------------------------------------- /emoji/疑问.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/疑问.png -------------------------------------------------------------------------------- /emoji/真棒.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/真棒.png -------------------------------------------------------------------------------- /emoji/睡觉.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/睡觉.png -------------------------------------------------------------------------------- /emoji/笑眼.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/笑眼.png -------------------------------------------------------------------------------- /emoji/阴险.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/emoji/阴险.png -------------------------------------------------------------------------------- /images/emotion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/emotion.png -------------------------------------------------------------------------------- /images/keyinput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/keyinput.png -------------------------------------------------------------------------------- /images/robot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/robot.jpg -------------------------------------------------------------------------------- /images/speech0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/speech0.png -------------------------------------------------------------------------------- /images/speech1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/speech1.png -------------------------------------------------------------------------------- /images/speech2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/speech2.png -------------------------------------------------------------------------------- /images/voice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novicezk/partner-robot/6c88fa0be16bc9deb590107166850b9fdc1ca17d/images/voice.png -------------------------------------------------------------------------------- /pages/index/index.js: -------------------------------------------------------------------------------- 1 | var util = require('../../utils/util') 2 | const app = getApp() 3 | const host = app.globalData.host 4 | var emojis = app.globalData.emojis 5 | Page({ 6 | data: { 7 | messages: [], 8 | isSpeech: false, 9 | scrollHeight: 0, 10 | toView: '', 11 | windowHeight: 0, 12 | windowWidth: 0, 13 | pxToRpx: 2, 14 | msg: '', 15 | emotionBox: false, 16 | emotions: [], 17 | speechText: '按住 说话', 18 | changeImageUrl: '/images/voice.png', 19 | speechIcon: '/images/speech0.png', 20 | defaultSpeechIcon: '/images/speech0.png', 21 | emotionIcon: '/images/emotion.png', 22 | playingSpeech: '' 23 | }, 24 | chooseEmotion(e) { 25 | this.setData({ 26 | msg: this.data.msg + '[' + e.target.dataset.name + ']', 27 | }) 28 | }, 29 | sendMessage(e) { 30 | this.setData({ 31 | msg: e.detail.value, 32 | }) 33 | }, 34 | onLoad() { 35 | var that = this 36 | let emotions = [] 37 | for (let i = 0; i < emojis.length; i++) { 38 | emotions.push({ 39 | src: '/emoji/' + util.getEmojiEn(emojis[i]) + '.png', 40 | id: i, 41 | name: emojis[i] 42 | }) 43 | } 44 | this.setData({ 45 | emotions: emotions 46 | }) 47 | wx.getSystemInfo({ 48 | success: (res) => { 49 | this.setData({ 50 | windowHeight: res.windowHeight, 51 | pxToRpx: 750 / res.screenWidth, 52 | scrollHeight: (res.windowHeight - 50) * 750 / res.screenWidth 53 | }) 54 | } 55 | }) 56 | }, 57 | onShareAppMessage: function () { 58 | return { 59 | title: '伙伴小Q', 60 | path: '/pages/index/index' 61 | } 62 | }, 63 | emotionBtn() { 64 | if (this.data.emotionBox) { 65 | this.setData({ 66 | emotionBox: false, 67 | scrollHeight: (this.data.windowHeight - 50) * this.data.pxToRpx 68 | }) 69 | } else { 70 | this.setData({ 71 | emotionBox: true, 72 | scrollHeight: (this.data.windowHeight - 285) * this.data.pxToRpx 73 | }) 74 | if (this.data.isSpeech) { 75 | this.setData({ 76 | isSpeech: false, 77 | changeImageUrl: '/images/voice.png' 78 | }); 79 | } 80 | } 81 | }, changeType: function () { 82 | if (this.data.isSpeech) { 83 | this.setData({ 84 | isSpeech: false, 85 | changeImageUrl: '/images/voice.png' 86 | }); 87 | } else { 88 | this.setData({ 89 | isSpeech: true, 90 | changeImageUrl: '/images/keyinput.png', 91 | emotionBox: false, 92 | scrollHeight: (this.data.windowHeight - 50) * this.data.pxToRpx 93 | }); 94 | } 95 | }, 96 | send: function () { 97 | var that = this; 98 | let msg = this.data.msg 99 | let contents = util.getContents(msg) 100 | let id = 'id_' + Date.parse(new Date()) / 1000; 101 | let data = { id: id, contents: contents, me: true, avatar: wx.getStorageSync('userInfo').avatarUrl, speech: false } 102 | let messages = this.data.messages 103 | messages.push(data) 104 | this.setData({ 105 | messages: messages, 106 | msg: '' 107 | }) 108 | this.setData({ 109 | toView: id 110 | }) 111 | wx.request({ 112 | url: host + '/wx/robot', 113 | method: 'POST', 114 | data: { 'info': msg, 'userid': wx.getStorageSync('openid'), 'username': wx.getStorageSync('userInfo').nickName }, 115 | header: { 116 | "content-type": "application/json" 117 | }, 118 | success: function (res) { 119 | if (res.statusCode == 200) { 120 | let answer = res.data.text; 121 | let contents = util.getContents(answer, res.data.url) 122 | let id = 'id_' + Date.parse(new Date()) / 1000; 123 | let data = { id: id, contents: contents, me: false, avatar: '/images/robot.jpg', speech: false } 124 | let messages = that.data.messages 125 | messages.push(data) 126 | console.log(messages) 127 | that.setData({ 128 | messages: messages 129 | }) 130 | that.setData({ 131 | toView: id 132 | }) 133 | } 134 | }, 135 | fail: function (res) { 136 | console.log(res) 137 | } 138 | }) 139 | }, 140 | startRecord: function () { 141 | var that = this; 142 | this.setData({ 143 | speechText: '松开 发送' 144 | }) 145 | var seconds = 0; 146 | var interval = setInterval(function () { 147 | seconds++ 148 | }, 1000); 149 | wx.startRecord({ 150 | success: function (res) { 151 | clearInterval(interval); 152 | var tempFilePath = res.tempFilePath 153 | seconds = seconds == 0 ? 1 : seconds; 154 | let id = 'id_' + Date.parse(new Date()) / 1000; 155 | let data = { id: id, me: true, avatar: wx.getStorageSync('userInfo').avatarUrl, speech: true, seconds: seconds, filePath: tempFilePath } 156 | let messages = that.data.messages 157 | messages.push(data) 158 | that.setData({ 159 | messages: messages 160 | }); 161 | that.setData({ 162 | toView: id 163 | }) 164 | let nickName = wx.getStorageSync('userInfo').nickName; 165 | if (!nickName) nickName = 'null'; 166 | wx.uploadFile({ 167 | url: host + '/wx/uploadSilk', 168 | filePath: tempFilePath, 169 | name: 'file', 170 | formData: { 171 | 'userid': wx.getStorageSync('openid'), 172 | 'username': wx.getStorageSync('userInfo').nickName 173 | }, 174 | success: function (res) { 175 | let resData = JSON.parse(res.data); 176 | if (resData.code == 102) { 177 | let answer = resData.text; 178 | let contents = util.getContents(answer) 179 | let id = 'id_' + Date.parse(new Date()) / 1000; 180 | let data = { id: id, contents: contents, me: false, avatar: '/images/robot.jpg', speech: false } 181 | let messages = that.data.messages 182 | messages.push(data) 183 | that.setData({ 184 | messages: messages 185 | }) 186 | that.setData({ 187 | toView: id 188 | }) 189 | } else if (resData.code == 101) { 190 | var isFirst = true; 191 | wx.playBackgroundAudio({ 192 | dataUrl: host + '/static/' + resData.text 193 | }); 194 | wx.onBackgroundAudioPlay(function () { 195 | wx.getBackgroundAudioPlayerState({ 196 | success: function (res) { 197 | if (!isFirst) { 198 | return; 199 | } 200 | isFirst = false; 201 | let duration = res.duration; 202 | wx.stopBackgroundAudio(); 203 | let id = 'id_' + Date.parse(new Date()) / 1000; 204 | let data = { id: id, me: false, avatar: '/images/robot.jpg', speech: true, seconds: duration == 0 ? 1 : duration, filePath: host + '/static/' + resData.text } 205 | let messages = that.data.messages 206 | messages.push(data) 207 | that.setData({ 208 | messages: messages 209 | }); 210 | that.setData({ 211 | toView: id 212 | }) 213 | } 214 | }) 215 | }); 216 | } 217 | }, 218 | fail: function (err) { 219 | console.log(err) 220 | } 221 | }) 222 | }, 223 | fail: function (err) { 224 | console.log(err) 225 | } 226 | }) 227 | }, 228 | stopRecord: function () { 229 | this.setData({ 230 | speechText: '按住 说话' 231 | }) 232 | wx.stopRecord(); 233 | }, 234 | playSpeech: function (event) { 235 | var that = this; 236 | var filePath = event.currentTarget.dataset.filepath; 237 | that.setData({ 238 | playingSpeech: filePath 239 | }); 240 | var num = 1; 241 | var interval = setInterval(function () { 242 | that.setData({ 243 | speechIcon: '/images/speech' + num % 3 + '.png' 244 | }); 245 | num++; 246 | }, 500); 247 | wx.playVoice({ 248 | filePath: filePath, 249 | complete: function () { 250 | clearInterval(interval); 251 | that.setData({ 252 | speechIcon: '/images/speech0.png', 253 | playingSpeech: '' 254 | }); 255 | } 256 | }) 257 | }, 258 | playRobotSpeech: function (event) { 259 | var that = this; 260 | var filePath = event.currentTarget.dataset.filepath; 261 | that.setData({ 262 | playingSpeech: filePath 263 | }); 264 | var num = 1; 265 | var interval = setInterval(function () { 266 | that.setData({ 267 | speechIcon: '/images/speech' + num % 3 + '.png' 268 | }); 269 | num++; 270 | }, 500); 271 | wx.playBackgroundAudio({ 272 | dataUrl: filePath 273 | }); 274 | wx.onBackgroundAudioStop(function () { 275 | clearInterval(interval); 276 | that.setData({ 277 | speechIcon: '/images/speech0.png', 278 | playingSpeech: '' 279 | }); 280 | }) 281 | } 282 | }) 283 | 284 | -------------------------------------------------------------------------------- /pages/index/index.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /pages/index/index.wxml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | {{i.seconds}}″ 15 | 16 | 17 | 18 |