index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* eslint-disable arrow-parens */
  2. import history from './../../utils/history';
  3. import { updateGlobalData } from '../../utils/initMallList';
  4. import { connect } from 'herculex';
  5. import { clearCart, checkAllCar, checkOneCar, queryCarTypeList } from './service';
  6. import { queryForCarList, updateCar } from '../mall-index/service';
  7. import showToast from '../../utils/showToast';
  8. import { buyIsAvailable } from '../../utils';
  9. Component(connect({
  10. mapStateToProps: {
  11. globalTabs: state => state.globalTabs || [],
  12. globalGoodsList: state => state.globalGoodsList || [],
  13. globalCar: state => state.$global.globalCar || [],
  14. globalShopId: state => state.$global.globalShopId || ''
  15. }
  16. })({
  17. props: {
  18. onChange: () => null,
  19. tabBarHight: 0,
  20. showFlag: false,
  21. text: '开通会员享优惠',
  22. type: 'car'
  23. },
  24. didMount() {},
  25. didUpdate() {},
  26. data: {
  27. popUpShow: false,
  28. deliveryPopShow: false,
  29. checkedNum: 0,
  30. showAuto: false,
  31. deliverTypeList: [],
  32. itemIndex: 99,
  33. memberPopUpShow: false,
  34. memberItem: null
  35. },
  36. methods: {
  37. saveRef(ref) {
  38. // 存储自定义组件实例,方便以后调用
  39. this.test = ref;
  40. },
  41. cancelClick() {
  42. this.onClosePopup();
  43. },
  44. onClosePopup() {
  45. this.setData({
  46. popUpShow: false,
  47. deliveryPopShow: false,
  48. deliverTypeList: [],
  49. itemIndex: 99
  50. });
  51. },
  52. openPopup() {
  53. const {
  54. carts = []
  55. } = this.data.globalCar;
  56. if (carts.length) {
  57. this.setData({
  58. popUpShow: true
  59. });
  60. }
  61. },
  62. // 公共函数 回传shopId 执行fn更新 查询购物车
  63. async publicUpdateCar(fn) {
  64. const {
  65. globalShopId
  66. } = this.data;
  67. try {
  68. my.showLoading();
  69. await fn(globalShopId); // 获取购物车数据
  70. const car = await queryForCarList({
  71. shopId: globalShopId
  72. });
  73. my.hideLoading();
  74. updateGlobalData.call(this, car);
  75. if (car.carts && !car.carts.length) {
  76. this.onClosePopup();
  77. }
  78. } catch (err) {
  79. my.hideLoading();
  80. showToast({
  81. content: err.msg,
  82. type: 'none'
  83. });
  84. }
  85. },
  86. // 点击清空按钮 展示二次确认弹框
  87. clearCar() {
  88. my.confirm({
  89. title: '',
  90. content: '确认清空购物车吗?',
  91. confirmButtonText: '确认',
  92. cancelButtonText: '取消',
  93. success: result => {
  94. if (result.confirm) {
  95. // 发起请求
  96. this.publicUpdateCar(async globalShopId => {
  97. await clearCart({
  98. shopId: globalShopId
  99. });
  100. });
  101. this.setData({
  102. popUpShow: false
  103. });
  104. }
  105. }
  106. });
  107. },
  108. // 单选
  109. handelItemCheck(e) {
  110. const {
  111. skuId,
  112. checkedState,
  113. status
  114. } = e.target.dataset;
  115. if (status === 'N') {
  116. return;
  117. } // 发起请求
  118. this.publicUpdateCar(async globalShopId => {
  119. await checkOneCar({
  120. shopId: globalShopId,
  121. skuId,
  122. checkedState: checkedState === 'Y' ? 'N' : 'Y'
  123. });
  124. });
  125. },
  126. // 全选 or 全不选
  127. checkedAllChange() {
  128. // 发起请求
  129. // todo
  130. const {
  131. globalCar: {
  132. carts: carList = []
  133. }
  134. } = this.data;
  135. const checkedState = carList.every(v => {
  136. if (v.status === 'N') {
  137. return true;
  138. }
  139. return v.checkedState === 'Y';
  140. });
  141. const status = checkedState ? 'N' : 'Y';
  142. this.publicUpdateCar(async globalShopId => {
  143. await checkAllCar({
  144. shopId: globalShopId,
  145. checkedState: status
  146. });
  147. });
  148. },
  149. // 购物车中 点击加号
  150. handelItemChange(e) {
  151. const {
  152. skuId,
  153. number,
  154. type
  155. } = e; // 发起请求
  156. this.publicUpdateCar(async globalShopId => {
  157. await updateCar({
  158. shopId: globalShopId,
  159. skuId,
  160. num: number,
  161. type
  162. });
  163. });
  164. },
  165. // 关闭弹框 跳转去判断配送弹窗 结算
  166. goToConfirm({
  167. totalPrice,
  168. carCheckedList,
  169. disCountTotalPrice,
  170. deliveryCost,
  171. deliverType,
  172. packingFee,
  173. estimatedPrice,
  174. deliverTypeItem
  175. }) {
  176. this.onClosePopup();
  177. if (estimatedPrice) {
  178. const {
  179. componentData
  180. } = this.props;
  181. return this.setData({
  182. memberPopUpShow: true,
  183. memberItem: {
  184. totalPrice,
  185. carCheckedList,
  186. disCountTotalPrice,
  187. deliveryCost,
  188. deliverType,
  189. packingFee,
  190. estimatedPrice,
  191. deliverTypeItem,
  192. ...componentData
  193. }
  194. });
  195. } else {
  196. const {
  197. componentData
  198. } = this.props;
  199. history.push({
  200. title: '订单确认',
  201. pageType: 'order-confirm',
  202. componentData: {
  203. totalPrice,
  204. carCheckedList,
  205. disCountTotalPrice,
  206. deliveryCost,
  207. deliverType,
  208. packingFee,
  209. deliverTypeItem,
  210. ...componentData
  211. },
  212. header: 'show'
  213. });
  214. }
  215. },
  216. onCheckBoxClick({
  217. itemIndex,
  218. isChecked
  219. }) {
  220. const {
  221. deliverTypeList
  222. } = this.data;
  223. const ListWithCheck = deliverTypeList.map((v, index) => {
  224. if (index === itemIndex) {
  225. if (v.deliverType === 'DELIVERY_LOGISTICS' && !v.deliveryTemplate) {
  226. my.showToast({
  227. content: '当前门店无配送模版,暂不支持配送'
  228. });
  229. return { ...v,
  230. isChecked: false
  231. };
  232. }
  233. return { ...v,
  234. isChecked
  235. };
  236. }
  237. return { ...v,
  238. isChecked: false
  239. };
  240. });
  241. const isPopActive = !ListWithCheck.every(v => !v.isChecked);
  242. this.setData({
  243. deliverTypeList: ListWithCheck,
  244. itemIndex: isPopActive ? itemIndex : 99,
  245. isPopActive
  246. });
  247. },
  248. async queryOnceCarTypeList() {
  249. const {
  250. globalShopId
  251. } = this.data;
  252. const list = await queryCarTypeList(globalShopId);
  253. return list.map(v => ({ ...v,
  254. isChecked: false
  255. }));
  256. },
  257. // 去结算判断是否弹框选择单一类型
  258. async judgeDeliverType() {
  259. try {
  260. const list = await this.queryOnceCarTypeList();
  261. if (list.length > 1) {
  262. // 两种类型 需要弹框
  263. my.alert({
  264. content: '已选商品支持多种配送方式,请选择结算方式',
  265. success: () => {
  266. this.setData({
  267. deliveryPopShow: true,
  268. deliverTypeList: list
  269. });
  270. }
  271. });
  272. } else {
  273. // 单一类型 去结算
  274. const {
  275. totalPrice = '',
  276. disCountTotalPrice = '',
  277. deliveryCost = 0,
  278. carts = [],
  279. deliverType = '',
  280. packingFee = 0,
  281. estimatedPrice = 0
  282. } = list[0];
  283. const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y'); // this.setData({
  284. // memberPopUpShow: true,
  285. // });
  286. this.goToConfirm({
  287. totalPrice,
  288. disCountTotalPrice,
  289. deliveryCost,
  290. carCheckedList,
  291. deliverType,
  292. packingFee,
  293. estimatedPrice,
  294. deliverTypeItem: list[0]
  295. });
  296. }
  297. } catch (err) {
  298. my.showToast({
  299. content: err.msg || '请求失败'
  300. });
  301. }
  302. },
  303. // 去订单确认页面
  304. gotoCreateOrder() {
  305. const {
  306. deliverTypeList,
  307. itemIndex
  308. } = this.data;
  309. if (!deliverTypeList[itemIndex]) {
  310. return;
  311. }
  312. const {
  313. totalPrice = '',
  314. disCountTotalPrice = '',
  315. deliveryCost = '',
  316. carts = [],
  317. packingFee = '',
  318. deliverType = '',
  319. estimatedPrice = 0
  320. } = deliverTypeList[itemIndex];
  321. const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y');
  322. this.goToConfirm({
  323. totalPrice,
  324. disCountTotalPrice,
  325. deliveryCost,
  326. carCheckedList,
  327. deliverType,
  328. packingFee,
  329. estimatedPrice,
  330. deliverTypeItem: deliverTypeList[itemIndex]
  331. });
  332. },
  333. // 去结算
  334. async handelCreateOrder() {
  335. try {
  336. const {
  337. carts: preCar
  338. } = this.data.globalCar;
  339. const {
  340. globalShopId
  341. } = this.data;
  342. const car = await queryForCarList({
  343. shopId: globalShopId
  344. });
  345. updateGlobalData.call(this, car);
  346. const {
  347. carts
  348. } = car;
  349. const carCheckedList = carts.filter(v => v.checkedState === 'Y' && v.status === 'Y'); // 没预约
  350. if (carCheckedList.some(v => v.userLimitType === 'BESPEAK' && !v.isbespeak)) {
  351. return showToast({
  352. content: '部分勾选商品需要预约,请前往预约',
  353. type: 'none'
  354. });
  355. } // 不同类型商品不能一起下单
  356. if (!buyIsAvailable(carCheckedList)) {
  357. return showToast({
  358. content: '商品发货类型不同,请分别购买',
  359. type: 'none'
  360. });
  361. } // 选择的商品全停售
  362. if (!carCheckedList.length) {
  363. return showToast({
  364. content: '商品已停售,请选择其他商品',
  365. type: 'none'
  366. });
  367. } // 部分停售
  368. if (preCar.filter(v => v.checkedState === 'Y').length !== carCheckedList.length) {
  369. return my.confirm({
  370. content: '部分商品已停售,已为您取消勾选',
  371. confirmButtonText: '继续购买',
  372. cancelButtonText: '重新查看',
  373. success: e => {
  374. const {
  375. confirm
  376. } = e;
  377. if (confirm) {
  378. this.judgeDeliverType();
  379. }
  380. },
  381. fail: () => 0
  382. });
  383. } // 有库存标志 但是库存不足
  384. if (carCheckedList.filter(v => v.stockStatus === 'Y' && v.quantity > v.stock).length) {
  385. return showToast({
  386. content: '您的订单存在库存不足商品,请核对数量',
  387. type: 'none'
  388. });
  389. } // 有限购 超过购了
  390. if (carCheckedList.filter(v => v.buyLimitType === 'GENERAL' && v.quantity > v.buyLimitCount).length) {
  391. return showToast({
  392. content: '您的订单存在超出限购商品,请核对数量',
  393. type: 'none'
  394. });
  395. }
  396. this.setData({
  397. popUpShow: false
  398. }); // 不属于购物车有更新 判断类型
  399. this.judgeDeliverType();
  400. } catch (err) {
  401. showToast({
  402. content: '查询购物车失败',
  403. type: 'fail'
  404. });
  405. }
  406. },
  407. // 点击了会员卡按钮弹窗
  408. handleMemberBtnClick({
  409. type,
  410. memberItem
  411. }) {
  412. if (type === 'none') {
  413. // 原价结算
  414. history.push({
  415. title: '订单确认',
  416. pageType: 'order-confirm',
  417. componentData: { ...memberItem
  418. },
  419. header: 'show'
  420. });
  421. } else {
  422. const {
  423. type: carMemberType
  424. } = this.props;
  425. if (carMemberType === 'car') {
  426. this.commitGlobal && this.commitGlobal({
  427. openMallMemberCard: true
  428. });
  429. } else {
  430. my.navigateTo({
  431. url: '/pages/one/index?pageCode=shopIndex'
  432. });
  433. }
  434. }
  435. },
  436. // 关闭会员开卡弹框
  437. onMemberPopClose() {
  438. this.setData({
  439. memberPopUpShow: false
  440. });
  441. }
  442. }
  443. }));