zhoudw
2022-01-10 6e0dc60c729fa23cb53b8bdbc20e33be6cbebfc2
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
 
<template>
  <div class="iframe">
      <iframe
        ref='iframe' allowfullscreen
        :src="live_url"
        frameborder="0"
        width='100%'
        height='100%'>
      </iframe>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
export default {
  data(){
    return{
      live_url:"",
      scenes_id:'',
      scenes_name:'',
      client_id:'',
      jsload:false,//js是否加载成功
      signature:'',//微信签名
      timeStamp:'',//生成签名时间戳
      nonceStr:'',//随机字符串
      appId:'',//企业微信ID
      access_token:'',
      iframeWin:'',// iframe对象
    };
  },
  computed: {
    ...mapState({ token: "token" })
  },
  mounted(){
    this.iframeWin = this.$refs.iframe.contentWindow;
  },
  created () {
     let item = JSON.parse(localStorage.getItem("item"));
     this.scenes_id = item.scenes_id;
     this.scenes_name = item.scenes_name;
     this.client_id = 'wechat_enterprise';
     let url = process.env.INTERFACE_SERVER_PATH+process.env.ETS_APP_NEWRECORD_URL;
     this.live_url = url + "?scenes_id="+this.scenes_id+"&scenes_name="+this.scenes_name+"&client_id="+this.client_id+"&sid="+this.token;
     console.log(this.live_url);
     this.loadWxjs();
  },
  methods:{
    ...mapActions({
        clientconfig: "clientconfig", // 鉴权
    }),
    getwsConfig(){
      let _this = this;
      let url = location.href;
      _this.clientconfig({ query: {
          client_id:_this.client_id,
          url:url
      } }).then((res) => {
          console.log(res);
          if(res.data.access_token){
              _this.access_token = res.data.access_token;
              _this.corpId = res.data.corpId;
              _this.nonceStr = res.data.nonceStr;
              _this.signature = res.data.signature;
              _this.timeStamp = res.data.timeStamp;
              _this.postMessageEvent();
          }else{
              _this.$toast.fail('企业微信鉴权失败');
          }
      }).catch(() => {
          _this.$toast.fail('企业微信鉴权失败');
      });;
    },
     postMessageEvent(){
       let _this = this;
       window.addEventListener("message", function(e){
            var data = e.data;
            console.log(data,'收到消息');
            switch (data.action) {
                case 'init': // 初始化
                    wx.config({
                        beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                        appId: _this.corpId, // 必填,企业微信的corpID
                        timestamp: _this.timeStamp, // 必填,生成签名的时间戳
                        nonceStr: _this.nonceStr, // 必填,生成签名的随机串
                        signature: _this.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
                        jsApiList: [
                                'startRecord',
                                'stopRecord',
                                'uploadVoice'
                        ], //必填,传入需要使用的接口名称
                        success: function (res) {
                          sendMsg({
                            action:'init',
                            state:'ok',
                            data:{}
                          });
                        }
                    });
                    wx.error(function(res){
                        sendMsg({
                          action:'init',
                          state:'fail',
                          data:{}
                        });
                    })
                  break;
                case 'start': // 开始录音
                  wx.startRecord({
                    success: function (res) {
                      sendMsg({
                        action:'start',
                        state:'ok',
                        data:{}
                      });
                    }
                  });
                  wx.error(function(res){
                      sendMsg({
                        action:'start',
                        state:'fail',
                        data:{}
                      });
                  })
                  break;
                case 'stop':
                  wx.stopRecord({
                    success: function (res) {
                      var localId = res.localId;
                      wx.uploadVoice({
                          localId: localId, // 需要识别的音频的本地Id,由录音相关接口获得,音频时长不能超过60秒
                          isShowProgressTips: 0, // 默认为1,显示进度提示
                          success: function (ures) {
                            let serverId = ures.serverId;
                            sendMsg({
                              action:'stop',
                              state:'ok',
                              data:{
                                access_token:_this.access_token,
                                serverId:serverId
                              }
                            });
                          }
                      });
                    }
                  });
                  wx.error(function(res){
                      sendMsg({
                        action:'start',
                        state:'fail',
                        data:{}
                      });
                  })
                  break;
            }
       })
     },
     sendMsg(msg){
       this.iframeWin.postMessage(msg,'*');
     },
     loadWxjs(){
        let _this = this;
        let wx_js = process.env.WX_API_URL;
        const isInclude = $("script[src='"+wx_js+"']").length; //引用次数
        if(isInclude == 0){
            const script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = wx_js;
            document.body.appendChild(script);
            script.onload=function(){
              _this.jsload = true;
            }
        }else{
          _this.jsload = true;
        }
        this.getwsConfig();
     }
  }
}
</script>
<style lang="less" scoped>
.iframe{
  position: relative;
  width: 100%;
  height: 100%;
}
</style>