UNPKG

react-app-shell

Version:

react打包脚本和example, 这里的版本请忽略

76 lines (69 loc) 2.09 kB
import { fetchUtils } from '../utils'; import { appConfig } from '../config'; const mobileDomain = appConfig.domain.mobileDomain; /** * 根据课程id 获取课程信息 * @param productId */ export const getProduct = (productId) => { const url = mobileDomain.concat(`/pay/api/getProductInfo`); const param = { productId }; return fetchUtils.get(url, param, { successCode: 0 }).then((res) => { if (res.code === 0) { return { name: res.result.name, price: res.result.price / 100, // 转换成单位: 元 originPrice: res.result.originPrice / 100 }; } return Promise.reject({ msg: '获取课程失败!' }); }); }; /** * 创建订单 * @param courseId */ export const createOrder = (courseId) => { const url = mobileDomain.concat(`/pay/api/create_order`); const param = { product_id: courseId }; return fetchUtils .post(url, param, { successCode: 0 }) .catch((res) => { // 用户未登录或者登录状态过期 if (res && res.code && res.code === 400010010) { return Promise.reject({ code: 'LOGIN_REQUIRED', msg: '请您先登录!' }); } return Promise.reject(res); }) .then((res) => { switch (res.result.code) { case 30012: // ORDER_CREATE_SUCCESS return res.result; case 30017: return Promise.reject({ msg: '您已经购买过此商品,不能重复购买!' }); case 30015: return Promise.reject({ msg: '商品不存在!' }); case 30016: return Promise.reject({ msg: '商品已卖完!' }); case 30006: return Promise.reject({ msg: '商品已下架!' }); case 50003: return Promise.reject({ msg: '服务出错!' }); default: return Promise.reject({ msg: '发生错误! 错误码:' + res.result.code }); } }) .then((result) => { if (result.order_no) { return result.order_no; } return Promise.reject({ msg: `订单创建失败! ${result.code}` }); }); };