index.js 4.5 KB

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