seckill-card.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. (function (global,factory) {
  2. typeof define && define.amd && define(['vue'],factory);
  3. })(this,function(Vue){
  4. var template = '<div class=shop-card>' +
  5. '<div class=model-bg :class="{on:show == true}" @click=close @touchmove.prevent>' +
  6. '</div>' +
  7. '<div class=card-model :class="{up:show == true}">' +
  8. '<div class=cm-header @touchmove.prevent>' +
  9. '<div class=header-product>' +
  10. '<img :src=product.image>' +
  11. '</div>' +
  12. '<div class=header-info>' +
  13. '<p class=money>' +
  14. '¥<i v-text=product.price></i>' +
  15. '</p>' +
  16. '<span class=stock>' +
  17. '库存<i v-text=product.stock></i>' +
  18. '件</span>' +
  19. '</div>' +
  20. '</div>' +
  21. '<div class="amount clearfix" @touchmove.prevent>' +
  22. '<div class="text fl">购买数量</div>' +
  23. '<div class="count fr">' +
  24. '<div @click=descCartNum>-</div>' +
  25. '<input type=number v-model=cartNum readonly disabled style="-webkit-text-fill-color: #666;-webkit-opacity:1; opacity: 1;">' +
  26. '<div @click=incCartNum>+</div>' +
  27. '</div>' +
  28. '</div>' +
  29. '<div class=model-footer @touchmove.prevent>' +
  30. '<a class=footer-buy href="javascript:void(0);" v-if="buy == true" v-show="product.stock > 0" @click=goBuy>' +
  31. '立即秒杀' +
  32. '</a>' +
  33. '<a class="footer-buy no" href="javascript:void(0);" v-show="!product.stock || product.stock <= 0">' +
  34. '无货' +
  35. '</a>' +
  36. '</div>' +
  37. '<div class=model-close @click=close>' +
  38. '</div>' +
  39. '</div>' +
  40. '</div>';
  41. var shopCard = Vue.extend({
  42. template:template,
  43. props:{
  44. onShow:Function,
  45. onClose:Function,
  46. onChange:Function,
  47. onBuy:Function,
  48. onCart:Function,
  49. cart:{
  50. type:Boolean,
  51. default:true
  52. },
  53. buy:{
  54. type:Boolean,
  55. default:true
  56. },
  57. product:{
  58. type:Object,
  59. default:function(){
  60. return {
  61. image:'',
  62. price:'',
  63. stock:0
  64. }
  65. }
  66. },
  67. attrList:{
  68. type:Array,
  69. default:function (){
  70. return [];
  71. }
  72. },
  73. show:Boolean
  74. },
  75. data:function(){
  76. return {
  77. cartNum:1
  78. }
  79. },
  80. watch:{
  81. cartNum:function(v){
  82. if(this.product.stock <= 0) this.cartNum = 0;
  83. else if(v > this.product.stock) this.cartNum = this.product.stock;
  84. else if(v < 1) this.cartNum = 1;
  85. },
  86. attrList:{
  87. handler:function (v) {
  88. this.$nextTick(function(){
  89. this.onChange && this.onChange(this.getCheckedValue());
  90. })
  91. },
  92. deep:true
  93. },
  94. product:function(){
  95. this.cartNum = 1;
  96. }
  97. },
  98. methods:{
  99. changeProduct:function(product){
  100. this.product = product;
  101. },
  102. getCheckedValue:function(){
  103. return this.attrList.map(function(attr){
  104. return attr.checked;
  105. });
  106. },
  107. close:function(){
  108. this.show = false;
  109. this.onClose && this.onClose();
  110. },
  111. active:function(){
  112. this.show = true;
  113. this.onShow && this.onShow();
  114. },
  115. incCartNum:function(){
  116. if(this.product.num <= this.cartNum){
  117. $h.pushMsg('每次购买数量不能超过'+this.product.num+'个')
  118. this.cartNum = this.product.num;
  119. }else{
  120. this.cartNum+=1;
  121. }
  122. },
  123. descCartNum:function(){
  124. this.cartNum-=1;
  125. },
  126. changeChecked:function(value,attr,index){
  127. attr.checked = value;
  128. this.$set(this.attrList,index,attr);
  129. },
  130. goCart:function(){
  131. if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
  132. this.onCart && this.onCart(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
  133. },
  134. goBuy:function(){
  135. if(this.cartNum > this.product.stock || this.cartNum <= 0) return false;
  136. this.onBuy && this.onBuy(this.getCheckedValue(),this.cartNum,this.product) == true && this.init();
  137. },
  138. init:function(){
  139. var that = this;
  140. this.attrList.map(function(attr,index){
  141. attr.checked = attr.attr_values[0];
  142. that.$set(that.attrList,index,attr);
  143. });
  144. this.cartNum = this.product.stock >=1 ? 1 : 0;
  145. },
  146. _setData:function(opt){
  147. this.product = opt.product;
  148. this.attrList = opt.attrList == undefined ? [] : opt.attrList;
  149. this.onChange = opt.onChange;
  150. this.onClose = opt.onClose;
  151. this.onCart = opt.onCart;
  152. this.onBuy = opt.onBuy;
  153. this.cart = opt.cart == undefined ? true : opt.cart;
  154. this.buy = opt.buy == undefined ? true : opt.buy;
  155. this.init();
  156. }
  157. },
  158. mounted:function(){
  159. var that = this;
  160. this.init();
  161. this.$el.reload = function(){
  162. that.init();
  163. };
  164. }
  165. });
  166. shopCard.install = function(Vue){
  167. Vue.prototype.$shopCard = function(opt){
  168. var $vm = new shopCard().$mount(),$el = $vm.$el;
  169. document.body.appendChild($el);
  170. $vm._setData(opt);
  171. $vm.remove = function(){
  172. document.body.removeChild($el);
  173. };
  174. this.$nextTick(function(){
  175. setTimeout(function(){
  176. opt.show == true && $vm.active();
  177. },0);
  178. });
  179. return $vm;
  180. };
  181. };
  182. return shopCard;
  183. });