You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.2 KiB
JavaScript
123 lines
3.2 KiB
JavaScript
import { create } from "zustand";
|
|
import { VonageClient, ClientConfig, ConfigRegion, LoggingLevel } from '@vonage/client-sdk'
|
|
import { fetchJSON } from "@/utils/request";
|
|
import { prepareUrl, isNotEmpty, } from "@/utils/commons";
|
|
import { VONAGE_URL, DATETIME_FORMAT } from "@/config";
|
|
import dayjs from "dayjs";
|
|
|
|
const callCenterStore = create((set, get) => ({
|
|
client: new VonageClient({
|
|
region: ConfigRegion.AP,
|
|
// apiUrl: "https://api-ap-3.vonage.com", websocketUrl: "wss://ws-ap-3.vonage.com",
|
|
// loggingLevel: LoggingLevel.Debug,
|
|
}),
|
|
call_id: 0,
|
|
loading: false,
|
|
logs: "",
|
|
|
|
//初始化 Vonage
|
|
init_vonage: user_id => {
|
|
const { client, log } = get();
|
|
set({ loading: true });
|
|
// const client = new VonageClient({
|
|
// loggingLevel: LoggingLevel.DEBUG,
|
|
// region: ConfigRegion.AP,
|
|
// })
|
|
// update some options after initialization.
|
|
// const vonageConfig = new ClientConfig(ConfigRegion.AP);
|
|
// client.setConfig(vonageConfig);
|
|
|
|
const fetchUrl = prepareUrl(VONAGE_URL + "/jwt")
|
|
.append("user_id", user_id)
|
|
.build();
|
|
return fetchJSON(fetchUrl).then(res => {
|
|
const json = res.result;
|
|
if (json?.status === 200) {
|
|
let jwt = json.token;
|
|
|
|
client
|
|
.createSession(jwt)
|
|
.then(sessionId => {
|
|
log("Id of created session: ", sessionId);
|
|
})
|
|
.catch(error => {
|
|
log("Error creating session: ", error);
|
|
});
|
|
// debug:
|
|
// client
|
|
// .getUser('me')
|
|
// .then((user) => log('getUser --me', user))
|
|
// .catch((error) => log);
|
|
|
|
client.on("sessionError", reason => {
|
|
// After creating a session
|
|
log("Session error reason: ", reason);
|
|
});
|
|
|
|
client.on("legStatusUpdate", (callId, legId, status) => {
|
|
// After creating a session
|
|
log({ callId, legId, status });
|
|
});
|
|
|
|
client.on("callInvite", (callId, from, channelType) => {
|
|
log({ callId, from, channelType }); // Answer / Reject Call
|
|
});
|
|
|
|
client.on("callHangup", (callId, callQuality, reason) => {
|
|
log(`Call ${callId} has hung up, callQuality:${callQuality}, reason:${reason}`);
|
|
set({ call_id: 0 });
|
|
});
|
|
|
|
client.on("sessionError", error => {
|
|
log({ error });
|
|
});
|
|
} else {
|
|
throw new Error("请求jwt失败");
|
|
}
|
|
set({ loading: false, client });
|
|
});
|
|
},
|
|
|
|
log: (...message) => {
|
|
const { logs } = get();
|
|
console.log(message);
|
|
set({ logs: [...logs, dayjs().format(DATETIME_FORMAT) + " : " + JSON.stringify(message)] });
|
|
},
|
|
|
|
// 创建一个语音通话
|
|
make_call: phone_number => {
|
|
const { client, log } = get();
|
|
if (!isNotEmpty(phone_number)) {
|
|
log("请输入电话号码");
|
|
return;
|
|
}
|
|
log("开始拨号:" + phone_number);
|
|
if (client) {
|
|
set({ loading: true });
|
|
client
|
|
.serverCall({ to: phone_number, })
|
|
.then(callId => {
|
|
log("Id of created call: ", callId);
|
|
set({ call_id: callId });
|
|
set({ loading: false });
|
|
})
|
|
.catch(error => {
|
|
log("Error making call: ", error);
|
|
set({ loading: false });
|
|
});
|
|
}
|
|
},
|
|
|
|
// 挂断语音通话
|
|
hang_up: () => {
|
|
const { client, call_id, log } = get();
|
|
log("挂断电话");
|
|
if (call_id) {
|
|
client.hangup(call_id);
|
|
set({ call_id: 0 });
|
|
}
|
|
},
|
|
}));
|
|
|
|
export default callCenterStore;
|