agent.js 26 KB

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