main.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. */
  173. userLogout() {
  174. $A.token("");
  175. $A.storage("userInfo", {});
  176. $A.triggerUserInfoListener({});
  177. if (typeof $A.app === "object") {
  178. $A.app.goForward({path: '/'}, true);
  179. } else {
  180. window.location.href = window.location.origin;
  181. }
  182. },
  183. /**
  184. * 权限是否通过
  185. * @param role
  186. * @returns {boolean}
  187. */
  188. identity(role) {
  189. let userInfo = $A.getUserInfo();
  190. let identity = userInfo.identity;
  191. let isRole = false;
  192. $A.each(identity, (index, res) => {
  193. if (res === role) {
  194. isRole = true;
  195. }
  196. });
  197. return isRole;
  198. },
  199. /**
  200. * 监听用户信息发生变化
  201. * @param listenerName 监听标识
  202. * @param callback 监听回调
  203. */
  204. setOnUserInfoListener(listenerName, callback) {
  205. if (typeof listenerName != "string") {
  206. return;
  207. }
  208. if (typeof callback === "function") {
  209. $A.__userInfoListenerObject[listenerName] = {
  210. callback: callback,
  211. }
  212. }
  213. },
  214. triggerUserInfoListener(userInfo) {
  215. let key, item;
  216. for (key in $A.__userInfoListenerObject) {
  217. if (!$A.__userInfoListenerObject.hasOwnProperty(key)) continue;
  218. item = $A.__userInfoListenerObject[key];
  219. if (typeof item.callback === "function") {
  220. item.callback(userInfo, $A.getToken() !== false);
  221. }
  222. }
  223. },
  224. __userInfoListenerObject: {},
  225. /**
  226. * 监听任务发生变化
  227. * @param listenerName 监听标识
  228. * @param callback 监听回调
  229. * @param callSpecial 是否监听几种特殊情况
  230. */
  231. setOnTaskInfoListener(listenerName, callback, callSpecial) {
  232. if (typeof listenerName != "string") {
  233. return;
  234. }
  235. if (typeof callback === "function") {
  236. $A.__taskInfoListenerObject[listenerName] = {
  237. special: callSpecial === true,
  238. callback: callback,
  239. }
  240. }
  241. },
  242. triggerTaskInfoListener(act, taskDetail, sendToWS = true) {
  243. let key, item;
  244. for (key in $A.__taskInfoListenerObject) {
  245. if (!$A.__taskInfoListenerObject.hasOwnProperty(key)) continue;
  246. item = $A.__taskInfoListenerObject[key];
  247. if (typeof item.callback === "function") {
  248. if (['deleteproject', 'deletelabel', 'leveltask'].indexOf(act) === -1 || item.special === true) {
  249. if (typeof taskDetail.__modifyUsername === "undefined") {
  250. taskDetail.__modifyUsername = $A.getUserName();
  251. }
  252. item.callback(act, taskDetail);
  253. }
  254. }
  255. }
  256. if (sendToWS === true) {
  257. $A.WS.sendTo('team', null, {
  258. type: "taskA",
  259. act: act,
  260. taskDetail: taskDetail
  261. });
  262. }
  263. },
  264. __taskInfoListenerObject: {},
  265. /**
  266. * 获取待推送的日志并推送
  267. * @param taskid
  268. */
  269. triggerTaskInfoChange(taskid) {
  270. $A.aAjax({
  271. url: 'project/task/pushlog',
  272. data: {
  273. taskid: taskid,
  274. pagesize: 20
  275. },
  276. success: (res) => {
  277. if (res.ret === 1) {
  278. res.data.lists.forEach((item) => {
  279. let msgData = {
  280. type: 'taskB',
  281. username: item.username,
  282. userimg: item.userimg,
  283. indate: item.indate,
  284. text: item.detail,
  285. other: item.other
  286. };
  287. res.data.follower.forEach((username) => {
  288. if (username != msgData.username) {
  289. $A.WS.sendTo('user', username, msgData);
  290. }
  291. });
  292. });
  293. }
  294. }
  295. });
  296. }
  297. });
  298. /**
  299. * =============================================================================
  300. * ***************************** websocket assist ****************************
  301. * =============================================================================
  302. */
  303. $.extend({
  304. WS: {
  305. __instance: null,
  306. __connected: false,
  307. __callbackid: {},
  308. __autoLine() {
  309. let tempId = $A.randomString(16);
  310. this.__autoId = tempId;
  311. console.log("[WS] Connection auto 30s ...");
  312. setTimeout(() => {
  313. if (this.__autoId === tempId) {
  314. console.log("[WS] Connection auto ...");
  315. this.connection();
  316. }
  317. }, 30 * 1000);
  318. },
  319. /**
  320. * 连接
  321. */
  322. connection(force = false) {
  323. let url = $A.getObject(window.webSocketConfig, 'URL');
  324. url += ($A.strExists(url, "?") ? "&" : "?") + "token=" + $A.getToken();
  325. if (!$A.leftExists(url, "ws://") && !$A.leftExists(url, "wss://")) {
  326. console.log("[WS] Connection nourl ...");
  327. return;
  328. }
  329. if ($A.getToken() === false) {
  330. console.log("[WS] Connection noid ...");
  331. return;
  332. }
  333. if (this.__instance !== null && force !== true) {
  334. console.log("[WS] Connection already ...");
  335. return;
  336. }
  337. // 初始化客户端套接字并建立连接
  338. this.__instance = new WebSocket(url);
  339. // 连接建立时触发
  340. this.__instance.onopen = (event) => {
  341. console.log("[WS] Connection open ...");
  342. }
  343. // 接收到服务端推送时执行
  344. this.__instance.onmessage = (event) => {
  345. // console.log("[WS] Connection message ...");
  346. let msgDetail = $A.jsonParse(event.data);
  347. if (msgDetail.messageType === 'open') {
  348. console.log("[WS] Connection connected ...");
  349. this.__connected = true;
  350. }
  351. if (msgDetail.messageType === 'feedback') {
  352. typeof this.__callbackid[msgDetail.messageId] === "function" && this.__callbackid[msgDetail.messageId](msgDetail.content);
  353. delete this.__callbackid[msgDetail.messageId];
  354. return;
  355. }
  356. this.triggerMsgListener(msgDetail);
  357. };
  358. // 连接关闭时触发
  359. this.__instance.onclose = (event) => {
  360. console.log("[WS] Connection closed ...");
  361. this.__connected = false;
  362. this.__instance = null;
  363. this.__autoLine();
  364. }
  365. // 连接出错
  366. this.__instance.onerror = (event) => {
  367. console.log("[WS] Connection error ...");
  368. this.__connected = false;
  369. this.__instance = null;
  370. this.__autoLine();
  371. }
  372. return this;
  373. },
  374. /**
  375. * 添加消息监听
  376. * @param listenerName
  377. * @param callback
  378. */
  379. setOnMsgListener(listenerName, callback) {
  380. if (typeof listenerName != "string") {
  381. return;
  382. }
  383. if (typeof callback === "function") {
  384. this.__msgListenerObject[listenerName] = {
  385. callback: callback,
  386. }
  387. }
  388. return this;
  389. },
  390. triggerMsgListener(msgDetail) {
  391. let key, item;
  392. for (key in this.__msgListenerObject) {
  393. if (!this.__msgListenerObject.hasOwnProperty(key)) continue;
  394. item = this.__msgListenerObject[key];
  395. if (typeof item.callback === "function") {
  396. item.callback(msgDetail);
  397. }
  398. }
  399. },
  400. __msgListenerObject: {},
  401. /**
  402. * 发送消息
  403. * @param type 会话类型:user:指定target、team:团队会员
  404. * @param target 接收方的标识,仅type=user时有效
  405. * @param content 发送内容
  406. * @param callback 发送回调
  407. * @param againNum
  408. */
  409. sendTo(type, target, content, callback, againNum = 0) {
  410. if (typeof target === "function") {
  411. content = target;
  412. target = null;
  413. }
  414. if (typeof content === "function") {
  415. callback = content;
  416. content = null;
  417. }
  418. //
  419. if (this.__instance === null) {
  420. if (againNum < 10 && type != 'team') {
  421. setTimeout(() => {
  422. this.sendTo(type, target, content, callback, $A.runNum(againNum) + 1)
  423. }, 600);
  424. if (againNum === 0) {
  425. this.connection();
  426. }
  427. } else {
  428. console.log("[WS] 服务未连接");
  429. typeof callback === "function" && callback({status: 0, message: '服务未连接'});
  430. }
  431. return;
  432. }
  433. if (this.__connected === false) {
  434. console.log("[WS] 未连接成功");
  435. typeof callback === "function" && callback({status: 0, message: '未连接成功'});
  436. return;
  437. }
  438. if (['unread', 'read', 'user', 'team'].indexOf(type) === -1) {
  439. console.log("[WS] 错误的消息类型-" + type);
  440. typeof callback === "function" && callback({status: 0, message: '错误的消息类型-' + type});
  441. return;
  442. }
  443. let messageId = '';
  444. if (typeof callback === "function") {
  445. messageId = $A.randomString(16);
  446. this.__callbackid[messageId] = callback;
  447. }
  448. this.__instance.send(JSON.stringify({
  449. messageType: 'send',
  450. messageId: messageId,
  451. type: type,
  452. sender: $A.getUserName(),
  453. target: target,
  454. content: content,
  455. time: Math.round(new Date().getTime() / 1000),
  456. }));
  457. return this;
  458. },
  459. /**
  460. * 关闭连接
  461. */
  462. close() {
  463. if (this.__instance === null) {
  464. console.log("[WS] 服务未连接");
  465. return;
  466. }
  467. if (this.__connected === false) {
  468. console.log("[WS] 未连接成功");
  469. return;
  470. }
  471. this.__instance.close();
  472. }
  473. }
  474. });
  475. window.$A = $;
  476. })(window);