123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- const app = getApp();
- Page({
- authdialog: null,
- product_ids: '',
- product_num: '',
- data: {
- isUseAddress: false,
- settlement: 0.00
- },
- onLoad: function (options) {
- var that = this;
- app.showLoading();
- //获取自定义授权组件
- that.authdialog = this.selectComponent("#authdialog");
- app.request('product.api.carts',{},{
- success: function(data, res){
- app.setList(that, data.carts);
- that.doSettlement();
- app.hideLoading();
- }
- });
- },
- /**
- * 删除订单
- */
- delOrder: function(e){
- var that = this;
- var pid;
- if (e == null && arguments.length==2){
- pid = arguments[1];
- }else{
- pid = app.getData(e).id;
- }
- var delfun = function(){
- app.request('product.api.delCarts',{
- product_id: pid
- },{
- success: function(data, res){
- app.toast('删除商品成功!');
- that.onLoad();
- },
- fail: function(res){
- app.toast('删除商品失败!');
- }
- });
- }
- wx.showModal({
- content: '确认删除该商品吗?',
- success: function(res){
- if (res.confirm){
- delfun();
- }
- }
- })
- },
- /**
- * 减少商品数量>0 and <=1
- */
- doSub: function(e){
- var id = app.getData(e).id;
- var list = this.data.list;
- for(var i=0; i<list.length; i++){
- if (list[i].product_id == id){
- if(list[i].num-1 <= 0){
- list[i].num = 1;
- this.delOrder(null, id);
- }else{
- list[i].num -= 1;
- }
- break;
- }
- }
- app.setList(this, list);
- this.doSettlement();
- },
- /**
- * 增加商品数量>1 and <=库存
- */
- doAdd: function(e){
- var id = app.getData(e).id;
- var list = this.data.list;
- for (var i = 0; i < list.length; i++) {
- if (list[i].product_id == id) {
- ++list[i].num;
- break;
- }
- }
- app.setList(this, list);
- this.doSettlement();
- },
- /**
- * 结算金额
- */
- doSettlement: function(){
- var list = this.data.list;
- var count=0;
- this.product_ids = '';
- this.product_num = '';
- for(var i=0; i<list.length; i++){
- var product_id = list[i].product_id;
- var pay_price = list[i].product.pay_price;
- var num = list[i].num;
- count += pay_price*num;
- if(i==0){
- this.product_ids += product_id;
- this.product_num += num;
- }else{
- this.product_ids += ',' + product_id;
- this.product_num += ',' + num;
- }
- }
- app.set(this, 'settlement', count.toFixed(2));
- },
- /**
- * 立即支付
- */
- doPay: function(){
- var that = this;
- var addrObj = that.data.addrObj;
- if (!addrObj){
- app.toast('请选择收货地址');
- return ;
- }
- app.showLoading();
- app.request('product.api.buy',{
- product_id: that.product_ids,
- num: that.product_num,
- name: addrObj.userName,
- address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
- mobile: addrObj.telNumber,
- invite_uid: app.getInviteUid()
- },{
- success:function(data, res){
- var pay = data.pay;
- console.log('requestPayment-1', pay);
- wx.requestPayment({
- timeStamp: pay.time,
- nonceStr: pay.nonce_str,
- package: 'prepay_id=' + pay.prepay_id,
- signType: pay.sign_type,
- paySign: pay.sign,
- success: function(){//微信支付成功
- app.saveInviteUid('');
- var id = that.product_ids;
- let idx = id.indexOf(',');
- if (idx > -1) {
- id = id.substring(0, idx);
- }
- //跳转支付结果页
- app.redirect('pay/index?id=' + id);
- },
- fail: function(res){//微信支付失败
- app.toast('支付失败!');
- console.log('支付失败!', res);
- }
- })
- },
- fail: function(res){
- app.toast('支付失败!');
- },
- complete: function (res) {
- app.hideLoading();
- }
- })
- },
- /**
- * 使用微信地址
- */
- chooseAddress: function(event){
- var that = this;
- var success = function(){
- wx.chooseAddress({
- success: function (res) {
- app.set(that,'addrObj', res);
- app.set(that,'isUseAddress', true);
- }
- })
- }
- wx.getSetting({
- success: function(res){
- if (res.authSetting['scope.address']){
- success();
- }else{
- wx.authorize({
- scope: 'scope.address',
- success: function () {
- success();
- },
- fail: function(res){
- that.authdialog.showDialog();
- }
- })
- }
- }
- })
- }
- })
|