main.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /**
  2. * 页面专用
  3. */
  4. import '../../sass/main.scss';
  5. (function (window) {
  6. let apiUrl = window.location.origin + '/api/';
  7. let $ = window.$A;
  8. $.extend({
  9. fillUrl(str) {
  10. if (str.substring(0, 2) === "//" ||
  11. str.substring(0, 7) === "http://" ||
  12. str.substring(0, 8) === "https://" ||
  13. str.substring(0, 6) === "ftp://" ||
  14. str.substring(0, 1) === "/") {
  15. return str;
  16. }
  17. return window.location.origin + '/' + str;
  18. },
  19. aUrl(str) {
  20. if (str.substring(0, 2) === "//" ||
  21. str.substring(0, 7) === "http://" ||
  22. str.substring(0, 8) === "https://" ||
  23. str.substring(0, 6) === "ftp://" ||
  24. str.substring(0, 1) === "/") {
  25. return str;
  26. }
  27. return apiUrl + str;
  28. },
  29. aAjax(params) {
  30. if (typeof params !== 'object') return false;
  31. if (typeof params.success === 'undefined') params.success = () => { };
  32. params.url = this.aUrl(params.url);
  33. //
  34. let beforeCall = params.beforeSend;
  35. params.beforeSend = () => {
  36. $A.aAjaxLoad++;
  37. $A(".w-spinner").show();
  38. //
  39. if (typeof beforeCall == "function") {
  40. beforeCall();
  41. }
  42. };
  43. //
  44. let completeCall = params.complete;
  45. params.complete = () => {
  46. $A.aAjaxLoad--;
  47. if ($A.aAjaxLoad <= 0) {
  48. $A(".w-spinner").hide();
  49. }
  50. //
  51. if (typeof completeCall == "function") {
  52. completeCall();
  53. }
  54. };
  55. //
  56. let callback = params.success;
  57. params.success = (data, status, xhr) => {
  58. if (typeof data === 'object') {
  59. if (data.ret === -1 && params.checkRole !== false) {
  60. //身份丢失
  61. $A.app.$Modal.error({
  62. title: '温馨提示',
  63. content: data.msg,
  64. onOk: () => {
  65. $A.token("");
  66. $A.userLogout();
  67. }
  68. });
  69. return;
  70. }
  71. if (data.ret === -2 && params.role !== false) {
  72. //没有权限
  73. $A.app.$Modal.error({
  74. title: '权限不足',
  75. content: data.msg ? data.msg : "你没有相关的权限查看或编辑!"
  76. });
  77. }
  78. }
  79. if (typeof callback === "function") {
  80. callback(data, status, xhr);
  81. }
  82. };
  83. //
  84. $A.ajax(params);
  85. },
  86. aAjaxLoad: 0,
  87. /**
  88. * 编辑器参数配置
  89. * @returns {{modules: {toolbar: *[]}}}
  90. */
  91. editorOption() {
  92. return {
  93. modules: {
  94. toolbar: [
  95. ['bold', 'italic'],
  96. [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  97. [{ 'size': ['small', false, 'large', 'huge'] }],
  98. [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  99. [{ 'color': [] }, { 'background': [] }],
  100. [{ 'align': [] }]
  101. ]
  102. }
  103. };
  104. },
  105. /**
  106. * 获取token
  107. * @returns {boolean}
  108. */
  109. getToken() {
  110. let token = $A.token();
  111. return $A.count(token) < 10 ? false : token;
  112. },
  113. /**
  114. * 设置token
  115. * @param token
  116. */
  117. setToken(token) {
  118. $A.token(token);
  119. },
  120. /**
  121. * 获取会员昵称
  122. * @returns string
  123. */
  124. getUserName() {
  125. if ($A.getToken() === false) {
  126. return "";
  127. }
  128. let userInfo = $A.getUserInfo();
  129. return $A.ishave(userInfo.username) ? userInfo.username : '';
  130. },
  131. /**
  132. * 获取用户信息(并保存)
  133. * @param callback 网络请求获取到用户信息回调(监听用户信息发生变化)
  134. * @param continueListenerName 持续监听标识(字符串或boolean,true:自动生成监听标识,false:自动生成监听标识但首次不请求网络)
  135. * @returns Object
  136. */
  137. getUserInfo(callback, continueListenerName) {
  138. if (typeof callback === 'function' || (typeof callback === "boolean" && callback === true)) {
  139. if (typeof continueListenerName === "boolean") {
  140. if (continueListenerName === true) {
  141. continueListenerName = "auto-" + $A.randomString(6);
  142. } else {
  143. $A.setOnUserInfoListener("auto-" + $A.randomString(6), callback);
  144. return $A.jsonParse($A.storage("userInfo"));
  145. }
  146. }
  147. //
  148. $A.aAjax({
  149. url: 'users/info',
  150. error: () => {
  151. this.userLogout();
  152. },
  153. success: (res) => {
  154. if (res.ret === 1) {
  155. $A.storage("userInfo", res.data);
  156. $A.setToken(res.data.token);
  157. $A.triggerUserInfoListener(res.data);
  158. typeof callback === "function" && callback(res.data, $A.getToken() !== false);
  159. }
  160. },
  161. afterComplete: () => {
  162. if (typeof continueListenerName == "string" && continueListenerName) {
  163. $A.setOnUserInfoListener(continueListenerName, callback);
  164. }
  165. },
  166. });
  167. }
  168. return $A.jsonParse($A.storage("userInfo"));
  169. },
  170. /**
  171. * 根据用户名获取用户基本信息
  172. * @param username
  173. * @param callback
  174. * @param cacheTime
  175. */
  176. getUserBasic(username, callback, cacheTime = 300) {
  177. if (typeof callback !== "function") {
  178. return;
  179. }
  180. if (!username) {
  181. callback({}, false);
  182. return;
  183. }
  184. //
  185. let keyName = '__userBasic:' + username.substring(0, 1) + '__';
  186. let localData = $A.jsonParse(window.localStorage[keyName]);
  187. if ($A.getObject(localData, username + '.success') === true) {
  188. callback(localData[username].data, true);
  189. if (localData[username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
  190. return;
  191. }
  192. }
  193. //
  194. $A.__userBasicObject.push({
  195. username: username,
  196. callback: callback
  197. });
  198. //
  199. $A.__userBasicTimeout++;
  200. let timeout = $A.__userBasicTimeout;
  201. setTimeout(() => {
  202. timeout === $A.__userBasicTimeout && $A.__userBasicEvent();
  203. }, 100);
  204. },
  205. __userBasicEvent() {
  206. if ($A.__userBasicLoading === true) {
  207. return;
  208. }
  209. $A.__userBasicLoading = true;
  210. //
  211. let userArray = [];
  212. $A.__userBasicObject.some((item) => {
  213. userArray.push(item.username);
  214. if (userArray.length >= 30) {
  215. return true;
  216. }
  217. });
  218. //
  219. $A.aAjax({
  220. url: 'users/basic',
  221. data: {
  222. username: $A.jsonStringify(userArray),
  223. },
  224. error: () => {
  225. userArray.forEach((username) => {
  226. let tmpLists = $A.__userBasicObject.filter((item) => { return item.username == username });
  227. tmpLists.forEach((item) => {
  228. if (typeof item.callback === "function") {
  229. item.callback({}, false);
  230. item.callback = null;
  231. }
  232. });
  233. });
  234. //
  235. $A.__userBasicLoading = false;
  236. $A.__userBasicObject = $A.__userBasicObject.filter((item) => { return typeof item.callback === "function"});
  237. if ($A.__userBasicObject.length > 0) {
  238. $A.__userBasicEvent();
  239. }
  240. },
  241. success: (res) => {
  242. if (res.ret === 1) {
  243. res.data.forEach((data) => {
  244. let keyName = '__userBasic:' + data.username.substring(0, 1) + '__';
  245. let localData = $A.jsonParse(window.localStorage[keyName]);
  246. localData[data.username] = {
  247. success: true,
  248. update: Math.round(new Date().getTime() / 1000),
  249. data: data
  250. };
  251. window.localStorage[keyName] = $A.jsonStringify(localData);
  252. });
  253. }
  254. userArray.forEach((username) => {
  255. let tmpLists = $A.__userBasicObject.filter((item) => { return item.username == username });
  256. tmpLists.forEach((item) => {
  257. if (typeof item.callback === "function") {
  258. let info = res.data.filter((data) => { return data.username == username });
  259. if (info.length === 0) {
  260. item.callback({}, false);
  261. } else {
  262. item.callback(info[0], true);
  263. }
  264. item.callback = null;
  265. }
  266. });
  267. });
  268. //
  269. $A.__userBasicLoading = false;
  270. $A.__userBasicObject = $A.__userBasicObject.filter((item) => { return typeof item.callback === "function"});
  271. if ($A.__userBasicObject.length > 0) {
  272. $A.__userBasicEvent();
  273. }
  274. }
  275. });
  276. },
  277. __userBasicTimeout: 0,
  278. __userBasicLoading: false,
  279. __userBasicObject: [],
  280. /**
  281. * 打开登录页面
  282. */
  283. userLogout() {
  284. $A.token("");
  285. $A.storage("userInfo", {});
  286. $A.triggerUserInfoListener({});
  287. if (typeof $A.app === "object") {
  288. $A.app.goForward({path: '/'}, true);
  289. } else {
  290. window.location.href = window.location.origin;
  291. }
  292. },
  293. /**
  294. * 权限是否通过
  295. * @param role
  296. * @returns {boolean}
  297. */
  298. identity(role) {
  299. let userInfo = $A.getUserInfo();
  300. return $A.identityRaw(role, userInfo.identity);
  301. },
  302. /**
  303. * 权限是否通过
  304. * @param role
  305. * @returns {boolean}
  306. */
  307. identityRaw(role, identity) {
  308. let isRole = false;
  309. $A.each(identity, (index, res) => {
  310. if (res === role) {
  311. isRole = true;
  312. }
  313. });
  314. return isRole;
  315. },
  316. /**
  317. * 监听用户信息发生变化
  318. * @param listenerName 监听标识
  319. * @param callback 监听回调
  320. */
  321. setOnUserInfoListener(listenerName, callback) {
  322. if (typeof listenerName != "string") {
  323. return;
  324. }
  325. if (typeof callback === "function") {
  326. $A.__userInfoListenerObject[listenerName] = {
  327. callback: callback,
  328. }
  329. }
  330. },
  331. triggerUserInfoListener(userInfo) {
  332. let key, item;
  333. for (key in $A.__userInfoListenerObject) {
  334. if (!$A.__userInfoListenerObject.hasOwnProperty(key)) continue;
  335. item = $A.__userInfoListenerObject[key];
  336. if (typeof item.callback === "function") {
  337. item.callback(userInfo, $A.getToken() !== false);
  338. }
  339. }
  340. },
  341. __userInfoListenerObject: {},
  342. /**
  343. * 监听任务发生变化
  344. * @param listenerName 监听标识
  345. * @param callback 监听回调
  346. * @param callSpecial 是否监听几种特殊情况
  347. */
  348. setOnTaskInfoListener(listenerName, callback, callSpecial) {
  349. if (typeof listenerName != "string") {
  350. return;
  351. }
  352. if (typeof callback === "function") {
  353. $A.__taskInfoListenerObject[listenerName] = {
  354. special: callSpecial === true,
  355. callback: callback,
  356. }
  357. }
  358. },
  359. triggerTaskInfoListener(act, taskDetail, sendToWS = true) {
  360. let key, item;
  361. for (key in $A.__taskInfoListenerObject) {
  362. if (!$A.__taskInfoListenerObject.hasOwnProperty(key)) continue;
  363. item = $A.__taskInfoListenerObject[key];
  364. if (typeof item.callback === "function") {
  365. if (['deleteproject', 'deletelabel', 'leveltask'].indexOf(act) === -1 || item.special === true) {
  366. if (typeof taskDetail.__modifyUsername === "undefined") {
  367. taskDetail.__modifyUsername = $A.getUserName();
  368. }
  369. item.callback(act, taskDetail);
  370. }
  371. }
  372. }
  373. if (sendToWS === true) {
  374. $A.WSOB.sendTo('team', {
  375. type: "taskA",
  376. act: act,
  377. taskDetail: taskDetail
  378. });
  379. }
  380. },
  381. __taskInfoListenerObject: {},
  382. /**
  383. * 获取待推送的日志并推送
  384. * @param taskid
  385. */
  386. triggerTaskInfoChange(taskid) {
  387. $A.aAjax({
  388. url: 'project/task/pushlog',
  389. data: {
  390. taskid: taskid,
  391. pagesize: 20
  392. },
  393. success: (res) => {
  394. if (res.ret === 1) {
  395. res.data.lists.forEach((item) => {
  396. let msgData = {
  397. type: 'taskB',
  398. username: item.username,
  399. userimg: item.userimg,
  400. indate: item.indate,
  401. text: item.detail,
  402. other: item.other
  403. };
  404. res.data.follower.forEach((username) => {
  405. if (username != msgData.username) {
  406. $A.WSOB.sendTo('user', username, msgData, 'special');
  407. }
  408. });
  409. });
  410. }
  411. }
  412. });
  413. }
  414. });
  415. /**
  416. * =============================================================================
  417. * ***************************** websocket assist ****************************
  418. * =============================================================================
  419. */
  420. $.extend({
  421. /**
  422. * @param config {username, url, token, channel, logCallback}
  423. */
  424. WTWS: function (config) {
  425. this.__instance = null;
  426. this.__connected = false;
  427. this.__callbackid = {};
  428. this.__openNum = 0;
  429. this.__autoNum = 0;
  430. this.__autoLine = function (timeout) {
  431. var tempNum = this.__autoNum;
  432. var thas = this;
  433. setTimeout(function () {
  434. if (tempNum === thas.__autoNum) {
  435. thas.__autoNum++
  436. if (!thas.__config.token) {
  437. thas.__log("[WS] No token");
  438. thas.__autoLine(timeout + 5);
  439. } else {
  440. thas.sendTo('refresh', function (res) {
  441. thas.__log("[WS] Connection " + (res.status ? 'success' : 'error'));
  442. thas.__autoLine(timeout + 5);
  443. });
  444. }
  445. }
  446. }, Math.min(timeout, 30) * 1000);
  447. }
  448. this.__log = function (text, event) {
  449. typeof this.__config.logCallback === "function" && this.__config.logCallback(text, event);
  450. }
  451. this.__lExists = function (string, find, lower) {
  452. string += "";
  453. find += "";
  454. if (lower !== true) {
  455. string = string.toLowerCase();
  456. find = find.toLowerCase();
  457. }
  458. return (string.substring(0, find.length) === find);
  459. }
  460. this.__rNum = function (str, fixed) {
  461. var _s = Number(str);
  462. if (_s + "" === "NaN") {
  463. _s = 0;
  464. }
  465. if (/^[0-9]*[1-9][0-9]*$/.test(fixed)) {
  466. _s = _s.toFixed(fixed);
  467. var rs = _s.indexOf('.');
  468. if (rs < 0) {
  469. _s += ".";
  470. for (var i = 0; i < fixed; i++) {
  471. _s += "0";
  472. }
  473. }
  474. }
  475. return _s;
  476. }
  477. this.__jParse = function (str, defaultVal) {
  478. if (str === null) {
  479. return defaultVal ? defaultVal : {};
  480. }
  481. if (typeof str === "object") {
  482. return str;
  483. }
  484. try {
  485. return JSON.parse(str);
  486. } catch (e) {
  487. return defaultVal ? defaultVal : {};
  488. }
  489. }
  490. this.__randString = function (len) {
  491. len = len || 32;
  492. var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678oOLl9gqVvUuI1';
  493. var maxPos = $chars.length;
  494. var pwd = '';
  495. for (var i = 0; i < len; i++) {
  496. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  497. }
  498. return pwd;
  499. }
  500. this.__urlParams = function(url, params) {
  501. if (typeof params === "object" && params !== null) {
  502. url+= "";
  503. url+= url.indexOf("?") === -1 ? '?' : '';
  504. for (var key in params) {
  505. if (!params.hasOwnProperty(key)) {
  506. continue;
  507. }
  508. url+= '&' + key + '=' + params[key];
  509. }
  510. }
  511. return url.replace("?&", "?");
  512. }
  513. this.__isArr = function (obj){
  514. return Object.prototype.toString.call(obj)=='[object Array]';
  515. }
  516. /**
  517. * 设置参数
  518. * @param config
  519. */
  520. this.config = function (config) {
  521. if (typeof config !== "object" || config === null) {
  522. config = {};
  523. }
  524. config.username = config.username || '';
  525. config.url = config.url || '';
  526. config.token = config.token || '';
  527. config.channel = config.channel || '';
  528. config.logCallback = config.logCallback || null;
  529. this.__config = config;
  530. return this;
  531. }
  532. /**
  533. * 连接
  534. * @param force
  535. */
  536. this.connection = function (force) {
  537. if (!this.__lExists(this.__config.url, "ws://") && !this.__lExists(this.__config.url, "wss://")) {
  538. this.__log("[WS] No connection address");
  539. return this;
  540. }
  541. if (!this.__config.token) {
  542. this.__log("[WS] No connected token");
  543. return this;
  544. }
  545. if (this.__instance !== null && force !== true) {
  546. this.__log("[WS] Connection exists");
  547. return this;
  548. }
  549. var thas = this;
  550. // 初始化客户端套接字并建立连接
  551. this.__instance = new WebSocket(this.__urlParams(this.__config.url, {
  552. token: this.__config.token,
  553. channel: this.__config.channel
  554. }));
  555. // 连接建立时触发
  556. this.__instance.onopen = function (event) {
  557. thas.__log("[WS] Connection opened", event);
  558. }
  559. // 接收到服务端推送时执行
  560. this.__instance.onmessage = function (event) {
  561. var msgDetail = thas.__jParse(event.data);
  562. if (msgDetail.messageType === 'open') {
  563. thas.__log("[WS] Connection connected");
  564. msgDetail.openNum = thas.__openNum;
  565. msgDetail.config = thas.__config;
  566. thas.__openNum++;
  567. thas.__connected = true;
  568. thas.__autoLine(30);
  569. } else if (msgDetail.messageType === 'back') {
  570. typeof thas.__callbackid[msgDetail.messageId] === "function" && thas.__callbackid[msgDetail.messageId](msgDetail.body);
  571. delete thas.__callbackid[msgDetail.messageId];
  572. return;
  573. }
  574. if (thas.__rNum(msgDetail.contentId) > 0) {
  575. thas.sendTo('roger', msgDetail.contentId);
  576. }
  577. thas.triggerMsgListener(msgDetail);
  578. };
  579. // 连接关闭时触发
  580. this.__instance.onclose = function (event) {
  581. thas.__log("[WS] Connection closed", event);
  582. thas.__connected = false;
  583. thas.__instance = null;
  584. thas.__autoLine(5);
  585. }
  586. // 连接出错
  587. this.__instance.onerror = function (event) {
  588. thas.__log("[WS] Connection error", event);
  589. thas.__connected = false;
  590. thas.__instance = null;
  591. thas.__autoLine(5);
  592. }
  593. return this;
  594. }
  595. /**
  596. * 添加消息监听
  597. * @param listenerName
  598. * @param listenerType
  599. * @param callback
  600. */
  601. this.setOnMsgListener = function (listenerName, listenerType, callback) {
  602. if (typeof listenerName != "string") {
  603. return this;
  604. }
  605. if (typeof listenerType === "function") {
  606. callback = listenerType;
  607. listenerType = [];
  608. }
  609. if (!this.__isArr(listenerType)) {
  610. listenerType = [listenerType];
  611. }
  612. if (typeof callback === "function") {
  613. this.__msgListenerObject[listenerName] = {
  614. callback: callback,
  615. listenerType: listenerType,
  616. }
  617. }
  618. return this;
  619. }
  620. this.triggerMsgListener = function (msgDetail) {
  621. var key, item;
  622. for (key in this.__msgListenerObject) {
  623. if (!this.__msgListenerObject.hasOwnProperty(key)) {
  624. continue;
  625. }
  626. item = this.__msgListenerObject[key];
  627. if (item.listenerType.length > 0 && item.listenerType.indexOf(msgDetail.messageType) === -1) {
  628. continue;
  629. }
  630. if (typeof item.callback === "function") {
  631. item.callback(msgDetail);
  632. }
  633. }
  634. }
  635. this.__msgListenerObject = {}
  636. /**
  637. * 添加特殊监听
  638. * @param listenerName
  639. * @param callback
  640. */
  641. this.setOnSpecialListener = function (listenerName, callback) {
  642. if (typeof listenerName != "string") {
  643. return this;
  644. }
  645. if (typeof callback === "function") {
  646. this.__specialListenerObject[listenerName] = {
  647. callback: callback,
  648. }
  649. }
  650. return this;
  651. }
  652. this.triggerSpecialListener = function (simpleMsg) {
  653. var key, item;
  654. for (key in this.__specialListenerObject) {
  655. if (!this.__specialListenerObject.hasOwnProperty(key)) {
  656. continue;
  657. }
  658. item = this.__specialListenerObject[key];
  659. if (typeof item.callback === "function") {
  660. item.callback(simpleMsg);
  661. }
  662. }
  663. }
  664. this.__specialListenerObject = {}
  665. /**
  666. * 发送消息
  667. * @param messageType 会话类型
  668. * - refresh: 刷新
  669. * - unread: 未读信息总数量
  670. * - read: 已读会员信息
  671. * - roger: 收到信息回执
  672. * - user: 指定target
  673. * - update: 指定target
  674. * - team: 团队会员
  675. * @param target 发送目标
  676. * @param body 发送内容(对象或数组)
  677. * @param callback 发送回调
  678. * @param againNum
  679. */
  680. this.sendTo = function (messageType, target, body, callback, againNum = 0) {
  681. if (typeof target === "object" && typeof body === "undefined") {
  682. body = target;
  683. target = null;
  684. }
  685. if (typeof target === "function") {
  686. body = target;
  687. target = null;
  688. }
  689. if (typeof body === "function") {
  690. callback = body;
  691. body = null;
  692. }
  693. if (body === null || typeof body !== "object") {
  694. body = {};
  695. }
  696. //
  697. var thas = this;
  698. if (this.__instance === null || this.__connected === false) {
  699. if (againNum < 10 && messageType != 'team') {
  700. setTimeout(function () {
  701. thas.sendTo(messageType, target, body, callback, thas.__rNum(againNum) + 1)
  702. }, 600);
  703. if (againNum === 0) {
  704. this.connection();
  705. }
  706. } else {
  707. if (this.__instance === null) {
  708. this.__log("[WS] Service not connected");
  709. typeof callback === "function" && callback({status: 0, message: '服务未连接'});
  710. } else {
  711. this.__log("[WS] Failed connection");
  712. typeof callback === "function" && callback({status: 0, message: '未连接成功'});
  713. }
  714. }
  715. return this;
  716. }
  717. if (['refresh', 'unread', 'read', 'roger', 'user', 'info', 'team'].indexOf(messageType) === -1) {
  718. this.__log("[WS] Wrong message messageType: " + messageType);
  719. typeof callback === "function" && callback({status: 0, message: '错误的消息类型: ' + messageType});
  720. return this;
  721. }
  722. //
  723. var contentId = 0;
  724. if (messageType === 'roger') {
  725. contentId = target;
  726. target = null;
  727. }
  728. var messageId = '';
  729. if (typeof callback === "string" && callback === 'special') {
  730. callback = function (res) {
  731. res.status === 1 && thas.triggerSpecialListener({
  732. target: target,
  733. body: body,
  734. });
  735. }
  736. }
  737. if (typeof callback === "function") {
  738. messageId = this.__randString(16);
  739. this.__callbackid[messageId] = callback;
  740. }
  741. this.__instance.send(JSON.stringify({
  742. messageType: messageType,
  743. messageId: messageId,
  744. contentId: contentId,
  745. channel: this.__config.channel,
  746. username: this.__config.username,
  747. target: target,
  748. body: body,
  749. time: Math.round(new Date().getTime() / 1000),
  750. }));
  751. return this;
  752. }
  753. /**
  754. * 关闭连接
  755. */
  756. this.close = function () {
  757. if (this.__instance === null) {
  758. this.__log("[WS] Service not connected");
  759. return this;
  760. }
  761. if (this.__connected === false) {
  762. this.__log("[WS] Failed connection");
  763. return this;
  764. }
  765. this.__instance.close();
  766. return this;
  767. }
  768. return this.config(config);
  769. },
  770. WSOB: {
  771. instance: null,
  772. isClose: false,
  773. /**
  774. * 初始化
  775. */
  776. initialize() {
  777. let url = $A.getObject(window.webSocketConfig, 'URL');
  778. if (!url) {
  779. url = window.location.origin;
  780. url = url.replace("https://", "wss://");
  781. url = url.replace("http://", "ws://");
  782. url+= "/ws";
  783. }
  784. let config = {
  785. username: $A.getUserName(),
  786. url: url,
  787. token: $A.getToken(),
  788. channel: 'web'
  789. };
  790. if (this.instance === null) {
  791. this.instance = new $A.WTWS(config);
  792. this.instance.connection()
  793. } else if (this.isClose) {
  794. this.isClose = false
  795. this.instance.config(config);
  796. this.instance.connection();
  797. }
  798. },
  799. /**
  800. * 主动连接
  801. */
  802. connection() {
  803. this.initialize();
  804. this.instance.connection();
  805. },
  806. /**
  807. * 监听消息
  808. * @param listenerName
  809. * @param listenerType
  810. * @param callback
  811. */
  812. setOnMsgListener(listenerName, listenerType, callback) {
  813. this.initialize();
  814. this.instance.setOnMsgListener(listenerName, listenerType, callback);
  815. },
  816. /**
  817. * 添加特殊监听
  818. * @param listenerName
  819. * @param callback
  820. */
  821. setOnSpecialListener(listenerName, callback) {
  822. this.initialize();
  823. this.instance.setOnSpecialListener(listenerName, callback);
  824. },
  825. /**
  826. * 发送消息
  827. * @param messageType
  828. * @param target
  829. * @param body
  830. * @param callback
  831. */
  832. sendTo(messageType, target, body, callback) {
  833. this.initialize();
  834. this.instance.sendTo(messageType, target, body, callback);
  835. },
  836. /**
  837. * 关闭连接
  838. */
  839. close() {
  840. if (this.instance === null) {
  841. return;
  842. }
  843. this.isClose = true
  844. this.instance.config(null).close();
  845. },
  846. /**
  847. * 获取消息描述
  848. * @param content
  849. * @returns {string}
  850. */
  851. getMsgDesc(content) {
  852. let desc;
  853. switch (content.type) {
  854. case 'text':
  855. desc = content.text;
  856. break;
  857. case 'image':
  858. desc = $A.app.$L('[图片]');
  859. break;
  860. case 'taskB':
  861. desc = content.text + " " + $A.app.$L("[来自关注任务]");
  862. break;
  863. case 'report':
  864. desc = content.text + " " + $A.app.$L("[来自工作报告]");
  865. break;
  866. case 'video':
  867. desc = $A.app.$L('[视频通话]');
  868. break;
  869. case 'voice':
  870. desc = $A.app.$L('[语音通话]');
  871. break;
  872. default:
  873. desc = $A.app.$L('[未知类型]');
  874. break;
  875. }
  876. return desc;
  877. }
  878. }
  879. });
  880. window.$A = $;
  881. })(window);