agent.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. columns: [
  21. [
  22. {checkbox: true},
  23. {field: 'user_id', title: __('ID'), width: 60},
  24. {field: 'user.nickname', title: __('用户昵称'), operate: 'LIKE'},
  25. {field: 'user.mobile', title: __('手机号'), operate: 'LIKE'},
  26. {field: 'level_info.name', title: __('分销等级'), formatter: function(value, row, index) {
  27. if (row.level_info) {
  28. return row.level_info.name + '(等级' + row.level_info.level + ')';
  29. }
  30. return row.level || '-';
  31. }},
  32. {field: 'status', title: __('状态'), searchList: {
  33. "normal": __('正常'),
  34. "pending": __('审核中'),
  35. "freeze": __('冻结'),
  36. "forbidden": __('禁用'),
  37. "reject": __('拒绝')
  38. }, formatter: function(value, row, index) {
  39. var colorMap = {
  40. 'normal': 'success',
  41. 'pending': 'warning',
  42. 'freeze': 'info',
  43. 'forbidden': 'danger',
  44. 'reject': 'danger'
  45. };
  46. var textMap = {
  47. 'normal': '正常',
  48. 'pending': '审核中',
  49. 'freeze': '冻结',
  50. 'forbidden': '禁用',
  51. 'reject': '拒绝'
  52. };
  53. var color = colorMap[value] || 'default';
  54. var text = textMap[value] || value;
  55. return '<span class="label label-' + color + '">' + text + '</span>';
  56. }},
  57. {field: 'total_income', title: __('总收益'), operate: 'BETWEEN'},
  58. {field: 'child_agent_count_1', title: __('直推分销商'), width: 80},
  59. {field: 'child_agent_count_all', title: __('团队分销商'), width: 80},
  60. {field: 'child_user_count_all', title: __('团队人数'), width: 80},
  61. {field: 'pending_reward', title: __('待结算佣金'), width: 80},
  62. {field: 'become_time', title: __('成为分销商时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
  63. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
  64. buttons: [
  65. {
  66. name: 'detail',
  67. text: __('详情'),
  68. title: __('详情'),
  69. classname: 'btn btn-xs btn-primary btn-dialog',
  70. icon: 'fa fa-list',
  71. url: 'commission/agent/detail',
  72. callback: function (data) {
  73. table.bootstrapTable('refresh');
  74. }
  75. },
  76. {
  77. name: 'team',
  78. text: __('团队'),
  79. title: __('团队'),
  80. classname: 'btn btn-xs btn-info btn-dialog',
  81. icon: 'fa fa-users',
  82. url: 'commission/agent/team',
  83. callback: function (data) {
  84. table.bootstrapTable('refresh');
  85. }
  86. },
  87. ],
  88. formatter: Table.api.formatter.operate}
  89. ]
  90. ]
  91. });
  92. // 为表格绑定事件
  93. Table.api.bindevent(table);
  94. },
  95. detail: function () {
  96. var agentId = Fast.api.query('id');
  97. var agentData = {};
  98. // 获取分销商详情
  99. function getDetail() {
  100. Fast.api.ajax({
  101. url: 'commission/agent/detail',
  102. data: {id: agentId}
  103. }, function(data) {
  104. agentData = data;
  105. updateDetailView(data);
  106. });
  107. }
  108. // 更新详情视图
  109. function updateDetailView(data) {
  110. if (data.user) {
  111. $('#user-avatar').attr('src', data.user.avatar || '/assets/img/avatar.png');
  112. $('#user-nickname').text(data.user.nickname || data.user_id);
  113. }
  114. // 状态
  115. var statusMap = {
  116. 'normal': {text: '正常', class: 'label-success'},
  117. 'pending': {text: '审核中', class: 'label-warning'},
  118. 'freeze': {text: '冻结', class: 'label-info'},
  119. 'forbidden': {text: '禁用', class: 'label-danger'},
  120. 'reject': {text: '拒绝', class: 'label-danger'}
  121. };
  122. var status = statusMap[data.status] || {text: data.status, class: 'label-default'};
  123. $('#agent-status').text(status.text).attr('class', 'label ' + status.class);
  124. // 等级
  125. if (data.level_info) {
  126. $('#agent-level').text(data.level_info.name + '(等级' + data.level_info.level + ')');
  127. if (data.level_info.commission_rules) {
  128. $('#commission-1').text(data.level_info.commission_rules.commission_1 || '0.00');
  129. $('#commission-2').text(data.level_info.commission_rules.commission_2 || '0.00');
  130. $('#commission-3').text(data.level_info.commission_rules.commission_3 || '0.00');
  131. }
  132. } else {
  133. $('#agent-level').text(data.level);
  134. }
  135. // 待升级等级
  136. if (data.level_status > 0 && data.level_status_info) {
  137. $('#upgrade-level-group').show();
  138. $('#upgrade-level').text(data.level_status_info.name + '(等级' + data.level_status + ')');
  139. } else {
  140. $('#upgrade-level-group').hide();
  141. }
  142. // 上级分销商
  143. if (data.user && data.user.parent_user) {
  144. $('#parent-agent').text(data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
  145. } else {
  146. $('#parent-agent').text('无');
  147. }
  148. // 允许升级
  149. $('#upgrade-lock').prop('checked', data.upgrade_lock == 0);
  150. // 统计数据
  151. $('#child-user-count-all').text(data.child_user_count_all || 0);
  152. $('#child-user-count-1').text(data.child_user_count_1 || 0);
  153. $('#child-agent-count-all').text(data.child_agent_count_all || 0);
  154. $('#child-agent-count-1').text(data.child_agent_count_1 || 0);
  155. $('#child-order-money-all').text((data.child_order_money_all || 0) + '元');
  156. $('#child-order-count-all').text(data.child_order_count_all || 0);
  157. $('#child-order-money-1').text((data.child_order_money_1 || 0) + '元');
  158. $('#child-order-count-1').text(data.child_order_count_1 || 0);
  159. $('#total-income').text((data.total_income || 0) + '元');
  160. $('#pending-reward').text((data.pending_reward || 0) + '元');
  161. $('#total-consume').text(((data.user && data.user.total_consume) || 0) + '元');
  162. // 申请信息
  163. if (data.apply_info && data.apply_info.length > 0) {
  164. var applyHtml = '<form class="form-horizontal">';
  165. data.apply_info.forEach(function(item) {
  166. applyHtml += '<div class="form-group">';
  167. applyHtml += '<label class="col-sm-3 control-label">' + item.name + ':</label>';
  168. applyHtml += '<div class="col-sm-9">';
  169. if (item.type === 'image') {
  170. applyHtml += '<img src="' + item.value + '" style="max-width: 200px; max-height: 120px;">';
  171. } else {
  172. applyHtml += '<p class="form-control-static">' + item.value + '</p>';
  173. }
  174. applyHtml += '</div></div>';
  175. });
  176. applyHtml += '</form>';
  177. $('#apply-info').html(applyHtml);
  178. } else {
  179. $('#apply-info').html('<p class="text-muted">暂无申请信息</p>');
  180. }
  181. }
  182. // 详情页面特有的全局函数
  183. window.changeStatus = function(userId, currentStatus) {
  184. Layer.prompt({
  185. title: '修改状态',
  186. formType: 2,
  187. value: currentStatus,
  188. select: ['normal', 'pending', 'freeze', 'forbidden', 'reject'],
  189. selectTips: ['正常', '审核中', '冻结', '禁用', '拒绝']
  190. }, function(value, index) {
  191. Fast.api.ajax({
  192. url: 'commission/agent/edit',
  193. data: {ids: userId, status: value}
  194. }, function() {
  195. Layer.close(index);
  196. location.reload();
  197. });
  198. });
  199. };
  200. window.changeLevel = function(userId) {
  201. Toastr.info('等级修改功能待完善');
  202. };
  203. window.changeParentUser = function(userId) {
  204. Fast.api.open('commission/agent/select?id=' + userId, '更换上级分销商', {
  205. callback: function() {
  206. location.reload();
  207. }
  208. });
  209. };
  210. window.approveLevel = function(userId, levelStatus) {
  211. Fast.api.ajax({
  212. url: 'commission/agent/edit',
  213. data: {ids: userId, level_status: levelStatus}
  214. }, function() {
  215. location.reload();
  216. });
  217. };
  218. window.rejectLevel = function(userId) {
  219. Fast.api.ajax({
  220. url: 'commission/agent/edit',
  221. data: {ids: userId, level_status: 0}
  222. }, function() {
  223. location.reload();
  224. });
  225. };
  226. window.toggleUpgradeLock = function(userId, currentLock) {
  227. var newLock = currentLock == 0 ? 1 : 0;
  228. Fast.api.ajax({
  229. url: 'commission/agent/edit',
  230. data: {ids: userId, upgrade_lock: newLock}
  231. }, function() {
  232. location.reload();
  233. });
  234. };
  235. window.viewTeam = function(userId) {
  236. Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
  237. };
  238. // 事件绑定
  239. $(document).ready(function() {
  240. $('.btn-refresh').click(function() {
  241. location.reload();
  242. });
  243. $('#refresh-detail').click(function() {
  244. getDetail();
  245. });
  246. $('#edit-status').click(function() {
  247. Layer.prompt({
  248. title: '修改状态',
  249. formType: 2,
  250. value: agentData.status,
  251. select: ['normal', 'pending', 'freeze', 'forbidden', 'reject'],
  252. selectTips: ['正常', '审核中', '冻结', '禁用', '拒绝']
  253. }, function(value, index) {
  254. Fast.api.ajax({
  255. url: 'commission/agent/edit',
  256. data: {ids: agentId, status: value}
  257. }, function() {
  258. Layer.close(index);
  259. getDetail();
  260. });
  261. });
  262. });
  263. $('#change-parent').click(function() {
  264. Fast.api.open('commission/agent/select?id=' + agentId, '更换上级分销商', {
  265. callback: function() {
  266. getDetail();
  267. }
  268. });
  269. });
  270. $('#upgrade-lock').change(function() {
  271. var value = $(this).is(':checked') ? 0 : 1;
  272. Fast.api.ajax({
  273. url: 'commission/agent/edit',
  274. data: {ids: agentId, upgrade_lock: value}
  275. }, function() {
  276. getDetail();
  277. });
  278. });
  279. $('#approve-level').click(function() {
  280. Fast.api.ajax({
  281. url: 'commission/agent/edit',
  282. data: {ids: agentId, level_status: agentData.level_status}
  283. }, function() {
  284. getDetail();
  285. });
  286. });
  287. $('#reject-level').click(function() {
  288. Fast.api.ajax({
  289. url: 'commission/agent/edit',
  290. data: {ids: agentId, level_status: 0}
  291. }, function() {
  292. getDetail();
  293. });
  294. });
  295. // 初始化
  296. if (agentId) {
  297. getDetail();
  298. }
  299. });
  300. Controller.api.bindevent();
  301. },
  302. select: function () {
  303. var userId = Fast.api.query('id');
  304. var selectedParentId = null;
  305. var currentPage = 1;
  306. var pageSize = 10;
  307. // 获取当前用户信息
  308. function getCurrentUserInfo() {
  309. Fast.api.ajax({
  310. url: 'commission/agent/detail',
  311. data: {id: userId}
  312. }, function(data) {
  313. var html = '<label>当前推荐人:</label>';
  314. if (data.user && data.user.parent_user) {
  315. html += '<img src="' + (data.user.parent_user.avatar || '/assets/img/avatar.png') + '" ';
  316. html += 'style="width: 20px; height: 20px; border-radius: 50%; margin-right: 5px;">';
  317. html += (data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
  318. selectedParentId = data.user.parent_user_id;
  319. } else {
  320. html += '无';
  321. selectedParentId = 0;
  322. }
  323. $('#current-parent').html(html);
  324. });
  325. }
  326. // 获取分销商列表
  327. function getAgentList() {
  328. var searchType = $('#search-type').val();
  329. var searchValue = $('#search-value').val();
  330. var searchData = {
  331. page: currentPage,
  332. limit: pageSize
  333. };
  334. if (searchValue) {
  335. searchData[searchType] = searchValue;
  336. }
  337. Fast.api.ajax({
  338. url: 'commission/agent/select',
  339. data: searchData
  340. }, function(ret) {
  341. var html = '';
  342. if (ret.data.rows && ret.data.rows.length > 0) {
  343. ret.data.rows.forEach(function(item) {
  344. var statusClass = '';
  345. var statusText = '';
  346. switch(item.status) {
  347. case 'normal':
  348. statusClass = 'label-success';
  349. statusText = '正常';
  350. break;
  351. case 'pending':
  352. statusClass = 'label-warning';
  353. statusText = '审核中';
  354. break;
  355. case 'freeze':
  356. statusClass = 'label-info';
  357. statusText = '冻结';
  358. break;
  359. case 'forbidden':
  360. statusClass = 'label-danger';
  361. statusText = '禁用';
  362. break;
  363. case 'reject':
  364. statusClass = 'label-danger';
  365. statusText = '拒绝';
  366. break;
  367. default:
  368. statusClass = 'label-default';
  369. statusText = item.status;
  370. }
  371. html += '<tr' + (selectedParentId == item.user_id ? ' class="warning"' : '') + '>';
  372. html += '<td><input type="radio" name="parent_agent" value="' + item.user_id + '"' + (selectedParentId == item.user_id ? ' checked' : '') + '></td>';
  373. html += '<td>' + item.user_id + '</td>';
  374. html += '<td>';
  375. html += '<img src="' + (item.user.avatar || '/assets/img/avatar.png') + '" style="width: 32px; height: 32px; border-radius: 50%; margin-right: 5px;">';
  376. html += (item.user.nickname || '用户' + item.user_id);
  377. html += '</td>';
  378. html += '<td>';
  379. if (item.level_info) {
  380. html += item.level_info.name + '<br><small>等级' + item.level_info.level + '</small>';
  381. } else {
  382. html += '等级' + item.level;
  383. }
  384. html += '</td>';
  385. html += '<td>' + (item.user.mobile || '-') + '</td>';
  386. html += '<td><span class="label ' + statusClass + '">' + statusText + '</span></td>';
  387. html += '<td>';
  388. if (selectedParentId == item.user_id) {
  389. html += '<span class="text-success">已选择</span>';
  390. } else {
  391. html += '<button type="button" class="btn btn-xs btn-primary" onclick="selectAgent(' + item.user_id + ')">选择</button>';
  392. }
  393. html += '</td>';
  394. html += '</tr>';
  395. });
  396. } else {
  397. html = '<tr><td colspan="7" class="text-center">暂无数据</td></tr>';
  398. }
  399. $('#agent-list').html(html);
  400. // 更新分页
  401. updatePagination(ret.data.total);
  402. });
  403. }
  404. // 更新分页
  405. function updatePagination(total) {
  406. var totalPages = Math.ceil(total / pageSize);
  407. var html = '<ul class="pagination">';
  408. // 上一页
  409. if (currentPage > 1) {
  410. html += '<li><a href="javascript:;" onclick="changePage(' + (currentPage - 1) + ')">上一页</a></li>';
  411. }
  412. // 页码
  413. for (var i = 1; i <= totalPages; i++) {
  414. if (i == currentPage) {
  415. html += '<li class="active"><a href="javascript:;">' + i + '</a></li>';
  416. } else {
  417. html += '<li><a href="javascript:;" onclick="changePage(' + i + ')">' + i + '</a></li>';
  418. }
  419. }
  420. // 下一页
  421. if (currentPage < totalPages) {
  422. html += '<li><a href="javascript:;" onclick="changePage(' + (currentPage + 1) + ')">下一页</a></li>';
  423. }
  424. html += '</ul>';
  425. $('#pagination').html(html);
  426. }
  427. // 选择页面特有的全局函数
  428. window.searchAgent = function() {
  429. currentPage = 1;
  430. getAgentList();
  431. };
  432. window.selectAgent = function(agentId) {
  433. selectedParentId = agentId;
  434. $('input[name="parent_agent"]').prop('checked', false);
  435. $('input[value="' + agentId + '"]').prop('checked', true);
  436. $('#agent-table tbody tr').removeClass('warning');
  437. $('input[value="' + agentId + '"]').closest('tr').addClass('warning');
  438. $('#set-platform').prop('checked', false);
  439. };
  440. window.changePage = function(page) {
  441. currentPage = page;
  442. getAgentList();
  443. };
  444. window.confirmChange = function() {
  445. if (selectedParentId === null) {
  446. Toastr.error('请选择上级分销商');
  447. return;
  448. }
  449. Fast.api.ajax({
  450. url: 'commission/agent/changeParentUser',
  451. data: {
  452. id: userId,
  453. parent_user_id: selectedParentId
  454. }
  455. }, function() {
  456. Toastr.success('更换成功');
  457. Fast.api.close();
  458. });
  459. };
  460. // 事件绑定
  461. $(document).ready(function() {
  462. // 设为平台直推
  463. $('#set-platform').change(function() {
  464. if ($(this).is(':checked')) {
  465. selectedParentId = 0;
  466. $('input[name="parent_agent"]').prop('checked', false);
  467. $('#agent-table tbody tr').removeClass('warning');
  468. }
  469. });
  470. // 搜索框回车事件
  471. $('#search-value').keypress(function(e) {
  472. if (e.which == 13) {
  473. searchAgent();
  474. }
  475. });
  476. // 初始化
  477. getCurrentUserInfo();
  478. getAgentList();
  479. });
  480. },
  481. team: function () {
  482. // 团队页面特有的全局函数
  483. window.viewTeam = function(userId) {
  484. Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
  485. };
  486. window.viewDetail = function(userId) {
  487. Fast.api.open('commission/agent/detail?id=' + userId, '分销商详情');
  488. };
  489. Controller.api.bindevent();
  490. },
  491. api: {
  492. bindevent: function () {
  493. Form.api.bindevent($("form[role=form]"));
  494. }
  495. }
  496. };
  497. return Controller;
  498. });