| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- const app = getApp()
- const api = require('../../../../request/api');
- const http = require('../../../../request/http');
- const utils = require('../../../../utils/util.js');
- const storeKeys = require('../../../../utils/storageKeys.js');
- Page({
- data: {
- isIphoneX: app.globalData.isIphoneX,
- isLoading: true, //是否处于加载
- mode: 'buyNow', //页面来源
- dataParams: {}, //参数对象
- model_addr: false,
- goodsObj: {}, //商品返回数据
- addressId: '', //地址id
- isDisabled: false, //是否禁用按钮
- remark: '', //备注
- orderId: '',
- buyNumber: 1, //购买数量
- buySelfNumber: 1, //自提购买数量
- cardModel: false,
- riceList: [],
- riceCardId: 0, //确认选中的米卡
- riceCardCheckItem: {}, //选择使用的米卡
- isCommit: false,
- orderType: 10, //配送方式 默认10 10-快递配送 20-门店自提
- formeType: 1, //1:拼团 2:裂变拼团 3:秒杀
- couponModel:false, //是否显示优惠卷弹窗
- couponList:[], //优惠卷列表
- couponId: 0, //确认选中的优惠卷id
- couponCheckItem: {}, //确认选中的优惠卷
- couponMoney:'0.00', //确认选中的优惠价格
- discountModel: false, //是否显示优惠明细弹窗
- showZpLowStock: false, // 赠品库存不足
- kimmyModel:false, //金米粒抵扣弹窗
- isUseGoldRice: 1, //是否使用金米粒 -1禁用 0不使用 1使用
- },
- onLoad: function (options) {
- if (options.data) {
- let dataObj = JSON.parse(options.data)
- if(dataObj.mode==='groupbuylb'){
- this.data.formeType = 2
- }
- console.log(dataObj)
- this.setData({
- dataParams: dataObj,
- mode: dataObj.mode,
- buyNumber: dataObj.goodsNum,
- buySelfNumber: dataObj.goodsNum,
- })
- console.log(this.data.mode)
- }
- },
- //切换配送方式
- changeTitleTab(e) {
- let index = e.currentTarget.dataset.index;
- if (index == this.data.orderType) {
- return;
- }
- this.setData({
- orderType: index,
- ['dataParams.goodsNum']: index == 10 ? this.data.buyNumber : this.data.buySelfNumber
- })
- this.getData();
- },
- //增加/删减购买数量 线上物流
- changeGoodsNumber: utils.throttle(function (e) {
- let state = e.currentTarget.dataset.state;
- let item = e.currentTarget.dataset.item;
- let buyNumber = this.data.buyNumber;
- if(this.dataParams.mode=='za'){
- return;
- }
- // console.log(item)
- // state:1增加 0删减
- if (state == 1) {
- if (item.skuInfo.stock_num <= 0) {
- utils.toast("库存不足")
- return;
- }
- if (buyNumber >= item.skuInfo.stock_num) {
- utils.toast("不能再加了,达到商品最大库存了")
- return;
- }
- buyNumber++
- } else {
- if (buyNumber <= 1) {
- utils.toast("不能再减了")
- return;
- }
- buyNumber--
- }
- this.setData({
- buyNumber,
- ['dataParams.goodsNum']: buyNumber
- })
- this.getData()
- }, 500),
- //增加/删减购买数量 门店自提
- changeGoodsSelfNumber: utils.throttle(function (e) {
- let state = e.currentTarget.dataset.state;
- let item = e.currentTarget.dataset.item;
- let buySelfNumber = this.data.buySelfNumber;
- if (state == 1) {
- if (item.skuInfo.stock_num <= 0) {
- utils.toast("库存不足")
- return;
- }
- if (buySelfNumber >= item.skuInfo.stock_num) {
- utils.toast("不能再加了,达到商品最大库存了")
- return;
- }
- buySelfNumber++
- } else {
- if (buySelfNumber <= 1) {
- utils.toast("不能再减了")
- return;
- }
- buySelfNumber--
- }
- this.setData({
- buySelfNumber,
- ['dataParams.goodsNum']: buySelfNumber
- })
- this.getData()
- }, 500),
- //监听商品数量输入 线上物流
- changeBuyNumber(e) {
- let buyNumber = e.detail.value.replace(/\s+/g, '');
- this.setData({
- buyNumber,
- ['dataParams.goodsNum']: buyNumber
- })
- },
- //监听商品数量输入 门店自提
- changeBuySelfNumber(e) {
- let buySelfNumber = e.detail.value.replace(/\s+/g, '');
- this.setData({
- buySelfNumber,
- ['dataParams.goodsNum']: buySelfNumber
- })
- },
- //商品数量输入失去焦点 线上物流
- numberBlur(e) {
- //输入的数量大于库存数时
- let item = e.currentTarget.dataset.item,
- that = this;
- if (parseInt(this.data.buyNumber) > parseInt(item.skuInfo.stock_num)) {
- this.setData({
- buyNumber: item.skuInfo.stock_num,
- ['dataParams.goodsNum']: item.skuInfo.stock_num
- })
- }
- setTimeout(() => {
- that.getData()
- }, 200)
- },
- //商品数量输入失去焦点 门店自提
- selfnumberBlur(e) {
- //输入的数量大于库存数时
- let item = e.currentTarget.dataset.item,
- that = this;
- if (parseInt(this.data.buySelfNumber) > parseInt(item.skuInfo.stock_num)) {
- this.setData({
- buySelfNumber: item.skuInfo.stock_num,
- ['dataParams.goodsNum']: item.skuInfo.stock_num
- })
- }
- setTimeout(() => {
- that.getData()
- }, 200)
- },
- toAddr() {
- wx.navigateTo({
- url: "/pages/user/pages/addressList/addressList?sourceState=1"
- })
- },
- detailInput: function (e) {
- this.setData({
- remark: e.detail.value
- })
- },
- //获取订单列表
- getData() {
- let that = this;
- wx.showLoading({
- title: "加载中",
- mask: true
- });
- let url = '',
- dataParams = this.data.dataParams
- if (!dataParams.goodsSkuId) {
- dataParams.goodsSkuId = 0
- }
- //本地分享人id
- let staffUserId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || "";
- // 推广监控ID
- let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID) || 0;
- /**
- * dataParams参数详解
- * mode:下单模式 buyNow:商品详情立即下单 bargain:砍价活动下单 cart:购物车下单
- * goodsId:商品id
- * goodsSkuId:商品规则id
- * goodsNum:购买数量
- * cartIds:购物车id集合(字符串逗号隔开)
- * addressId:用户选用的收获地址id
- * riceCardId:用户选用的米卡id 不使用传-1
- * couponId:用户选用的优惠券id 不使用传-1
- * staffUserId:上级推广人id
- * orderType:配送方式 默认10 10-快递配送 20-门店自提
- * isUseGoldRice:是否使用金米粒 默认1 -1禁用 0不使用 1使用
- * promotionMonitorId:渠道推广id
- */
- if(this.data.isUseGoldRice == -1){
- this.data.isUseGoldRice = 1;
- }
- //商品详情立即下单
- if (this.data.mode == 'buyNow') {
- url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
- '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId + '&riceCardId=' + this.data.riceCardId + '&couponId='+this.data.couponId +
- '&staffUserId=' + staffUserId + '&delivery=' + this.data.orderType+'&isUseGoldRice='+this.data.isUseGoldRice + "&promotionMonitorId="+promotionMonitorId
- } else if(this.data.mode == 'bargain'){
- //砍价活动下单
- url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
- '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId + '&bargainId=' + dataParams.bargainId +
- '&staffUserId=' + staffUserId + '&riceCardId=' + this.data.riceCardId + '&delivery=' + this.data.orderType+'&isUseGoldRice='+this.data.isUseGoldRice + "&promotionMonitorId="+promotionMonitorId
- }else if(this.data.mode == 'miaosha'){
- //秒杀活动下单
- url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
- '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId +
- '&staffUserId=' + staffUserId + '&delivery=' + this.data.orderType + '&msId=' + dataParams.msId
- console.log(url)
- }else if(this.data.mode == 'za'){
- // 买一赠一订单
- url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
- '&goodsNum=' + dataParams.goodsNum + '&addressId=' + this.data.addressId +
- '&staffUserId=' + staffUserId + '&delivery=' + this.data.orderType + '&zaGoodsId=' + dataParams.zaGoodsId + '&riceCardId=' + this.data.riceCardId
- console.log(url)
- }else if(this.data.mode == 'zareceive'){
- // 买一赠一领取
- url = '/api/checkout/order?mode=' + dataParams.mode + '&addressId=' + this.data.addressId +
- '&staffUserId=' + staffUserId + '&signStr=' + dataParams.signStr
- console.log(url)
- }else if(this.data.mode == 'pointsExchange'){
- // 积分兑换
- url = '/api/checkout/order?mode=' + dataParams.mode + '&goodsId=' + dataParams.goodsId + '&goodsSkuId=' + dataParams.goodsSkuId +
- '&goodsNum=' + dataParams.goodsNum+
- '&staffUserId=' + staffUserId
- console.log(url)
- }
- else {
- // 购物车下单
- url = '/api/checkout/order?mode=' + dataParams.mode + '&cartIds=' + dataParams.cartIds + '&addressId=' + this.data.addressId +
- '&riceCardId=' + this.data.riceCardId +'&couponId='+this.data.couponId+ '&staffUserId=' + staffUserId +
- '&delivery=' + this.data.orderType+'&isUseGoldRice='+this.data.isUseGoldRice + "&promotionMonitorId="+promotionMonitorId
- }
- // http://ysctest.cookhome.club/api/checkout/order?mode=za&cartIds=undefined&addressId=&riceCardId=0&couponId=0&staffUserId=&delivery=10&isUseGoldRice=1&promotionMonitorId=0
- http.request({
- url: api.URL + url,
- method: 'GET',
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- console.log(res)
- wx.hideLoading();
- let orderObj = res.data.data.order;
- if (this.data.orderType == 20 && !orderObj.isPickup) {
- this.setData({
- orderType: 10,
- ['goodsObj.isPickup']: false
- })
- return;
- }
- //来源直接购买 切换时库存大于输入库存时 重新赋值输入的值
- if (this.data.mode == "buyNow") {
- let goodsList = orderObj.goodsList || []
- let buyNumber = this.data.buyNumber;
- let buySelfNumber = this.data.buySelfNumber;
- for (let i = 0; i < goodsList.length; i++) {
- let item = goodsList[i];
- if (!item.stock_enough) {
- // 商品类型,10普通商品;20赠品;30套餐商品
- if(item.goods_type != 20){
- if(this.data.orderType == 20){
- this.setData({
- buySelfNumber: item.skuInfo.stock_num || buySelfNumber
- })
- }else{
- this.setData({
- buyNumber: item.skuInfo.stock_num || buyNumber
- })
- }
- this.setData({
- ['dataParams.goodsNum']: item.skuInfo.stock_num
- })
- }
- break;
- }
- }
- }
- //选中的优惠价id
- let couponId = res.data.data.order.couponId;
- let couponCheckItem = {couponId};
- //选中的米卡id
- let riceCardId = res.data.data.order.riceCardId;
- let riceCardCheckItem = {id:riceCardId}
- //赋予是否选用金米粒默认值
- let isUseGoldRice = res.data.data.order.isUseGoldRice;
- this.setData({
- goodsObj: res.data.data.order,
- couponMoney:res.data.data.order.couponMoney,
- couponId,
- riceCardId,
- couponCheckItem,
- riceCardCheckItem,
- isLoading: false,
- riceList: res.data.data.order.userRiceCardList,
- couponList: res.data.data.order.couponList,
- isUseGoldRice
- })
- if (orderObj.errorStatus == 423 || orderObj.hasError || orderObj.errorStatus == 424) {
- this.setData({
- isDisabled: true
- })
- } else {
- this.setData({
- isDisabled: false
- })
- }
- if (orderObj.errorStatus == 423) {
- this.setData({
- model_addr: true
- })
- } else if (orderObj.errorStatus == 424) {
- this.setData({
- showZpLowStock: true
- })
- } else {
- this.setData({
- model_addr: false,
- showZpLowStock: false
- })
- }
- },
- error: (res) => {},
- })
- },
- // 清除无货商品
- cleanInvalidGoods: utils.throttle(function (e) {
- let invalidGoodsList = this.data.goodsObj.invalidGoodsList || [];
- let cartIds = [];
- invalidGoodsList && invalidGoodsList.forEach((item) => {
- cartIds.push(item.cart_id);
- })
- console.log(cartIds)
- let data = {
- cartIds
- }
- wx.showLoading({
- title: "加载中"
- });
- http.request({
- url: api.URL + '/api/cart/delete',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getData();
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- }),
- clickModel() {
- this.setData({
- model_addr: false
- })
- },
- commit: utils.throttle(function (e) {
- if (this.data.orderType == 20 && this.data.goodsObj.invalidGoodsList && this.data.goodsObj.invalidGoodsList.length > 0) {
- utils.toast("请先清除无货商品!")
- return;
- }
- let that = this;
- if(this.data.orderType == 10 && !this.data.goodsObj.address){
- utils.toast("请先填写收货地址")
- return;
- }
- wx.showLoading({
- title: "加载中"
- });
- //本地分享人id
- let staffUserId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || "";
- let dataParams = this.data.dataParams
- dataParams.addressId = this.data.addressId
- dataParams.remark = this.data.remark
- dataParams.riceCardId = this.data.riceCardId
- dataParams.couponId = this.data.couponId
- dataParams.delivery = this.data.orderType
- dataParams.staffUserId = staffUserId
- dataParams.isUseGoldRice=this.data.isUseGoldRice
- if (!dataParams.goodsSkuId) {
- dataParams.goodsSkuId = 0
- }
-
- /**
- * 对接地址:https://ad.weixin.qq.com/main.html#/docs/232
- * 参数说明地址:https://ad.weixin.qq.com/main.html#/docs/232?branch_id=2IhtO
- * clickId 存在则是来源微信广告
- */
- let clickId = utils.getStorageSync(storeKeys.CLICKID) || "";
- if(clickId){
- let pages = getCurrentPages()
- let currentPage = pages[pages.length-1]
- dataParams.adUrl = "http://www."+"/"+currentPage.route
- dataParams.adClickId = clickId
- }
- // 推广监控ID
- let promotionMonitorId = utils.getStorageSync(storeKeys.SOURECID)
- console.log("下单监控id"+promotionMonitorId)
- if(promotionMonitorId){
- dataParams.promotionMonitorId = promotionMonitorId
- }
- console.log(dataParams)
- http.request({
- url: api.URL + '/api/checkout/submit',
- method: 'POST',
- token: utils.getStorageSync(storeKeys.TOKEN),
- data: dataParams,
- success: (res) => {
- wx.hideLoading();
- let orderId = res.data.data.order_id
- // "pay_type": 20, //支付类型 10余额支付 20微信支付 30现金卡支付 40买一赠一领取
- if (res.data.data.pay_type == 20) {
- that.setData({
- isCommit: true
- })
- that.wxPayment(res.data.data.payment, orderId);
- }
- if (res.data.data.pay_type == 30 || res.data.data.pay_type == 40 || res.data.data.pay_type == 50) {
- utils.toast('支付成功');
- app.getCartNumber();
- that.toNextPage(orderId)
- }
- },
- error: (res) => {
- console.log(res)
- that.setData({
- isDisabled: true
- })
- },
- })
- }, 800),
- toNextPage(orderId){
- let url = "/pages/cart/pages/paySuccess/paySuccess?orderType=" + this.data.orderType + "&orderId=" + orderId
- if(this.data.mode == 'za'){
- url = url + '&type=3'
- }
- if(this.data.mode == 'zareceive'){
- url = url + '&zareceive=1'
- }
- wx.reLaunch({
- url: url
- })
- },
- //调起微信支付
- wxPayment(data, orderId) {
- let _this = this;
- wx.requestPayment({
- nonceStr: data.nonceStr,
- package: 'prepay_id=' + data.prepay_id,
- paySign: data.paySign,
- signType: "MD5",
- timeStamp: data.timeStamp,
- success(res) {
- if (res.errMsg == "requestPayment:ok") {
- app.getCartNumber();
- utils.toast('支付成功');
- _this.toNextPage(orderId);
- // wx.reLaunch({
- // url: "/pages/cart/pages/paySuccess/paySuccess?orderType=" + _this.data.orderType
- // })
- }
- },
- fail(res) {
- if (res.errMsg == "requestPayment:fail cancel") {
- app.getCartNumber();
- utils.toast('已取消支付');
- // wx.reLaunch({
- // url: "/pages/cart/pages/noPayment/noPayment?orderId=" + orderId
- // })
- }
- }
- })
- },
- // 选择米卡
- toCheckRiceCard() {
- //选中的米卡id
- let riceCardId = this.data.goodsObj.riceCardId;
- let riceCardCheckItem = {id:riceCardId}
- this.setData({
- cardModel: true,
- riceCardCheckItem
- })
- },
- hideModel() {
- this.setData({
- cardModel: false
- })
- },
- checkRiceCard(e) {
- let item0 = {
- id: -1
- }
- let item = e.currentTarget.dataset.item || item0;
- console.log(item)
- this.setData({
- riceCardCheckItem: item
- })
- },
- confirmRiceCard() {
- console.log(this.data.riceCardCheckItem.id)
- if (!this.data.riceCardCheckItem.id) {
- utils.toast("请选择要使用的米卡")
- return;
- }
- this.setData({
- cardModel: false,
- riceCardId: this.data.riceCardCheckItem.id
- })
- this.getData()
- },
- //选择优惠券
- checkCoupon(e) {
- let couponId = e.currentTarget.dataset.id || -1;
- let couponCheckItem = {couponId}
- this.setData({
- couponCheckItem
- })
- },
- //确认优惠券
- confirmCoupon() {
- if (!this.data.couponCheckItem.couponId) {
- utils.toast("请选择要使用的优惠券")
- return;
- }
- this.setData({
- couponModel: false,
- couponId: this.data.couponCheckItem.couponId
- })
- this.getData()
- },
- onShow: function () {
- if (this.data.isCommit) {
- return;
- }
- this.getData()
- },
- //弹出选择优惠卷
- couponModel(){
- if(!this.data.couponList || this.data.couponList.length <= 0){
- return;
- }
- let couponId = this.data.goodsObj.couponId;
- let couponCheckItem = {couponId};
- this.setData({
- couponModel:true,
- couponCheckItem
- })
- },
- //隐藏选择优惠券
- hideCouponModel(){
- this.setData({
- couponModel: false
- })
- },
- //取消下单
- cancel: utils.throttle(function (e) {
- wx.navigateBack({
- delta: 1
- })
- }),
- //显示优惠详情
- showDiscountModel(){
- this.setData({
- discountModel:true
- })
- },
- //隐藏优惠详情
- hideDiscountModel(){
- this.setData({
- discountModel:false
- })
- },
- // 无赠品弹窗 返回上一页
- closeZpModel() {
- wx.navigateBack({
- delta: 1,
- })
- },
- //显示选择金米粒
- showKimmyModel(){
- this.setData({
- kimmyModel:true,
- isUseGoldRice:this.data.goodsObj.isUseGoldRice
- })
- },
- //隐藏选择金米粒
- hideKimmyModel(){
- this.setData({
- kimmyModel:false
- })
- },
- //切换选择金米粒
- checkKimmy(e) {
- //禁用金米粒
- if(this.data.goodsObj.isUseGoldRice == -1){
- return;
- }
- let isUseGoldRice = e.currentTarget.dataset.id || 0;
-
- this.setData({
- isUseGoldRice
- })
- },
- //确认金米粒
- confirmKimmy() {
- this.setData({
- kimmyModel: false
- })
- //禁用金米粒
- if(this.data.goodsObj.isUseGoldRice == -1){
- return;
- }
- console.log(this.data.isUseGoldRice)
- this.getData()
- },
- })
|