index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* eslint-disable arrow-parens */
  2. import { connect } from 'herculex';
  3. import { updateGlobalData } from '../../utils/initMallList';
  4. import { createSubscribe } from 'applet-page-component';
  5. import { updateCar, queryForCarList, queryForStartTip } from '../mall-index/service';
  6. import getLocationInfo from '../../utils/getLocationInfo';
  7. import { queryForGoodsDetail } from './service';
  8. import { getJumpStoreId } from '../../service/store';
  9. import { DEFAULT_GOODS } from '../../utils/const';
  10. import { isActive } from '../../utils';
  11. const app = getApp();
  12. Component(createSubscribe({
  13. async onShow() {
  14. const {
  15. skuId = 0
  16. } = this.$page.data.query;
  17. const {
  18. globalShopId
  19. } = this.data;
  20. await this.dispatch('getCardLevel');
  21. if (skuId && globalShopId) {
  22. this.initData({
  23. skuId,
  24. globalShopId
  25. });
  26. } else {
  27. const {
  28. jumpStoreId,
  29. skuId: newSkuId
  30. } = this.data;
  31. this.initData({
  32. skuId: newSkuId,
  33. globalShopId: jumpStoreId
  34. });
  35. }
  36. }
  37. })(connect({
  38. mapStateToProps: {
  39. globalCar: state => state.$global.globalCar || [],
  40. globalShopId: state => state.$global.globalShopId || '',
  41. jumpSkuId: state => state.$global.jumpSkuId || '',
  42. memberLevel: state => state.$global.memberLevel || 'NONE'
  43. }
  44. })({
  45. props: {},
  46. data: {
  47. goodsDetail: null,
  48. btnText: '去结算',
  49. tabBarHight: 0,
  50. skuId: 0,
  51. errorImg: DEFAULT_GOODS,
  52. componentData: {},
  53. currentIndex: 1,
  54. showFlag: false,
  55. startDeliveryAmount: 0,
  56. jumpStoreId: '',
  57. productId: '',
  58. header: '',
  59. isMember: false,
  60. memberPrice: 0
  61. },
  62. async didMount() {
  63. const {
  64. skuId = ''
  65. } = this.$page.data.query;
  66. const {
  67. globalShopId
  68. } = this.data;
  69. await this.dispatch('getCardLevel');
  70. if (skuId && globalShopId) {
  71. this.initData({
  72. skuId,
  73. globalShopId
  74. });
  75. } else {
  76. // 外部跳转
  77. await getLocationInfo();
  78. const {
  79. regionData
  80. } = app.globalData;
  81. const {
  82. query: {
  83. productId
  84. } = {}
  85. } = this.$page.data;
  86. const {
  87. latitude,
  88. longitude
  89. } = regionData || {};
  90. if (productId) {
  91. // 外部跳转
  92. const {
  93. shopId = '',
  94. skuId: newSkuId = ''
  95. } = (await getJumpStoreId({
  96. productId,
  97. latitude,
  98. longitude
  99. })) || {};
  100. if (!shopId) {
  101. return my.alert({
  102. content: '关联商品已不可购买,请选购商城其他商品',
  103. success: () => {
  104. my.navigateTo({
  105. url: '/pages/one/index?pageCode=shopIndex'
  106. });
  107. }
  108. });
  109. }
  110. this.initData({
  111. skuId: newSkuId,
  112. globalShopId: shopId
  113. });
  114. this.setData({
  115. jumpStoreId: shopId,
  116. productId,
  117. header: 'show'
  118. });
  119. my.setNavigationBar({
  120. title: '商品详情'
  121. });
  122. } else {
  123. my.showToast({
  124. content: '商场配置的跳转参数不正确'
  125. });
  126. }
  127. }
  128. },
  129. methods: {
  130. // swiper 滑动
  131. onSwiperChange(e) {
  132. const {
  133. detail: {
  134. current = 1
  135. }
  136. } = e;
  137. this.setData({
  138. currentIndex: current + 1
  139. });
  140. },
  141. // 获取当前商品在购物车中的数量
  142. getCarQuantity({
  143. skuId = '',
  144. carList = []
  145. }) {
  146. const index = carList.findIndex(v => v.skuId === skuId); // 找到了
  147. if (index !== -1) {
  148. return carList[index].quantity || 0;
  149. }
  150. return 0;
  151. },
  152. async initData({
  153. skuId,
  154. globalShopId
  155. }) {
  156. try {
  157. // 查询购物车
  158. const car = await queryForCarList({
  159. shopId: globalShopId
  160. });
  161. updateGlobalData.call(this, car); // 查询对应skuId的商品
  162. const res = await queryForGoodsDetail({
  163. skuId
  164. }); // 获取购物车中商品数量
  165. const quantity = this.getCarQuantity({
  166. skuId,
  167. carList: car.carts
  168. }); // 查询起送信息
  169. const {
  170. showFlag,
  171. startDeliveryAmount
  172. } = await queryForStartTip({
  173. shopId: globalShopId
  174. });
  175. const {
  176. memberLevel
  177. } = this.data;
  178. const {
  179. memberPrices
  180. } = res;
  181. if (memberPrices) {
  182. this.setData({
  183. isMember: true,
  184. memberPrice: this.getMemberPrice(memberPrices, memberLevel)
  185. });
  186. }
  187. this.setData({
  188. goodsDetail: { ...res,
  189. quantity
  190. },
  191. skuId,
  192. showFlag,
  193. startDeliveryAmount
  194. });
  195. } catch (err) {
  196. console.log(err);
  197. my.showToast({
  198. content: err.msg || '查询商品详情失败',
  199. type: 'fail'
  200. });
  201. }
  202. },
  203. getMemberPrice(priceSetting, level) {
  204. if (priceSetting.length === 1) {
  205. // 统一会员价
  206. return priceSetting[0].membershipPrice;
  207. }
  208. if (level === 'NONE') {
  209. return priceSetting[0] ? priceSetting[0].membershipPrice : 0;
  210. }
  211. const item = priceSetting.filter(v => v.level === level);
  212. return item[0] ? item[0].membershipPrice : 0;
  213. },
  214. // saveCarRef 调用购物车 子组件方法
  215. saveCarRef(ref) {
  216. // 存储自定义组件实例,方便以后调用
  217. this.carRef = ref;
  218. },
  219. // 将自组件的确认方法 放到确认按钮
  220. handleConfirm() {
  221. this.carRef.handelCreateOrder();
  222. },
  223. // 将自组件的打开购物车方法 放到购物车按钮
  224. handleCarClick() {
  225. this.carRef.openPopup();
  226. },
  227. // 点击stepper回调 @params { number: value ,type: ADD | SUBTRACT }
  228. changeNum(e) {
  229. const {
  230. type
  231. } = e;
  232. const {
  233. goodsDetail,
  234. skuId
  235. } = this.data;
  236. if (!isActive(goodsDetail) && type === 'ADD') {
  237. return;
  238. }
  239. this.handelCarChange({
  240. skuId,
  241. number: 1,
  242. type
  243. });
  244. },
  245. // 点击添加购物车按钮 显示 步进器
  246. async handleAddCarClick() {
  247. // 商品在售状态
  248. const {
  249. goodsDetail,
  250. skuId
  251. } = this.data; // 不在售
  252. if (!isActive(goodsDetail)) {
  253. return;
  254. }
  255. this.handelCarChange({
  256. skuId,
  257. number: 1,
  258. type: 'ADD'
  259. });
  260. },
  261. /**
  262. * 加入购物车操作被点击
  263. * @params { skuId, number: value ,type: ADD | SUBTRACT }
  264. */
  265. async handelCarChange(e) {
  266. const {
  267. skuId,
  268. number,
  269. type
  270. } = e;
  271. const {
  272. globalShopId,
  273. skuId: curSkuId
  274. } = this.data; // 发起请求 更新购物车
  275. try {
  276. // @param shopId skuId num type
  277. my.showLoading();
  278. await updateCar({
  279. shopId: globalShopId,
  280. skuId,
  281. num: number,
  282. type
  283. }); // 发起请求 查询购物车
  284. const newCar = await queryForCarList({
  285. shopId: globalShopId
  286. });
  287. updateGlobalData.call(this, newCar);
  288. my.hideLoading();
  289. } catch (err) {
  290. my.hideLoading();
  291. my.showToast({
  292. content: err.msg,
  293. type: 'none'
  294. });
  295. } // 查询最新skuId的商品
  296. const res = await queryForGoodsDetail({
  297. skuId: curSkuId
  298. }); // 查询购物车
  299. const car = await queryForCarList({
  300. shopId: globalShopId
  301. }); // 获取购物车中商品数量
  302. const quantity = this.getCarQuantity({
  303. skuId,
  304. carList: car.carts
  305. });
  306. const detail = { ...res,
  307. quantity
  308. };
  309. this.setData({
  310. goodsDetail: detail
  311. });
  312. }
  313. }
  314. })));