main.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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);
  159. }
  160. //
  161. if (typeof continueListenerName == "string" && continueListenerName) {
  162. $A.setOnUserInfoListener(continueListenerName, callback);
  163. }
  164. }
  165. });
  166. }
  167. return $A.jsonParse($A.storage("userInfo"));
  168. },
  169. /**
  170. * 打开登录页面
  171. */
  172. userLogout() {
  173. $A.token("");
  174. $A.storage("userInfo", {});
  175. $A.triggerUserInfoListener({});
  176. if (typeof $A.app === "object") {
  177. $A.app.goForward({path: '/'}, true);
  178. } else {
  179. window.location.href = window.location.origin;
  180. }
  181. },
  182. /**
  183. * 权限是否通过
  184. * @param role
  185. * @returns {boolean}
  186. */
  187. identity(role) {
  188. let userInfo = $A.getUserInfo();
  189. let identity = userInfo.identity;
  190. let isRole = false;
  191. $A.each(identity, (index, res) => {
  192. if (res === role) {
  193. isRole = true;
  194. }
  195. });
  196. return isRole;
  197. },
  198. /**
  199. * 监听用户信息发生变化
  200. * @param listenerName 监听标识
  201. * @param callback 监听回调
  202. */
  203. setOnUserInfoListener(listenerName, callback) {
  204. if (typeof listenerName != "string") {
  205. return;
  206. }
  207. if (typeof callback === "function") {
  208. $A.__userInfoListenerObject[listenerName] = {
  209. callback: callback,
  210. }
  211. }
  212. },
  213. triggerUserInfoListener(userInfo) {
  214. let key, item;
  215. for (key in $A.__userInfoListenerObject) {
  216. if (!$A.__userInfoListenerObject.hasOwnProperty(key)) continue;
  217. item = $A.__userInfoListenerObject[key];
  218. if (typeof item.callback === "function") {
  219. item.callback(userInfo);
  220. }
  221. }
  222. },
  223. __userInfoListenerObject: {},
  224. /**
  225. * 监听任务发生变化
  226. * @param listenerName 监听标识
  227. * @param callback 监听回调
  228. * @param callSpecial 是否监听几种特殊情况
  229. */
  230. setOnTaskInfoListener(listenerName, callback, callSpecial) {
  231. if (typeof listenerName != "string") {
  232. return;
  233. }
  234. if (typeof callback === "function") {
  235. $A.__taskInfoListenerObject[listenerName] = {
  236. special: callSpecial === true,
  237. callback: callback,
  238. }
  239. }
  240. },
  241. triggerTaskInfoListener(act, taskDetail) {
  242. let key, item;
  243. for (key in $A.__taskInfoListenerObject) {
  244. if (!$A.__taskInfoListenerObject.hasOwnProperty(key)) continue;
  245. item = $A.__taskInfoListenerObject[key];
  246. if (typeof item.callback === "function") {
  247. if (act.indexOf(['deleteproject', 'deletelabel', 'leveltask']) === -1 || item.special === true) {
  248. item.callback(act, taskDetail);
  249. }
  250. }
  251. }
  252. },
  253. __taskInfoListenerObject: {},
  254. });
  255. window.$A = $;
  256. })(window);