123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- Page({
- dever: getApp().dever,
- authdialog: null,
- product_ids: '',
- product_num: '',
- data: {
- isUseAddress: false,
- settlement: 0.00
- },
- onLoad: function (options) {
- var that = this;
- that.dever.showLoading();
- //获取自定义授权组件
- that.authdialog = this.selectComponent("#authdialog");
- that.dever.request('product.api.carts',{
- signature: that.dever.getSignature()
- },{
- success: function(data, res){
- that.dever.setList(that, data.carts);
- that.doSettlement();
- that.dever.hideLoading();
- }
- });
- },
- /**
- * 删除订单
- */
- delOrder: function(e){
- var that = this;
- var pid;
- if (e == null && arguments.length==2){
- pid = arguments[1];
- }else{
- pid = e.currentTarget.dataset.id;
- }
- var delfun = function(){
- that.dever.request('product.api.delCarts',{
- json: 1,
- signature: that.dever.getSignature(),
- product_id: pid
- },{
- success: function(data, res){
- that.dever.alert('删除商品成功!');
- that.onLoad();
- },
- fail: function(res){
- that.dever.alert('删除商品失败!');
- }
- });
- }
- wx.showModal({
- content: '确认删除该商品吗?',
- success: function(res){
- if (res.confirm){
- delfun();
- }
- }
- })
- },
- /**
- * 减少商品数量>0 and <=1
- */
- doSub: function(e){
- var id = e.currentTarget.dataset.id;
- var list = this.data.list;
- for(var i=0; i<list.length; i++){
- if (list[i].product_id == id){
- //list[i].num = list[i].num-1<=0?1:list[i].num-1;
- if(list[i].num-1 <= 0){
- list[i].num = 1;
- this.delOrder(null, id);
- }else{
- list[i].num -= 1;
- }
- break;
- }
- }
- this.dever.setList(this, list);
- this.doSettlement();
- },
- /**
- * 增加商品数量>1 and <=库存
- */
- doAdd: function(e){
- var id = e.currentTarget.dataset.id;
- var list = this.data.list;
- for (var i = 0; i < list.length; i++) {
- if (list[i].product_id == id) {
- ++list[i].num;
- break;
- }
- }
- this.dever.setList(this, list);
- this.doSettlement();
- },
- /**
- * 结算金额
- */
- doSettlement: function(){
- var list = this.data.list;
- var count=0;
- 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;
- }
- }
- this.dever.set(this, 'settlement', count.toFixed(2));
- },
- /**
- * 去支付
- */
- doPay: function(e){
- var that = this;
- var addrObj = that.data.addrObj;
- if (!addrObj){
- that.dever.alert('请选择收货地址');
- }
- that.dever.request('product.api.buy',{
- signature: that.dever.getSignature(),
- product_id: that.product_ids,
- num: that.product_num,
- name: addrObj.userName,
- address: addrObj.provinceName + addrObj.cityName + addrObj.countyName + addrObj.detailInfo,
- mobile: addrObj.telNumber
- },{
- success:function(data, res){
- that.dever.redirect('pay/index');
- that.dever.log('doPay success', res);
- },
- fail: function(res){
- that.dever.alert('支付失败!');
- that.dever.log('doPay fail', res);
- }
- });
- },
- /**
- * 使用微信地址
- */
- chooseAddress: function(event){
- var that = this;
- var success = function(){
- wx.chooseAddress({
- success: function (res) {
- that.dever.log('wx.chooseAddress', res);
- that.dever.set(that,'addrObj', res);
- that.dever.set(that,'isUseAddress', true);
- },
- fail: function (res) {
- that.dever.log('wx.chooseAddress', 'fail');
- }
- })
- }
- 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();
- }
- })
- }
- }
- })
- }
- })
|