chat.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. YeIMUniSDK,
  3. YeIMUniSDKDefines
  4. } from '@/uni_modules/wzJun1-YeIM-Uni-SDK/js_sdk/yeim-uni-sdk.min.js'
  5. import {
  6. login
  7. } from '@/request/api/chat.js'
  8. import md5 from '@/js_sdk/js-md5/build/md5.min.js';
  9. import config from "@/config";
  10. let userIds, isListenerChatList = false;
  11. const connect = success => {
  12. if (!userIds) return;
  13. let code = YeIMUniSDK.getInstance().readyState();
  14. if (code !== 3) {
  15. if (success) success();
  16. return;
  17. }
  18. YeIMUniSDK.getInstance().connect({
  19. userId: userIds,
  20. token: uni.getStorageSync('chatToken'),
  21. success: (response) => {
  22. if (response.code === 200) {
  23. $chat.listenerList();
  24. if (success) success();
  25. }
  26. },
  27. fail: (err) => {
  28. logins();
  29. }
  30. });
  31. }
  32. const logins = () => {
  33. let timestamp = (new Date()).getTime() + 86400 * 1000; //1000天后过期
  34. let sign = md5(String(userIds) + timestamp + "50abd47112ebe8c5a73f4694c96a49ce");
  35. login({
  36. userId: userIds,
  37. timestamp: timestamp,
  38. sign: sign
  39. }).then(res => {
  40. if (res.code === 200) {
  41. uni.setStorageSync('chatToken', res.data.token);
  42. connect();
  43. }
  44. })
  45. }
  46. const $chat = {
  47. init() {
  48. //初始化YeIMUniSDK
  49. uni.$YeIMUniSDKDefines = YeIMUniSDKDefines;
  50. uni.$YeIM = YeIMUniSDK.init({
  51. baseURL: config.baseUrl + '/im', // YeIMServer http url (如无特殊需求,服务端启动后仅需修改ip或者域名即可)
  52. socketURL: config.socketURL, // YeIMServer socket url(如无特殊需求,服务端启动后仅需修改ip或者域名即可)
  53. /**
  54. * 日志等级
  55. * 0 普通日志,日志量较多,接入时建议使用
  56. * 1 关键性日志,日志量较少,生产环境时建议使用
  57. * 2 无日志级别,SDK 将不打印任何日志
  58. */
  59. logLevel: 2, // 日志等级,
  60. reConnectInterval: 3000, // 重连时间间隔
  61. reConnectTotal: 99, // 最大重连次数,0不限制一直重连
  62. heartInterval: 35000, //心跳时间间隔(默认30s)
  63. });
  64. },
  65. connect(userId) {
  66. userIds = userId;
  67. if (!userId) return;
  68. if (!uni.getStorageSync('chatToken')) {
  69. logins()
  70. } else {
  71. connect();
  72. }
  73. },
  74. getConversationList(success) {
  75. connect(() => {
  76. YeIMUniSDK.getInstance().getConversationList({
  77. page: 1, //页码
  78. limit: 100, //每页数量
  79. success: success,
  80. fail: (err) => {
  81. console.log('error');
  82. }
  83. });
  84. });
  85. },
  86. getHistoryMessageList(nextMessageId, userId, success, fail) {
  87. connect(() => {
  88. YeIMUniSDK.getInstance().getHistoryMessageList({
  89. nextMessageId: nextMessageId,
  90. conversationId: userId,
  91. success: success,
  92. fail: fail
  93. });
  94. });
  95. },
  96. sendImage(userId, success) {
  97. connect(() => {
  98. uni.chooseImage({
  99. count: 1, //图片数量
  100. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  101. sourceType: ['album'], //从相册选择
  102. success: res => {
  103. uni.getImageInfo({
  104. src: res.tempFilePaths[0],
  105. success: image => {
  106. //创建图片消息
  107. let message = YeIMUniSDK.getInstance()
  108. .createImageMessage({
  109. toId: userId, //接收者用户ID字符串
  110. conversationType: YeIMUniSDKDefines
  111. .CONVERSATION_TYPE
  112. .PRIVATE, //会话类型:私聊
  113. body: {
  114. file: {
  115. tempFilePath: res.tempFilePaths[
  116. 0], //本地图片临时路径
  117. width: image.width, //图片宽度
  118. height: image.height //图片高度
  119. }
  120. },
  121. extra: "",
  122. onProgress: (progress) => {
  123. // console.log(progress);
  124. }
  125. });
  126. //发送消息
  127. uni.showLoading();
  128. YeIMUniSDK.getInstance().sendMessage({
  129. message: message,
  130. success: success,
  131. fail: (err) => {
  132. uni.hideLoading();
  133. uni.showToast({
  134. title: '发送失败',
  135. icon: "none"
  136. });
  137. }
  138. });
  139. }
  140. });
  141. }
  142. });
  143. });
  144. },
  145. sendText(userId, content, success) {
  146. connect(() => {
  147. //创建文字消息
  148. let message = YeIMUniSDK.getInstance().createTextMessage({
  149. toId: userId, //接收者用户ID字符串
  150. conversationType: YeIMUniSDKDefines.CONVERSATION_TYPE.PRIVATE, //会话类型:私聊
  151. body: {
  152. text: content //文本消息内容字符串
  153. },
  154. extra: ""
  155. });
  156. //发送消息
  157. YeIMUniSDK.getInstance().sendMessage({
  158. message: message,
  159. success: success,
  160. fail: (err) => {
  161. uni.hideLoading();
  162. uni.showToast({
  163. title: '发送失败',
  164. icon: "none"
  165. });
  166. }
  167. });
  168. });
  169. },
  170. disConnect() {
  171. YeIMUniSDK.getInstance().disConnect();
  172. },
  173. listenerList() {
  174. if (isListenerChatList) return;
  175. connect(() => {
  176. isListenerChatList = true;
  177. //监听会话列表更新
  178. YeIMUniSDK.getInstance().addEventListener(YeIMUniSDKDefines.EVENT.CONVERSATION_LIST_CHANGED, (
  179. list) => {
  180. uni.$emit('changeChatList', list);
  181. });
  182. })
  183. },
  184. clearConversationUnread(conversationId) {
  185. //清除指定会话未读数,并给对方发送已读回执
  186. YeIMUniSDK.getInstance().clearConversationUnread(conversationId);
  187. }
  188. }
  189. export default $chat;