Blame view

apps/cart/models/shopping.js 8.79 KB
陈轩 authored
1 2
'use strict';
const _ = require('lodash');
郭成尧 authored
3
郭成尧 authored
4 5 6
class shoppingModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
陈轩 authored
7 8
    }
郭成尧 authored
9 10 11 12 13 14 15 16
    /**
     * 购物车结算
     * doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
     */
    cartPayAPI(params) {
        if (!params.activityInfo) {
            params.activityInfo = null;
        }
郭成尧 authored
17
郭成尧 authored
18
        let param = {
郭成尧 authored
19
            method: params.activityInfo ? 'app.Shopping.easyPayment' : 'app.Shopping.enhancedPayment',
郭成尧 authored
20 21
            enable_red_envelopes: 0, // h5不返回红包
            cart_type: params.cart_type,
22
            yoho_coin_mode: params.yoho_coin_mode || 0,
郭成尧 authored
23 24 25 26 27 28 29
            uid: params.uid
        };

        // 购买限购商品时需要传递product_sku_list参数
        if (!_.isEmpty(params.product_sku_list)) {
            param.product_sku_list = JSON.stringify(params.product_sku_list);
        }
郭成尧 authored
30
郭成尧 authored
31 32 33 34 35
        // 购买套装和量贩商品时数据处理
        if (params.activityInfo) {
            param.activity_id = params.activityInfo.activity_id;
            param.product_sku_list = JSON.stringify(params.activityInfo.product_sku_list);
        }
陈轩 authored
36
郭成尧 authored
37
        return this.get({ data: param, param: { cache: false } });
陈轩 authored
38 39
    }
郭成尧 authored
40 41 42 43 44 45 46 47 48 49 50 51
    /**
     * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
     * doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
     */
    orderComputeAPI(params) {
        let finalParams = {
            method: params.activityInfo ? 'app.Shopping.easyCompute' : 'app.Shopping.compute',
            cart_type: params.cart_type,
            delivery_way: params.delivery_way,
            payment_type: params.payment_type,
            uid: params.uid
        };
陈轩 authored
52
郭成尧 authored
53 54 55 56
        // 有货币
        if (params.use_yoho_coin) {
            finalParams.use_yoho_coin = params.use_yoho_coin;
        }
陈轩 authored
57
郭成尧 authored
58
        // 优惠券
郭成尧 authored
59 60 61
        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }
郭成尧 authored
62
郭成尧 authored
63 64 65
        // 礼品卡
        if (params.gift_card_code) {
            finalParams.gift_card_code = params.gift_card_code;
郭成尧 authored
66
        }
郭成尧 authored
67
郭成尧 authored
68 69 70
        if (!_.isEmpty(params.product_sku_list) && !params.activityInfo) {
            finalParams.product_sku_list = JSON.stringify(params.product_sku_list);
        }
郭成尧 authored
71
郭成尧 authored
72 73 74 75 76
        // 购买套餐商品需要的数据
        if (params.activityInfo) {
            finalParams.activity_id = params.activityInfo.activity_id;
            finalParams.product_sku_list = JSON.stringify(params.activityInfo.product_sku_list);
        }
郭成尧 authored
77
郭成尧 authored
78
        return this.get({data: finalParams, param: {code: 200}});
郭成尧 authored
79 80
    }
郭成尧 authored
81 82 83 84 85 86 87 88 89 90
    /**
     * 校验电子票
     * @param int $uid
     * @param int $productSku
     * @param int $buyNumber
     * @param int $useYohoCoin
     * @param bool|int $yohoCoinMode 1:使用有货币;0:不使用有货币
     * @return array
     * @author sefon 2016-7-2 18:12:30
     */
郭成尧 authored
91
    checkTickets(uid, productSku, buyNumber, useYohoCoin, yohoCoinMode, gift_card_code) {
郭成尧 authored
92 93 94
        if (!useYohoCoin) {
            useYohoCoin = 0;
        }
郭成尧 authored
95
郭成尧 authored
96 97 98
        if (!yohoCoinMode) {
            yohoCoinMode = true;
        }
郭成尧 authored
99
郭成尧 authored
100 101 102 103 104
        let params = {
            method: 'app.shopping.ticket',
            uid: uid,
            product_sku: productSku,
            buy_number: buyNumber,
郭成尧 authored
105
            use_yoho_coin: useYohoCoin,
郭成尧 authored
106 107 108 109 110 111 112
        };

        if (!yohoCoinMode) {
            _.assign(params, {
                yoho_coin_mode: 0
            });
        }
郭成尧 authored
113
郭成尧 authored
114 115 116 117 118 119
        if (gift_card_code) {
            _.assign(params, {
                gift_card_code: gift_card_code
            });
        }
郭成尧 authored
120
        return this.get({data: params, param: {code: 200}});
郭成尧 authored
121 122
    }
郭成尧 authored
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    /**
     * 购物车结算--提交结算信息
     */
    orderSub(params) {
        let finalParams = {
            method: params.activityInfo ? 'app.Shopping.easySubmit' : 'app.Shopping.submit',
            address_id: params.address_id,
            cart_type: params.cart_type,
            delivery_time: params.delivery_time,
            delivery_way: params.delivery_way,
            payment_id: params.payment_id,
            payment_type: params.payment_type,
            remark: params.remark,
            uid: params.uid,
            udid: params.udid,
            is_print_price: params.is_print_price
        };

        /* tar add 161130 结算优化 */
        if (params.times === 2 || params.times === '2') {
            finalParams.is_continue_buy = 'Y';
        } else {
            finalParams.is_continue_buy = 'N';
        }
郭成尧 authored
147
郭成尧 authored
148 149 150 151
        // 有货币
        if (params.use_yoho_coin) {
            finalParams.use_yoho_coin = params.use_yoho_coin;
        }
郭成尧 authored
152
郭成尧 authored
153 154 155 156 157 158 159 160 161 162
        // 优惠券
        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }

        // 礼品卡
        if (params.gift_card_code) {
            finalParams.gift_card_code = params.gift_card_code;
        }
郭成尧 authored
163 164 165 166
        // 发票内容写死明细
        if (params.invoices.invoices_type_id) {
            finalParams.invoice_content = 12;
        }
郭成尧 authored
167
郭成尧 authored
168 169 170 171
        // 发票类型 纸质 1 ,电子 2
        if (params.invoices.invoices_type) {
            finalParams.invoices_type = params.invoices.invoices_type;
        }
郭成尧 authored
172
郭成尧 authored
173 174 175 176
        // 发票类型:个人 1, 企业 2
        if (params.invoices.invoice_payable_type) {
            finalParams.invoice_payable_type = params.invoices.invoice_payable_type;
        }
郭成尧 authored
177
郭成尧 authored
178 179 180 181
        // 收票人手机号码
        if (params.invoices.receiverMobile) {
            finalParams.receiverMobile = params.invoices.receiverMobile;
        }
郭成尧 authored
182
郭成尧 authored
183 184 185 186
        // 发票抬头
        if (params.invoices.invoices_title) {
            finalParams.invoices_title = params.invoices.invoices_title;
        }
郭成尧 authored
187
郭成尧 authored
188 189 190 191 192 193 194 195
        // 购买方纳税人识别号,需要开具电子发票且发票抬头为单位信息时为必填项
        if (params.invoices.buyerTaxNumber) {
            finalParams.buyerTaxNumber = params.invoices.buyerTaxNumber;
        }

        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }
郭成尧 authored
196
郭成尧 authored
197 198 199 200 201 202 203 204
        if (params.use_yoho_coin) {
            finalParams.use_yoho_coin = params.use_yoho_coin;
        }

        // 购买限购商品时需要传递product_sku_list参数
        if (params.product_sku_list && !params.activityInfo) {
            finalParams.product_sku_list = JSON.stringify(params.product_sku_list);
        }
郭成尧 authored
205
郭成尧 authored
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        // 购买套餐商品需要的数据
        if (params.activityInfo) {
            finalParams.activity_id = params.activityInfo.activity_id;
            finalParams.product_sku_list = JSON.stringify(params.activityInfo.product_sku_list); // TODO 检查 JSON 是否正常
        }

        // 友盟有关信息的传递
        if (params.qhy_union) {
            finalParams.qhy_union = params.qhy_union;
        }

        // 是否是微信商城
        if (params.isWechat) {
            finalParams.client_type = 'wechat';
        }
郭成尧 authored
222
        return this.post({data: finalParams, param: {
郭成尧 authored
223 224 225 226
            headers: {
                'X-Forwarded-For': params.ip || '',
                'User-Agent': params.userAgent
            }
郭成尧 authored
227
        }});
郭成尧 authored
228 229
    }
郭成尧 authored
230 231 232 233 234 235 236

    /**
     *  获取用户可用的优惠券张数
     *  doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
     *  @param uid int 用户uid
     *  @return see doc
     */
郭成尧 authored
237
    getValidCouponCount(params) {
郭成尧 authored
238 239
        let param = {
            method: 'app.Shopping.countUsableCoupon',
郭成尧 authored
240
            uid: params.uid,
郭成尧 authored
241
            delivery_way: params.delivery_way || 1
郭成尧 authored
242 243
        };
郭成尧 authored
244
        return this.get({data: param});
郭成尧 authored
245 246
    }
郭成尧 authored
247 248 249 250 251 252 253 254

    /**
     *  获取用户优惠券列表,可用和不可用
     *  @param uid int 用户uid
     *  @return see doc
     */
    listCoupon(params) {
        let param = {
郭成尧 authored
255
            method: 'app.Shopping.selectCoupon',
郭成尧 authored
256 257
            uid: params.uid,
            delivery_way: params.delivery_way,
郭成尧 authored
258
            coupon_code: params.coupon_code
郭成尧 authored
259 260
        };
郭成尧 authored
261 262 263 264 265 266
        return this.get({
            data: param,
            param: {
                cache: false
            }
        });
郭成尧 authored
267 268
    }
郭成尧 authored
269
    /**
郭成尧 authored
270
     * 购物车结算--兑换优惠券
郭成尧 authored
271 272 273 274 275 276
     * @param uid int   用户uid
     * @param couponCode string 优惠券代码
     * @return
     */
    useCoupon(uid, couponCode) {
        let param = {
郭成尧 authored
277
            method: 'app.coupons.bindPCoupon',
郭成尧 authored
278 279 280 281
            coupon_code: couponCode,
            uid
        };
郭成尧 authored
282
        return this.get({data: param});
郭成尧 authored
283 284
    }
郭成尧 authored
285 286 287 288 289 290 291 292 293

    /**
     * 获取可用的礼品卡张数
     */
    countUsableGiftCard(uid) {
        return this.get({data: {
            method: 'app.Shopping.countUsableGiftCard',
            uid: uid
        }});
郭成尧 authored
294 295
    }
郭成尧 authored
296 297 298 299 300 301 302 303
    /**
     * 获取礼品卡列表
     */
    listGiftCard(uid) {
        return this.get({data: {
            method: 'app.Shopping.listGiftCard',
            uid: uid
        }});
陈峰 authored
304
    }
郭成尧 authored
305
}
陈峰 authored
306
郭成尧 authored
307
module.exports = shoppingModel;