接口请求

接口请求

对请求做相对应判断处理,交互更合理。

普通请求

  • vue发送请求(借助cube-ui做简单交互)

    getData:function () {
        const pop = this.$createToast({
            time: 10000,
        });
        pop.show()
        this.axios.get('/list', {
            params: {
               
            }
        }).then(res => {
            var datas = res.data;
            pop.hide();
             if (datas.error_code == 0) {
               var obj = datas.data;
               //.....  
               } else {
               this.info = this.$createToast({
               txt: '查询失败',
               type: 'txt'
               })
                 this.info.show();
               }
        }).catch(error => {
            this.toast = this.$createToast({
                txt: ' 请求错误:storelist',
                type: 'txt'
            })
            this.toast.show();
        })
    }
    
  • 小程序发送请求

      getData: function() {
            wx.showLoading({
                title: '加载中',
            })
            wx.request({
                url: this.data.config + '/list',
                data: {
                },
                success: (data) => {
                    var datas = data.data;
                    wx.hideLoading();
                    if (datas.error_code == 0) {
                        var obj = datas.data;
                        //...
                    } else {
                        wx.showToast({
                            title: datas.error_msg,
                            icon: 'none'
                        })
                    }
                },
                fail: (res) => {
                    console.log(res)
                    wx.showToast({
                        title: '请求错误:list',
                        icon: 'none'
                    })
                }
            })
        }
    

模态框交互(cube-ui)

  • vue

      delete: function (e) {
                    var keys = e.target.dataset.index;
                    this.$createDialog({
                        type: 'confirm',
                        title: '删除订单',
                        content: '确认删除该订单?',
                        onConfirm: () => {
                            const pop = this.$createToast({
                                time: 10000,
                            });
                            pop.show();
                            this.axios.get('/delorder', {
                                params: {
                                }
                            }).then(res => {
                                var datas = res.data;
                                var info = datas.error_msg;
                                pop.hide();
                                if (datas.error_code == 0) {
                                    this.info = this.$createToast({
                                        txt: '删除成功',
                                        type: 'txt'
                                    })
                                    this.info.show();
                                    orderList[keys].hide = true;
                                    this.orderList = orderList.filter((item) => {
                                        return item.hide != true;
                                    });
                                } else {
                                    this.info = this.$createToast({
                                        txt: '操作失败' + info,
                                        type: 'txt'
                                    })
                                    this.info.show();
                                }
                            }).catch(error => {
                                this.toast = this.$createToast({
                                    txt: '请求错误:delorder',
                                    type: 'txt'
                                })
                                this.toast.show();
                            })
                        }
                    }).show()
                }
    
  • 小程序

      deleteOrder: function(e) {
            var keys = e.currentTarget.dataset.index;
            var orderList = this.data.orderList;
            wx.showModal({
                title: '删除订单',
                content: '确认删除该订单?',
                success: (res) => {
                    if (res.confirm) {
                        wx.showLoading({
                            title: '删除中..',
                        })
                     wx.request({
                            url: this.data.config + '/delorder',
                            data: {
                            },
                            success: (data) => {
                                var datas = data.data;
                                wx.hideLoading();
                                if (datas.error_code == 0) {
                                    wx.showToast({
                                        title: '删除成功'
                                    })
                                    orderList[keys].hide = true;
                                    this.setData({
                                        orderList: orderList
                                    })
                                } else {
                                    wx.showToast({
                                        title: '操作失败:' + datas.error_msg,
                                        icon: 'none'
                                    })
                                }
                            }
                        })
                    }
                }
            })
        },
    

你可能感兴趣的:(接口请求)