call.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {
  2. startPhoneListener,
  3. stopPhoneListener,
  4. checkIsAutoRecord,
  5. toCallAutoRecorderPage,
  6. navigateToCallRecordingSettings,
  7. jumpToPermissionPage,
  8. makePhoneCall,
  9. allRecorderFilesAction
  10. } from '@/uni_modules/yao-lister';
  11. import permision from "@/js_sdk/wa-permission/permission.js";
  12. export default {
  13. namespaced: true,
  14. state: {
  15. filelist: [],
  16. isAutoRecord: false, // 通话自动录音状态
  17. hasStoragePermission: false, // 文件访问权限状态
  18. hasReadPhoneStatePermission: false, // 读取手机状态权限
  19. hasCallPhonePermission: false, // 拨打电话权限
  20. hasReadCallLogPermission: false, // 读取通话记录权限
  21. hasReadPhoneNumbersPermission: false, // 读取电话号码权限
  22. form: {
  23. clueId: undefined,
  24. fileUrl : undefined,
  25. fileName : undefined,
  26. type : '3',
  27. list : [],
  28. caller : '',
  29. callee : ''
  30. },
  31. },
  32. mutations: {
  33. SET_FILE_LIST: (state, data) => {
  34. state.filelist = data.data
  35. },
  36. SET_AUTO_RECORD: (state, value) => {
  37. state.isAutoRecord = value;
  38. },
  39. SET_STORAGE_PERMISSION: (state, value) => {
  40. state.hasStoragePermission = value;
  41. },
  42. SET_READ_PHONE_STATE_PERMISSION: (state, value) => {
  43. state.hasReadPhoneStatePermission = value;
  44. },
  45. SET_CALL_PHONE_PERMISSION: (state, value) => {
  46. state.hasCallPhonePermission = value;
  47. },
  48. SET_READ_CALL_LOG_PERMISSION: (state, value) => {
  49. state.hasReadCallLogPermission = value;
  50. },
  51. SET_READ_PHONE_NUMBERS_PERMISSION: (state, value) => {
  52. state.hasReadPhoneNumbersPermission = value;
  53. },
  54. },
  55. actions: {
  56. getFileList({dispatch,state, commit}) {
  57. dispatch("checkStoragePermission").then(()=>{
  58. if(state.hasStoragePermission){
  59. allRecorderFilesAction(res => {
  60. if(res && res.length > 0) {
  61. // 将文件路径数组转换为包含filePath和fileName的对象数组
  62. const fileListPromise = res.map(filePath => {
  63. return new Promise((resolve) => {
  64. // 从路径中提取文件名
  65. const fileName = filePath.split('/').pop();
  66. // 获取文件信息,包括创建时间
  67. plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
  68. entry.getMetadata((metadata) => {
  69. resolve({
  70. filePath: filePath,
  71. fileName: fileName,
  72. createTime: metadata.modificationTime ? metadata.modificationTime : 0, // 文件创建时间
  73. });
  74. }, (error) => {
  75. // 如果获取metadata失败,返回基本信息
  76. resolve({
  77. filePath: filePath,
  78. fileName: fileName,
  79. createTime: 0, // 设为0,排序时会在最前面
  80. });
  81. });
  82. }, (error) => {
  83. // 如果解析文件路径失败,返回基本信息
  84. resolve({
  85. filePath: filePath,
  86. fileName: fileName,
  87. createTime: 0,
  88. size: 0
  89. });
  90. });
  91. });
  92. });
  93. // 等待所有文件信息获取完成后进行排序
  94. Promise.all(fileListPromise).then((fileList) => {
  95. // 按创建时间排序,最新的文件排在前面(降序)
  96. fileList.sort((a, b) => {
  97. return b.createTime - a.createTime;
  98. });
  99. // 提交文件列表到state
  100. commit('SET_FILE_LIST', { data: fileList });
  101. }).catch((error) => {
  102. console.error('获取文件列表失败:', error);
  103. // 即使排序失败,也尝试提交原始列表
  104. const fallbackFileList = res.map(filePath => {
  105. const fileName = filePath.split('/').pop();
  106. return {
  107. filePath: filePath,
  108. fileName: fileName
  109. };
  110. });
  111. commit('SET_FILE_LIST', { data: fallbackFileList });
  112. });
  113. }
  114. })
  115. }else{
  116. dispatch("requestStoragePermission");
  117. }
  118. })
  119. },
  120. // 检查所有权限状态
  121. checkAllPermissions({ commit }) {
  122. return new Promise((resolve, reject) => {
  123. try {
  124. // 检查通话自动录音状态
  125. const autoRecordStatus = checkIsAutoRecord();
  126. commit('SET_AUTO_RECORD', autoRecordStatus);
  127. // 检查文件访问权限
  128. this.dispatch('call/checkStoragePermission').then(storagePermission => {
  129. commit('SET_STORAGE_PERMISSION', storagePermission);
  130. // 检查其他权限
  131. const permissions = [
  132. {
  133. id: 'android.permission.READ_PHONE_STATE',
  134. mutation: 'SET_READ_PHONE_STATE_PERMISSION'
  135. },
  136. {
  137. id: 'android.permission.CALL_PHONE',
  138. mutation: 'SET_CALL_PHONE_PERMISSION'
  139. },
  140. {
  141. id: 'android.permission.READ_CALL_LOG',
  142. mutation: 'SET_READ_CALL_LOG_PERMISSION'
  143. },
  144. {
  145. id: 'android.permission.READ_PHONE_NUMBERS',
  146. mutation: 'SET_READ_PHONE_NUMBERS_PERMISSION'
  147. }
  148. ];
  149. const permissionPromises = permissions.map(perm => {
  150. return permision.requestAndroidPermission(perm.id).then(result => {
  151. commit(perm.mutation, result === 1);
  152. return { id: perm.id, granted: result === 1 };
  153. });
  154. });
  155. Promise.all(permissionPromises).then(results => {
  156. resolve({
  157. autoRecordStatus,
  158. storagePermission,
  159. permissions: results
  160. });
  161. });
  162. });
  163. } catch (error) {
  164. reject(error);
  165. }
  166. });
  167. },
  168. // 检查文件访问权限
  169. checkStoragePermission({ commit }) {
  170. return new Promise((resolve) => {
  171. let hasPermission = false;
  172. try {
  173. // 获取Android版本号
  174. let androidVersion = 0;
  175. uni.getSystemInfo({
  176. success: (res) => {
  177. if (res.system && res.system.includes('Android')) {
  178. const versionMatch = res.system.match(/Android (\d+\.?\d*)/);
  179. if (versionMatch && versionMatch[1]) {
  180. androidVersion = parseFloat(versionMatch[1]);
  181. }
  182. }
  183. if (androidVersion >= 11) {
  184. // Android 11及以上版本,使用MANAGE_EXTERNAL_STORAGE权限
  185. const Environment = plus.android.importClass('android.os.Environment');
  186. hasPermission = Environment.isExternalStorageManager();
  187. commit('SET_STORAGE_PERMISSION', hasPermission);
  188. resolve(hasPermission);
  189. } else {
  190. // Android 11以下版本,使用READ_EXTERNAL_STORAGE权限
  191. permision.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE')
  192. .then(result => {
  193. hasPermission = result === 1;
  194. commit('SET_STORAGE_PERMISSION', hasPermission);
  195. resolve(hasPermission);
  196. });
  197. }
  198. }
  199. });
  200. } catch (e) {
  201. hasPermission = false;
  202. commit('SET_STORAGE_PERMISSION', hasPermission);
  203. resolve(hasPermission);
  204. }
  205. });
  206. },
  207. // 请求文件访问权限
  208. requestStoragePermission() {
  209. jumpToPermissionPage();
  210. },
  211. // 跳转到通话录音设置页面
  212. toCallRecorderSettings() {
  213. toCallAutoRecorderPage();
  214. },
  215. // 跳转到系统通话录音界面
  216. toSystemRecorderSettings() {
  217. navigateToCallRecordingSettings();
  218. },
  219. // 打开系统设置
  220. handleOpenSet() {
  221. permision.gotoAppPermissionSetting('android.permission.RECORD_AUDIO');
  222. }
  223. },
  224. }