agent.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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: 'commission/agent/index' + location.search,
  8. add_url: '',
  9. multi_url: 'commission/agent/multi',
  10. import_url: 'commission/agent/import',
  11. table: 'commission_agent',
  12. }
  13. });
  14. var table = $("#table");
  15. // 初始化表格
  16. table.bootstrapTable({
  17. url: $.fn.bootstrapTable.defaults.extend.index_url,
  18. pk: 'user_id',
  19. sortName: 'user_id',
  20. fixedColumns: true, // 启用固定列功能
  21. fixedRightNumber: 1, // 固定右侧最后1列(操作列)
  22. columns: [
  23. [
  24. {checkbox: true},
  25. {field: 'user_id', title: __('ID'), width: 90},
  26. {field: 'user.nickname', title: __('分销商信息'), width: 200, operate: 'LIKE', formatter: function(value, row, index) {
  27. var avatarUrl = row.user && row.user.avatar ? Fast.api.cdnurl(row.user.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
  28. var nickname = row.user && row.user.nickname ? row.user.nickname : 'yimizi';
  29. var mobile = row.user && row.user.mobile ? row.user.mobile : '';
  30. var html = '<div style="display:flex;align-items:center;">';
  31. html += '<img src="' + avatarUrl + '" style="width:40px;height:40px;border-radius:50%;margin-right:10px;" />';
  32. html += '<div>';
  33. html += '<div style="color:#337ab7;font-weight:bold;">' + nickname + '</div>';
  34. if (mobile) {
  35. html += '<div style="color:#6c757d;font-size:12px;">' + mobile + '</div>';
  36. }
  37. html += '</div></div>';
  38. return html;
  39. }},
  40. {field: 'level_info.name', title: __('等级'), width: 120, formatter: function(value, row, index) {
  41. if (row.level_info) {
  42. var html = '<div>' + row.level_info.name + '</div>';
  43. html += '<div style="color:#999;font-size:12px;">(等级' + row.level_info.level + ')</div>';
  44. return html;
  45. }
  46. return '默认等级<br><small>(等级' + (row.level || 1) + ')</small>';
  47. }},
  48. {field: 'total_income', title: __('累计佣金'), width: 120, operate: 'BETWEEN', formatter: function(value, row, index) {
  49. return (row.total_income || 0) + '元';
  50. }},
  51. {field: 'user.total_consume', title: __('消费金额'), width: 120, formatter: function(value, row, index) {
  52. return ((row.user && row.user.total_consume) || 0) + '元';
  53. }},
  54. {field: 'child_order_money_0', title: __('自购分销业绩'), width: 140, formatter: function(value, row, index) {
  55. var orderCount = row.child_order_count_first;
  56. var orderMoney = row.child_order_money_first;
  57. var html = '<div>' + orderCount + '单</div>';
  58. html += '<div style="color:#999;font-size:12px;">' + orderMoney + '元</div>';
  59. return html;
  60. }},
  61. {field: 'user.parent_user.nickname', title: __('上级分销商'), width: 150, formatter: function(value, row, index) {
  62. if (row.user && row.user.parent_user) {
  63. var parentAvatar = row.user.parent_user.avatar ? Fast.api.cdnurl(row.user.parent_user.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
  64. var parentNickname = row.user.parent_user.nickname || ('用户' + row.user.parent_user_id);
  65. var html = '<div style="display:flex;align-items:center;justify-content:center;">';
  66. html += '<img src="' + parentAvatar + '" style="width:30px;height:30px;border-radius:50%;margin-right:8px;" />';
  67. html += '<div style="text-align:left;">';
  68. html += '<div style="font-size:12px;">' + parentNickname + '</div>';
  69. html += '</div></div>';
  70. return html;
  71. }
  72. return '<div style="text-align:center;color:#999;">平台直推</div>';
  73. }},
  74. {field: 'child_order_money_second', title: __('一级用户人数'), width: 120, formatter: function(value, row, index) {
  75. var count = row.child_user_count_first;
  76. return count + '人';
  77. }},
  78. {field: 'child_agent_count_1', title: __('一级分销商人数'), width: 160, formatter: function(value, row, index) {
  79. var count = row.child_agent_count_first;
  80. return '<div>' + count + '人</div>';
  81. }},
  82. {field: 'become_time', title: __('创建时间'), width: 172, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
  83. {field: 'status', title: __('状态'), searchList: {
  84. "normal": __('正常'),
  85. "pending": __('审核中'),
  86. "freeze": __('冻结'),
  87. "forbidden": __('禁用'),
  88. "reject": __('拒绝')
  89. }, formatter: function(value, row, index) {
  90. var colorMap = {
  91. 'normal': 'success',
  92. 'pending': 'warning',
  93. 'freeze': 'info',
  94. 'forbidden': 'danger',
  95. 'reject': 'danger'
  96. };
  97. var textMap = {
  98. 'normal': '正常',
  99. 'pending': '审核中',
  100. 'freeze': '冻结',
  101. 'forbidden': '禁用',
  102. 'reject': '拒绝'
  103. };
  104. var color = colorMap[value] || 'default';
  105. var text = textMap[value] || value;
  106. return '<span class="label label-' + color + '">' + text + '</span>';
  107. }},
  108. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
  109. buttons: [
  110. {
  111. name: 'detail',
  112. text: __('详情'),
  113. title: __('详情'),
  114. classname: 'btn btn-xs btn-primary btn-dialog',
  115. icon: 'fa fa-list',
  116. url: 'commission/agent/detail',
  117. callback: function (data) {
  118. table.bootstrapTable('refresh');
  119. }
  120. },
  121. {
  122. name: 'team',
  123. text: __('团队'),
  124. title: __('团队'),
  125. classname: 'btn btn-xs btn-info btn-dialog',
  126. icon: 'fa fa-users',
  127. url: 'commission/agent/team',
  128. callback: function (data) {
  129. table.bootstrapTable('refresh');
  130. }
  131. },
  132. ],
  133. formatter: Table.api.formatter.operate}
  134. ]
  135. ]
  136. });
  137. // 为表格绑定事件
  138. Table.api.bindevent(table);
  139. },
  140. detail: function () {
  141. var agentId = Fast.api.query('id');
  142. var agentData = {};
  143. // 获取分销商详情
  144. function getDetail() {
  145. Fast.api.ajax({
  146. url: 'commission/agent/detail',
  147. data: {id: agentId}
  148. }, function(data) {
  149. agentData = data;
  150. updateDetailView(data);
  151. });
  152. }
  153. // 更新详情视图
  154. function updateDetailView(data) {
  155. if (data.user) {
  156. $('#user-avatar').attr('src', data.user.avatar || '/assets/img/avatar.png');
  157. $('#user-nickname').text(data.user.nickname || data.user_id);
  158. }
  159. // 状态
  160. var statusMap = {
  161. 'normal': {text: '正常', class: 'label-success'},
  162. 'pending': {text: '审核中', class: 'label-warning'},
  163. 'freeze': {text: '冻结', class: 'label-info'},
  164. 'forbidden': {text: '禁用', class: 'label-danger'},
  165. 'reject': {text: '拒绝', class: 'label-danger'}
  166. };
  167. var status = statusMap[data.status] || {text: data.status, class: 'label-default'};
  168. $('#agent-status').text(status.text).attr('class', 'label ' + status.class);
  169. // 等级
  170. if (data.level_info) {
  171. $('#agent-level').text(data.level_info.name + '(等级' + data.level_info.level + ')');
  172. if (data.level_info.commission_rules) {
  173. $('#commission-1').text(data.level_info.commission_rules.commission_1 || '0.00');
  174. $('#commission-2').text(data.level_info.commission_rules.commission_2 || '0.00');
  175. $('#commission-3').text(data.level_info.commission_rules.commission_3 || '0.00');
  176. }
  177. } else {
  178. $('#agent-level').text(data.level);
  179. }
  180. // 待升级等级
  181. if (data.level_status > 0 && data.level_status_info) {
  182. $('#upgrade-level-group').show();
  183. $('#upgrade-level').text(data.level_status_info.name + '(等级' + data.level_status + ')');
  184. } else {
  185. $('#upgrade-level-group').hide();
  186. }
  187. // 上级分销商
  188. if (data.user && data.user.parent_user) {
  189. $('#parent-agent').text(data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
  190. } else {
  191. $('#parent-agent').text('无');
  192. }
  193. // 允许升级
  194. $('#upgrade-lock').prop('checked', data.upgrade_lock == 0);
  195. // 统计数据
  196. $('#child-user-count-all').text(data.child_user_count_all || 0);
  197. $('#child-user-count-1').text(data.child_user_count_1 || 0);
  198. $('#child-agent-count-all').text(data.child_agent_count_all || 0);
  199. $('#child-agent-count-1').text(data.child_agent_count_1 || 0);
  200. $('#child-order-money-all').text((data.child_order_money_all || 0) + '元');
  201. $('#child-order-count-all').text(data.child_order_count_all || 0);
  202. $('#child-order-money-1').text((data.child_order_money_1 || 0) + '元');
  203. $('#child-order-count-1').text(data.child_order_count_1 || 0);
  204. $('#total-income').text((data.total_income || 0) + '元');
  205. $('#pending-reward').text((data.pending_reward || 0) + '元');
  206. $('#total-consume').text(((data.user && data.user.total_consume) || 0) + '元');
  207. // 申请信息
  208. if (data.apply_info && data.apply_info.length > 0) {
  209. var applyHtml = '<form class="form-horizontal">';
  210. data.apply_info.forEach(function(item) {
  211. applyHtml += '<div class="form-group">';
  212. applyHtml += '<label class="col-sm-3 control-label">' + item.name + ':</label>';
  213. applyHtml += '<div class="col-sm-9">';
  214. if (item.type === 'image') {
  215. applyHtml += '<img src="' + item.value + '" style="max-width: 200px; max-height: 120px;">';
  216. } else {
  217. applyHtml += '<p class="form-control-static">' + item.value + '</p>';
  218. }
  219. applyHtml += '</div></div>';
  220. });
  221. applyHtml += '</form>';
  222. $('#apply-info').html(applyHtml);
  223. } else {
  224. $('#apply-info').html('<p class="text-muted">暂无申请信息</p>');
  225. }
  226. }
  227. // 详情页面特有的全局函数
  228. // 修改状态事件委托
  229. $(document).on('click', '.btn-edit-status', function () {
  230. var userId = $(this).data('user-id');
  231. var currentStatus = $(this).data('current-status') || 'normal';
  232. var html = Template('statusedittml', {
  233. currentStatus: currentStatus,
  234. userId: userId
  235. });
  236. layer.open({
  237. type: 1,
  238. skin: 'layui-layer-demo',
  239. title: '修改分销商状态',
  240. anim: 2,
  241. area: ['400px', '280px'],
  242. shadeClose: 1,
  243. content: html,
  244. btn: ['确定', '取消'],
  245. zIndex: 999999,
  246. yes: function (index) {
  247. var newStatus = $('input[name="status"]:checked').val();
  248. if (!newStatus) {
  249. Toastr.error('请选择状态');
  250. return false;
  251. }
  252. // 发送AJAX请求
  253. Fast.api.ajax({
  254. url: 'commission/agent/edit',
  255. data: {
  256. ids: $('#status_user_id').val(),
  257. status: newStatus
  258. }
  259. }, function(data, ret) {
  260. window.location.reload();
  261. Toastr.success('修改成功');
  262. layer.close(index);
  263. });
  264. },
  265. success: function (layero, index) {
  266. // 使用FastAdmin标准方式绑定表单事件
  267. Form.api.bindevent(layero.find("form"));
  268. }
  269. });
  270. });
  271. // 修改到期时间 - 使用事件委托
  272. $(document).on('click', '.btn-edit-expire-time', function () {
  273. var userId = $(this).data('user-id');
  274. var expireTime = $(this).data('expire-time');
  275. // 将时间戳转换为日期字符串
  276. var currentTime = '';
  277. if (expireTime && expireTime != '') {
  278. var date = new Date(expireTime * 1000);
  279. currentTime = date.getFullYear() + '-' +
  280. String(date.getMonth() + 1).padStart(2, '0') + '-' +
  281. String(date.getDate()).padStart(2, '0') + ' ' +
  282. String(date.getHours()).padStart(2, '0') + ':' +
  283. String(date.getMinutes()).padStart(2, '0') + ':' +
  284. String(date.getSeconds()).padStart(2, '0');
  285. }
  286. // 使用Template渲染HTML
  287. var html = Template('expiretimetpl', {
  288. currentTime: currentTime,
  289. userId: userId
  290. });
  291. layer.open({
  292. type: 1,
  293. skin: 'layui-layer-demo',
  294. title: '修改分销商到期时间',
  295. anim: 2,
  296. area: ['500px', '500px'],
  297. shadeClose: 1,
  298. content: html,
  299. btn: ['确定', '取消'],
  300. zIndex: 999999,
  301. yes: function (index) {
  302. var newTime = ($('#expire_time').val()).trim();
  303. // 验证时间格式
  304. if (newTime && !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(newTime)) {
  305. Toastr.error('时间格式不正确,请使用格式: 2024-01-01 00:00:00');
  306. return;
  307. }
  308. var userId = $('#user_id').val();
  309. Fast.api.ajax({
  310. url: 'commission/agent/updateExpireTime',
  311. data: {
  312. user_id: userId,
  313. expire_time: newTime
  314. }
  315. }, function(data, ret) {
  316. window.location.reload();
  317. Toastr.success('修改成功');
  318. layer.close(index);
  319. });
  320. },
  321. success: function (layero, index) {
  322. // 使用FastAdmin标准方式绑定表单事件
  323. Form.api.bindevent(layero.find("form"));
  324. }
  325. });
  326. });
  327. window.changeLevel = function(userId) {
  328. Toastr.info('等级修改功能待完善');
  329. };
  330. window.changeParentUser = function(userId) {
  331. Fast.api.open('commission/agent/select?id=' + userId, '更换上级分销商', {
  332. callback: function() {
  333. location.reload();
  334. }
  335. });
  336. };
  337. window.approveLevel = function(userId, levelStatus) {
  338. Fast.api.ajax({
  339. url: 'commission/agent/edit',
  340. data: {ids: userId, level_status: levelStatus}
  341. }, function() {
  342. location.reload();
  343. });
  344. };
  345. window.rejectLevel = function(userId) {
  346. Fast.api.ajax({
  347. url: 'commission/agent/edit',
  348. data: {ids: userId, level_status: 0}
  349. }, function() {
  350. location.reload();
  351. });
  352. };
  353. window.toggleUpgradeLock = function(userId, currentLock) {
  354. var newLock = currentLock == 0 ? 1 : 0;
  355. Fast.api.ajax({
  356. url: 'commission/agent/edit',
  357. data: {ids: userId, upgrade_lock: newLock}
  358. }, function() {
  359. location.reload();
  360. });
  361. };
  362. window.viewTeam = function(userId) {
  363. Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
  364. };
  365. // 事件绑定
  366. $(document).ready(function() {
  367. $('.btn-refresh').click(function() {
  368. location.reload();
  369. });
  370. $('#refresh-detail').click(function() {
  371. getDetail();
  372. });
  373. $('#edit-status').click(function() {
  374. Layer.prompt({
  375. title: '修改状态',
  376. formType: 2,
  377. value: agentData.status,
  378. select: ['normal', 'pending', 'freeze', 'forbidden', 'reject'],
  379. selectTips: ['正常', '审核中', '冻结', '禁用', '拒绝']
  380. }, function(value, index) {
  381. Fast.api.ajax({
  382. url: 'commission/agent/edit',
  383. data: {ids: agentId, status: value}
  384. }, function() {
  385. Layer.close(index);
  386. getDetail();
  387. });
  388. });
  389. });
  390. $('#change-parent').click(function() {
  391. Fast.api.open('commission/agent/select?id=' + agentId, '更换上级分销商', {
  392. callback: function() {
  393. getDetail();
  394. }
  395. });
  396. });
  397. $('#upgrade-lock').change(function() {
  398. var value = $(this).is(':checked') ? 0 : 1;
  399. Fast.api.ajax({
  400. url: 'commission/agent/edit',
  401. data: {ids: agentId, upgrade_lock: value}
  402. }, function() {
  403. getDetail();
  404. });
  405. });
  406. $('#approve-level').click(function() {
  407. Fast.api.ajax({
  408. url: 'commission/agent/edit',
  409. data: {ids: agentId, level_status: agentData.level_status}
  410. }, function() {
  411. getDetail();
  412. });
  413. });
  414. $('#reject-level').click(function() {
  415. Fast.api.ajax({
  416. url: 'commission/agent/edit',
  417. data: {ids: agentId, level_status: 0}
  418. }, function() {
  419. getDetail();
  420. });
  421. });
  422. // 初始化
  423. if (agentId) {
  424. getDetail();
  425. }
  426. });
  427. Controller.api.bindevent();
  428. },
  429. select: function () {
  430. var userId = Fast.api.query('id');
  431. var selectedParentId = null;
  432. var currentPage = 1;
  433. var pageSize = 10;
  434. // 获取当前用户信息
  435. function getCurrentUserInfo() {
  436. Fast.api.ajax({
  437. url: 'commission/agent/detail',
  438. data: {id: userId}
  439. }, function(data) {
  440. var html = '<label>当前推荐人:</label>';
  441. if (data.user && data.user.parent_user) {
  442. html += '<img src="' + (data.user.parent_user.avatar || '/assets/img/avatar.png') + '" ';
  443. html += 'style="width: 20px; height: 20px; border-radius: 50%; margin-right: 5px;">';
  444. html += (data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
  445. selectedParentId = data.user.parent_user_id;
  446. } else {
  447. html += '无';
  448. selectedParentId = 0;
  449. }
  450. $('#current-parent').html(html);
  451. });
  452. }
  453. // 获取分销商列表
  454. function getAgentList() {
  455. var searchType = $('#search-type').val();
  456. var searchValue = $('#search-value').val();
  457. var searchData = {
  458. page: currentPage,
  459. limit: pageSize
  460. };
  461. if (searchValue) {
  462. searchData[searchType] = searchValue;
  463. }
  464. Fast.api.ajax({
  465. url: 'commission/agent/select',
  466. data: searchData
  467. }, function(ret) {
  468. var html = '';
  469. if (ret.data.rows && ret.data.rows.length > 0) {
  470. ret.data.rows.forEach(function(item) {
  471. var statusClass = '';
  472. var statusText = '';
  473. switch(item.status) {
  474. case 'normal':
  475. statusClass = 'label-success';
  476. statusText = '正常';
  477. break;
  478. case 'pending':
  479. statusClass = 'label-warning';
  480. statusText = '审核中';
  481. break;
  482. case 'freeze':
  483. statusClass = 'label-info';
  484. statusText = '冻结';
  485. break;
  486. case 'forbidden':
  487. statusClass = 'label-danger';
  488. statusText = '禁用';
  489. break;
  490. case 'reject':
  491. statusClass = 'label-danger';
  492. statusText = '拒绝';
  493. break;
  494. default:
  495. statusClass = 'label-default';
  496. statusText = item.status;
  497. }
  498. html += '<tr' + (selectedParentId == item.user_id ? ' class="warning"' : '') + '>';
  499. html += '<td><input type="radio" name="parent_agent" value="' + item.user_id + '"' + (selectedParentId == item.user_id ? ' checked' : '') + '></td>';
  500. html += '<td>' + item.user_id + '</td>';
  501. html += '<td>';
  502. html += '<img src="' + (item.user.avatar || '/assets/img/avatar.png') + '" style="width: 32px; height: 32px; border-radius: 50%; margin-right: 5px;">';
  503. html += (item.user.nickname || '用户' + item.user_id);
  504. html += '</td>';
  505. html += '<td>';
  506. if (item.level_info) {
  507. html += item.level_info.name + '<br><small>等级' + item.level_info.level + '</small>';
  508. } else {
  509. html += '等级' + item.level;
  510. }
  511. html += '</td>';
  512. html += '<td>' + (item.user.mobile || '-') + '</td>';
  513. html += '<td><span class="label ' + statusClass + '">' + statusText + '</span></td>';
  514. html += '<td>';
  515. if (selectedParentId == item.user_id) {
  516. html += '<span class="text-success">已选择</span>';
  517. } else {
  518. html += '<button type="button" class="btn btn-xs btn-primary" onclick="selectAgent(' + item.user_id + ')">选择</button>';
  519. }
  520. html += '</td>';
  521. html += '</tr>';
  522. });
  523. } else {
  524. html = '<tr><td colspan="7" class="text-center">暂无数据</td></tr>';
  525. }
  526. $('#agent-list').html(html);
  527. // 更新分页
  528. updatePagination(ret.data.total);
  529. });
  530. }
  531. // 更新分页
  532. function updatePagination(total) {
  533. var totalPages = Math.ceil(total / pageSize);
  534. var html = '<ul class="pagination">';
  535. // 上一页
  536. if (currentPage > 1) {
  537. html += '<li><a href="javascript:;" onclick="changePage(' + (currentPage - 1) + ')">上一页</a></li>';
  538. }
  539. // 页码
  540. for (var i = 1; i <= totalPages; i++) {
  541. if (i == currentPage) {
  542. html += '<li class="active"><a href="javascript:;">' + i + '</a></li>';
  543. } else {
  544. html += '<li><a href="javascript:;" onclick="changePage(' + i + ')">' + i + '</a></li>';
  545. }
  546. }
  547. // 下一页
  548. if (currentPage < totalPages) {
  549. html += '<li><a href="javascript:;" onclick="changePage(' + (currentPage + 1) + ')">下一页</a></li>';
  550. }
  551. html += '</ul>';
  552. $('#pagination').html(html);
  553. }
  554. // 选择页面特有的全局函数
  555. window.searchAgent = function() {
  556. currentPage = 1;
  557. getAgentList();
  558. };
  559. window.selectAgent = function(agentId) {
  560. selectedParentId = agentId;
  561. $('input[name="parent_agent"]').prop('checked', false);
  562. $('input[value="' + agentId + '"]').prop('checked', true);
  563. $('#agent-table tbody tr').removeClass('warning');
  564. $('input[value="' + agentId + '"]').closest('tr').addClass('warning');
  565. $('#set-platform').prop('checked', false);
  566. };
  567. window.changePage = function(page) {
  568. currentPage = page;
  569. getAgentList();
  570. };
  571. window.confirmChange = function() {
  572. if (selectedParentId === null) {
  573. Toastr.error('请选择上级分销商');
  574. return;
  575. }
  576. Fast.api.ajax({
  577. url: 'commission/agent/changeParentUser',
  578. data: {
  579. id: userId,
  580. parent_user_id: selectedParentId
  581. }
  582. }, function() {
  583. Toastr.success('更换成功');
  584. Fast.api.close();
  585. });
  586. };
  587. // 事件绑定
  588. $(document).ready(function() {
  589. // 设为平台直推
  590. $('#set-platform').change(function() {
  591. if ($(this).is(':checked')) {
  592. selectedParentId = 0;
  593. $('input[name="parent_agent"]').prop('checked', false);
  594. $('#agent-table tbody tr').removeClass('warning');
  595. }
  596. });
  597. // 搜索框回车事件
  598. $('#search-value').keypress(function(e) {
  599. if (e.which == 13) {
  600. searchAgent();
  601. }
  602. });
  603. // 初始化
  604. getCurrentUserInfo();
  605. getAgentList();
  606. });
  607. },
  608. team: function () {
  609. // 优先从Config中获取ID,否则从URL参数获取
  610. var agentId = Config.current_agent_id || Fast.api.query('id');
  611. console.log('agentId:', agentId);
  612. // 初始化表格参数
  613. Table.api.init({
  614. extend: {
  615. index_url: 'commission/agent/team?id=' + agentId,
  616. add_url: '',
  617. edit_url: '',
  618. del_url: '',
  619. multi_url: '',
  620. import_url: '',
  621. table: 'user',
  622. }
  623. });
  624. var table = $("#table");
  625. // 初始化表格
  626. table.bootstrapTable({
  627. url: $.fn.bootstrapTable.defaults.extend.index_url,
  628. pk: 'id',
  629. sortName: 'id',
  630. columns: [
  631. [
  632. {checkbox: true},
  633. {field: 'id', title: __('ID'), width: 90},
  634. {field: 'nickname', title: __('团队用户'), width: 150, operate: 'LIKE', formatter: function(value, row, index) {
  635. var avatarUrl = row.avatar ? Fast.api.cdnurl(row.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
  636. var html = '<div style="display:flex;align-items:center;cursor:pointer;" onclick="viewTeam(' + row.id + ')">';
  637. html += '<img src="' + avatarUrl + '" style="width:40px;height:40px;border-radius:50%;margin-right:10px;" />';
  638. html += '<div>';
  639. html += '<div style="color:#337ab7;font-weight:bold;">' + (row.nickname || '用户' + row.id) + '</div>';
  640. html += '<div style="color:#6c757d;font-size:12px;">' + (row.mobile || '') + '</div>';
  641. html += '</div></div>';
  642. return html;
  643. }},
  644. {field: 'agent.level_info.name', title: __('等级'), width: 150, formatter: function(value, row, index) {
  645. if (row.agent && row.agent.level_info) {
  646. var html = '<div style="display:flex;align-items:center;">';
  647. if (row.agent.level_info.image) {
  648. html += '<img src="' + Fast.api.cdnurl(row.agent.level_info.image) + '" style="width:32px;height:32px;margin-right:8px;" />';
  649. }
  650. html += '<span>' + row.agent.level_info.name + '</span>';
  651. html += '</div>';
  652. return html;
  653. } else if (row.agent) {
  654. return '等级' + (row.agent.level || '-');
  655. }
  656. return '<span class="text-muted">非分销商</span>';
  657. }},
  658. {field: 'agent.status', title: __('状态'), width: 120, align: 'center', searchList: {
  659. "normal": __('正常'),
  660. "pending": __('审核中'),
  661. "freeze": __('冻结'),
  662. "forbidden": __('禁用'),
  663. "reject": __('拒绝')
  664. }, formatter: function(value, row, index) {
  665. if (!row.agent) {
  666. return '<span style="color:#777;">非分销商</span>';
  667. }
  668. var status = row.agent.status;
  669. var colorMap = {
  670. 'normal': '#5cb85c',
  671. 'pending': '#f0ad4e',
  672. 'freeze': '#5bc0de',
  673. 'forbidden': '#d9534f',
  674. 'reject': '#d9534f'
  675. };
  676. var textMap = {
  677. 'normal': '正常',
  678. 'pending': '审核中',
  679. 'freeze': '冻结',
  680. 'forbidden': '禁用',
  681. 'reject': '拒绝'
  682. };
  683. var color = colorMap[status] || '#777';
  684. var text = textMap[status] || status;
  685. return '<span style="color:' + color + ';">' + text + '</span>';
  686. }},
  687. {field: 'agent.child_user_count_all', title: __('团队人数/分销商人数'), width: 160, align: 'center', formatter: function(value, row, index) {
  688. if (!row.agent) return '0人/0人';
  689. var userCount = row.agent.child_user_count_all || 0;
  690. var agentCount = row.agent.child_agent_count_all || 0;
  691. return userCount + '人/' + agentCount + '人';
  692. }},
  693. {field: 'agent.child_user_count_first', title: __('一级团队人数/一级分销商人数'), width: 220, align: 'center', formatter: function(value, row, index) {
  694. if (!row.agent) return '0人/0人';
  695. var userCount = row.agent.child_user_count_first || 0;
  696. var agentCount = row.agent.child_agent_count_first || 0;
  697. return userCount + '人/' + agentCount + '人';
  698. }},
  699. {field: 'agent.child_user_count_second', title: __('二级团队人数/二级分销商人数'), width: 220, align: 'center', formatter: function(value, row, index) {
  700. if (!row.agent) return '0人/0人';
  701. var userCount = row.agent.child_user_count_second || 0;
  702. var agentCount = row.agent.child_agent_count_second || 0;
  703. return userCount + '人/' + agentCount + '人';
  704. }},
  705. {field: 'agent.child_order_money_all', title: __('团队分销总额/团队分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
  706. if (!row.agent) return '0元/0单';
  707. var money = row.agent.child_order_money_all || 0;
  708. var count = row.agent.child_order_count_all || 0;
  709. return money + '元/' + count + '单';
  710. }},
  711. {field: 'agent.child_order_money_first', title: __('一级分销总额/一级分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
  712. if (!row.agent) return '0元/0单';
  713. var money = row.agent.child_order_money_first || 0;
  714. var count = row.agent.child_order_count_first || 0;
  715. return money + '元/' + count + '单';
  716. }},
  717. {field: 'agent.child_order_money_second', title: __('二级分销总额/二级分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
  718. if (!row.agent) return '0元/0单';
  719. var money = row.agent.child_order_money_second || 0;
  720. var count = row.agent.child_order_count_second || 0;
  721. return money + '元/' + count + '单';
  722. }},
  723. {field: 'agent.child_order_money_0', title: __('自购分销总金额/订单数'), width: 220, align: 'center', formatter: function(value, row, index) {
  724. if (!row.agent) return '0元/0单';
  725. // 自购数据可能存储在其他字段中,这里使用0级别或者特定字段
  726. var money = row.agent.self_order_money || 0;
  727. var count = row.agent.self_order_count || 0;
  728. return money + '元/' + count + '单';
  729. }},
  730. {field: 'agent.total_income', title: __('累计佣金'), width: 160, align: 'center', operate: 'BETWEEN', formatter: function(value, row, index) {
  731. return row.agent ? ((row.agent.total_income || 0) + '元') : '0元';
  732. }},
  733. {field: 'total_consume', title: __('消费金额'), width: 160, align: 'center', formatter: function(value, row, index) {
  734. return ((row.total_consume) || 0) + '元';
  735. }},
  736. {field: 'agent.pending_reward', title: __('待入账佣金'), width: 160, align: 'center', formatter: function(value, row, index) {
  737. return row.agent ? ((row.agent.pending_reward || 0) + '元') : '0元';
  738. }},
  739. {field: 'createtime', title: __('加入时间'), width: 172, align: 'center', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime}
  740. ]
  741. ]
  742. });
  743. // 为表格绑定事件
  744. Table.api.bindevent(table);
  745. // 团队页面特有的全局函数
  746. window.viewTeam = function(userId) {
  747. Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
  748. };
  749. window.viewDetail = function(userId) {
  750. Fast.api.open('commission/agent/detail?id=' + userId, '分销商详情');
  751. };
  752. Controller.api.bindevent();
  753. },
  754. api: {
  755. bindevent: function () {
  756. Form.api.bindevent($("form[role=form]"));
  757. }
  758. }
  759. };
  760. return Controller;
  761. });