index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if(list[i].num-1 <= 0){
  65. list[i].num = 1;
  66. this.delOrder(null, id);
  67. }else{
  68. list[i].num -= 1;
  69. }
  70. break;
  71. }
  72. }
  73. app.setList(this, list);
  74. this.doSettlement();
  75. },
  76. /**
  77. * 增加商品数量>1 and <=库存
  78. */
  79. doAdd: function(e){
  80. var id = app.getData(e).id;
  81. var list = this.data.list;
  82. for (var i = 0; i < list.length; i++) {
  83. if (list[i].product_id == id) {
  84. ++list[i].num;
  85. break;
  86. }
  87. }
  88. app.setList(this, list);
  89. this.doSettlement();
  90. },
  91. /**
  92. * 结算金额
  93. */
  94. doSettlement: function(){
  95. var list = this.data.list;
  96. var count=0;
  97. this.product_ids = '';
  98. this.product_num = '';
  99. for(var i=0; i<list.length; i++){
  100. var product_id = list[i].product_id;
  101. var pay_price = list[i].product.pay_price;
  102. var num = list[i].num;
  103. count += pay_price*num;
  104. if(i==0){
  105. this.product_ids += product_id;
  106. this.product_num += num;
  107. }else{
  108. this.product_ids += ',' + product_id;
  109. this.product_num += ',' + num;
  110. }
  111. }
  112. app.set(this, 'settlement', count.toFixed(2));
  113. },
  114. /**
  115. * 立即支付
  116. */
  117. doPay: function(){
  118. var that = this;
  119. var addrObj = that.data.addrObj;
  120. if (!addrObj){
  121. app.toast('请选择收货地址');
  122. return ;
  123. }
  124. app.showLoading();
  125. app.request('product.api.buy',{
  126. product_id: that.product_ids,
  127. num: that.product_num,
  128. name: addrObj.userName,
  129. address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
  130. mobile: addrObj.telNumber,
  131. invite_uid: app.getInviteUid()
  132. },{
  133. success:function(data, res){
  134. var pay = data.pay;
  135. console.log('requestPayment-1', pay);
  136. wx.requestPayment({
  137. timeStamp: pay.time,
  138. nonceStr: pay.nonce_str,
  139. package: 'prepay_id=' + pay.prepay_id,
  140. signType: pay.sign_type,
  141. paySign: pay.sign,
  142. success: function(){//微信支付成功
  143. app.saveInviteUid('');
  144. var id = that.product_ids;
  145. let idx = id.indexOf(',');
  146. if (idx > -1) {
  147. id = id.substring(0, idx);
  148. }
  149. //跳转支付结果页
  150. app.redirect('pay/index?id=' + id);
  151. },
  152. fail: function(res){//微信支付失败
  153. app.toast('支付失败!');
  154. console.log('支付失败!', res);
  155. }
  156. })
  157. },
  158. fail: function(res){
  159. app.toast('支付失败!');
  160. },
  161. complete: function (res) {
  162. app.hideLoading();
  163. }
  164. })
  165. },
  166. /**
  167. * 使用微信地址
  168. */
  169. chooseAddress: function(event){
  170. var that = this;
  171. var success = function(){
  172. wx.chooseAddress({
  173. success: function (res) {
  174. app.set(that,'addrObj', res);
  175. app.set(that,'isUseAddress', true);
  176. }
  177. })
  178. }
  179. wx.getSetting({
  180. success: function(res){
  181. if (res.authSetting['scope.address']){
  182. success();
  183. }else{
  184. wx.authorize({
  185. scope: 'scope.address',
  186. success: function () {
  187. success();
  188. },
  189. fail: function(res){
  190. that.authdialog.showDialog();
  191. }
  192. })
  193. }
  194. }
  195. })
  196. }
  197. })