userchance.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: 'lottery/userchance/index' + location.search,
  8. add_url: 'lottery/userchance/add',
  9. edit_url: 'lottery/userchance/edit',
  10. del_url: 'lottery/userchance/del',
  11. multi_url: 'lottery/userchance/multi',
  12. table: 'shop_lottery_user_chance',
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. pk: 'id',
  20. sortName: 'id',
  21. sortOrder: 'desc',
  22. columns: [
  23. [
  24. {checkbox: true},
  25. {field: 'id', title: __('Id'), operate: false},
  26. {field: 'activity.name', title: __('活动名称'), operate: 'LIKE'},
  27. {field: 'user.nickname', title: __('用户昵称'), operate: 'LIKE'},
  28. {field: 'user.mobile', title: __('用户手机'), operate: 'LIKE'},
  29. {field: 'chance_count', title: __('抽奖次数'), operate: 'BETWEEN'},
  30. {field: 'used_count', title: __('已使用'), operate: 'BETWEEN'},
  31. {field: 'remain_count', title: __('剩余次数'), operate: false, formatter: function(value, row, index) {
  32. var remain = row.chance_count - row.used_count;
  33. var color = remain > 0 ? 'success' : 'default';
  34. return '<span class="label label-' + color + '">' + remain + '</span>';
  35. }},
  36. {field: 'source_type', title: __('来源类型'), searchList: {"1":"购买商品","2":"订单消费","3":"充值","4":"累计消费","5":"管理员发放","6":"签到奖励","7":"邀请奖励","8":"活动奖励"}, formatter: Table.api.formatter.normal},
  37. {field: 'source_value', title: __('来源描述'), operate: 'LIKE'},
  38. {field: 'expire_time', title: __('过期时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: function(value, row, index) {
  39. if (!value || value == 0) {
  40. return '<span class="text-success">永不过期</span>';
  41. }
  42. var now = Math.floor(Date.now() / 1000);
  43. var color = value > now ? 'info' : 'danger';
  44. return '<span class="text-' + color + '">' + Table.api.formatter.datetime(value) + '</span>';
  45. }},
  46. {field: 'status', title: __('状态'), searchList: {"0":"已失效","1":"有效"}, formatter: Table.api.formatter.status},
  47. {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  48. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  49. ]
  50. ]
  51. });
  52. // 为表格绑定事件
  53. Table.api.bindevent(table);
  54. // 批量发放按钮
  55. $(document).on('click', '.btn-batchgrant', function(){
  56. Fast.api.open('lottery/userchance/batchgrant', '批量发放抽奖机会', {
  57. area: ['800px', '600px'],
  58. callback: function(data) {
  59. table.bootstrapTable('refresh');
  60. }
  61. });
  62. });
  63. // 统计按钮
  64. $(document).on('click', '.btn-statistics', function(){
  65. Fast.api.open('lottery/userchance/statistics', '抽奖机会统计', {
  66. area: ['1000px', '700px']
  67. });
  68. });
  69. // 清理过期机会按钮
  70. $(document).on('click', '.btn-clearexpired', function(){
  71. Layer.confirm('确定要清理所有过期的抽奖机会吗?', {icon: 3, title: '提示'}, function(index){
  72. Fast.api.ajax({
  73. url: 'lottery/userchance/clearExpired',
  74. type: 'post'
  75. }, function(data, ret) {
  76. Layer.close(index);
  77. Layer.msg(ret.msg);
  78. table.bootstrapTable('refresh');
  79. });
  80. });
  81. });
  82. },
  83. add: function () {
  84. Controller.api.bindevent();
  85. },
  86. edit: function () {
  87. Controller.api.bindevent();
  88. },
  89. batchGrant: function () {
  90. Controller.api.bindevent();
  91. },
  92. statistics: function () {
  93. Controller.api.bindevent();
  94. // 加载统计数据
  95. Controller.api.loadStatistics();
  96. // 活动切换时重新加载统计
  97. $(document).on('change', '#activity_id', function(){
  98. Controller.api.loadStatistics();
  99. });
  100. },
  101. api: {
  102. bindevent: function () {
  103. Form.api.bindevent($("form[role=form]"));
  104. // 来源类型变化时的处理
  105. $(document).on('change', 'input[name="row[source_type]"]', function(){
  106. var source_type = $(this).val();
  107. $('.source-desc').text('');
  108. switch(source_type) {
  109. case '1':
  110. $('.source-desc').text('用户购买商品后获得');
  111. break;
  112. case '2':
  113. $('.source-desc').text('用户订单消费达到条件后获得');
  114. break;
  115. case '3':
  116. $('.source-desc').text('用户充值达到条件后获得');
  117. break;
  118. case '4':
  119. $('.source-desc').text('用户累计消费达到条件后获得');
  120. break;
  121. case '5':
  122. $('.source-desc').text('管理员手动发放');
  123. break;
  124. case '6':
  125. $('.source-desc').text('用户签到奖励获得');
  126. break;
  127. case '7':
  128. $('.source-desc').text('用户邀请奖励获得');
  129. break;
  130. case '8':
  131. $('.source-desc').text('其他活动奖励获得');
  132. break;
  133. }
  134. });
  135. // 过期时间控制
  136. $(document).on('change', 'input[name="expire_enable"]', function(){
  137. var expire_enable = $(this).val();
  138. $('.expire-time-group').toggle(expire_enable == '1');
  139. });
  140. // 用户选择器
  141. $(document).on('click', '.btn-choose-users', function(){
  142. Fast.api.open('user/user/select', '选择用户', {
  143. area: ['80%', '80%'],
  144. callback: function(data) {
  145. if (data && data.length > 0) {
  146. var userIds = [];
  147. var userNames = [];
  148. $.each(data, function(i, user) {
  149. userIds.push(user.id);
  150. userNames.push(user.nickname);
  151. });
  152. $('#user_ids').val(userIds.join(','));
  153. $('#user_names').val(userNames.join(','));
  154. }
  155. }
  156. });
  157. });
  158. },
  159. loadStatistics: function() {
  160. var activity_id = $('#activity_id').val();
  161. Fast.api.ajax({
  162. url: 'lottery/userchance/statistics',
  163. type: 'get',
  164. data: {activity_id: activity_id}
  165. }, function(data, ret) {
  166. if (ret.code == 1 && ret.data) {
  167. $('#total_users').text(ret.data.total_users);
  168. $('#total_chances').text(ret.data.total_chances);
  169. $('#used_chances').text(ret.data.used_chances);
  170. $('#remain_chances').text(ret.data.remain_chances);
  171. // 更新来源统计表格
  172. var tbody = $('#source_stats tbody');
  173. tbody.empty();
  174. if (ret.data.source_stats && ret.data.source_stats.length > 0) {
  175. $.each(ret.data.source_stats, function(i, item) {
  176. var sourceTypes = {"1":"购买商品","2":"订单消费","3":"充值","4":"累计消费","5":"管理员发放","6":"签到奖励","7":"邀请奖励","8":"活动奖励"};
  177. var sourceText = sourceTypes[item.source_type] || '未知';
  178. var remainChance = item.total_chance - item.used_chance;
  179. var row = '<tr>' +
  180. '<td>' + sourceText + '</td>' +
  181. '<td>' + item.count + '</td>' +
  182. '<td>' + item.total_chance + '</td>' +
  183. '<td>' + item.used_chance + '</td>' +
  184. '<td>' + remainChance + '</td>' +
  185. '</tr>';
  186. tbody.append(row);
  187. });
  188. } else {
  189. tbody.append('<tr><td colspan="5" class="text-center text-muted">暂无数据</td></tr>');
  190. }
  191. }
  192. });
  193. }
  194. }
  195. };
  196. return Controller;
  197. });