chatRoom.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /** 消息类别的code */
  2. export const msgTypeEnum = {
  3. //#region 在线互动
  4. 文本消息: 1000,
  5. 图片消息: 1001,
  6. 文本消息: 1000,
  7. 图片消息: 1001,
  8. 文件消息: 1002,
  9. 视频消息: 1003,
  10. 患教资料: 1004,
  11. 问卷量表: 1005,
  12. 患教已读: 1104,
  13. 问卷已填写: 1105,
  14. 申请: 2000,
  15. 申请通过: 2001,
  16. 申请拒绝: 2002,
  17. 申请取消: 2003,
  18. 申请超时: 2004,
  19. 结束服务: 3000,
  20. //#endregion
  21. //#region 随访消息
  22. 随访群发消息: 0,
  23. 随访问卷通知: 1,
  24. 随访文章通知: 2,
  25. 随访复诊提醒: 3,
  26. 随访用药提醒: 4,
  27. 随访换药提醒: 5,
  28. 随访手术提醒: 6,
  29. 随访注意事项: 7,
  30. 随访检查预约提醒: 8,
  31. 随访报告已出提醒: 9,
  32. 随访体征上传提醒: 10,
  33. //#endregion
  34. };
  35. /** 列表显示的消息 */
  36. class ContentExtractFactory {
  37. //#region 在线互动
  38. [msgTypeEnum.文本消息](content) {
  39. const obj = this.parse(content);
  40. return decodeURI(obj.content);
  41. }
  42. [msgTypeEnum.图片消息]() {
  43. return '[图片消息]';
  44. }
  45. [msgTypeEnum.视频消息]() {
  46. return '[视频通话]';
  47. }
  48. [msgTypeEnum.患教资料]() {
  49. return '[健康宣教]';
  50. }
  51. [msgTypeEnum.问卷量表]() {
  52. return '[问卷量表]';
  53. }
  54. [msgTypeEnum.申请]() {
  55. return '[申请交流互动]';
  56. }
  57. [msgTypeEnum.申请通过](content) {
  58. const obj = this.parse(content);
  59. return `[申请通过]:${obj.content}`;
  60. }
  61. [msgTypeEnum.申请拒绝](content) {
  62. const obj = this.parse(content);
  63. return `[申请被拒绝]:您好,您的互动申请暂未通过,${obj.content}`;
  64. }
  65. [msgTypeEnum.申请取消](content) {
  66. const obj = this.parse(content);
  67. return `[申请取消]:${obj.content}`;
  68. }
  69. [msgTypeEnum.申请超时](content) {
  70. const obj = this.parse(content);
  71. return `[申请超时]:${obj.content}`;
  72. }
  73. [msgTypeEnum.结束服务](content) {
  74. const obj = this.parse(content);
  75. return `[系统]:${obj.content}`;
  76. }
  77. [msgTypeEnum.问卷已填写](content) {
  78. const obj = this.parse(content);
  79. return obj.content;
  80. }
  81. [msgTypeEnum.患教已读]() {
  82. return '[健康宣教]';
  83. }
  84. //#endregion
  85. //#region 随访消息
  86. [msgTypeEnum.随访群发消息](content) {
  87. const obj = this.parse(content);
  88. return `[通知]:${obj.txt}`;
  89. }
  90. [msgTypeEnum.随访问卷通知]() {
  91. return `[问卷量表]`;
  92. }
  93. [msgTypeEnum.随访文章通知]() {
  94. return `[健康宣教]`;
  95. }
  96. [msgTypeEnum.随访复诊提醒](content) {
  97. const obj = this.parse(content);
  98. return `[复诊提醒]:${obj.txt}`;
  99. }
  100. [msgTypeEnum.随访用药提醒](content) {
  101. const obj = this.parse(content);
  102. return `[用药提醒]:${obj.txt}`;
  103. }
  104. [msgTypeEnum.随访换药提醒](content) {
  105. const obj = this.parse(content);
  106. return `[换药提醒]:${obj.txt}`;
  107. }
  108. [msgTypeEnum.随访手术提醒](content) {
  109. const obj = this.parse(content);
  110. return `[手术提醒]:${obj.txt}`;
  111. }
  112. [msgTypeEnum.随访注意事项](content) {
  113. const obj = this.parse(content);
  114. return `[注意事项]:${obj.txt}`;
  115. }
  116. [msgTypeEnum.随访检查预约提醒](content) {
  117. const obj = this.parse(content);
  118. return `[检查预约提醒]:${obj.txt}`;
  119. }
  120. [msgTypeEnum.随访报告已出提醒](content) {
  121. const obj = this.parse(content);
  122. return `[报告已出提醒]:${obj.txt}`;
  123. }
  124. [msgTypeEnum.随访体征上传提醒](content) {
  125. const obj = this.parse(content);
  126. return `[体征上传提醒]:${obj.txt}`;
  127. }
  128. //#endregion
  129. parse(content) {
  130. if (typeof content == 'string') {
  131. try {
  132. const data = JSON.parse(content);
  133. return data;
  134. } catch (error) {
  135. return content;
  136. }
  137. }
  138. }
  139. }
  140. /** 消息内容提取 */
  141. export const lastMsgContentExtract = (msg) => {
  142. const factory = new ContentExtractFactory();
  143. return factory[msg.contentType] ? factory[msg.contentType](msg.content) : '';
  144. };
  145. /** 消息内容组件 */
  146. export const ContentComponents = {
  147. //#region 在线互动
  148. [msgTypeEnum['文本消息']]: 'textMsg',
  149. [msgTypeEnum['图片消息']]: 'imgMsg',
  150. [msgTypeEnum['视频消息']]: 'videoCallMsg',
  151. [msgTypeEnum['患教资料']]: 'articleMsg',
  152. [msgTypeEnum['问卷量表']]: 'questionnaireMsg',
  153. [msgTypeEnum['申请']]: 'applyMsg',
  154. [msgTypeEnum['申请通过']]: 'textMsg',
  155. [msgTypeEnum['申请拒绝']]: 'textMsg',
  156. [msgTypeEnum['申请取消']]: 'systemMsg',
  157. [msgTypeEnum['申请超时']]: 'systemMsg',
  158. [msgTypeEnum['结束服务']]: 'systemMsg',
  159. [msgTypeEnum['患教已读']]: 'articleMsg',
  160. [msgTypeEnum['问卷已填写']]: 'questionnaireMsg',
  161. //#endregion
  162. //#region 随访消息
  163. [msgTypeEnum['随访群发消息']]: 'textMsg',
  164. [msgTypeEnum['随访问卷通知']]: 'questionnaireMsg',
  165. [msgTypeEnum['随访文章通知']]: 'articleMsg',
  166. [msgTypeEnum['随访复诊提醒']]: 'textMsg',
  167. [msgTypeEnum['随访用药提醒']]: 'textMsg',
  168. [msgTypeEnum['随访换药提醒']]: 'textMsg',
  169. [msgTypeEnum['随访手术提醒']]: 'textMsg',
  170. [msgTypeEnum['随访注意事项']]: 'textMsg',
  171. [msgTypeEnum['随访检查预约提醒']]: 'textMsg',
  172. [msgTypeEnum['随访报告已出提醒']]: 'textMsg',
  173. [msgTypeEnum['随访体征上传提醒']]: 'textMsg',
  174. //#endregion
  175. };
  176. /** 消息内容组件内容prop */
  177. const ContentProps = {
  178. //#region 在线互动
  179. [msgTypeEnum['文本消息']]: {
  180. content: 'Content.content',
  181. },
  182. [msgTypeEnum['图片消息']]: {
  183. content: 'Content.content',
  184. },
  185. [msgTypeEnum['视频消息']]: {
  186. content: 'Content.content',
  187. },
  188. [msgTypeEnum['患教资料']]: {
  189. title: 'Content.title',
  190. id: 'Content.id',
  191. read: 'Content.read',
  192. subTaskId: 'Content.subTaskId',
  193. },
  194. [msgTypeEnum['患教已读']]: {
  195. title: 'Content.title',
  196. id: 'Content.id',
  197. read: 'Content.read',
  198. subTaskId: 'Content.subTaskId',
  199. },
  200. [msgTypeEnum['问卷量表']]: {
  201. title: 'Content.title',
  202. id: 'Content.subjectId',
  203. subTaskId: 'Content.subTaskId',
  204. },
  205. [msgTypeEnum['问卷已填写']]: {
  206. id: 'Content.subjectId',
  207. title: 'Content.title',
  208. content: 'Content.content',
  209. sampleId: 'Content.sampleId',
  210. recordId: 'Content.recordId',
  211. subTaskId: 'Content.subTaskId',
  212. },
  213. [msgTypeEnum['申请']]: {
  214. content: 'Content.content',
  215. imgList: 'Content.imgList',
  216. id: 'Id',
  217. },
  218. [msgTypeEnum['申请通过']]: {
  219. content: 'Content.content',
  220. },
  221. [msgTypeEnum['申请拒绝']]: {
  222. content: 'Content.content',
  223. format(obj) {
  224. return `您好,您的互动申请暂未通过,${obj.content}`;
  225. },
  226. },
  227. [msgTypeEnum['申请超时']]: {
  228. content: 'Content.content',
  229. },
  230. [msgTypeEnum['申请取消']]: {
  231. content: 'Content.content',
  232. },
  233. [msgTypeEnum['结束服务']]: {
  234. content: 'Content.content',
  235. },
  236. //#endregion
  237. //#region 随访消息
  238. [msgTypeEnum['随访群发消息']]: {
  239. content: 'Content.txt',
  240. },
  241. [msgTypeEnum['随访问卷通知']]: {
  242. title: 'Content.txt',
  243. id: 'Content.id',
  244. subTaskId: 'Content.subTaskId',
  245. },
  246. [msgTypeEnum['随访文章通知']]: {
  247. title: 'Content.txt',
  248. id: 'Content.id',
  249. msgId: 'Content.msgId',
  250. subTaskId: 'Content.subTaskId',
  251. },
  252. [msgTypeEnum['随访复诊提醒']]: {
  253. content: 'Content.txt',
  254. },
  255. [msgTypeEnum['随访用药提醒']]: {
  256. content: 'Content.txt',
  257. },
  258. [msgTypeEnum['随访换药提醒']]: {
  259. content: 'Content.txt',
  260. },
  261. [msgTypeEnum['随访手术提醒']]: {
  262. content: 'Content.txt',
  263. },
  264. [msgTypeEnum['随访注意事项']]: {
  265. content: 'Content.txt',
  266. },
  267. [msgTypeEnum['随访检查预约提醒']]: {
  268. content: 'Content.txt',
  269. },
  270. [msgTypeEnum['随访报告已出提醒']]: {
  271. content: 'Content.txt',
  272. },
  273. [msgTypeEnum['随访体征上传提醒']]: {
  274. content: 'Content.txt',
  275. },
  276. //#endregion
  277. };
  278. /** 转换消息 */
  279. export const parseMessageEntry = (message, type) => {
  280. const obj = {};
  281. const props = ContentProps[type];
  282. if (!props) return;
  283. let hasFormat = typeof props['format'] == 'function';
  284. Object.keys(props).map((key) => {
  285. if (key != 'format') {
  286. const path = props[key].split('.');
  287. if (path.length <= 1) {
  288. obj[key] = message[path];
  289. } else {
  290. obj[key] = path.reduce((item, p) => {
  291. if (item instanceof Array) {
  292. const [v] = item.filter((i) => i.key == p);
  293. return v?.value;
  294. } else {
  295. try {
  296. if (typeof item[p] == 'string') {
  297. const value = JSON.parse(item[p]);
  298. typeof value == 'object' && (item[p] = value);
  299. }
  300. return item[p];
  301. } catch (error) {
  302. return item[p];
  303. }
  304. }
  305. }, message);
  306. }
  307. }
  308. });
  309. hasFormat && (obj['content'] = props['format'](obj));
  310. return obj;
  311. };
  312. /** 会话记录排序*/
  313. export const msgListSort = (
  314. list,
  315. props = {
  316. sendTime: 'CreateTime',
  317. }
  318. ) => {
  319. // 时间排序 从小到大 最新的在后面
  320. list.sort((a, b) => {
  321. return new Date(a[props.sendTime]).getTime() - new Date(b[props.sendTime]).getTime();
  322. });
  323. return list;
  324. };
  325. /** 判断消息是否显示时间 */
  326. export const msgJudgeTime = (
  327. list,
  328. lastMsg = null,
  329. props = {
  330. sendTime: 'CreateTime',
  331. }
  332. ) => {
  333. for (let i = 0; i < list.length; i++) {
  334. if (i == 0) {
  335. list[i].showSendTime = lastMsg
  336. ? compareTime(list[i][props.sendTime], lastMsg[props.sendTime])
  337. : true;
  338. } else {
  339. list[i].showSendTime = compareTime(list[i][props.sendTime], list[i - 1][props.sendTime]);
  340. }
  341. list[i].isNew = !!lastMsg;
  342. }
  343. return list;
  344. };
  345. /** 对比时间 */
  346. export const compareTime = (startTime, endTime) => {
  347. const times = new Date(startTime).getTime();
  348. const preTimes = new Date(endTime).getTime();
  349. return times - preTimes >= 5 * 60 * 1000;
  350. };