index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Page({
  2. dever: getApp().dever,
  3. authdialog: null,
  4. product_ids: '',
  5. product_num: '',
  6. data: {
  7. isUseAddress: false,
  8. settlement: 0.00
  9. },
  10. onLoad: function (options) {
  11. var that = this;
  12. that.dever.showLoading();
  13. //获取自定义授权组件
  14. that.authdialog = this.selectComponent("#authdialog");
  15. that.dever.request('product.api.carts',{
  16. signature: that.dever.getSignature()
  17. },{
  18. success: function(data, res){
  19. that.dever.setList(that, data.carts);
  20. that.doSettlement();
  21. that.dever.hideLoading();
  22. }
  23. });
  24. },
  25. /**
  26. * 删除订单
  27. */
  28. delOrder: function(e){
  29. var that = this;
  30. var pid;
  31. if (e == null && arguments.length==2){
  32. pid = arguments[1];
  33. }else{
  34. pid = e.currentTarget.dataset.id;
  35. }
  36. var delfun = function(){
  37. that.dever.request('product.api.delCarts',{
  38. json: 1,
  39. signature: that.dever.getSignature(),
  40. product_id: pid
  41. },{
  42. success: function(data, res){
  43. that.dever.alert('删除商品成功!');
  44. that.onLoad();
  45. },
  46. fail: function(res){
  47. that.dever.alert('删除商品失败!');
  48. }
  49. });
  50. }
  51. wx.showModal({
  52. content: '确认删除该商品吗?',
  53. success: function(res){
  54. if (res.confirm){
  55. delfun();
  56. }
  57. }
  58. })
  59. },
  60. /**
  61. * 减少商品数量>0 and <=1
  62. */
  63. doSub: function(e){
  64. var id = e.currentTarget.dataset.id;
  65. var list = this.data.list;
  66. for(var i=0; i<list.length; i++){
  67. if (list[i].product_id == id){
  68. //list[i].num = list[i].num-1<=0?1:list[i].num-1;
  69. if(list[i].num-1 <= 0){
  70. list[i].num = 1;
  71. this.delOrder(null, id);
  72. }else{
  73. list[i].num -= 1;
  74. }
  75. break;
  76. }
  77. }
  78. this.dever.setList(this, list);
  79. this.doSettlement();
  80. },
  81. /**
  82. * 增加商品数量>1 and <=库存
  83. */
  84. doAdd: function(e){
  85. var id = e.currentTarget.dataset.id;
  86. var list = this.data.list;
  87. for (var i = 0; i < list.length; i++) {
  88. if (list[i].product_id == id) {
  89. ++list[i].num;
  90. break;
  91. }
  92. }
  93. this.dever.setList(this, list);
  94. this.doSettlement();
  95. },
  96. /**
  97. * 结算金额
  98. */
  99. doSettlement: function(){
  100. var list = this.data.list;
  101. var count=0;
  102. for(var i=0; i<list.length; i++){
  103. var product_id = list[i].product_id;
  104. var pay_price = list[i].product.pay_price;
  105. var num = list[i].num;
  106. count += pay_price*num;
  107. if(i==0){
  108. this.product_ids += product_id;
  109. this.product_num += num;
  110. }else{
  111. this.product_ids += ',' + product_id;
  112. this.product_num += ',' + num;
  113. }
  114. }
  115. this.dever.set(this, 'settlement', count.toFixed(2));
  116. },
  117. /**
  118. * 去支付
  119. */
  120. doPay: function(e){
  121. var that = this;
  122. var addrObj = that.data.addrObj;
  123. if (!addrObj){
  124. that.dever.alert('请选择收货地址');
  125. }
  126. that.dever.request('product.api.buy',{
  127. signature: that.dever.getSignature(),
  128. product_id: that.product_ids,
  129. num: that.product_num,
  130. name: addrObj.userName,
  131. address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
  132. mobile: addrObj.telNumber
  133. },{
  134. success:function(data, res){
  135. that.dever.redirect('pay/index');
  136. that.dever.log('doPay success', res);
  137. },
  138. fail: function(res){
  139. that.dever.alert('支付失败!');
  140. that.dever.log('doPay fail', res);
  141. }
  142. });
  143. },
  144. /**
  145. * 使用微信地址
  146. */
  147. chooseAddress: function(event){
  148. var that = this;
  149. var success = function(){
  150. wx.chooseAddress({
  151. success: function (res) {
  152. that.dever.log('wx.chooseAddress', res);
  153. that.dever.set(that,'addrObj', res);
  154. that.dever.set(that,'isUseAddress', true);
  155. },
  156. fail: function (res) {
  157. that.dever.log('wx.chooseAddress', 'fail');
  158. }
  159. })
  160. }
  161. wx.getSetting({
  162. success: function(res){
  163. if (res.authSetting['scope.address']){
  164. success();
  165. }else{
  166. wx.authorize({
  167. scope: 'scope.address',
  168. success: function () {
  169. success();
  170. },
  171. fail: function(res){
  172. that.authdialog.showDialog();
  173. }
  174. })
  175. }
  176. }
  177. })
  178. }
  179. })