websdk.shim.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ;(function () {
  2. 'use strict'
  3. var _im = window.WebIM,
  4. _c = _im.connection,
  5. _m = _im.message,
  6. _e = function () {},
  7. https = location.protocol === 'https:'
  8. _im.Helper = _im.utils
  9. _im.Emoji = _im.EMOTIONS = {
  10. path: 'static/img/faces/',
  11. map: {
  12. '[):]': 'ee_1.png',
  13. '[:D]': 'ee_2.png',
  14. '[;)]': 'ee_3.png',
  15. '[:-o]': 'ee_4.png',
  16. '[:p]': 'ee_5.png',
  17. '[(H)]': 'ee_6.png',
  18. '[:@]': 'ee_7.png',
  19. '[:s]': 'ee_8.png',
  20. '[:$]': 'ee_9.png',
  21. '[:(]': 'ee_10.png',
  22. '[:\'(]': 'ee_11.png',
  23. '[:|]': 'ee_12.png',
  24. '[(a)]': 'ee_13.png',
  25. '[8o|]': 'ee_14.png',
  26. '[8-|]': 'ee_15.png',
  27. '[+o(]': 'ee_16.png',
  28. '[<o)]': 'ee_17.png',
  29. '[|-)]': 'ee_18.png',
  30. '[*-)]': 'ee_19.png',
  31. '[:-#]': 'ee_20.png',
  32. '[:-*]': 'ee_21.png',
  33. '[^o)]': 'ee_22.png',
  34. '[8-)]': 'ee_23.png',
  35. '[(|)]': 'ee_24.png',
  36. '[(u)]': 'ee_25.png',
  37. '[(S)]': 'ee_26.png',
  38. '[(*)]': 'ee_27.png',
  39. '[(#)]': 'ee_28.png',
  40. '[(R)]': 'ee_29.png',
  41. '[({)]': 'ee_30.png',
  42. '[(})]': 'ee_31.png',
  43. '[(k)]': 'ee_32.png',
  44. '[(F)]': 'ee_33.png',
  45. '[(W)]': 'ee_34.png',
  46. '[(D)]': 'ee_35.png'
  47. }
  48. }
  49. _im.Helper.EmotionPicData = (function () {
  50. var ems = {}
  51. for (var o in _im.EMOTIONS.map) {
  52. if (_im.EMOTIONS.map.hasOwnProperty(o)) {
  53. ems[o] = _im.EMOTIONS.map[o]
  54. }
  55. }
  56. return ems
  57. }())
  58. _c.prototype.init = function (options) {
  59. for (var o in options) {
  60. if (options.hasOwnProperty(o)) {
  61. this[o] = options[o]
  62. }
  63. }
  64. this.listen(options)
  65. }
  66. var _send = function (type, options) {
  67. var msg = new _m(type)
  68. options.id && (msg.id = options.id)
  69. msg.set(options)
  70. options.type === 'groupchat' && msg.setGroup(options.type)
  71. this.send(msg.body)
  72. }
  73. _c.prototype.sendTextMessage = function (options) {
  74. _send.call(this, 'txt', options)
  75. }
  76. _c.prototype.sendPicture = function (options) {
  77. _send.call(this, 'img', options)
  78. }
  79. _c.prototype.sendAudio = function (options) {
  80. _send.call(this, 'audio', options)
  81. }
  82. _c.prototype.sendFile = function (options) {
  83. _send.call(this, 'file', options)
  84. }
  85. _c.prototype.sendLocationMessage = function (options) {
  86. _send.call(this, 'location', options)
  87. }
  88. _c.prototype.sendCmdMessage = function (options) {
  89. _send.call(this, 'cmd', options)
  90. }
  91. _c.prototype.sendPictureMessage = _e
  92. _c.prototype.sendAudioMessage = _e
  93. _c.prototype.sendFileMessage = _e
  94. _im.Helper.innerBase64 = (function () {
  95. var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  96. var obj = {
  97. /**
  98. * Encodes a string in base64
  99. *
  100. * @param {String}
  101. * input The string to encode in base64.
  102. */
  103. encode: function (input) {
  104. var output = ''
  105. var chr1, chr2, chr3
  106. var enc1, enc2, enc3, enc4
  107. var i = 0
  108. do {
  109. chr1 = input.charCodeAt(i++)
  110. chr2 = input.charCodeAt(i++)
  111. chr3 = input.charCodeAt(i++)
  112. enc1 = chr1 >> 2
  113. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
  114. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
  115. enc4 = chr3 & 63
  116. if (isNaN(chr2)) {
  117. enc3 = enc4 = 64
  118. } else if (isNaN(chr3)) {
  119. enc4 = 64
  120. }
  121. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
  122. + keyStr.charAt(enc3) + keyStr.charAt(enc4)
  123. } while (i < input.length)
  124. return output
  125. },
  126. byteEncode: function (bytes) {
  127. var output = ''
  128. var chr1, chr2, chr3
  129. var enc1, enc2, enc3, enc4
  130. var i = 0
  131. do {
  132. chr1 = bytes[i++]
  133. chr2 = bytes[i++]
  134. chr3 = bytes[i++]
  135. enc1 = chr1 >> 2
  136. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
  137. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
  138. enc4 = chr3 & 63
  139. if (isNaN(chr2)) {
  140. enc3 = enc4 = 64
  141. } else if (isNaN(chr3)) {
  142. enc4 = 64
  143. }
  144. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
  145. + keyStr.charAt(enc3) + keyStr.charAt(enc4)
  146. } while (i < bytes.length)
  147. return output
  148. },
  149. /**
  150. * Decodes a base64 string.
  151. *
  152. * @param {String}
  153. * input The string to decode.
  154. */
  155. decode: function (input) {
  156. var output = ''
  157. var chr1, chr2, chr3
  158. var enc1, enc2, enc3, enc4
  159. var i = 0
  160. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  161. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
  162. do {
  163. enc1 = keyStr.indexOf(input.charAt(i++))
  164. enc2 = keyStr.indexOf(input.charAt(i++))
  165. enc3 = keyStr.indexOf(input.charAt(i++))
  166. enc4 = keyStr.indexOf(input.charAt(i++))
  167. chr1 = (enc1 << 2) | (enc2 >> 4)
  168. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
  169. chr3 = ((enc3 & 3) << 6) | enc4
  170. output = output + String.fromCharCode(chr1)
  171. if (enc3 != 64) {
  172. output = output + String.fromCharCode(chr2)
  173. }
  174. if (enc4 != 64) {
  175. output = output + String.fromCharCode(chr3)
  176. }
  177. } while (i < input.length)
  178. return output
  179. }
  180. }
  181. return obj
  182. })()
  183. window.Easemob = window.Easemob || {}
  184. window.Easemob.im = window.Easemob.im || _im
  185. window.Easemob.im.Connection = _c
  186. window.Easemob.im.Utils = _im.utils
  187. window.Easemob.im.Utils.parseEmotions = _im.utils.parseEmoji
  188. window.Easemob.im.Utils.parseTextMessage = function (message, faces) {
  189. var faces = _im.Emoji
  190. if (typeof message !== 'string') {
  191. return
  192. }
  193. if (Object.prototype.toString.call(faces) !== '[object Object]') {
  194. return {
  195. isemotion: false,
  196. body: [
  197. {
  198. type: 'txt',
  199. data: message
  200. }
  201. ]
  202. }
  203. }
  204. var receiveMsg = message
  205. var emessage = []
  206. var expr = /\[[^[\]]{2,3}\]/mg
  207. var emotions = receiveMsg.match(expr)
  208. if (!emotions || emotions.length < 1) {
  209. return {
  210. isemotion: false,
  211. body: [
  212. {
  213. type: 'txt',
  214. data: message
  215. }
  216. ]
  217. }
  218. }
  219. var isemotion = false
  220. for (var i = 0; i < emotions.length; i++) {
  221. var tmsg = receiveMsg.substring(0, receiveMsg.indexOf(emotions[i])),
  222. existEmotion = Easemob.im.EMOTIONS.map[emotions[i]]
  223. if (tmsg) {
  224. emessage.push({
  225. type: 'txt',
  226. data: tmsg
  227. })
  228. }
  229. if (!existEmotion) {
  230. emessage.push({
  231. type: 'txt',
  232. data: emotions[i]
  233. })
  234. continue
  235. }
  236. var emotion = Easemob.im.EMOTIONS.map ? Easemob.im.EMOTIONS.path + existEmotion : null
  237. if (emotion) {
  238. isemotion = true
  239. emessage.push({
  240. type: 'emotion',
  241. data: emotion
  242. })
  243. } else {
  244. emessage.push({
  245. type: 'txt',
  246. data: emotions[i]
  247. })
  248. }
  249. var restMsgIndex = receiveMsg.indexOf(emotions[i]) + emotions[i].length
  250. receiveMsg = receiveMsg.substring(restMsgIndex)
  251. }
  252. if (receiveMsg) {
  253. emessage.push({
  254. type: 'txt',
  255. data: receiveMsg
  256. })
  257. }
  258. if (isemotion) {
  259. return {
  260. isemotion: isemotion,
  261. body: emessage
  262. }
  263. }
  264. return {
  265. isemotion: false,
  266. body: [
  267. {
  268. type: 'txt',
  269. data: message
  270. }
  271. ]
  272. }
  273. }
  274. }())