orderBy.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. // pages/orderBy/orderBy.js
  2. import {
  3. get,
  4. post
  5. } from '../../utils/http'
  6. import {
  7. PROJECT_TYPE
  8. } from '../../utils/global'
  9. import {
  10. getStoreInfo
  11. } from '../../utils/util'
  12. const app = getApp()
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {},
  18. initData: {
  19. current: 0,
  20. currentType: 1,
  21. currentList: [{
  22. id: 0,
  23. title: '精选项目',
  24. type: PROJECT_TYPE.project,
  25. source: 'project',
  26. child: []
  27. },
  28. {
  29. id: 1,
  30. title: '项目耗材',
  31. type: PROJECT_TYPE.product,
  32. source: 'product',
  33. child: []
  34. },
  35. {
  36. id: 2,
  37. title: '项目卡券',
  38. type: PROJECT_TYPE.cardgoods,
  39. source: 'cardgoods',
  40. child: []
  41. },
  42. {
  43. id: 3,
  44. title: '美妆产品',
  45. type: PROJECT_TYPE.goods,
  46. source: 'goods',
  47. child: []
  48. },
  49. {
  50. id: 4,
  51. title: '次卡',
  52. type: PROJECT_TYPE.secondaryCard,
  53. source: 'secondaryCard',
  54. child: []
  55. }
  56. ],
  57. currentKeys: {},
  58. currentItemIndex: 0,
  59. currentItemCatId: 0,
  60. productData: {},
  61. productDataLoading: false,
  62. storeId: 0,
  63. storeData: {},
  64. inFirstIn: false,
  65. navBarData: app.globalData.navBarData,
  66. navBarBgClass: '',
  67. selectProductList: 0,
  68. productListV2: [],
  69. showProducePup: false,
  70. selectProductData: {},
  71. selectProductIndex: '',
  72. selectProductCar: [],
  73. onCloseCar: false,
  74. showMoney: 0.0,
  75. toView: '',
  76. orderDiscount: {},
  77. onMakeup: false,
  78. // 美妆产品购物车列表
  79. selectMakeupList: [],
  80. page: 1,
  81. limit: 20,
  82. showMoneyGood: 0,
  83. // 点击美妆产品购物袋图标标识
  84. isIcon: false,
  85. // 产品id
  86. sourceId: '',
  87. // 每个商品数据
  88. proItem: '',
  89. // 美妆产品总数量
  90. sum: 0,
  91. // 商品价格
  92. v2_price: '',
  93. goods_ids: [], //购物车中的产品id
  94. exchangeInfo: [], //需要加购多少商品的价格信息
  95. isNewUser: 0, //结果,1是新用户,0不是新用户
  96. },
  97. /**
  98. * 首页数据
  99. */
  100. getHomeData() {
  101. get('api/home', {}, (res) => {
  102. this.setData({
  103. homeData: res.data
  104. }, )
  105. })
  106. },
  107. /**
  108. * 获取当前用户折扣
  109. * 折扣类型,0.无折扣,20.任意三个项目8折,50.新人五折活动,100.仪器免费使用权益
  110. */
  111. orderDiscount() {
  112. get('v2/api/order/discount', {}, (res) => {
  113. this.setData({
  114. orderDiscount: res.data
  115. }, )
  116. })
  117. },
  118. // 选择项目左边的操作
  119. selectProductList(e) {
  120. let selectProductList = e.currentTarget.dataset.index
  121. this.setData({
  122. selectProductList: selectProductList,
  123. toView: 'A' + selectProductList
  124. })
  125. },
  126. selectProduct(e) {
  127. let index = e.currentTarget.dataset.index
  128. this.setData({
  129. selectProductIndex: index
  130. })
  131. },
  132. rpxTopx(rpx) {
  133. let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  134. let px = (deviceWidth / 750) * Number(rpx)
  135. return Math.floor(px);
  136. },
  137. onDoHeight() {
  138. let productListV2 = this.data.productListV2
  139. let inHeight = 0
  140. let doHeight = []
  141. doHeight.push(0)
  142. for (let i = 0; i < productListV2.length; i++) {
  143. if (productListV2[i].products[0] && productListV2[i].products[0].type == 'one') {
  144. let needAdd = 0
  145. for (let j = 0; j < productListV2[i].products.length; j++) {
  146. needAdd = needAdd + 280 * productListV2[i].products[j].items.length + 140
  147. }
  148. inHeight = inHeight + this.rpxTopx(20 + 40 + 20 + needAdd)
  149. // console.log('xxxxxxxxxxx走了这里哈哈哈' + needAdd)
  150. } else {
  151. inHeight = inHeight + this.rpxTopx(20 + 40 + 20 + 280 * productListV2[i].products.length)
  152. }
  153. doHeight.push(inHeight)
  154. }
  155. this.setData({
  156. doHeight: doHeight
  157. })
  158. // console.log(doHeight)
  159. },
  160. scroll(e) {
  161. let that = this
  162. let doHeight = this.data.doHeight
  163. // console.log(e.detail.scrollTop)
  164. if (e.detail.scrollTop < doHeight[1] - 20) {
  165. that.setData({
  166. selectProductList: 0,
  167. })
  168. return
  169. }
  170. for (let i = 0; i < doHeight.length; i++) {
  171. if (e.detail.scrollTop > (doHeight[i] - 20) && e.detail.scrollTop < doHeight[i + 1]) {
  172. // console.log(e.detail.scrollTop)
  173. that.setData({
  174. selectProductList: i,
  175. })
  176. }
  177. }
  178. // if (e.detail.scrollTop > )
  179. },
  180. // 关闭产品包
  181. onClosePay() {
  182. this.setData({
  183. showProducePup: false
  184. })
  185. this.setData({
  186. isIcon: this.data.showProducePup
  187. })
  188. },
  189. buttomCar: function () {
  190. this.setData({
  191. onCloseCar: true
  192. })
  193. },
  194. // 删除购物车
  195. inDeleteCarOne(e) {
  196. let index = e.currentTarget.dataset.index
  197. let selectProductCars = this.data.selectProductCar
  198. selectProductCars.splice(index, 1)
  199. this.setData({
  200. selectProductCar: selectProductCars
  201. })
  202. this.showCarMoney()
  203. },
  204. toSubmit: function () {
  205. if (this.data.selectProductCar.length == 0) {
  206. return
  207. }
  208. // 判断是否是新用户
  209. let money = 0
  210. if (this.data.isNewUser == 1) {
  211. money = this.data.showMoney
  212. } else {
  213. money = this.data.showMoney
  214. }
  215. wx.navigateTo({
  216. url: '../reserveCar/reserveCar?data=' + JSON.stringify(this.data.selectProductCar) + '&&money=' + this.data.showMoney + '&&type=project',
  217. })
  218. },
  219. onKeySubmit: function (e) {
  220. let keySubmit = e.currentTarget.dataset.item
  221. wx.navigateTo({
  222. url: '../reserveCar/reserveCar?data=' + JSON.stringify(keySubmit) + '&&money=' + keySubmit.price + '&&type=one',
  223. })
  224. },
  225. showCarMoney() {
  226. let that = this
  227. let money = 0
  228. let selectProductCars = this.data.current == 3 ? this.data.selectMakeupList : this.data.selectProductCar
  229. console.log(selectProductCars);
  230. for (let i = 0; i < selectProductCars.length; i++) {
  231. // 判断是否是新人有新人价格
  232. let price = this.data.isNewUser == 1 && selectProductCars[i].new_price != 0 ? selectProductCars[i].new_price : selectProductCars[i].price
  233. money = money + price
  234. console.log(money, 'moneymoney');
  235. if (selectProductCars[i].selectMaterial) {
  236. money = money + selectProductCars[i].selectMaterial.price * 1
  237. }
  238. }
  239. this.setData({
  240. showMoney: money.toFixed(2)
  241. })
  242. },
  243. onCloseCar: function () {
  244. this.setData({
  245. onCloseCar: false
  246. })
  247. },
  248. // 选择项目
  249. toSelectProduct(e) {
  250. let toSelectProduct = e.currentTarget.dataset.item
  251. for (let i = 0; i < this.data.selectProductCar.length; i++) {
  252. if (toSelectProduct.id == this.data.selectProductCar[i].id) {
  253. wx.showToast({
  254. title: '单项目只能选一次!',
  255. icon: "none"
  256. })
  257. return
  258. }
  259. }
  260. this.setData({
  261. toSelectProduct: toSelectProduct,
  262. selectProductIndex: '空',
  263. showProducePup: true
  264. })
  265. this.productV2(toSelectProduct.id)
  266. },
  267. submit_bottom: function () {
  268. let selectProductCars = this.data.selectProductCar
  269. let toSelectProducts = this.data.toSelectProduct
  270. if (this.data.selectProductIndex == '空') {
  271. toSelectProducts.selectMaterial = ''
  272. } else {
  273. toSelectProducts.selectMaterial = this.data.selectProductData.upgrade[this.data.selectProductIndex]
  274. }
  275. selectProductCars.push(toSelectProducts)
  276. this.setData({
  277. showProducePup: false,
  278. selectProductCar: selectProductCars
  279. })
  280. this.showCarMoney()
  281. },
  282. /**
  283. * 跳转店铺列表
  284. */
  285. goToStore() {
  286. if (this.data.inFirstIn) {
  287. return;
  288. }
  289. wx.navigateTo({
  290. url: '/pages/store/store?defaultStore=' +
  291. JSON.stringify(this.data.homeData.store)
  292. })
  293. },
  294. /**
  295. * 店铺信息
  296. * /api/store/info
  297. */
  298. getStoreInfo(store_id) {
  299. get(
  300. 'api/store/info', {
  301. store_id
  302. },
  303. (res) => {
  304. this.setData({
  305. ['homeData.store']: res.data
  306. },
  307. () => {
  308. wx.setStorageSync('store_id', String(store_id))
  309. app.globalData.storeData = res.data
  310. }
  311. )
  312. console.log(res)
  313. }
  314. )
  315. },
  316. /**
  317. * 获取项目产品数据
  318. * /api/store/info
  319. */
  320. productList() {
  321. get(
  322. 'v2/api/product/list', {
  323. store_id: this.data.storeId
  324. },
  325. (res) => {
  326. // 新人价格去掉.00
  327. res.data.forEach(item => {
  328. item.products.forEach(item => {
  329. item.new_price = Number(item.new_price)
  330. })
  331. });
  332. this.setData({
  333. productListV2: res.data
  334. })
  335. this.onDoHeight()
  336. if (app.globalData.toViewIndex != 0) {
  337. this.setData({
  338. toView: 'A' + (app.globalData.toViewIndex - 1),
  339. selectProductList: app.globalData.toViewIndex - 1
  340. })
  341. } else {
  342. this.setData({
  343. toView: 'A' + 0,
  344. selectProductList: 0
  345. })
  346. }
  347. console.log(res)
  348. }
  349. )
  350. },
  351. /**
  352. * 获取升级产品列表
  353. * /api/store/info
  354. */
  355. productV2(id) {
  356. get(
  357. 'v2/api/product/product', {
  358. id: id,
  359. store_id: this.data.storeId
  360. },
  361. (res) => {
  362. this.setData({
  363. selectProductData: res.data
  364. })
  365. console.log(res)
  366. }
  367. )
  368. },
  369. /**
  370. * 生命周期函数--监听页面加载
  371. */
  372. onLoad: function (options) {
  373. console.log(options, 'options');
  374. this.setData({
  375. navBarHeight: app.globalData.navBarData.navBarHeight,
  376. })
  377. // this.getHomeData()
  378. let data = JSON.parse(JSON.stringify(this.initData))
  379. data.storeData = getApp().globalData.storeData
  380. data.storeId = data.storeData.id || wx.getStorageSync('store_id')
  381. // 存在传入的分类类型
  382. if (options.hasOwnProperty('cat_type')) {
  383. data.currentKeys = this.buildKeys(data.currentList, 'type')
  384. let _current = data.currentKeys['key_' + options.cat_type]
  385. if (_current != undefined) {
  386. data.current = _current
  387. data.currentType = options.cat_type
  388. }
  389. }
  390. // 存在传入的店铺ID
  391. if (options.hasOwnProperty('store_id')) {
  392. data.storeId = options.store_id
  393. // 更新店铺信息
  394. getStoreInfo(data.storeId)
  395. .then((res) => {
  396. data.storeData = res
  397. this.setData({
  398. storeData: res
  399. })
  400. })
  401. .catch(() => {})
  402. }
  403. this.setData(data, () => {
  404. this.getProductCategory(options)
  405. })
  406. this.productList()
  407. this.needAddGood()
  408. // 进入页面判断是否是直接跳转到美妆产品的是的话调用修改类型
  409. if (app.globalData.showGoods) {
  410. this.gotoGoods()
  411. }
  412. },
  413. // 监听页面隐藏
  414. onHide() {
  415. // 将全局的项目分类下标重置
  416. app.globalData.toViewIndex = 0
  417. },
  418. noneEnoughPeople() {
  419. },
  420. /**
  421. * 生命周期函数--监听页面初次渲染完成
  422. */
  423. onReady: function () {},
  424. /**
  425. * 生命周期函数--监听页面显示
  426. */
  427. onShow: function () {
  428. // this.orderDiscount()
  429. let version1 = wx.getStorageSync('version1')
  430. if (!version1 || version1 != 1) {
  431. this.setData({
  432. // inFirstIn: true
  433. })
  434. wx.setStorageSync('version1', 1)
  435. }
  436. // 当切换了店铺时,重新请求一次数据
  437. let storeData = getApp().globalData.storeData
  438. let storeId = storeData.id || wx.getStorageSync('store_id')
  439. if (storeId != this.data.storeId && this.data.storeId) {
  440. let data = JSON.parse(JSON.stringify(this.initData))
  441. this.setData(data, () => {
  442. this.setData({
  443. storeId: storeId,
  444. storeData: storeData
  445. }, () => {
  446. this.getProductCategory()
  447. this.getStoreInfo(storeId)
  448. this.productList()
  449. })
  450. })
  451. } else {
  452. this.getStoreInfo(storeId)
  453. }
  454. let that = this
  455. let curres = wx.getStorageSync('curre')
  456. // console.log('haves1')
  457. if (curres && curres == 1) {
  458. // console.log('haves')
  459. let item = that.data.currentList[0]
  460. this.setData({
  461. current: item.id,
  462. currentItemIndex: 0,
  463. currentType: item.type
  464. },
  465. () => {
  466. // this.getProduct(item.type, item.child[0].id)
  467. let _dataset = {
  468. currentTarget: {
  469. dataset: {
  470. item: item.child[0],
  471. index: 0
  472. }
  473. }
  474. }
  475. this.onTabsItem(_dataset)
  476. }
  477. )
  478. wx.setStorageSync('curre', 0)
  479. }
  480. setTimeout(() => {
  481. if (this.data.current == 3) {
  482. this.needAddGood()
  483. } else {
  484. this.needAddCar()
  485. }
  486. }, 500)
  487. if (app.globalData.toViewIndex != 0) {
  488. this.setData({
  489. toView: 'A' + (app.globalData.toViewIndex - 1),
  490. selectProductList: app.globalData.toViewIndex - 1
  491. })
  492. } else {
  493. this.setData({
  494. toView: 'A' + 0,
  495. selectProductList: 0
  496. })
  497. }
  498. // 检查是否是新用户
  499. this.checkNewUser()
  500. },
  501. //精选项目
  502. needAddCar() {
  503. let toSelectProduct = wx.getStorageSync('toSelectProduct')
  504. if (toSelectProduct && JSON.parse(toSelectProduct) == []) {
  505. wx.setStorageSync('toSelectProduct', JSON.stringify([]))
  506. return
  507. }
  508. let cardAddData = []
  509. if (toSelectProduct) {
  510. cardAddData = JSON.parse(toSelectProduct)
  511. }
  512. wx.setStorageSync('toSelectProduct', JSON.stringify([]))
  513. let selectProductCarData = this.data.selectProductCar
  514. for (let i = 0; i < selectProductCarData.length; i++) {
  515. if (selectProductCarData[i].id == cardAddData[i].id || cardAddData.id) {
  516. return
  517. }
  518. }
  519. console.log(selectProductCarData, 'cardAddData', cardAddData);
  520. selectProductCarData = selectProductCarData.concat(cardAddData)
  521. this.setData({ //对上一个页面data中的参数进行赋值(上一个页面的数据是在当前页面进行赋值的),就变相实现了向上一个页面传值
  522. selectProductCar: selectProductCarData
  523. })
  524. this.showCarMoney()
  525. },
  526. //美妆产品
  527. needAddGood() {
  528. let params = {
  529. store_id: this.data.storeId,
  530. page: this.data.page,
  531. limit: this.data.limit
  532. }
  533. get('v2/api/car/list', params, (res) => {
  534. if (res.code == 200) {
  535. let selectProductCarData = []
  536. selectProductCarData = selectProductCarData.concat(res.data.list)
  537. // 购物车产品总数量
  538. let sum = 0
  539. selectProductCarData.map(item => {
  540. sum += item.num
  541. })
  542. selectProductCarData.sum = sum
  543. // 设置美妆产品购物车总数量
  544. this.setData({
  545. sum: sum
  546. })
  547. this.setData({
  548. selectMakeupList: selectProductCarData,
  549. showMoneyGood: res.data.total_money == null ? "0.00" : res.data.total_money
  550. })
  551. if (this.data.selectMakeupList < res.data.count) {
  552. this.setData({
  553. page: this.data.page + 1
  554. })
  555. this.needAddGood()
  556. }
  557. if (res.data.list.length == 0) {
  558. this.setData({
  559. onMakeup: false
  560. })
  561. }
  562. }
  563. })
  564. },
  565. /**
  566. * 页面上拉触底事件的处理函数
  567. */
  568. onReachBottom: function () {
  569. this.getProduct(this.data.currentType, this.data.currentItemCatId, 1)
  570. },
  571. /**
  572. * 用户点击右上角分享
  573. */
  574. onShareAppMessage: function () {},
  575. /**
  576. * 转化对象
  577. */
  578. buildKeys: function (params, key) {
  579. let keys = {}
  580. for (let i = 0; i < params.length; i++) {
  581. let item = params[i]
  582. keys['key_' + item[key]] = i
  583. }
  584. return keys
  585. },
  586. /**
  587. * 切换tabs
  588. */
  589. onTabsChange(e) {
  590. if (this.data.inFirstIn) {
  591. return;
  592. }
  593. let item = e.currentTarget.dataset.item
  594. this.setData({
  595. current: item.id,
  596. currentItemIndex: 0,
  597. currentType: item.type
  598. },
  599. () => {
  600. // this.getProduct(item.type, item.child[0].id)
  601. let _dataset = {
  602. currentTarget: {
  603. dataset: {
  604. item: item.child[0],
  605. index: 0
  606. }
  607. }
  608. }
  609. this.onTabsItem(_dataset)
  610. if (this.data.current == 3) {
  611. this.needAddGood()
  612. return
  613. }
  614. this.showCarMoney()
  615. }
  616. )
  617. },
  618. /**
  619. * 切换子tabs
  620. */
  621. onTabsItem(e) {
  622. console.log(e, 'e')
  623. if (this.data.inFirstIn) {
  624. return;
  625. }
  626. // this.setData({
  627. // inFirstIn:true
  628. // })
  629. let that = this
  630. let item = e.currentTarget.dataset.item
  631. let currentItemIndex = e.currentTarget.dataset.index
  632. if (!item) {
  633. return
  634. }
  635. this.setData({
  636. currentItemIndex: currentItemIndex,
  637. currentItemCatId: item.id
  638. },
  639. () => {
  640. console.log(this.data.current, 'this.data.current')
  641. // if(this.data.current === 4){
  642. // // 查询次卡列表
  643. // this.getSecondaryCardList(that.data.currentType, item.id)
  644. // } else {
  645. // that.getProduct(that.data.currentType, item.id)
  646. // }
  647. that.getProduct(that.data.currentType, item.id)
  648. }
  649. )
  650. },
  651. knowSubmit: function () {
  652. this.setData({
  653. inFirstIn: false
  654. })
  655. },
  656. /**
  657. * 获取产品分类
  658. */
  659. getProductCategory(options = {}) {
  660. let {
  661. currentList,
  662. current,
  663. currentType,
  664. currentItemIndex,
  665. currentItemCatId
  666. } = this.data
  667. get('api/product/category', {}, (res) => {
  668. console.log(res, 'res')
  669. let {
  670. project,
  671. product,
  672. cardgoods,
  673. other
  674. } = res.data
  675. currentList[0].child = project
  676. currentList[1].child = product
  677. currentList[2].child = cardgoods
  678. currentList[3].child = other
  679. currentList[4].child = cardgoods
  680. // 存在传入的分类ID
  681. if (options.hasOwnProperty('cat_id')) {
  682. let catKyes = this.buildKeys(currentList[current].child, 'id')
  683. let _currentItemIndex = catKyes['key_' + options.cat_id]
  684. if (_currentItemIndex != undefined) {
  685. currentItemIndex = _currentItemIndex
  686. currentItemCatId = options.cat_id
  687. }
  688. } else {
  689. // 判断是否是跳转到下单页面直接显示美妆产品
  690. if (app.globalData.showGoods) {
  691. current = (other.length > 0 && '3') ||
  692. (project.length > 0 && '0') ||
  693. (product.length > 0 && '1') ||
  694. (cardgoods.length > 0 && '2')
  695. } else {
  696. current = (project.length > 0 && '0') ||
  697. (product.length > 0 && '1') ||
  698. (cardgoods.length > 0 && '2') ||
  699. (other.length > 0 && '3')
  700. }
  701. switch (current) {
  702. // 默认值
  703. case 0:
  704. currentItemCatId = project[0].id
  705. break
  706. case 1:
  707. currentItemCatId = product[0].id
  708. break
  709. case 2:
  710. currentItemCatId = cardgoods[0].id
  711. break
  712. case 3:
  713. currentItemCatId = other[0].id
  714. break
  715. }
  716. }
  717. this.setData({
  718. currentList,
  719. current,
  720. currentItemIndex,
  721. currentItemCatId
  722. },
  723. () => {
  724. if (project.length > 0) {
  725. this.getProduct(currentType, currentItemCatId)
  726. }
  727. }
  728. )
  729. })
  730. },
  731. /**
  732. * 项目/产品列表
  733. * type: 数据类型,1项目,2项目耗材,3美妆产品,默认1
  734. */
  735. getProduct(ptype, pcat_id, turn = 0) {
  736. let that = this
  737. let key = `pkey_${ptype}_${pcat_id}`
  738. let {
  739. productData,
  740. productDataLoading,
  741. storeId
  742. } = that.data
  743. let pcatData = productData[key] || {
  744. total: 0,
  745. list: [],
  746. page: 1,
  747. has_more: 1,
  748. type: ptype,
  749. cat_id: pcat_id
  750. }
  751. if (productDataLoading || !pcatData.has_more) return
  752. // 是否为翻页
  753. if (!turn && pcatData.total > 0) return
  754. this.setData({
  755. productDataLoading: true
  756. })
  757. let page = pcatData.page
  758. get(
  759. this.data.current !== 4 ? 'api/product' : 'v2/api/card/list', this.data.current !== 4 ? {
  760. store_id: storeId,
  761. type: ptype,
  762. category_id: pcat_id,
  763. page: page
  764. } : {
  765. store_id: storeId,
  766. category_id: pcat_id,
  767. page: page,
  768. limit: 5
  769. },
  770. (res) => {
  771. that.setData({
  772. productDataLoading: false
  773. })
  774. let plist = pcatData.list.concat(res.data.list)
  775. page++
  776. pcatData = Object.assign(pcatData, {
  777. total: res.data.total,
  778. page: page,
  779. list: plist,
  780. has_more: plist.length < res.data.total && res.data.list.length > 0
  781. })
  782. productData[key] = pcatData
  783. this.setData({
  784. productData: productData
  785. })
  786. },
  787. (err) => {
  788. that.setData({
  789. productDataLoading: false
  790. })
  791. }
  792. )
  793. },
  794. // 获取需要加购多少商品的价格
  795. getExchangePrice() {
  796. post('v2/api/car/plus_project', {
  797. store_id: this.data.storeId || wx.getStorageSync('store_id'),
  798. goods_id: '',
  799. type: 1
  800. }, (res) => {
  801. if (res.code == 200) {
  802. this.setData({
  803. exchangeInfo: res.data
  804. })
  805. }
  806. })
  807. },
  808. onOpenMakeup() {
  809. // 获取需要加购多少商品的价格
  810. this.getExchangePrice()
  811. this.setData({
  812. onMakeup: true
  813. })
  814. },
  815. onCloseMakeup() {
  816. this.setData({
  817. onMakeup: false
  818. })
  819. },
  820. // 删除购物车
  821. inDeleteCarGood(e) {
  822. let index = e.currentTarget.dataset.index
  823. get('v2/api/car/del', {
  824. car_id: this.data.selectMakeupList[index].id
  825. }, res => {
  826. if (res.code == 200) {
  827. this.needAddGood()
  828. // 获取需要加购多少商品的价格
  829. this.getExchangePrice()
  830. }
  831. })
  832. },
  833. //购物车结算
  834. toSubmitGood: function () {
  835. if (this.data.selectMakeupList.length == 0) {
  836. return
  837. }
  838. // let _projectData = this.data.selectMakeupList
  839. // 筛选出购物车中所有商品的id
  840. let {
  841. goods_ids
  842. } = this.data
  843. goods_ids = []
  844. this.data.selectMakeupList.map(item => {
  845. goods_ids.push(item.goods_id)
  846. })
  847. wx.navigateTo({
  848. url: `/pages/goodsOrderConfirm/goodsOrderConfirm?source=goods&storeId=${this.data.storeId}&goods_ids=${goods_ids}&type=1`,
  849. })
  850. },
  851. onChangeNum(e) {
  852. let params = {
  853. car_id: this.data.selectMakeupList[e].id,
  854. num: this.data.selectMakeupList[e].num
  855. }
  856. get('/v2/api/car/add_number', params, res => {
  857. if (res.code == 200) {
  858. this.needAddGood()
  859. // 获取加购多少商品价格
  860. this.getExchangePrice()
  861. }
  862. })
  863. },
  864. onReduce(e) {
  865. let index = e.currentTarget.dataset.index
  866. let num = this.data.selectMakeupList[index].num
  867. num = num - 1
  868. if (num < 0) {
  869. num = 0
  870. }
  871. let dataNum = `selectMakeupList[${index}].num`
  872. this.setData({
  873. [dataNum]: num
  874. })
  875. this.onChangeNum(index)
  876. },
  877. onLationAdd(e) {
  878. let index = e.currentTarget.dataset.index
  879. let num = this.data.selectMakeupList[index].num
  880. num = num + 1
  881. let dataNum = `selectMakeupList[${index}].num`
  882. this.setData({
  883. [dataNum]: num
  884. })
  885. this.onChangeNum(index)
  886. },
  887. // 点击美妆产品购物袋图标显示弹出购物车框
  888. showProduce(e) {
  889. console.log(e);
  890. this.setData({
  891. showProducePup: true,
  892. sourceId: e.target.dataset.proitem.id,
  893. proItem: e.target.dataset.proitem
  894. })
  895. this.setData({
  896. isIcon: this.data.showProducePup
  897. })
  898. },
  899. // 点击美妆产品购物袋加入购物车
  900. submit_add(type) {
  901. let params = {
  902. goods_id: this.data.sourceId,
  903. store_id: this.data.storeId
  904. }
  905. post('v2/api/car/add', params, (res) => {
  906. if (res.code == 200) {
  907. if (type == 'shopping') {
  908. wx.navigateTo({
  909. url: `/pages/goodsOrderConfirm/goodsOrderConfirm?storeId=${this.data.storeId}`,
  910. })
  911. } else {
  912. wx.showToast({
  913. title: '成功加入购物车!',
  914. icon: 'none'
  915. })
  916. this.needAddGood()
  917. setTimeout(() => {
  918. // wx.switchTab({
  919. // url: '/pages/orderBy/orderBy?cat_type=3',
  920. // })
  921. this.setData({
  922. showProducePup: false,
  923. isIcon: false
  924. })
  925. }, 1000)
  926. }
  927. }
  928. })
  929. },
  930. // 点击商品或者按钮直接跳转到下单页面的美妆产品页面(进入页面将类型改为美妆产品)
  931. gotoGoods() {
  932. this.setData({
  933. current: 3,
  934. currentType: 3,
  935. })
  936. },
  937. // 监听tab切换(判断进入页面是是否是上次跳转进来的美妆产品如果是直接刷新(current==3判断类型))
  938. onTabItemTap() {
  939. //具体操作
  940. if (this.data.current == 3) {
  941. wx.reLaunch({
  942. url: '/pages/orderBy/orderBy',
  943. })
  944. }
  945. },
  946. // 检查是否是新用户
  947. checkNewUser() {
  948. get('v2/api/order/check_new_user', {}, (res => {
  949. // 结果,1是新用户,0不是新用户
  950. this.setData({
  951. isNewUser: res.data.status
  952. })
  953. }))
  954. }
  955. })