123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- const app = getApp()
- const CONFIG = require('../../config.js')
- const WXAPI = require('apifm-wxapi')
- const AUTH = require('../../utils/auth')
- const wxpay = require('../../utils/pay.js')
- Page({
- data: {
- totalScoreToPay: 0,
- goodsList: [],
- isNeedLogistics: 0, // 是否需要物流信息
- yunPrice: 0,
- allGoodsAndYunPrice: 0,
- goodsJsonStr: "",
- orderType: "", //订单类型,购物车下单或立即支付下单,默认是购物车,
- pingtuanOpenId: undefined, //拼团的话记录团号
- hasNoCoupons: true,
- coupons: [],
- couponAmount: 0, //优惠券金额
- curCoupon: null, // 当前选择使用的优惠券
- curCouponShowText: '请选择使用优惠券', // 当前选择使用的优惠券
- peisongType: 'kd', // 配送方式 kd,zq 分别表示快递/到店自取
- remark: '',
- shopIndex: -1,
- pageIsEnd: false,
- bindMobileStatus: 0, // 0 未判断 1 已绑定手机号码 2 未绑定手机号码
- userScore: 0, // 用户可用积分
- deductionScore: '0' // 本次交易抵扣的积分数
- },
- onShow(){
- if (this.data.pageIsEnd) {
- return
- }
- AUTH.checkHasLogined().then(isLogined => {
- if (isLogined) {
- this.doneShow()
- } else {
- AUTH.authorize().then(res => {
- this.doneShow()
- })
- }
- })
- AUTH.wxaCode().then(code => {
- this.data.code = code
- })
- },
- async doneShow() {
- let goodsList = [];
- const token = wx.getStorageSync('token')
- //立即购买下单
- if ("buyNow" == this.data.orderType) {
- var buyNowInfoMem = wx.getStorageSync('buyNowInfo');
- this.data.kjId = buyNowInfoMem.kjId;
- if (buyNowInfoMem && buyNowInfoMem.shopList) {
- goodsList = buyNowInfoMem.shopList
- }
- } else {
- //购物车下单
- const res = await WXAPI.shippingCarInfo(token)
- if (res.code == 0) {
- goodsList = res.data.items.filter(ele => { return ele.selected })
- }
- }
- this.setData({
- goodsList,
- peisongType: this.data.peisongType
- });
- this.initShippingAddress()
- this.userAmount()
- },
- onLoad(e) {
- let _data = {
- isNeedLogistics: 1
- }
- if (e.orderType) {
- _data.orderType = e.orderType
- }
- if (e.pingtuanOpenId) {
- _data.pingtuanOpenId = e.pingtuanOpenId
- }
- this.setData(_data)
- this.getUserApiInfo()
- },
- async userAmount() {
- const res = await WXAPI.userAmount(wx.getStorageSync('token'))
- if (res.code == 0) {
- this.setData({
- balance: res.data.balance,
- userScore: res.data.score
- })
- }
- },
- getDistrictId: function (obj, aaa) {
- if (!obj) {
- return "";
- }
- if (!aaa) {
- return "";
- }
- return aaa;
- },
- remarkChange(e){
- this.data.remark = e.detail.value
- },
- async goCreateOrder(){
- // 检测实名认证状态
- if (wx.getStorageSync('needIdCheck') == 1) {
- console.log(123);
-
- const res = await WXAPI.userDetail(wx.getStorageSync('token'))
- if (res.code == 0 && !res.data.base.isIdcardCheck) {
- wx.navigateTo({
- url: '/pages/idCheck/index',
- })
- return
- }
- }
- const subscribe_ids = wx.getStorageSync('subscribe_ids')
- if (subscribe_ids) {
- wx.requestSubscribeMessage({
- tmplIds: subscribe_ids.split(','),
- success(res) {
-
- },
- fail(e) {
- console.error(e)
- },
- complete: (e) => {
- this.createOrder(true)
- },
- })
- } else {
- this.createOrder(true)
- }
- },
- createOrder: function (e) {
- var that = this;
- var loginToken = wx.getStorageSync('token') // 用户登录 token
- var remark = this.data.remark; // 备注信息
- let postData = {
- token: loginToken,
- goodsJsonStr: that.data.goodsJsonStr,
- remark: remark,
- peisongType: that.data.peisongType,
- deductionScore: this.data.deductionScore
- };
- if (that.data.kjId) {
- postData.kjid = that.data.kjId
- }
- if (that.data.pingtuanOpenId) {
- postData.pingtuanOpenId = that.data.pingtuanOpenId
- }
- if (postData.peisongType == 'kd' && that.data.curAddressData && that.data.curAddressData.provinceId) {
- postData.provinceId = that.data.curAddressData.provinceId;
- }
- if (postData.peisongType == 'kd' && that.data.curAddressData && that.data.curAddressData.cityId) {
- postData.cityId = that.data.curAddressData.cityId;
- }
- if (postData.peisongType == 'kd' && that.data.curAddressData && that.data.curAddressData.districtId) {
- postData.districtId = that.data.curAddressData.districtId;
- }
- if (e && that.data.isNeedLogistics > 0 && postData.peisongType == 'kd') {
- if (!that.data.curAddressData) {
- wx.hideLoading();
- wx.showToast({
- title: '请设置收货地址',
- icon: 'none'
- })
- return;
- }
- if (postData.peisongType == 'kd') {
- postData.address = that.data.curAddressData.address;
- postData.linkMan = that.data.curAddressData.linkMan;
- postData.mobile = that.data.curAddressData.mobile;
- postData.code = that.data.curAddressData.code;
- }
- }
- if (that.data.curCoupon) {
- postData.couponId = that.data.curCoupon.id;
- }
- if (!e) {
- postData.calculate = "true";
- } else {
- if(postData.peisongType == 'zq' && this.data.shops && this.data.shopIndex == -1) {
- wx.showToast({
- title: '请选择自提门店',
- icon: 'none'
- })
- return;
- }
- const extJsonStr = {}
- if(postData.peisongType == 'zq') {
- if(!this.data.name) {
- wx.showToast({
- title: '请填写联系人',
- icon: 'none'
- })
- return;
- }
- if(!this.data.mobile) {
- wx.showToast({
- title: '请填写联系电话',
- icon: 'none'
- })
- return;
- }
- extJsonStr['联系人'] = this.data.name
- extJsonStr['联系电话'] = this.data.mobile
- }
- if(postData.peisongType == 'zq' && this.data.shops) {
- postData.shopIdZt = this.data.shops[this.data.shopIndex].id
- postData.shopNameZt = this.data.shops[this.data.shopIndex].name
- }
- postData.extJsonStr = JSON.stringify(extJsonStr)
- }
- WXAPI.orderCreate(postData).then(function (res) {
- that.data.pageIsEnd = true
- if (res.code != 0) {
- that.data.pageIsEnd = false
- wx.showModal({
- title: '错误',
- content: res.msg,
- showCancel: false
- })
- return;
- }
- if (e && "buyNow" != that.data.orderType) {
- // 清空购物车数据
- const keyArrays = []
- that.data.goodsList.forEach(ele => {
- keyArrays.push(ele.key)
- })
- WXAPI.shippingCarInfoRemoveItem(loginToken, keyArrays.join())
- }
- if (!e) {
- let hasNoCoupons = true
- let coupons = null
- if (res.data.couponUserList) {
- hasNoCoupons = false
- res.data.couponUserList.forEach(ele => {
- let moneyUnit = '元'
- if (ele.moneyType == 1) {
- moneyUnit = '%'
- }
- if (ele.moneyHreshold) {
- ele.nameExt = ele.name + ' [消费满' + ele.moneyHreshold + '元可减' + ele.money + moneyUnit +']'
- } else {
- ele.nameExt = ele.name + ' [减' + ele.money + moneyUnit + ']'
- }
- })
- coupons = res.data.couponUserList
- }
- // 计算积分抵扣规则 userScore
- let scoreDeductionRules = res.data.scoreDeductionRules
- if (scoreDeductionRules) {
- // 如果可叠加,计算可抵扣的最大积分数
- scoreDeductionRules.forEach(ele => {
- if (ele.loop) {
- let loopTimes = Math.floor(that.data.userScore / ele.score) // 按剩余积分取最大
- let loopTimesMax = Math.floor((res.data.amountTotle + res.data.deductionMoney) / ele.money) // 按金额取最大
- if (loopTimes > loopTimesMax) {
- loopTimes = loopTimesMax
- }
- ele.score = ele.score * loopTimes
- ele.money = ele.money * loopTimes
- }
- })
- // 剔除积分数为0的情况
- scoreDeductionRules = scoreDeductionRules.filter(ele => {
- return ele.score > 0
- })
- }
-
- that.setData({
- totalScoreToPay: res.data.score,
- isNeedLogistics: res.data.isNeedLogistics,
- allGoodsAndYunPrice: res.data.amountReal,
- yunPrice: res.data.amountLogistics,
- hasNoCoupons,
- coupons,
- deductionMoney: res.data.deductionMoney,
- couponAmount: res.data.couponAmount,
- scoreDeductionRules
- });
- that.data.pageIsEnd = false
- return;
- }
- that.processAfterCreateOrder(res)
- })
- },
- async processAfterCreateOrder(res) {
- // 直接弹出支付,取消支付的话,去订单列表
- const balance = this.data.balance
- if (balance || res.data.amountReal*1 == 0) {
- // 有余额
- const money = (res.data.amountReal * 1 - balance*1).toFixed(2)
- if (money <= 0) {
- // 余额足够
- wx.showModal({
- title: '请确认支付',
- content: `您当前可用余额¥${balance},使用余额支付¥${res.data.amountReal}?`,
- confirmText: "确认支付",
- cancelText: "暂不付款",
- success: res2 => {
- if (res2.confirm) {
- // 使用余额支付
- WXAPI.orderPay(wx.getStorageSync('token'), res.data.id).then(res3 => {
- if (res3.code != 0) {
- wx.showToast({
- title: res3.msg,
- icon: 'none'
- })
- return
- }
- wx.redirectTo({
- url: "/pages/order-list/index"
- })
- })
- } else {
- wx.redirectTo({
- url: "/pages/order-list/index"
- })
- }
- }
- })
- } else {
- // 余额不够
- wx.showModal({
- title: '请确认支付',
- content: `您当前可用余额¥${balance},仍需支付¥${money}`,
- confirmText: "确认支付",
- cancelText: "暂不付款",
- success: res2 => {
- if (res2.confirm) {
- // 使用余额支付
- wxpay.wxpay('order', money, res.data.id, "/pages/order-list/index");
- } else {
- wx.redirectTo({
- url: "/pages/order-list/index"
- })
- }
- }
- })
- }
- } else {
- // 没余额
- wxpay.wxpay('order', res.data.amountReal, res.data.id, "/pages/order-list/index");
- }
- },
- async initShippingAddress() {
- const res = await WXAPI.defaultAddress(wx.getStorageSync('token'))
- if (res.code == 0) {
- this.setData({
- curAddressData: res.data.info
- });
- } else {
- this.setData({
- curAddressData: null
- });
- }
- this.processYunfei();
- },
- processYunfei() {
- var goodsList = this.data.goodsList
- if (goodsList.length == 0) {
- return
- }
- const goodsJsonStr = []
- var isNeedLogistics = 0;
- let inviter_id = 0;
- let inviter_id_storge = wx.getStorageSync('referrer');
- if (inviter_id_storge) {
- inviter_id = inviter_id_storge;
- }
- for (let i = 0; i < goodsList.length; i++) {
- let carShopBean = goodsList[i];
- if (carShopBean.logistics || carShopBean.logisticsId) {
- isNeedLogistics = 1;
- }
- const _goodsJsonStr = {
- propertyChildIds: carShopBean.propertyChildIds
- }
- if (carShopBean.sku && carShopBean.sku.length > 0) {
- let propertyChildIds = ''
- carShopBean.sku.forEach(option => {
- propertyChildIds = propertyChildIds + ',' + option.optionId + ':' + option.optionValueId
- })
- _goodsJsonStr.propertyChildIds = propertyChildIds
- }
- if (carShopBean.additions && carShopBean.additions.length > 0) {
- let goodsAdditionList = []
- carShopBean.additions.forEach(option => {
- goodsAdditionList.push({
- pid: option.pid,
- id: option.id
- })
- })
- _goodsJsonStr.goodsAdditionList = goodsAdditionList
- }
- _goodsJsonStr.goodsId = carShopBean.goodsId
- _goodsJsonStr.number = carShopBean.number
- _goodsJsonStr.logisticsType = 0
- _goodsJsonStr.inviter_id = inviter_id
- goodsJsonStr.push(_goodsJsonStr)
- }
- this.setData({
- isNeedLogistics: isNeedLogistics,
- goodsJsonStr: JSON.stringify(goodsJsonStr)
- });
- this.createOrder();
- },
- addAddress: function () {
- wx.navigateTo({
- url: "/pages/address-add/index"
- })
- },
- selectAddress: function () {
- wx.navigateTo({
- url: "/pages/select-address/index"
- })
- },
- bindChangeCoupon: function (e) {
- const selIndex = e.detail.value;
- this.setData({
- curCoupon: this.data.coupons[selIndex],
- curCouponShowText: this.data.coupons[selIndex].nameExt
- });
- this.processYunfei()
- },
- radioChange (e) {
- this.setData({
- peisongType: e.detail.value
- })
- this.processYunfei()
- if (e.detail.value == 'zq') {
- this.fetchShops()
- }
- },
- cancelLogin() {
- wx.navigateBack()
- },
- processLogin(e) {
- if (!e.detail.userInfo) {
- wx.showToast({
- title: '已取消',
- icon: 'none',
- })
- return;
- }
- AUTH.register(this);
- },
- async fetchShops(){
- const res = await WXAPI.fetchShops()
- if (res.code == 0) {
- let shopIndex = this.data.shopIndex
- const shopInfo = wx.getStorageSync('shopInfo')
- if (shopInfo) {
- shopIndex = res.data.findIndex(ele => {
- return ele.id == shopInfo.id
- })
- }
- this.setData({
- shops: res.data,
- shopIndex
- })
- }
- },
- shopSelect(e) {
- this.setData({
- shopIndex: e.detail.value
- })
- },
- goMap() {
- const _this = this
- const shop = this.data.shops[this.data.shopIndex]
- const latitude = shop.latitude
- const longitude = shop.longitude
- wx.openLocation({
- latitude,
- longitude,
- scale: 18
- })
- },
- callMobile() {
- const shop = this.data.shops[this.data.shopIndex]
- wx.makePhoneCall({
- phoneNumber: shop.linkPhone,
- })
- },
- async getUserApiInfo() {
- const res = await WXAPI.userDetail(wx.getStorageSync('token'))
- if (res.code == 0) {
- this.setData({
- bindMobileStatus: res.data.base.mobile ? 1: 2, // 账户绑定的手机号码状态
- mobile: res.data.base.mobile,
- })
- }
- },
- async getPhoneNumber(e) {
- if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
- wx.showToast({
- title: e.detail.errMsg,
- icon: 'none'
- })
- return;
- }
- const res = await WXAPI.bindMobileWxapp(wx.getStorageSync('token'), this.data.code, e.detail.encryptedData, e.detail.iv)
- AUTH.wxaCode().then(code => {
- this.data.code = code
- })
- if (res.code == 0) {
- wx.showToast({
- title: '读取成功',
- icon: 'success'
- })
- this.setData({
- mobile: res.data,
- bindMobileStatus: 1
- })
- } else {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- }
- },
- deductionScoreChange(event) {
- this.setData({
- deductionScore: event.detail,
- })
- this.processYunfei()
- },
- deductionScoreClick(event) {
- const { name } = event.currentTarget.dataset;
- this.setData({
- deductionScore: name,
- })
- this.processYunfei()
- },
- })
|