123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- const App = getApp();
- // 工具类
- import Util from '../../../utils/util.js';
- // 验证类
- import Verify from '../../../utils/verify.js';
- // 枚举类:发货方式
- import DeliveryTypeEnum from '../../../utils/enum/DeliveryType.js';
- // 枚举类:支付方式
- import PayTypeEnum from '../../../utils/enum/order/PayType';
- // 对话框插件
- import Dialog from '../../../components/dialog/dialog';
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- // 当前页面参数
- options: {},
- // 系统设置
- setting: {
- delivery: [], // 支持的配送方式
- },
- // 配送方式
- isShowTab: false,
- DeliveryTypeEnum,
- curDelivery: null,
- // 支付方式
- PayTypeEnum,
- curPayType: PayTypeEnum.WECHAT.value,
- address: null, // 默认收货地址
- exist_address: false, // 是否存在收货地址
- selectedShopId: 0, // 选择的自提门店id
- linkman: '', // 自提联系人
- phone: '', // 自提联系电话
- // 商品信息
- goods: {},
- // 选择的优惠券
- selectCouponId: 0,
- // 是否使用积分抵扣
- isUsePoints: false,
- // 买家留言
- remark: '',
- // 禁用submit按钮
- disabled: false,
- has_error: false,
- error_msg: '',
- notRefresh: false, // 不允许刷新
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- let _this = this;
- // 当前页面参数
- _this.data.options = options;
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- let _this = this;
- // 获取当前订单信息
- !_this.data.notRefresh && _this.getOrderData();
- },
- /**
- * 获取当前订单信息
- */
- getOrderData() {
- let _this = this,
- options = _this.data.options;
- // 获取订单信息回调方法
- let callback = result => {
- let resData = result.data;
- if (result.code !== 1) {
- App.showError(result.msg);
- return false;
- }
- // 显示错误信息
- if (resData.has_error) {
- App.showError(resData.error_msg);
- }
- let data = {};
- // 当前选择的配送方式
- data.curDelivery = resData.delivery;
- // 如果只有一种配送方式则不显示选项卡
- data.isShowTab = resData.setting.delivery.length > 1;
- // 上门自提联系信息
- if (_this.data.linkman === '') {
- data.linkman = resData.last_extract.linkman;
- }
- if (_this.data.phone === '') {
- data.phone = resData.last_extract.phone;
- }
- // 设置页面数据
- _this.setData(Object.assign({}, resData, data));
- wx.hideLoading();
- };
- wx.showLoading({
- title: '加载中...',
- });
- App._get('sharing.order/checkout', {
- order_type: options.order_type,
- goods_id: options.goods_id,
- goods_num: options.goods_num,
- goods_sku_id: options.goods_sku_id,
- delivery: _this.data.curDelivery || 0,
- shop_id: _this.data.selectedShopId || 0,
- coupon_id: _this.data.selectCouponId || 0,
- is_use_points: _this.data.isUsePoints ? 1 : 0,
- }, result => {
- callback(result);
- });
- },
- /**
- * 切换配送方式
- */
- onSwichDelivery(e) {
- // 设置当前配送方式
- let _this = this;
- _this.setData({
- curDelivery: e.currentTarget.dataset.current
- });
- // 重新获取订单信息
- _this.getOrderData();
- },
- /**
- * 快递配送:选择收货地址
- */
- onSelectAddress() {
- let _this = this;
- // 允许刷新
- _this.setData({
- notRefresh: false
- });
- // 跳转到选择自提点
- wx.navigateTo({
- url: '../../address/' + (_this.data.exist_address ? 'index?from=flow' : 'create')
- });
- },
- /**
- * 上门自提:选择自提点
- */
- onSelectExtractPoint() {
- let _this = this,
- selectedId = _this.data.selectedShopId;
- // 允许刷新
- _this.setData({
- notRefresh: false
- });
- // 跳转到选择自提点
- wx.navigateTo({
- url: '../../_select/extract_point/index?selected_id=' + selectedId
- });
- },
- /**
- * 跳转到商品详情页
- */
- onTargetGoods(e) {
- wx.navigateTo({
- url: `../goods/index?goods_id=${e.currentTarget.dataset.id}`,
- })
- },
- /**
- * 订单提交
- */
- onSubmitOrder() {
- let _this = this,
- options = _this.data.options;
- if (_this.data.disabled) {
- return false;
- }
- // 表单验证
- if (!_this._onVerify()) {
- return false;
- }
- // 按钮禁用, 防止二次提交
- _this.data.disabled = true;
- // 提交到后端
- const onCommitCallback = () => {
- // 显示loading
- wx.showLoading({
- title: '正在处理...'
- });
- // 创建订单-立即购买
- App._post_form('sharing.order/checkout', {
- order_type: options.order_type || 10,
- goods_id: options.goods_id,
- goods_num: options.goods_num,
- goods_sku_id: options.goods_sku_id,
- delivery: _this.data.curDelivery || 0,
- pay_type: _this.data.curPayType,
- shop_id: _this.data.selectedShopId || 0,
- linkman: _this.data.linkman,
- phone: _this.data.phone,
- active_id: options.active_id || 0,
- coupon_id: _this.data.selectCouponId || 0,
- is_use_points: _this.data.isUsePoints ? 1 : 0,
- remark: _this.data.remark || '',
- }, result => {
- _this._onSubmitCallback(result);
- }, result => {}, () => {
- wx.hideLoading();
- // 解除按钮禁用
- _this.data.disabled = false;
- // 不允许刷新
- _this.setData({
- notRefresh: true
- });
- });
- };
- // 请求用户订阅消息
- _this._onRequestSubscribeMessage(onCommitCallback);
- },
- /**
- * 请求用户订阅消息
- */
- _onRequestSubscribeMessage(onCommitCallback) {
- let _this = this,
- tmplIds = _this.data.setting.order_submsg;
- if (tmplIds.length == 0) {
- onCommitCallback();
- return;
- }
- wx.requestSubscribeMessage({
- tmplIds,
- success(res) {},
- fail(res) {},
- complete(res) {
- onCommitCallback();
- },
- });
- },
- /**
- * 订单提交成功后回调
- */
- _onSubmitCallback(result) {
- let _this = this;
- // 订单创建成功后回调--微信支付
- if (result.code === -10) {
- App.showError(result.msg, () => {
- _this.redirectToOrderIndex();
- });
- return false;
- }
- // 发起微信支付
- if (result.data.pay_type == PayTypeEnum.WECHAT.value) {
- App.wxPayment({
- payment: result.data.payment,
- success: res => {
- _this.redirectToOrderIndex();
- },
- fail: res => {
- App.showError(result.msg.error, () => {
- _this.redirectToOrderIndex();
- });
- },
- });
- }
- // 余额支付
- if (result.data.pay_type == PayTypeEnum.BALANCE.value) {
- App.showSuccess(result.msg.success, () => {
- _this.redirectToOrderIndex();
- });
- }
- },
- /**
- * 表单验证
- */
- _onVerify() {
- let _this = this;
- if (_this.data.has_error) {
- App.showError(_this.data.error_msg);
- return false;
- }
- // 验证自提填写的联系方式
- if (_this.data.curDelivery == DeliveryTypeEnum.EXTRACT.value) {
- _this.setData({
- linkman: _this.data.linkman.trim(),
- phone: _this.data.phone.trim(),
- });
- if (_this.data.selectedShopId <= 0) {
- App.showError('请选择自提的门店');
- return false;
- }
- if (Verify.isEmpty(_this.data.linkman)) {
- App.showError('请填写自提联系人');
- return false;
- }
- if (Verify.isEmpty(_this.data.phone)) {
- App.showError('请填写自提联系电话');
- return false;
- }
- if (!Verify.isPhone(_this.data.phone)) {
- App.showError('请输入正确的联系电话');
- return false;
- }
- }
- return true;
- },
- /**
- * 买家留言
- */
- bindRemark(e) {
- let _this = this;
- _this.setData({
- remark: e.detail.value
- })
- },
- /**
- * 选择优惠券(弹出/隐藏)
- */
- onTogglePopupCoupon() {
- let _this = this;
- if (_this.data.coupon_list.length > 0) {
- _this.setData({
- showBottomPopup: !_this.data.showBottomPopup
- });
- }
- },
- /**
- * 选择优惠券
- */
- onSelectCoupon(e) {
- let _this = this;
- // 记录选中的优惠券id
- _this.setData({
- selectCouponId: e.currentTarget.dataset.id
- });
- // 重新获取订单信息
- _this.getOrderData();
- // 隐藏优惠券弹层
- _this.onTogglePopupCoupon();
- },
- /**
- * 不使用优惠券
- */
- onNotUseCoupon() {
- let _this = this;
- _this.setData({
- selectCouponId: 0
- });
- // 重新获取订单信息
- _this.getOrderData();
- // 隐藏优惠券弹层
- _this.onTogglePopupCoupon();
- },
- /**
- * 选择支付方式
- */
- onSelectPayType(e) {
- let _this = this;
- // 设置当前支付方式
- _this.setData({
- curPayType: e.currentTarget.dataset.value
- });
- },
- /**
- * 跳转到未付款订单
- */
- redirectToOrderIndex() {
- wx.redirectTo({
- url: '../order/index',
- });
- },
- /**
- * input绑定:联系人
- */
- onInputLinkman(e) {
- let _this = this;
- _this.setData({
- linkman: e.detail.value
- });
- },
- /**
- * input绑定:联系电话
- */
- onInputPhone(e) {
- let _this = this;
- _this.setData({
- phone: e.detail.value
- });
- },
- /**
- * 选择积分抵扣
- */
- onTriggerPoints({
- detail
- }) {
- let _this = this;
- _this.setData({
- isUsePoints: detail
- });
- // 重新获取订单信息
- _this.getOrderData();
- },
- /**
- * 显示积分说明
- */
- onShowPoints(e) {
- let _this = this;
- // 显示dialog
- let setting = _this.data.setting;
- Dialog({
- title: `${setting.points_name}说明`,
- message: setting.points_describe,
- selector: '#zan-base-dialog',
- isScroll: true, // 滚动
- buttons: [{
- text: '关闭',
- color: 'red',
- type: 'cash'
- }]
- });
- },
- });
|