voice.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { ref } from 'vue';
  2. // import { Timer } from '@/utils/timer';
  3. import { ElMessage } from 'element-plus'
  4. import usePermissionStore from './permission';
  5. const useVoiceStore = defineStore('voice', () => {
  6. const usePermission = usePermissionStore();
  7. let HS_CTI = null;
  8. // 当前系统状态 - 闲 or 忙
  9. const systemState = ref(false);
  10. const telephoneNumber = ref(null);
  11. // 是否是拨打电话 true: 外呼 false: 来电
  12. const isMakingCall = ref(false);
  13. // 刚来电
  14. const callAnswered = ref(false);
  15. // 接听中
  16. const callDialing = ref(false);
  17. // 横条来电显示
  18. const noiceBarVisibleState = ref(false);
  19. // 盒子来电显示
  20. const noiceBoxVisibleState = ref(false);
  21. // 通话时长
  22. const callTime = ref('00:00:00');
  23. // const timer = new Timer(callTime);
  24. // 开始时间
  25. const startTime = '';
  26. const endTime = '';
  27. const sessionId = ref(null);
  28. // 是否有拨打电话权限
  29. const isAuthPane = computed(() => usePermission.routes.findIndex(({ name }) => name === 'Console') !== -1);
  30. // 重置
  31. const reset = () => {
  32. noiceBarVisibleState.value = false;
  33. noiceBoxVisibleState.value = false;
  34. callAnswered.value = false;
  35. callDialing.value = false;
  36. // timer.resetTimer();
  37. }
  38. // 拨打电话
  39. const onMakingCall = async (phoneNum) => {
  40. if ( callAnswered.value ) {
  41. return ElMessage({
  42. message: '当前状态不允许操作,无法呼出',
  43. type: 'warning',
  44. })
  45. };
  46. if ( !systemState.value ) {
  47. return ElMessage({
  48. message: '作息状态置忙,无法呼出',
  49. type: 'warning',
  50. })
  51. }
  52. callAnswered.value = true;
  53. const { data } = await makeCall(phoneNum);
  54. sessionId.value = data;
  55. telephoneNumber.value = phoneNum;
  56. isMakingCall.value = true;
  57. // noiceBarVisibleState.value = true;
  58. }
  59. // 接听电话
  60. const onCallAnswered = async () => {
  61. callDialing.value = true;
  62. callAnswered.value = true;
  63. await answer();
  64. // timer.start();
  65. // TODO 这里需要补充其他逻辑
  66. }
  67. // 挂断电话
  68. const onCallDisconnected = async () => {
  69. reset();
  70. // const currentTimer = timer.updateDisplay();
  71. // 挂断
  72. await bye();
  73. ElMessage({
  74. message: '通话已经结束,挂断成功',
  75. type: 'success',
  76. plain: true,
  77. })
  78. }
  79. // 置忙
  80. const setBusy = () => {
  81. systemState.value = false;
  82. HS_CTI.setBusy().then(res => { console.log(res) });
  83. };
  84. // 置闲
  85. const setIdle = () => {
  86. systemState.value = true;
  87. HS_CTI.setIdle().then(res => { console.log(res) });
  88. }
  89. // 获取坐席状态
  90. const getAgentStatus = () => {
  91. HS_CTI.getAgentStatus().then(res => { console.log(res) })
  92. }
  93. // 主动外呼
  94. const makeCall = called => HS_CTI.makeCall({ called, caller: "待定" })
  95. // 接听电话
  96. const answer = () => {
  97. HS_CTI.answer().then(res => { console.log(res) })
  98. }
  99. // 挂断电话
  100. const bye = async () => {
  101. return await HS_CTI.serverBye().then(res => { console.log(res) })
  102. }
  103. // 卸载实例
  104. const unInit = async () => {
  105. return await HS_CTI.unInit()
  106. }
  107. // 下面开始事件监听
  108. const listenScoketEvent = (CTIEvent) => {
  109. HS_CTI.on(CTIEvent.OnAgentWorkReport, ({ workStatus, description, callId, phone }) => {
  110. console.log( "-----------------------" );
  111. console.log( "-----------------------" );
  112. console.log( "workStatus", workStatus );
  113. console.log( "phone", phone );
  114. console.log( "-----------------------" );
  115. console.log( "-----------------------" );
  116. // 销毁实例调用签出接口成功后 - 坐席签出
  117. if ( workStatus === -1 ) {
  118. ElMessage({
  119. message: '坐席签出成功',
  120. type: 'success',
  121. plain: true,
  122. })
  123. }
  124. // 登录CTI 成功
  125. if ( workStatus === 0 ) {
  126. }
  127. // 登录CTI 成功
  128. if ( workStatus === 2 ) {
  129. }
  130. // 调用置闲接口成功后
  131. if ( workStatus === 2 ) {
  132. }
  133. // 调用置忙接口成功后
  134. if ( workStatus === 3 ) {
  135. }
  136. // 座席振铃
  137. if ( workStatus === 5 ) {
  138. sessionId.value = callId;
  139. telephoneNumber.value = phone;
  140. isMakingCall.value = false;
  141. noiceBarVisibleState.value = true;
  142. // ElMessage({
  143. // message: '来电话了',
  144. // type: 'warning',
  145. // plain: true,
  146. // })
  147. }
  148. // 挂断 任意一方挂断
  149. if ( workStatus === 7 ) {
  150. reset();
  151. }
  152. // 座席接通呼入电话
  153. if ( workStatus === 10 ) {
  154. // timer.start();
  155. }
  156. // 挂断 任意一方挂断
  157. // if ( workStatus === 7 ) {
  158. // reset();
  159. // }
  160. })
  161. }
  162. // 初始化 通话实例
  163. const HS_CTI_INSTANCE = (agent_id) => {
  164. const { Scene, getInstance, LoggerLevels , CTIEvent} = window.HS_CTI;
  165. HS_CTI = getInstance({
  166. // 业务返回的坐席outId
  167. agent_id,
  168. // 根据城市可能不一样,
  169. saas_id:'mdj',
  170. // 业务场景详见 Scene 枚举,
  171. scene: Scene.Manual,
  172. // SDK 日志等级
  173. loggerLevel: LoggerLevels.debug,
  174. // 环境变量
  175. env: 'development'
  176. })
  177. HS_CTI.init();
  178. listenScoketEvent(CTIEvent);
  179. }
  180. return {
  181. sessionId,
  182. isAuthPane,
  183. callTime,
  184. isMakingCall,
  185. onMakingCall,
  186. onCallDisconnected,
  187. onCallAnswered,
  188. callAnswered,
  189. callDialing,
  190. noiceBarVisibleState,
  191. noiceBoxVisibleState,
  192. // 电话号码
  193. telephoneNumber,
  194. // 系统状态
  195. systemState,
  196. // 通话相关
  197. HS_CTI_INSTANCE,
  198. setBusy,
  199. setIdle,
  200. unInit
  201. }
  202. })
  203. export default useVoiceStore;