import request from '../../Utils/RequestUtils'; import Toast from '../../flooks/Toast'; import User from '../../flooks/User'; import MapModel from '../Map/model'; const DetailModel = (now) => ({ id: 0, merchantInfo: {}, cartMap: new Map([]), showSelect: false, selectInfo: null, cartRequest: null, heardColor: '', selectClass: '', goodsList: [], // 全部商品 payMethod: 'ALI_PAY', remark: '', tablewareNum: 1, cartMoneyInfo: {}, init(id) { now({ id: 0, merchantInfo: {}, goodsList: [] }); return request .get(`/merchant/getDTO/${id}`) .then((res) => { now({ id, merchantInfo: res, }); return request.get('/goods/goods', { params: { merchantId: id, }, }); }) .then((res) => { now({ goodsList: res }); }); }, setHeaderColor(color) { now({ heardColor: color, }); }, getRecomend() { const { id } = now(); now({ recommendGoods: [] }); if (!id) { return Promise.resolve(); } return request .get(`/goods/all`, { params: { query: { recommend: true, merchantId: id, }, }, }) .then((res) => { return Promise.resolve(res.content); }); }, getCart() { const { id, calculatePrice } = now(); now({ cartList: [] }); if (!id) { return Promise.resolve([]); } return request .get('/shoppingCart/my/merchant', { params: { merchantId: id, }, }) .then((res) => { now({ remark: '' }); calculatePrice(res); return Promise.resolve(res); }); }, setCartMap(map) { now({ cartMap: map, }); }, changeSelect(show) { now({ showSelect: show, }); }, checkgoodsSpecification(goodsInfo) { now({ selectInfo: goodsInfo, showSelect: true, }); }, addCart(goodsId, goodsSpecificationIds, num) { const { warnning } = now(Toast); const { cartRequest } = now(); const { chooseInfo } = now(MapModel); const { location } = chooseInfo; return request .post('/shoppingCart/cart', { data: { goodsId, goodsSpecificationIds, num, longitude: location.lng, latitude: location.lat, }, requestType: 'form', }) .then(() => { now({ showSelect: false }); if (cartRequest) { cartRequest.refresh(); } }) .catch((e) => { warnning(e.error); }); }, setCartRequest(cartRequest) { now({ cartRequest }); }, clearCart() { const { warnning, success } = now(Toast); const { cartRequest } = now(); return request .get('/shoppingCart/clearCart') .then(() => { if (cartRequest) { cartRequest.refresh(); success('清除成功'); } }) .catch((e) => { warnning(e.error); }); }, delLike(id) { return request.post(`/myCollection/del/${id}`); }, // 收藏 like() { const { merchantInfo } = now(); const { id } = now(User); return request.post('/myCollection/save', { data: { userId: id, merchantId: merchantInfo.mid, }, }); }, changeSelectClass(id) { now({ selectClass: id }); }, setPage(name) { now({ pageName: name, }); }, getUserAppraisal() { const { id } = now(); return request .get(`/appraisal/my`, { params: { merchantId: id, appraisalSort: 'ALL' }, }) .then((res) => { // return Promise.resolve(res.content); }); }, changeNum(specIds, num) { const { warnning } = now(Toast); const { cartRefreash } = now(); if (specIds.size > 1) { warnning('当前商品存在多个规格,需要在购物车中清理'); return Promise.resolve(); } else if (num === 0) { return request .get(`/shoppingCart/delGoods`, { params: { specId: [...specIds][0] }, }) .then(() => { cartRefreash(); }); } else { return request .get(`/shoppingCart/changeNum`, { params: { specId: [...specIds][0], num }, }) .then(() => { cartRefreash(); }); } }, changePay(method) { now({ payMethod: method }); }, setRemark(remark, tabs) { if (remark) { tabs.add(remark); } now({ remark: [...tabs].join(';'), }); }, changetablewareNum(num) { now({ tablewareNum: num, }); }, cartRefreash() { const { cartRequest } = now(); if (cartRequest) { cartRequest.refresh(); now({ remark: '', tablewareNum: 1 }); } }, calculatePrice(cartList) { const { chooseInfo } = now(MapModel); if (cartList.length > 0 && chooseInfo.location) { const ids = cartList.map((item) => { return item.shoppingCartId; }); request .get(`/shoppingCart/calculatePrice`, { params: { cartId: ids[0], longitude: chooseInfo.location.lng, latitude: chooseInfo.location.lat, }, }) .then((res) => { now({ cartMoneyInfo: res }); }); } else { now({ cartMoneyInfo: {} }); } }, // 投诉 complaintSave(userId, merchantId, type, content, img) { const { loading, warnning, success } = now(Toast); loading(); return request .post(`/merchantComplaint/save`, { data: { userId, merchantId, type, content, img, }, }) .then(() => { success('提交成功'); return Promise.resolve(); }) .catch((e) => { warnning(e.error); }); }, }); export default DetailModel;