index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. this.product_ids = '';
  34. for(var i=0; i<list.length; i++){
  35. var product_id = list[i].id;
  36. var pay_price = list[i].pay_price;
  37. count += pay_price*1;
  38. if(i==0){
  39. this.product_ids += product_id;
  40. }else{
  41. this.product_ids += ',' + product_id;
  42. }
  43. }
  44. app.set(this, 'settlement', count.toFixed(2));
  45. },
  46. /**
  47. * 立即兑换
  48. */
  49. doCash: function(e){
  50. var that = this;
  51. var addrObj = this.data.addrObj;
  52. if (!addrObj) {
  53. app.toast('请选择收货地址');
  54. }
  55. app.request('product.api.code',{
  56. product_id: that.product_ids,
  57. code: that.cash_code,
  58. name: addrObj.userName,
  59. address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
  60. mobile: addrObj.telNumber,
  61. invite_uid: app.getInviteUid()
  62. },{
  63. success:function(data, res){
  64. app.saveInviteUid('');
  65. var id = that.product_ids;
  66. let idx = id.indexOf(',');
  67. if (idx > -1) {
  68. id = id.substring(0, idx);
  69. }
  70. app.redirect('pay/index?id=' + id);
  71. },
  72. fail: function(res){
  73. app.toast('兑换失败!');
  74. }
  75. })
  76. },
  77. /**
  78. * 使用微信地址
  79. */
  80. chooseAddress: function(event){
  81. var that = this;
  82. var success = function(){
  83. wx.chooseAddress({
  84. success: function (res) {
  85. app.set(that,'addrObj', res);
  86. app.set(that,'isUseAddress', true);
  87. }
  88. })
  89. }
  90. wx.getSetting({
  91. success: function(res){
  92. if (res.authSetting['scope.address']){
  93. success();
  94. }else{
  95. wx.authorize({
  96. scope: 'scope.address',
  97. success: function () {
  98. success();
  99. },
  100. fail: function(res){
  101. that.authdialog.showDialog();
  102. }
  103. })
  104. }
  105. }
  106. })
  107. }
  108. })