Browse Source

Merge branch 'tt' of http://code.qutaovip.com/lihai/taro_chat_ai into weapp

黎海 2 years ago
parent
commit
182a4dcb78
3 changed files with 56 additions and 165 deletions
  1. 3 3
      project.config.json
  2. 50 140
      src/pages/chat/index.jsx
  3. 3 22
      src/pages/chat/index.less

+ 3 - 3
project.config.json

@@ -1,8 +1,8 @@
1 1
 {
2 2
   "miniprogramRoot": "dist/weapp/",
3
-  "projectname": "畅享短剧",
4
-  "description": "畅享短剧 ",
5
-  "appid": "wxe7aa01162d73df1f",
3
+  "projectname": "AI智能助手",
4
+  "description": "AI智能助手 ",
5
+  "appid": "tt6e291894f116667e01",
6 6
   "setting": {
7 7
     "urlCheck": true,
8 8
     "es6": false,

+ 50 - 140
src/pages/chat/index.jsx

@@ -6,8 +6,6 @@ import './index.less'
6 6
 import tool from '../../common/tool'
7 7
 
8 8
 export default class Albums_play extends Component {
9
-  plugin = requirePlugin("WechatSI");
10
-  manager = this.plugin.getRecordRecognitionManager();
11 9
   options = {
12 10
     sampleRate: 44100,
13 11
     numberOfChannels: 1,
@@ -35,9 +33,8 @@ export default class Albums_play extends Component {
35 33
   }
36 34
 
37 35
   componentDidShow () {
38
-    console.log('页面启动',this.state.userId);
36
+    console.log('页面启动', this.state.userId);
39 37
     this.userinfo()
40
-    this.initRecord()
41 38
     // this.setState({
42 39
     //   userId: this.generateUserID()
43 40
     // })
@@ -65,29 +62,29 @@ export default class Albums_play extends Component {
65 62
   // })
66 63
 
67 64
   componentDidHide () { }
68
-  
65
+
69 66
   // 获取用户信息并且登录,获取openid
70 67
   userinfo () {
71 68
     var that = this;
72 69
     that.setState({
73 70
       login: false
74 71
     })
75
-    Taro.getUserInfo({
76
-      success: function (res) {
77
-        console.log(res, 'userinfo');
78
-        var userInfo = res.userInfo
79
-        var nickName = userInfo.nickName
80
-        var avatarUrl = userInfo.avatarUrl
81
-        var gender = userInfo.gender //性别 0:未知、1:男、2:女
82
-        var province = userInfo.province
83
-        var city = userInfo.city
84
-        var country = userInfo.country
85
-        let data = {
86
-          "user_name": nickName,
87
-          "user_img": avatarUrl
88
-        };
89
-      }
90
-    })
72
+    // Taro.getUserInfo({
73
+    //   success: function (res) {
74
+    //     console.log(res, 'userinfo');
75
+    //     var userInfo = res.userInfo
76
+    //     var nickName = userInfo.nickName
77
+    //     var avatarUrl = userInfo.avatarUrl
78
+    //     var gender = userInfo.gender //性别 0:未知、1:男、2:女
79
+    //     var province = userInfo.province
80
+    //     var city = userInfo.city
81
+    //     var country = userInfo.country
82
+    //     let data = {
83
+    //       "user_name": nickName,
84
+    //       "user_img": avatarUrl
85
+    //     };
86
+    //   }
87
+    // })
91 88
   }
92 89
   // 提交文字
93 90
   submitTo () {
@@ -111,7 +108,10 @@ export default class Albums_play extends Component {
111 108
     })
112 109
     let params = {
113 110
       user_id: this.state.userId,
114
-      text: inputHtml
111
+      text: inputHtml,
112
+    }
113
+    if (process.env.TARO_ENV == 'tt') {
114
+      params['app_id'] = 'tt6e291894f116667e01'
115 115
     }
116 116
     api.getAsk(params).then(res => {
117 117
       if (res.code == 200) {
@@ -138,19 +138,19 @@ export default class Albums_play extends Component {
138 138
         })
139 139
       }
140 140
     })
141
-    .catch(res=>{
142
-      console.log("请求超时");
143
-      this.setState({
144
-        sendDisabled: true,//是否禁用发送按钮
145
-        value: '',
146
-        height: 63,
147
-        userId: 0,
148
-        isAnswer: true,//机器人是否已经回答
149
-        isRecognize: false,
150
-        showResult: false,
151
-        isSpeechSounds: false,//是否是语音写入
141
+      .catch(res => {
142
+        console.log("请求超时");
143
+        this.setState({
144
+          sendDisabled: true,//是否禁用发送按钮
145
+          value: '',
146
+          height: 63,
147
+          userId: 0,
148
+          isAnswer: true,//机器人是否已经回答
149
+          isRecognize: false,
150
+          showResult: false,
151
+          isSpeechSounds: false,//是否是语音写入
152
+        })
152 153
       })
153
-    })
154 154
   }
155 155
   //文字输入
156 156
   focus (e) {
@@ -178,76 +178,6 @@ export default class Albums_play extends Component {
178 178
       isSpeechSounds: !this.state.isSpeechSounds
179 179
     })
180 180
   }
181
-  // 初始化语音识别
182
-  initRecord () {
183
-    let that = this
184
-    console.log('initRecord');
185
-    this.manager.onRecognize = (res) => {
186
-      console.log("current result", res.result);
187
-    };
188
-    // 识别错误事件
189
-    this.manager.onError = function (res) {
190
-      Taro.hideLoading();
191
-    }
192
-    this.manager.onStop = (res) => {
193
-      Taro.hideLoading();
194
-      // 去除语音识别出的特殊符号
195
-      const text = res.result.replace(
196
-        /[`~%!@#^=''?~《》!@#¥……&——‘”“'?*()(),,。.、<>]/g,
197
-        ""
198
-      );
199
-      console.log(text, 'text');
200
-      if (text) {
201
-        that.sendText(text)
202
-        that.setState({
203
-          value: text
204
-        })
205
-        // 识别结果展示1.5秒后收起                               
206
-        setTimeout(() => {
207
-          that.setState({
208
-            isRecognize: false,
209
-            showResult: false,
210
-          })
211
-
212
-        }, 1500);
213
-      } else {
214
-
215
-        setTimeout(() => {
216
-          that.setState({
217
-            isRecognize: false,
218
-            showResult: false,
219
-          })
220
-        }, 1500);
221
-      }
222
-      that.setState({
223
-        showResult: true
224
-      })
225
-    };
226
-    this.manager.onError = (res) => {
227
-      Taro.hideLoading();
228
-      if (res.msg) {
229
-        Taro.showToast({ title: res.msg, icon: "none" });
230
-      }
231
-    };
232
-  };
233
-  // 开始录音
234
-  startRecognize = () => {
235
-    wx.vibrateShort()
236
-    Taro.showLoading({ title: "录音中..." });
237
-    this.setState({
238
-      isRecognize: true,
239
-      value: '',
240
-    })
241
-    this.manager.start({ duration: 30000, lang: "zh_CN" });
242
-  };
243
-  // 结束录音
244
-  endRecognize = () => {
245
-    console.log("endRecognize");
246
-    Taro.hideLoading();
247
-    Taro.showLoading({ title: "识别中..." });
248
-    this.manager.stop();
249
-
250
-  };
251 181
 
252 182
   render () {
253 183
     return (
@@ -301,42 +231,22 @@ export default class Albums_play extends Component {
301 231
             <View className='in_voice_icon_tips'>机器人思考中...</View>
302 232
           }
303 233
           <View className="sendmessage" style={`bottom:${this.state.input_bottom}px`}>
304
-            <Image className='add_icon' style={!this.state.isAnswer ? 'opacity: .3;' : ''} onClick={e => (this.changeSpeak(e))} id='1' src='https://we-spa.oss-cn-shenzhen.aliyuncs.com/total_picture/1677739244529.png'></Image>
305
-            {
306
-              this.state.isSpeechSounds ?
307
-                <View
308
-                  className="recognize-btn view-center"
309
-                  onClick={() => {
310
-                    // 按住按钮时间过短只会触发点击事件
311
-                    Taro.hideLoading();
312
-                    Taro.showToast({ title: "请长按说话", icon: "error" });
313
-                  }}
314
-                  onLongPress={e => (this.startRecognize())}
315
-                  onTouchEnd={e => (this.endRecognize())}
316
-                >
317
-                  <View>
318
-                    {this.state.isRecognize
319
-                      ? "松开发送"
320
-                      : "按住说话"}
321
-                  </View>
322
-                </View>
323
-                :
324
-                <Input
325
-                  autoHeight
326
-                  disabled={!this.state.isAnswer}
327
-                  id='advice_textarea'
328
-                  cursorSpacing='5'
329
-                  showConfirmBar=''
330
-                  confirmType='send'
331
-                  onConfirm={e => (this.submitTo(e))}
332
-                  adjustPosition
333
-                  value={this.state.value}
334
-                  onFocus={e => (this.focus(e))}
335
-                  onBlur={e => (this.blur(e))}
336
-                  onKeyboardHeightChange={e => (this.onKeyboardHeightChange(e))}
337
-                  onLineChange={e => (this.onLineChange(e))}
338
-                  placeholder="请输入您要咨询的问题" />
339
-            }
234
+            <Input
235
+              autoHeight
236
+              disabled={!this.state.isAnswer}
237
+              id='advice_textarea'
238
+              className='advice_textarea'
239
+              cursorSpacing='5'
240
+              showConfirmBar=''
241
+              confirmType='send'
242
+              onConfirm={e => (this.submitTo(e))}
243
+              adjustPosition
244
+              value={this.state.value}
245
+              onFocus={e => (this.focus(e))}
246
+              onBlur={e => (this.blur(e))}
247
+              onKeyboardHeightChange={e => (this.onKeyboardHeightChange(e))}
248
+              onLineChange={e => (this.onLineChange(e))}
249
+              placeholder="请输入" />
340 250
 
341 251
             <Button onClick={e => (this.submitTo(e))}
342 252
               disabled={this.state.sendDisabled}

+ 3 - 22
src/pages/chat/index.less

@@ -22,37 +22,21 @@ page {
22 22
   color: #F2F2F2;
23 23
   top: -70px;
24 24
   font-size: 28px;
25
-  padding: 10px 50px;
25
+  padding: 10px 50px 20px 50px;
26 26
   background-color: #948c8c94;
27 27
   border-radius: 5px;
28 28
   // background: url(https://we-spa.oss-cn-shenzhen.aliyuncs.com/total_picture/1677810251249.png);
29 29
 }
30 30
 
31 31
 .sendmessage {
32
-  margin: 20px;
32
+  margin: 20px 20px 40px 20px;
33 33
   z-index: 2;
34 34
   display: flex;
35 35
   align-items: flex-end;
36 36
   flex-direction: row;
37 37
 }
38
-.add_icon {
39
-  width: 60px;
40
-  height: 60px;
41
-  margin: 0 auto;
42
-}
43 38
 
44
-.sendmessage Input {
45
-  flex: 1;
46
-  padding: 10px;
47
-  height: 60px;
48
-  border-radius: 10px;
49
-  // max-height: 150px;
50
-  background-color: #f8f8f8;
51
-  font-size: 32px;
52
-  margin-left: 20px;
53
-  padding-left: 20px;
54
-}
55
-.recognize-btn{
39
+.sendmessage .advice_textarea {
56 40
   flex: 1;
57 41
   padding: 10px;
58 42
   height: 60px;
@@ -62,9 +46,6 @@ page {
62 46
   font-size: 32px;
63 47
   margin-left: 20px;
64 48
   padding-left: 20px;
65
-  display: flex;
66
-  justify-content: center;
67
-  align-items: center;
68 49
 }
69 50
 
70 51
 .sendmessage button {