order.js 11.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        success: false,
        fail: false,
        name: '',
        choudanNum: 0,
        addressId: 0,
        tel: '',
        area: '',
        address: '',
        cover_type: false,
        total: '',

        info: {
            address: {
                address: '',
                area: '',
                id: '',
                tel: '',
                name: ''
            }

        },
        isSubmitting: false // 控制按钮的提交状态
    },
    //加减商品数量
    num_change(e) {
        let that = this
        let num = Number(e.currentTarget.dataset.num)
        console.log(num)
        let type = e.currentTarget.dataset.type
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        let check = e.currentTarget.dataset.check
        let coudan = this.data.coudan || []
        let coudan_arr = this.data.coudan_arr || []
        let price = e.currentTarget.dataset.price;
        let price2 = e.currentTarget.dataset.price2;

        if (type == 1) {
            if (this.data.info.choudan * 100 < (price * 100)) {
                wx.showToast({
                    title: '当前剩余积分不足,请重新挑选',
                    icon: 'none'
                })
                return
            }
            num++
        } else {
            num--
            if (num < 0) {
                num = 0;
                return
            }
        }

        if (num < 1) {
            coudan.forEach((item, index) => {
                if (item.id == id) {
                    coudan.splice(index, 1)
                    coudan_arr.splice(index, 1)
                }
            })
        } else {
            if (type == 1 && num == 1) {
                //选中
                coudan.push({
                    id: id,
                    num: num
                })
                coudan_arr.push({
                    id: id,
                    num: num,
                    price: Number(price),
                    price2: Number(price2)
                })
            } else {
                coudan.forEach((item, index) => {
                    if (item.id == id) {
                        item.num = num
                        coudan_arr[index].num = num
                    }
                })
            }

        }
        that.setData({
            coudan: coudan,
            coudan_arr: coudan_arr,
            [`info.coudanProduct[${index}].num`]: num
            // [`info.coudanProduct[${index}].check`]: check
        })
        this.get_choudan()
    },
    // 计算凑单费用
    get_choudan() {
        let coudan_arr = this.data.coudan_arr
        let choudan_money = 0;
        let num = 0
        for (let obj of coudan_arr) {
            console.log(obj)
            console.log(obj.price2)
            if (obj.price2 != null) {
                console.log(111)
                choudan_money = (obj.price2 * 100 * obj.num) / 100 + choudan_money
            } else {
                console.log(222)
                choudan_money = (obj.price * 100 * obj.num) / 100 + choudan_money
            }
            num = num + obj.num
        }

        this.setData({
            choudan_money: choudan_money * 100,
            choudanNum: num,
            'info.choudan': this.data.info.syjf - choudan_money
            //  choudan_money: Math.floor(choudan_money* 100) / 100
        })
    },
    show_cover() {
        let that = this
        if (!that.data.cover_type) {
            if (that.data.info.status == 0) {
                that.setData({
                    fail: true
                })
                return
            }
            if (!that.data.info.address) {
                wx.showToast({
                    title: '请添加地址',
                    icon: 'none',
                    duration: 1000
                })
                return
            }
        }
        that.setData({
            cover_type: !this.data.cover_type
        })
    },
    // 去添加地址
    add_address() {
        wx.navigateTo({
            url: '/packageA/pages/address/address',
        })
    },


    //选择凑单商品
    chooseGoods(e) {
        // coudan 凑单商品组成的数组
        // coudan_type 凑单的状态  可能废弃
        let that = this
        let coudan = this.data.coudan ? this.data.coudan : []
        let coudan_arr = this.data.coudan_arr ? this.data.coudan_arr : []
        let check = e.currentTarget.dataset.check
        let num = e.currentTarget.dataset.num
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        let price = e.currentTarget.dataset.price;
        let price2 = e.currentTarget.dataset.price2;
        if (check) {
            check = 0
            coudan.forEach((item, index) => {
                if (item.id == id) {
                    coudan.splice(index, 1)
                    coudan_arr.splice(index, 1)
                }
            })
        } else {
            check = 1
            //选中
            coudan.push({
                id: id,
                num: num
            })
            coudan_arr.push({
                id: id,
                num: num,
                price: Number(price),
                price2: Number(price2)
            })
        }
        that.setData({
            coudan: coudan,
            coudan_arr: coudan_arr,
            [`info.coudanProduct[${index}].check`]: check
        })
        that.get_choudan()
    },

    //获取页面信息
    get_info(type, info, id) {
        let that = this
        let url = type == 4 ? app.interface.prev_pay : app.interface.now_prev_pay
        let params = {
            info: info
        }
        app.post(url, params).then((res) => {
            console.log('99999', res, Number(res.msg.total))
            res.msg.syjf = res.msg.choudan - res.msg.yunfei
            res.msg.choudan = res.msg.choudan - res.msg.yunfei
            res.msg.total = Number(res.msg.total * 100)
            res.msg.yunfei = Number(res.msg.yunfei * 100)

            let total = (res.msg.total + res.msg.yunfei).toFixed("1");
            console.log(total)

            that.setData({
                info: res.msg,
                total: total

            })
        })
    },


    //获取页面信息
    get_youfei(id) {
        let that = this
        let url = that.data.type == 4 ? app.interface.youfei1 : app.interface.youfei
        let params = {
            info: that.data.params,
            address_id: id
        }
        app.post(url, params).then((res) => {
            that.setData({
                "info.yunfei": Number(res.msg.yunfei) * 100
            })
        })
    },


    //点击支付
    submit() {
        let that = this
        let list = that.data.info.products
        let coudan = that.data.coudan
        let info = []
        // 检查是否已经提交中,防止多次点击
        if (that.data.isSubmitting) return;
        that.setData({ isSubmitting: true }); // 设置为提交中
        if (that.data.type == 4) {
            for (let obj of list) {
                info.push({
                    id: obj.product.id,
                    num: obj.product.num
                })
            }
        } else {
            for (let obj of list) {
                info.push({
                    id: obj.id,
                    num: obj.num
                })
            }
        }
        if (coudan && coudan.length > 0) {
            info = info.concat(coudan)
        }
        let url = app.interface.now_add
        let params = {
            address: that.data.info.address.id,
            info: JSON.stringify(info)
        }
        app.post(url, params).then((res) => {
            that.setData({
                cover_type: false
            })
            that.order_pay(res.msg)
        })
    },

    //开放订单支付
    order_pay(order_odd) {
        let that = this;
        let url = app.interface.order_pay
        let params = {
            order_odd: order_odd
        }
        app.post(url, params).then((res) => {
            console.log('resres',res);
            // that.payment(res.msg)
            if (res.msg.message == "支付成功") {
                // wx.showToast({
                //   title: '购买成功',
                //   icon:'none'
                // })
                this.setData({
                  success: true,
                  isSubmitting: false
                })
            } else if (res.msg.message == "余额不足") {
                this.setData({
                    fail: true,
                    isSubmitting: false
                })
            }
        })
    },
    payment(res) {
        var timeStamp = res.timeStamp.toString();
        wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success: function (res) {
                wx.redirectTo({
                    url: '/packageA/pages/order/order_list/order_list?status=3',
                })
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },
    //订单支付
    //   order_pay(order_odd) {
    //     let url = app.interface.order_pay
    //     let params = {
    //       order_odd: order_odd
    //     }
    //     app.post(url, params).then((res) => {
    //       this.setData({
    //         order: res.msg
    //       })
    //       if (res.msg.message == "支付成功") {
    //         // wx.showToast({
    //         //   title: '购买成功',
    //         //   icon:'none'
    //         // })
    //         this.setData({
    //           success: true
    //         })
    //       } else if (res.msg.message == "余额不足") {
    //         this.setData({
    //           fail: true
    //         })
    //       }
    //     })
    //   },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            type: options.type,
            params: options.info
        })
        this.get_info(options.type, options.info, options.id)
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        let pages = getCurrentPages();

        let currPage = pages[pages.length - 1]
        if (currPage.data.id) {
            this.setData({
                "info.address.address": currPage.data.address,
                "info.address.area": currPage.data.area,
                "info.address.id": currPage.data.id,
                "info.address.tel": currPage.data.tel,
                "info.address.name": currPage.data.name
            })
            this.get_youfei(currPage.data.id)
        }
        // if (wx.getStorageSync('no_address')) {
        //   wx.setStorageSync('no_address', false)
        //   this.setData({
        //     "info.address": null
        //   })
        // }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})