File size: 2,818 Bytes
b3b0b53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const plugin = requirePlugin('WechatSI');
const manager = plugin.getRecordRecognitionManager();

Page({
    data: {
        languages: {
            'zh': { name: '中文', flag: 'cn', code: 'zh_CN' },
            'en': { name: 'English', flag: 'us', code: 'en_US' }
        },
        langCodes: ['zh', 'en'], // Only CN and EN for this test version
        sourceLang: 'zh',
        targetLang: 'en',
        transcript: '',
        outputText: '',
        isRecording: false,
        sourceLanguages: [],
        targetLanguages: []
    },

    onLoad: function () {
        this.initializeLanguages();
        this.initRecorderManager();
    },

    // --- Language Selection Logic (Simplified for CN/EN only) ---
    initializeLanguages: function () {
        const { langCodes, languages, sourceLang, targetLang } = this.data;
        this.setData({
            sourceLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === sourceLang })),
            targetLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === targetLang }))
        });
    },
    selectSourceLanguage: function(e) { this.setData({ sourceLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); },
    selectTargetLanguage: function(e) { this.setData({ targetLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); },
    swapLanguages: function() {
        this.setData({
            sourceLang: this.data.targetLang,
            targetLang: this.data.sourceLang,
            transcript: this.data.outputText,
            outputText: this.data.transcript
        }, this.initializeLanguages);
    },

    // --- Recorder Manager Initialization (Pure Plugin Mode) ---
    initRecorderManager: function() {
        manager.onStart = () => {
            this.setData({ transcript: '正在聆听...', outputText: '' });
        };

        // Only onStop is used for final results
        manager.onStop = (res) => {
            this.setData({ isRecording: false });
            if (res.result) {
                this.setData({ transcript: res.result, outputText: res.translateResult || '' });
            } else {
                this.setData({ transcript: '识别结果为空', outputText: '' });
            }
        };

        manager.onError = (res) => {
            this.setData({ isRecording: false, transcript: '识别失败', outputText: res.msg });
        };
    },

    // --- Recording Start/Stop (Pure Plugin Mode) ---
    startRecording: function () {
        const { sourceLang, targetLang } = this.data;
        this.setData({ isRecording: true });
        manager.start({ lang: this.data.languages[sourceLang].code, translate: true, lto: this.data.languages[targetLang].code });
    },

    stopRecording: function () {
        manager.stop();
    }
});