model.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import request from '../../Utils/RequestUtils';
  2. import Toast from '../../flooks/Toast';
  3. import User from '../../flooks/User';
  4. import MapModel from '../Map/model';
  5. const DetailModel = (now) => ({
  6. id: 0,
  7. merchantInfo: {},
  8. cartMap: new Map([]),
  9. showSelect: false,
  10. selectInfo: null,
  11. cartRequest: null,
  12. heardColor: '',
  13. selectClass: '',
  14. goodsList: [], // 全部商品
  15. payMethod: 'ALI_PAY',
  16. remark: '',
  17. tablewareNum: 1,
  18. cartMoneyInfo: {},
  19. init(id) {
  20. now({ id: 0, merchantInfo: {}, goodsList: [] });
  21. return request
  22. .get(`/merchant/getDTO/${id}`)
  23. .then((res) => {
  24. now({
  25. id,
  26. merchantInfo: res,
  27. });
  28. return request.get('/goods/goods', {
  29. params: {
  30. merchantId: id,
  31. },
  32. });
  33. })
  34. .then((res) => {
  35. now({ goodsList: res });
  36. });
  37. },
  38. setHeaderColor(color) {
  39. now({
  40. heardColor: color,
  41. });
  42. },
  43. getRecomend() {
  44. const { id } = now();
  45. now({ recommendGoods: [] });
  46. if (!id) {
  47. return Promise.resolve();
  48. }
  49. return request
  50. .get(`/goods/all`, {
  51. params: {
  52. query: {
  53. recommend: true,
  54. merchantId: id,
  55. },
  56. },
  57. })
  58. .then((res) => {
  59. return Promise.resolve(res.content);
  60. });
  61. },
  62. getCart() {
  63. const { id, calculatePrice } = now();
  64. now({ cartList: [] });
  65. if (!id) {
  66. return Promise.resolve([]);
  67. }
  68. return request
  69. .get('/shoppingCart/my/merchant', {
  70. params: {
  71. merchantId: id,
  72. },
  73. })
  74. .then((res) => {
  75. now({ remark: '' });
  76. calculatePrice(res);
  77. return Promise.resolve(res);
  78. });
  79. },
  80. setCartMap(map) {
  81. now({
  82. cartMap: map,
  83. });
  84. },
  85. changeSelect(show) {
  86. now({
  87. showSelect: show,
  88. });
  89. },
  90. checkgoodsSpecification(goodsInfo) {
  91. now({
  92. selectInfo: goodsInfo,
  93. showSelect: true,
  94. });
  95. },
  96. addCart(goodsId, goodsSpecificationIds, num) {
  97. const { warnning } = now(Toast);
  98. const { cartRequest } = now();
  99. const { chooseInfo } = now(MapModel);
  100. const { location } = chooseInfo;
  101. return request
  102. .post('/shoppingCart/cart', {
  103. data: {
  104. goodsId,
  105. goodsSpecificationIds,
  106. num,
  107. longitude: location.lng,
  108. latitude: location.lat,
  109. },
  110. requestType: 'form',
  111. })
  112. .then(() => {
  113. now({ showSelect: false });
  114. if (cartRequest) {
  115. cartRequest.refresh();
  116. }
  117. })
  118. .catch((e) => {
  119. warnning(e.error);
  120. });
  121. },
  122. setCartRequest(cartRequest) {
  123. now({ cartRequest });
  124. },
  125. clearCart() {
  126. const { warnning, success } = now(Toast);
  127. const { cartRequest } = now();
  128. return request
  129. .get('/shoppingCart/clearCart')
  130. .then(() => {
  131. if (cartRequest) {
  132. cartRequest.refresh();
  133. success('清除成功');
  134. }
  135. })
  136. .catch((e) => {
  137. warnning(e.error);
  138. });
  139. },
  140. delLike(id) {
  141. return request.post(`/myCollection/del/${id}`);
  142. },
  143. // 收藏
  144. like() {
  145. const { merchantInfo } = now();
  146. const { id } = now(User);
  147. return request.post('/myCollection/save', {
  148. data: {
  149. userId: id,
  150. merchantId: merchantInfo.mid,
  151. },
  152. });
  153. },
  154. changeSelectClass(id) {
  155. now({ selectClass: id });
  156. },
  157. setPage(name) {
  158. now({
  159. pageName: name,
  160. });
  161. },
  162. getUserAppraisal() {
  163. const { id } = now();
  164. return request
  165. .get(`/appraisal/my`, {
  166. params: { merchantId: id, appraisalSort: 'ALL' },
  167. })
  168. .then((res) => {
  169. // return Promise.resolve(res.content);
  170. });
  171. },
  172. changeNum(specIds, num) {
  173. const { warnning } = now(Toast);
  174. const { cartRefreash } = now();
  175. if (specIds.size > 1) {
  176. warnning('当前商品存在多个规格,需要在购物车中清理');
  177. return Promise.resolve();
  178. } else if (num === 0) {
  179. return request
  180. .get(`/shoppingCart/delGoods`, {
  181. params: { specId: [...specIds][0] },
  182. })
  183. .then(() => {
  184. cartRefreash();
  185. });
  186. } else {
  187. return request
  188. .get(`/shoppingCart/changeNum`, {
  189. params: { specId: [...specIds][0], num },
  190. })
  191. .then(() => {
  192. cartRefreash();
  193. });
  194. }
  195. },
  196. changePay(method) {
  197. now({ payMethod: method });
  198. },
  199. setRemark(remark, tabs) {
  200. if (remark) {
  201. tabs.add(remark);
  202. }
  203. now({
  204. remark: [...tabs].join(';'),
  205. });
  206. },
  207. changetablewareNum(num) {
  208. now({
  209. tablewareNum: num,
  210. });
  211. },
  212. cartRefreash() {
  213. const { cartRequest } = now();
  214. if (cartRequest) {
  215. cartRequest.refresh();
  216. now({ remark: '', tablewareNum: 1 });
  217. }
  218. },
  219. calculatePrice(cartList) {
  220. const { chooseInfo } = now(MapModel);
  221. if (cartList.length > 0 && chooseInfo.location) {
  222. const ids = cartList.map((item) => {
  223. return item.shoppingCartId;
  224. });
  225. request
  226. .get(`/shoppingCart/calculatePrice`, {
  227. params: {
  228. cartId: ids[0],
  229. longitude: chooseInfo.location.lng,
  230. latitude: chooseInfo.location.lat,
  231. },
  232. })
  233. .then((res) => {
  234. now({ cartMoneyInfo: res });
  235. });
  236. } else {
  237. now({ cartMoneyInfo: {} });
  238. }
  239. },
  240. // 投诉
  241. complaintSave(userId, merchantId, type, content, img) {
  242. const { loading, warnning, success } = now(Toast);
  243. loading();
  244. return request
  245. .post(`/merchantComplaint/save`, {
  246. data: {
  247. userId,
  248. merchantId,
  249. type,
  250. content,
  251. img,
  252. },
  253. })
  254. .then(() => {
  255. success('提交成功');
  256. return Promise.resolve();
  257. })
  258. .catch((e) => {
  259. warnning(e.error);
  260. });
  261. },
  262. });
  263. export default DetailModel;