config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'pay/config/index' + location.search,
  8. add_url: 'pay/config/add',
  9. edit_url: 'pay/config/edit',
  10. del_url: 'pay/config/del',
  11. multi_url: 'pay/config/multi',
  12. import_url: 'pay/config/import',
  13. table: 'shop_pay_config',
  14. }
  15. });
  16. var table = $("#table");
  17. // 初始化表格
  18. table.bootstrapTable({
  19. url: $.fn.bootstrapTable.defaults.extend.index_url,
  20. pk: 'id',
  21. sortName: 'id',
  22. columns: [
  23. [
  24. {checkbox: true},
  25. {field: 'id', title: __('Id')},
  26. {field: 'name', title: __('Name'), table: table, class: 'autocontent', formatter: Table.api.formatter.content},
  27. {field: 'type_text', title: __('Type'),searchList: Controller.api.parseConfigJson('methodList')},
  28. // {field: 'params', title: __('Params'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
  29. {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
  30. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  31. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  32. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  33. ]
  34. ]
  35. });
  36. // 为表格绑定事件
  37. Table.api.bindevent(table);
  38. },
  39. recyclebin: function () {
  40. // 初始化表格参数配置
  41. Table.api.init({
  42. extend: {
  43. 'dragsort_url': ''
  44. }
  45. });
  46. var table = $("#table");
  47. // 初始化表格
  48. table.bootstrapTable({
  49. url: 'pay/config/recyclebin' + location.search,
  50. pk: 'id',
  51. sortName: 'id',
  52. columns: [
  53. [
  54. {checkbox: true},
  55. {field: 'id', title: __('Id')},
  56. {field: 'name', title: __('Name'), align: 'left'},
  57. {
  58. field: 'deletetime',
  59. title: __('Deletetime'),
  60. operate: 'RANGE',
  61. addclass: 'datetimerange',
  62. formatter: Table.api.formatter.datetime
  63. },
  64. {
  65. field: 'operate',
  66. width: '140px',
  67. title: __('Operate'),
  68. table: table,
  69. events: Table.api.events.operate,
  70. buttons: [
  71. {
  72. name: 'Restore',
  73. text: __('Restore'),
  74. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  75. icon: 'fa fa-rotate-left',
  76. url: 'pay/config/restore',
  77. refresh: true
  78. },
  79. {
  80. name: 'Destroy',
  81. text: __('Destroy'),
  82. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  83. icon: 'fa fa-times',
  84. url: 'pay/config/destroy',
  85. refresh: true
  86. }
  87. ],
  88. formatter: Table.api.formatter.operate
  89. }
  90. ]
  91. ]
  92. });
  93. // 为表格绑定事件
  94. Table.api.bindevent(table);
  95. },
  96. add: function () {
  97. Controller.api.bindevent();
  98. Controller.api.initFormFieldsVisibility();
  99. },
  100. edit: function () {
  101. Controller.api.bindevent();
  102. Controller.api.initFormFieldsVisibility();
  103. },
  104. api: {
  105. // 解析Config中的JSON字符串的辅助函数
  106. parseConfigJson: function(configKey, defaultValue) {
  107. var configValue = Config[configKey] || defaultValue || {};
  108. // 如果是字符串,尝试解析JSON
  109. if (typeof configValue === 'string') {
  110. try {
  111. return JSON.parse(configValue);
  112. } catch (e) {
  113. return defaultValue || {};
  114. }
  115. }
  116. return configValue;
  117. },
  118. bindevent: function () {
  119. Form.api.bindevent($("form[role=form]"));
  120. // 监听支付类型变化
  121. $(document).on('change', 'input[name="row[type]"]', function() {
  122. Controller.api.toggleFieldsVisibility();
  123. });
  124. // 监听商户类型变化
  125. $(document).on('change', 'input[name="row[params][mode]"]', function() {
  126. Controller.api.toggleFieldsVisibility();
  127. });
  128. },
  129. // 初始化字段显示状态
  130. initFormFieldsVisibility: function() {
  131. // 延迟执行,确保DOM已加载
  132. setTimeout(function() {
  133. console.log('初始化字段显示状态...');
  134. Controller.api.toggleFieldsVisibility();
  135. }, 100);
  136. },
  137. // 切换字段显示状态
  138. toggleFieldsVisibility: function() {
  139. var payType = $('input[name="row[type]"]:checked').val();
  140. var mode = $('input[name="row[params][mode]"]:checked').val();
  141. console.log('支付类型:', payType, '商户模式:', mode);
  142. // 隐藏所有条件字段并移除验证
  143. $('[data-visible]').hide().find('input[data-rule], textarea[data-rule]').each(function() {
  144. $(this).attr('data-rule-backup', $(this).attr('data-rule') || '');
  145. $(this).removeAttr('data-rule');
  146. });
  147. // 根据支付类型显示相应区域
  148. if (payType === 'wechat') {
  149. $('[data-visible="type=wechat"]').show().find('input[data-rule-backup], textarea[data-rule-backup]').each(function() {
  150. var rule = $(this).attr('data-rule-backup');
  151. if (rule) {
  152. $(this).attr('data-rule', rule);
  153. }
  154. });
  155. // 根据商户模式显示字段
  156. if (mode === '2') {
  157. // 服务商模式:显示主商户AppId和子商户相关字段
  158. $('[data-visible="type=wechat&mode=2"]').show().find('input[data-rule-backup], textarea[data-rule-backup]').each(function() {
  159. var rule = $(this).attr('data-rule-backup');
  160. if (rule) {
  161. $(this).attr('data-rule', rule);
  162. }
  163. });
  164. }
  165. } else if (payType === 'alipay') {
  166. $('[data-visible="type=alipay"]').show().find('input[data-rule-backup], textarea[data-rule-backup]').each(function() {
  167. var rule = $(this).attr('data-rule-backup');
  168. if (rule) {
  169. $(this).attr('data-rule', rule);
  170. }
  171. });
  172. // 根据商户模式显示字段
  173. if (mode === '2') {
  174. // 服务商模式:显示主商户ID
  175. $('[data-visible="type=alipay&mode=2"]').show().find('input[data-rule-backup], textarea[data-rule-backup]').each(function() {
  176. var rule = $(this).attr('data-rule-backup');
  177. if (rule) {
  178. $(this).attr('data-rule', rule);
  179. }
  180. });
  181. }
  182. } else if (payType === 'douyin') {
  183. $('[data-visible="type=douyin"]').show().find('input[data-rule-backup], textarea[data-rule-backup]').each(function() {
  184. var rule = $(this).attr('data-rule-backup');
  185. if (rule) {
  186. $(this).attr('data-rule', rule);
  187. }
  188. });
  189. }
  190. // 处理标签显示
  191. Controller.api.toggleLabelVisibility(payType, mode);
  192. // 重新初始化表单验证
  193. setTimeout(function() {
  194. if (typeof Form !== 'undefined' && Form.api && Form.api.bindevent) {
  195. Form.api.bindevent($("form[role=form]"));
  196. }
  197. }, 100);
  198. },
  199. // 切换标签显示
  200. toggleLabelVisibility: function(payType, mode) {
  201. // 先隐藏所有条件标签
  202. $('[data-visible] span[data-visible]').hide();
  203. // 显示匹配的标签
  204. if (payType) {
  205. $('span[data-visible="type=' + payType + '"]').show();
  206. }
  207. if (mode !== undefined && mode !== '') {
  208. $('span[data-visible="mode=' + mode + '"]').show();
  209. }
  210. },
  211. }
  212. };
  213. return Controller;
  214. });