base.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. define(['jquery'], function($) {
  2. // 测试
  3. /**
  4. * ajax请求函数
  5. * @param {string} type 请求类型post/get
  6. * @param {string} url 请求地址
  7. * @param {string} data 请求数据
  8. * @param {func} successFunc 成功回掉函数
  9. * @return {null}
  10. */
  11. function ajaxFunc(type, url, data, successFunc) {
  12. console.log('chen',data);
  13. $.ajax({
  14. type: type,
  15. dataType: 'json',
  16. contentType : 'application/json;charset=utf-8',
  17. url: config.baseUrl + url,
  18. data: data,
  19. success: function(e) {
  20. successFunc(e);
  21. },
  22. error: function(e) {
  23. console.log(e);
  24. }
  25. });
  26. }
  27. /**
  28. * 渲染window窗口
  29. * @param {string} selector 窗口selector
  30. * @param {object} config 配置文件
  31. * @return {null} [description]
  32. */
  33. function renderwindow(selector, config) {
  34. var defaultConfig = {
  35. width: 600,
  36. height: 400,
  37. modal: true,
  38. closed: true,
  39. collapsible: false,
  40. minimizable: false,
  41. maximizable: false,
  42. title: '默认提示框'
  43. };
  44. var compConfig = $.extend(defaultConfig, config);
  45. $(selector).window(compConfig);
  46. }
  47. function listType(selector, url, formatter, condition) {
  48. $(selector).combobox({
  49. url: url,
  50. valueField: 'id',
  51. textField: 'name',
  52. queryParams: condition,
  53. loadFilter: function(e) {
  54. switch(formatter) {
  55. case 0:
  56. return e.data;
  57. case 1:
  58. return e.data.list;
  59. }
  60. }
  61. });
  62. }
  63. /**
  64. * ajax请求后的提示
  65. * @param {object} e 请求返回事件
  66. * @param {string} wSelector 弹出框的类
  67. * @param {string} cSelector 数据表格的类
  68. * @param {string} mode 请求的模式,是否需要关闭window需要:0,不需要:1
  69. * @return {null} [description]
  70. */
  71. function requestTip(e, wSelector, cSelector, mode) {
  72. if (e.code === 0) {
  73. $.messager.alert('提示', e.msg, 'info');
  74. if (mode === 0) {
  75. $(wSelector).window('close');
  76. }
  77. $(cSelector).datagrid('reload');
  78. } else {
  79. $.messager.alert('警告', e.msg, 'error');
  80. }
  81. }
  82. function delInfo(selector, url, wSelector) {
  83. $(selector).on('click', function(e) {
  84. e.preventDefault();
  85. var id = $(this).data('id');
  86. $.messager.confirm('提示', '确认删除', function(e) {
  87. if (e) {
  88. base.ajaxFunc('get', url, {id:id},function(e) {
  89. base.requestTip(e, 'wSelector', wSelector, 1);
  90. });
  91. }
  92. });
  93. });
  94. }
  95. //切换选中样式
  96. function switchActive(selector) {
  97. $(selector).on('click', function(e) {
  98. e.preventDefault();
  99. $(this).addClass('active').siblings().removeClass('active');
  100. var href = $(this).data('href');
  101. $('.bif-con').load(href, function(e) {
  102. });
  103. });
  104. }
  105. return {
  106. ajaxFunc: ajaxFunc,
  107. renderwindow: renderwindow,
  108. listType: listType,
  109. requestTip: requestTip,
  110. delInfo: delInfo,
  111. switchActive: switchActive
  112. };
  113. });