index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Page({
  2. dever: getApp().dever,
  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. that.dever.showLoading();
  13. //获取自定义授权组件
  14. that.authdialog = this.selectComponent("#authdialog");
  15. //获取兑换商品信息
  16. that.cash_code = options.code;
  17. that.dever.request('product.api.checkCode',{
  18. signature: that.dever.getSignature(),
  19. code: that.cash_code
  20. },{
  21. success: function(data, res){
  22. that.dever.setList(that, data.product);
  23. that.doSettlement();
  24. that.dever.hideLoading();
  25. },
  26. fail: function(res){
  27. that.dever.log('product.api.checkCode', res);
  28. }
  29. });
  30. },
  31. /**
  32. * 结算金额
  33. */
  34. doSettlement: function(){
  35. var list = this.data.list;
  36. var count=0;
  37. for(var i=0; i<list.length; i++){
  38. var product_id = list[i].id;
  39. var pay_price = list[i].pay_price;
  40. count += pay_price*1;
  41. if(i==0){
  42. this.product_ids += product_id;
  43. }else{
  44. this.product_ids += ',' + product_id;
  45. }
  46. }
  47. this.dever.set(this, 'settlement', count.toFixed(2));
  48. },
  49. /**
  50. * 去支付
  51. */
  52. doCash: function(e){
  53. var that = this;
  54. var addrObj = that.data.addrObj;
  55. if (!addrObj) {
  56. that.dever.alert('请选择收货地址');
  57. }
  58. that.dever.request('product.api.code',{
  59. json: 1,
  60. signature: that.dever.getSignature(),
  61. product_id: that.product_ids,
  62. code: that.cash_code,
  63. name: addrObj.userName,
  64. address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
  65. mobile: addrObj.telNumber
  66. },{
  67. success:function(data, res){
  68. that.dever.redirect('pay/index');
  69. that.dever.log('doCash success', res);
  70. },
  71. fail: function(res){
  72. that.dever.alert('兑换失败!');
  73. that.dever.log('doCash fail', res);
  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. that.dever.log('wx.chooseAddress', res);
  86. that.dever.set(that,'addrObj', res);
  87. that.dever.set(that,'isUseAddress', true);
  88. },
  89. fail: function (res) {
  90. that.dever.log('wx.chooseAddress', 'fail');
  91. }
  92. })
  93. }
  94. wx.getSetting({
  95. success: function(res){
  96. if (res.authSetting['scope.address']){
  97. success();
  98. }else{
  99. wx.authorize({
  100. scope: 'scope.address',
  101. success: function () {
  102. success();
  103. },
  104. fail: function(res){
  105. that.authdialog.showDialog();
  106. }
  107. })
  108. }
  109. }
  110. })
  111. }
  112. })