人人商城

coupon.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. define(['jquery', 'underscore', 'bootstrap'], function($, _){
  2. var coupon = {
  3. 'defaultoptions' : {
  4. callback : null,
  5. type : 'all',
  6. multiple : true,
  7. ignore : {
  8. 'local' : false,
  9. 'wechat' : false
  10. }
  11. },
  12. 'init' : function(callback, options) {
  13. var $this = this;
  14. $this.options = $.extend({}, $this.defaultoptions, options);
  15. $this.options.callback = callback;
  16. $('#counpon-Modal').remove();
  17. $(document.body).append($this.buildHtml().mainDialog);
  18. $this.modalobj = $('#counpon-Modal');
  19. $this.modalobj.find('.modal-header .nav li a').click(function(){
  20. var type = $(this).data('type');
  21. $this.localPage(type, 1);
  22. $(this).tab('show')
  23. return false;
  24. });
  25. if (!$(this).data('init')) {
  26. if($this.options.type && $this.options.type != 'all') {
  27. $this.modalobj.find('.modal-header .nav li.' + $this.options.type + ' a').trigger('click');
  28. } else {
  29. $this.modalobj.find('.modal-header .nav li.show:first a').trigger('click');
  30. }
  31. }
  32. $this.modalobj.modal('show');
  33. return $this.modalobj;
  34. },
  35. 'localPage' : function(type, page) {
  36. var $this = this;
  37. var $history = $this.modalobj.find('.coupon-content #' + type);
  38. $history.html('<div class="info"><i class="fa fa-spinner fa-pulse fa-lg"></i> 数据加载中</div>');
  39. $.getJSON('./index.php?c=utility&a=coupon&do=' + type, {'page': page}, function(data){
  40. data = data.message;
  41. $this.modalobj.find('#coupon-list-pager').html('');
  42. if(!_.isEmpty(data.items)) {
  43. $this.modalobj.find('#btn-select').show();
  44. $history.data('attachment', data.items);
  45. $history.empty();
  46. var Dialog = type + 'Dialog';
  47. $history.html(_.template($this.buildHtml()[Dialog])(data));
  48. $this.modalobj.find('#coupon-list-pager').html(data.page);
  49. $this.modalobj.find('.pagination a').click(function(){
  50. $this.localPage(type, $(this).attr('page'));
  51. });
  52. $history.find('.conpon-list').click(function(){
  53. $this.selectCoupon(this);
  54. });
  55. } else {
  56. $history.html('<div class="info"><i class="fa fa-info-circle fa-lg"></i> 暂无数据</div>');
  57. }
  58. });
  59. $this.modalobj.find('.modal-footer .btn-primary').unbind('click').click(function(){
  60. var attachment = [];
  61. $history.find('.btn-primary').each(function(){
  62. attachment.push($history.data('attachment')[$(this).data('attachid')]);
  63. $(this).removeClass('btn-primary');
  64. });
  65. $this.finish(attachment);
  66. });
  67. return false;
  68. },
  69. 'selectCoupon' : function(obj) {
  70. var $this = this;
  71. $(obj).toggleClass('btn-primary');
  72. if (!$this.options.multiple) {
  73. $this.modalobj.find('.modal-footer .btn-primary').trigger('click');
  74. }
  75. },
  76. 'finish' : function(attachment) {
  77. var $this = this;
  78. if($.isFunction($this.options.callback)) {
  79. if ($this.options.multiple == false) {
  80. $this.options.callback(attachment[0]);
  81. } else {
  82. $this.options.callback(attachment);
  83. }
  84. $this.modalobj.modal('hide');
  85. }
  86. },
  87. 'buildHtml' : function() {
  88. var dialog = {};
  89. dialog['mainDialog'] = '<div id="counpon-Modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">\n' +
  90. ' <div class="modal-dialog">\n' +
  91. ' <div class="modal-content modal-lg">\n' +
  92. ' <div class="modal-header">\n' +
  93. ' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\n' +
  94. ' <h3>'+
  95. ' <ul role="tablist" class="nav nav-pills" style="font-size:14px; margin-top:-20px;">'+
  96. ' <li role="presentation" class="wechat ' + (this.options.ignore.wechat ? 'hide' : 'show') + '">'+
  97. ' <a data-toggle="tab" data-type="wechat" role="tab" aria-controls="wechat" href="#wechat">卡券列表</a>'+
  98. ' </li>'+
  99. ' </ul>'+
  100. ' </h3>'+
  101. ' </div>\n' +
  102. ' <div class="modal-body coupon-content">\n' +
  103. ' <div class="tab-content">'+
  104. ' <div id="local" class="tab-pane" class="active" role="tabpanel"></div>'+
  105. ' <div id="wechat" class="tab-pane" role="tabpanel"></div>'+
  106. ' </div>' +
  107. ' </div>\n' +
  108. ' <div class="modal-footer">\n' +
  109. ' <div style="float: left;">\n' +
  110. ' <nav id="coupon-list-pager">\n' +
  111. ' <ul class="pager" style="margin: 0;"></ul>\n' +
  112. ' </nav>\n' +
  113. ' </div>\n' +
  114. ' <div id="btn-select" style="float: right; display: none">\n' +
  115. ' <button '+(this.options.multiple ? '' : 'style="display:none;"')+' type="button" class="btn btn-primary">确认</button>\n' +
  116. (this.options.multiple ? '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>\n' : '') +
  117. ' </div>\n' +
  118. ' </div>\n'+
  119. ' </div>\n' +
  120. ' </div>\n' +
  121. '</div>';
  122. dialog['wechatDialog'] ='<table class="table table-hover table-bordered">\n'+
  123. ' <thead>\n'+
  124. ' <tr>\n'+
  125. ' <th width="130" class="text-center">标题</th>\n'+
  126. ' <th class="text-center">类型</th>\n'+
  127. ' <th width="250" class="text-center">卡券有效期</th>\n'+
  128. ' <th class="text-center">库存/每人限领</th>\n'+
  129. ' <th class="text-center">操作</th>\n'+
  130. ' </tr>'+
  131. ' </thead>'+
  132. ' <tbody>'+
  133. ' <%var items = _.sortBy(items, function(item) {return -item.id;});%>' +
  134. ' <%_.each(items, function(item) {%> \n' +
  135. ' <tr title="<%=item.title%>">' +
  136. ' <td><%=item.title%></td>' +
  137. ' <td><%if(item.type == "1") {%><span class="label label-info"><%=item.extra.discount%>折</span> <span class="label label-success">折扣券</span><%} else if(item.type == "2") {%><span class="label label-info"><%=item.extra.reduce_cost%>元</span> <span class="label label-danger">代金券</span><%} else if(item.type == "4") {%><span class="label label-danger">礼品券</span><%} else if(item.type == "3") {%><span class="label label-danger">团购券</span><%} else if(item.type == "5") {%><span class="label label-danger">优惠券</span><%}%></td>' +
  138. ' <td><%if(item.date_info.time_type == 1) {%><%=item.date_info.time_limit_start%> ~ <%=item.date_info.time_limit_end%><%} else {%>领取后<%=item.date_info.date_info%>天后生效,<%=item.date_info.limit%>天有效期<%}%></td>' +
  139. ' <td><%=item.quantity%>/<strong class="text-danger"><%=item.get_limit%></strong></td>' +
  140. ' <td><a href="javascript:;" class="btn btn-default conpon-list" data-title="<%=item.title%>" data-attachid="<%=item.id%>">选取</a></td>' +
  141. ' </tr>' +
  142. ' <%});%>' +
  143. ' </tbody>'+
  144. ' </table>';
  145. return dialog;
  146. }
  147. };
  148. return coupon;
  149. });