index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const app = getApp();
  2. Page({
  3. authdialog: null,
  4. product_ids: '',
  5. cash_code: '',
  6. data: {
  7. isUseAddress: false,
  8. settlement: 0.00
  9. },
  10. onLoad: function (options) {
  11. var that = this;
  12. app.showLoading();
  13. //获取自定义授权组件
  14. this.authdialog = this.selectComponent("#authdialog");
  15. //获取兑换商品信息
  16. this.cash_code = options.code;
  17. app.request('product.api.checkCode',{
  18. code: that.cash_code
  19. },{
  20. success: function(data, res){
  21. app.setList(that, data.product);
  22. that.doSettlement();
  23. app.hideLoading();
  24. }
  25. })
  26. },
  27. /**
  28. * 结算金额
  29. */
  30. doSettlement: function(){
  31. var list = this.data.list;
  32. var count=0;
  33. for(var i=0; i<list.length; i++){
  34. var product_id = list[i].id;
  35. var pay_price = list[i].pay_price;
  36. count += pay_price*1;
  37. if(i==0){
  38. this.product_ids += product_id;
  39. }else{
  40. this.product_ids += ',' + product_id;
  41. }
  42. }
  43. app.set(this, 'settlement', count.toFixed(2));
  44. },
  45. /**
  46. * 立即兑换
  47. */
  48. doCash: function(e){
  49. var that = this;
  50. var addrObj = this.data.addrObj;
  51. if (!addrObj) {
  52. app.toast('请选择收货地址');
  53. }
  54. app.request('product.api.code',{
  55. product_id: that.product_ids,
  56. code: that.cash_code,
  57. name: addrObj.userName,
  58. address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
  59. mobile: addrObj.telNumber,
  60. invite_uid: app.getInviteUid()
  61. },{
  62. success:function(data, res){
  63. app.saveInviteUid('');
  64. var id = that.product_ids;
  65. let idx = id.indexOf(',');
  66. if (idx > -1) {
  67. id = id.substring(0, idx);
  68. }
  69. app.redirect('pay/index?id=' + id);
  70. },
  71. fail: function(res){
  72. app.toast('兑换失败!');
  73. }
  74. })
  75. },
  76. /**
  77. * 使用微信地址
  78. */
  79. chooseAddress: function(event){
  80. var that = this;
  81. var success = function(){
  82. wx.chooseAddress({
  83. success: function (res) {
  84. app.set(that,'addrObj', res);
  85. app.set(that,'isUseAddress', true);
  86. }
  87. })
  88. }
  89. wx.getSetting({
  90. success: function(res){
  91. if (res.authSetting['scope.address']){
  92. success();
  93. }else{
  94. wx.authorize({
  95. scope: 'scope.address',
  96. success: function () {
  97. success();
  98. },
  99. fail: function(res){
  100. that.authdialog.showDialog();
  101. }
  102. })
  103. }
  104. }
  105. })
  106. }
  107. })