BaseUtil.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Ext.define('saas.util.BaseUtil', {
  2. statics: {
  3. /**
  4. * 打开/切换到新页签
  5. * @param xtype: view xtype
  6. * @param title: 标题
  7. * @param id: id
  8. * @param config: 绑定到view的其他配置
  9. */
  10. openTab: function (xtype, title, id, config) {
  11. var mainTab = Ext.getCmp('main-tab-panel');
  12. var panel = mainTab.query('[tabId="' + id + '"]')[0];
  13. if (!panel) {
  14. panel = Ext.create('saas.view.core.tab.Panel', {
  15. tabId: id,
  16. title: title,
  17. viewType: xtype,
  18. viewConfig: config
  19. });
  20. Ext.suspendLayouts();
  21. mainTab.setActiveTab(mainTab.add(panel));
  22. Ext.resumeLayouts(true);
  23. } else {
  24. panel.viewConfig = config;
  25. mainTab.setActiveTab(panel);
  26. }
  27. },
  28. /**
  29. * 重设tab标题
  30. */
  31. refreshTabTitle: function (id, title) {
  32. var currentTab = this.getCurrentTab();
  33. currentTab.tabId = id;
  34. currentTab.setTitle(title);
  35. },
  36. /**
  37. * 获得当前Tab
  38. */
  39. getCurrentTab: function () {
  40. var mainTab = Ext.getCmp('main-tab-panel');
  41. var currentTab = mainTab.getActiveTab();
  42. return currentTab;
  43. },
  44. /**
  45. * 显示toast提示(无需用户操作)
  46. * @param content: 内容
  47. * @param title: 标题
  48. *
  49. */
  50. showSuccessToast: function (content, title, configs) {
  51. Ext.toast(Ext.Object.merge({
  52. cls:'x-toast-success',
  53. html: content,
  54. title: title,
  55. closable: false,
  56. align: 't',
  57. autoCloseDelay:3000,
  58. autoClose:true,
  59. maxWidth: 400
  60. }, configs));
  61. },
  62. showErrorToast: function (content, title, configs) {
  63. Ext.toast(Ext.Object.merge({
  64. cls:'x-toast-error',
  65. html: content,
  66. title: title,
  67. closable: false,
  68. align: 't',
  69. autoCloseDelay:3000,
  70. autoClose:true,
  71. maxWidth: 400
  72. }, configs));
  73. },
  74. /**
  75. * 显示警告(需要选择是、否)
  76. * @param title: 标题
  77. * @param message: 内容
  78. * @return : Promise
  79. */
  80. showConfirm: function (title, message) {
  81. return new Ext.Promise(function (resolve, reject) {
  82. Ext.MessageBox.confirm(title, message, function (buttonId) {
  83. return resolve(buttonId);
  84. });
  85. })
  86. },
  87. warnMsg: function (msg, fn) {
  88. Ext.MessageBox.show({
  89. title: '提示',
  90. msg: msg,
  91. buttons: Ext.Msg.YESNO,
  92. icon: Ext.Msg.WARNING,
  93. fn: fn
  94. });
  95. },
  96. deleteWarn: function (msg, fn) {
  97. Ext.MessageBox.show({
  98. title: '提示',
  99. msg: msg || '确定要删除当前表单?',
  100. buttons: Ext.Msg.YESNO,
  101. icon: Ext.Msg.WARNING,
  102. fn: fn,
  103. renderTo: Ext.getCmp('main-tab-panel').getActiveTab().getEl()
  104. });
  105. },
  106. /**
  107. * 判断字符串是否日期字符串
  108. * 需要满足格式 yyyy-MM-dd hh:mm:ss
  109. * 或者yyyy-M-d h:m:s
  110. * @param str: 字符串
  111. * @returns Boolean
  112. */
  113. isDateString: function (str) {
  114. return (/^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/).test(str);
  115. },
  116. getCurrentUser: function () {
  117. return saas.util.State.get('session') ? saas.util.State.get('session').account : {};
  118. },
  119. /**
  120. * 发起Ajax请求
  121. * @param config: 请求参数
  122. */
  123. request: function (config) {
  124. var url = config.url,
  125. params = config.params,
  126. async = config.async || true,
  127. method = config.method || 'GET',
  128. timeout = config.timeout || 8000,
  129. defaultHeaders = {
  130. 'Access-Control-Allow-Origin': '*',
  131. "Content-Type": 'application/json;charset=UTF-8'
  132. };
  133. return new Ext.Promise(function (resolve, reject) {
  134. Ext.Ajax.request({
  135. url: url,
  136. params: params,
  137. async: async,
  138. method: method,
  139. timeout: timeout,
  140. headers: Ext.apply(defaultHeaders, config.headers),
  141. success: function (response, opts) {
  142. var res = Ext.decode(response.responseText);
  143. if (res.success) {
  144. return resolve(res);
  145. } else {
  146. console.error('server request failure with code ' + res.code + '.');
  147. console.error('failure message: ' + res.message);
  148. return reject(res);
  149. }
  150. },
  151. failure: function (response, opts) {
  152. console.error('server-side failure with status code ' + response);
  153. return reject(response);
  154. }
  155. });
  156. })
  157. },
  158. }
  159. });