index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* eslint-disable arrow-parens */
  2. import { getHeaderHeight } from '../../utils';
  3. import { connect } from 'herculex';
  4. import { createSubscribe } from 'applet-page-component';
  5. import { initMallData, updateGlobalData } from '../../utils/initMallList';
  6. import { updateCar, queryForCarList, queryForGoodsList, queryForStartTip } from './service';
  7. import showToast from '../../utils/showToast';
  8. Component(createSubscribe({
  9. onShow() {
  10. const {
  11. globalShopId
  12. } = this.data;
  13. const {
  14. globalShopId: stateShopId
  15. } = this.$store.stateConfig.$global;
  16. if (globalShopId === stateShopId) {
  17. this.initWithLoading();
  18. }
  19. },
  20. onPageScroll() {// this.isArriveTop();
  21. }
  22. })(connect({
  23. mapStateToProps: {
  24. globalTabs: state => state.globalTabs || [],
  25. globalGoodsList: state => state.globalGoodsList || [],
  26. globalCar: state => state.$global.globalCar || [],
  27. globalShopId: state => state.$global.globalShopId || '',
  28. headB2bHight: state => state.$global.headB2bHight || 0,
  29. userInfo: state => state.$global.userInfo || {},
  30. jumpSkuId: state => state.$global.jumpSkuId || '',
  31. memberLevel: state => state.$global.memberLevel || ''
  32. }
  33. })({
  34. props: {
  35. componentData: {}
  36. },
  37. async didUpdate(preProps, preData) {
  38. if (preData.globalShopId !== this.data.globalShopId && this.data.globalShopId !== '') {
  39. this.setData({
  40. hasLocation: true
  41. });
  42. this.initWithLoading();
  43. }
  44. },
  45. didMount() {},
  46. data: {
  47. hasLocation: false,
  48. activeTab: 0,
  49. headerHight: getHeaderHeight(),
  50. carTotalNum: 0,
  51. scrollArriveTop: false,
  52. tabBarHight: 0,
  53. jumpId: '',
  54. // jumpItem: [tabIndex, index]
  55. jumpItem: [0, 0],
  56. extraTotalHight: 0,
  57. showFlag: false,
  58. startDeliveryAmount: 0,
  59. emptyImage: 'https://gw.alipayobjects.com/mdn/rms_0e8a29/afts/img/A*uhaRQLLUyZ0AAAAAAAAAAAAAARQnAQ'
  60. },
  61. methods: {
  62. // saveRef 调用子组件click
  63. saveRef(ref) {
  64. // 存储自定义组件实例,方便以后调用
  65. this.childClickFunc = ref;
  66. },
  67. // saveCarRef 调用购物车 子组件方法
  68. saveCarRef(ref) {
  69. // 存储自定义组件实例,方便以后调用
  70. this.carRef = ref;
  71. },
  72. // 将自组件的确认方法 放到确认按钮
  73. handleConfirm() {
  74. this.carRef.handelCreateOrder();
  75. },
  76. // 将自组件的打开购物车方法 放到购物车按钮
  77. handleCarClick() {
  78. this.carRef.openPopup();
  79. },
  80. // 刷新界面
  81. refreshPage() {
  82. my.navigateTo({
  83. url: './index'
  84. });
  85. },
  86. // init with loading
  87. async initWithLoading() {
  88. my.showLoading();
  89. try {
  90. await this.initGoodsList();
  91. my.hideLoading(); // 获取底部 头部高度
  92. this.queryHight();
  93. const {
  94. jumpId,
  95. globalGoodsList
  96. } = this.data;
  97. if (jumpId) {
  98. globalGoodsList.some((tabItem, tabIndex) => {
  99. return tabItem.list.some((item, index) => {
  100. if (item.skuId === jumpId) {
  101. this.setData({
  102. jumpItem: [tabIndex, index]
  103. });
  104. return true;
  105. }
  106. return false;
  107. });
  108. });
  109. }
  110. const [jumpActive, jumpIndex] = this.data.jumpItem;
  111. this.setData({
  112. activeTab: jumpActive
  113. });
  114. const distance = await this.queryScrollDistance(jumpIndex);
  115. jumpId ? this.childClickFunc.handleGotoItem(jumpActive, distance) : this.childClickFunc.clickRef(jumpActive); // 获取配送提示
  116. // 获取配送提示栏高度
  117. this.extraComponentHight();
  118. } catch (err) {
  119. my.hideLoading();
  120. showToast({
  121. content: err.msg,
  122. type: 'none'
  123. });
  124. }
  125. },
  126. // init
  127. async initGoodsList() {
  128. const {
  129. jumpSkuId = ''
  130. } = this.data; // 是跳转过来的
  131. this.setData({
  132. jumpId: jumpSkuId
  133. });
  134. this.commitGlobal && this.commitGlobal({
  135. jumpSkuId: ''
  136. });
  137. const {
  138. globalShopId
  139. } = this.data; // 获取商品列表数据
  140. const list = await queryForGoodsList({
  141. shopId: globalShopId
  142. }); // 获取购物车数据
  143. const car = await queryForCarList({
  144. shopId: globalShopId
  145. }); // 查询起送信息
  146. const {
  147. showFlag,
  148. startDeliveryAmount
  149. } = await queryForStartTip({
  150. shopId: globalShopId
  151. }); // 初始化数据
  152. const {
  153. leftTabs,
  154. goodsList
  155. } = initMallData({
  156. tabs: list,
  157. carList: car.carts
  158. });
  159. this.commitGlobal && this.commitGlobal({
  160. globalCar: car
  161. });
  162. this.commit && this.commit({
  163. globalTabs: leftTabs,
  164. globalGoodsList: goodsList
  165. }); // header 高度
  166. this.setData({
  167. activeTab: 0,
  168. showFlag,
  169. startDeliveryAmount
  170. });
  171. },
  172. // 点击左侧栏目
  173. // handleChange(index) {
  174. // this.setData({
  175. // activeTab: index,
  176. // });
  177. // },
  178. // // 滑动右侧
  179. onChange(index) {
  180. this.setData({
  181. activeTab: index
  182. });
  183. },
  184. /**
  185. * 某个goodsItem 被点击
  186. * @params { skuId, number: value ,type: ADD | SUBTRACT }
  187. */
  188. async handelItemChange(e) {
  189. const {
  190. skuId,
  191. number,
  192. type
  193. } = e;
  194. const {
  195. globalShopId
  196. } = this.data; // 发起请求 更新购物车
  197. try {
  198. // @param shopId skuId num type
  199. my.showLoading();
  200. await updateCar({
  201. shopId: globalShopId,
  202. skuId,
  203. num: number,
  204. type
  205. }); // 发起请求 查询购物车
  206. const car = await queryForCarList({
  207. shopId: globalShopId
  208. });
  209. my.hideLoading();
  210. updateGlobalData.call(this, car);
  211. } catch (err) {
  212. my.hideLoading();
  213. showToast({
  214. content: err.msg
  215. });
  216. }
  217. },
  218. extraComponentHight() {
  219. my.createSelectorQuery().select('.shop-deliver-info').boundingClientRect().exec(ret => {
  220. ret && this.setData({
  221. extraTotalHight: ret.reduce((total, item) => total + (item ? item.height : 0), 0)
  222. });
  223. });
  224. },
  225. isArriveTop() {
  226. my.createSelectorQuery().selectAll('.group-text').boundingClientRect().select('.mall-index-container').boundingClientRect().exec(ret => {
  227. const {
  228. top: rightTop
  229. } = ret[0][0];
  230. const {
  231. top: totalTop
  232. } = ret[1]; // 到达顶部
  233. if (getHeaderHeight() - totalTop > 0) {
  234. // 往下滑
  235. if (this.preRightTop - rightTop > 0) {
  236. this.setData({
  237. scrollArriveTop: true
  238. });
  239. } else {
  240. // 往上滑
  241. this.setData({
  242. scrollArriveTop: !(totalTop - rightTop <= 1)
  243. });
  244. }
  245. }
  246. this.preRightTop = rightTop;
  247. });
  248. },
  249. // 获取商品高度
  250. queryScrollDistance(itemIndex) {
  251. return new Promise(resolve => {
  252. my.createSelectorQuery().select('.goods-item-container').boundingClientRect().select('.group-text').boundingClientRect().exec(ret => {
  253. const goodsItemHeight = ret[0] ? ret[0].height : 0;
  254. const titleHeight = ret[1] ? ret[1].height : 0;
  255. console.log(titleHeight, goodsItemHeight, 'Height');
  256. console.log(ret[0], ret[1], 'ret');
  257. resolve(titleHeight + goodsItemHeight * itemIndex);
  258. });
  259. });
  260. },
  261. // 获取tabbar高度
  262. queryHight() {
  263. my.createSelectorQuery().select('.tabbar-box.fixed').boundingClientRect().exec(ret => {
  264. const tabBarHight = ret[0] ? ret[0].height : 0;
  265. this.setData({
  266. tabBarHight
  267. });
  268. });
  269. }
  270. }
  271. })));