projectDetail.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http'
  5. import {
  6. PROJECT_TYPE
  7. } from '../../utils/global'
  8. var WxParse = require('../../components/local/wxParse/wxParse.js')
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. tempTime: null,
  15. visible2: false,
  16. deviceList: [],
  17. projectData: null,
  18. currentDevice: 0,
  19. currentChangeDevice: null,
  20. currentChangeTime: null,
  21. source: 'project',
  22. sourceType: 1,
  23. sourceId: 0,
  24. storeId: 0,
  25. topBannerCurrentSwiper: 0,
  26. navBarTitles: {
  27. project: '项目详情',
  28. product: '套餐详情',
  29. goods: '产品详情',
  30. cardgoods: '产品详情',
  31. secondaryCard: '次卡详情'
  32. },
  33. // 下面是选择项目的操作
  34. showProducePup: false,
  35. selectProductData: {},
  36. selectProductIndex: '',
  37. selectProductCar: [],
  38. onCloseCar: false,
  39. showMoney: 0.0,
  40. toSelectProduct: {}
  41. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad: function (options) {
  46. console.log(options, 'options')
  47. let source = options.source || 'project'
  48. let sourceId = options.project_id || options.id
  49. let sourceType = PROJECT_TYPE[source] || 1
  50. wx.setNavigationBarTitle({
  51. title: this.data.navBarTitles[source]
  52. })
  53. let storeId =
  54. options.store_id ||
  55. getApp().globalData.storeData.id ||
  56. wx.getStorageSync('store_id')
  57. this.setData({
  58. source,
  59. sourceId,
  60. sourceType,
  61. storeId
  62. }, () => {
  63. })
  64. },
  65. /**
  66. * 生命周期函数--监听页面初次渲染完成
  67. */
  68. onReady: function () {},
  69. /**
  70. * 生命周期函数--监听页面显示
  71. */
  72. onShow: function () {
  73. if(wx.getStorageSync('store_id')){
  74. this.getProjectInfo(this.data.sourceId, wx.getStorageSync('store_id'))
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面隐藏
  79. */
  80. onHide: function () {},
  81. /**
  82. * 生命周期函数--监听页面卸载
  83. */
  84. onUnload: function () {},
  85. /**
  86. * 页面相关事件处理函数--监听用户下拉动作
  87. */
  88. onPullDownRefresh: function () {},
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom: function () {},
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. console.log(this.data.sourceId, 'this.data.sourceId')
  98. return {
  99. path: `pages/projectDetail/projectDetail?project_id=${this.data.sourceId}&source=${this.data.source}&store_id=${wx.getStorageSync('store_id')}`, // 他人通过卡片进入小程序的路径,可以在后面拼接URL的形式带参数
  100. };
  101. },
  102. isShow() {},
  103. /**
  104. * swiper滚动事件
  105. * @param {*} e
  106. */
  107. swiperChange: function (e) {
  108. this.setData({
  109. topBannerCurrentSwiper: e.detail.current
  110. })
  111. },
  112. /**
  113. * 去支付
  114. */
  115. onPay() {
  116. // wx.requestPayment({
  117. // timeStamp: '',
  118. // nonceStr: '',
  119. // package: '',
  120. // signType: 'MD5',
  121. // paySign: '',
  122. // success(res) {},
  123. // fail(res) {}
  124. // })
  125. },
  126. /**
  127. * 弹框状态
  128. */
  129. onPopupState(e, key, value) {
  130. if (e) {
  131. key = e.currentTarget.dataset.key
  132. value = e.currentTarget.dataset.value
  133. }
  134. this.setData({
  135. [key]: value
  136. })
  137. },
  138. /**
  139. * 选择设备
  140. */
  141. onDeviceChange(e) {
  142. if (this.data.deviceList[e.currentTarget.dataset.index].status == 0) {
  143. return
  144. }
  145. this.setData({
  146. currentDevice: e.currentTarget.dataset.index
  147. })
  148. },
  149. onConfirmDevice() {
  150. let currentChangeDevice = this.data.deviceList[this.data.currentDevice]
  151. if (currentChangeDevice.status == 0) {
  152. wx.showToast({
  153. title: '当前设备已被预约',
  154. icon: 'none'
  155. })
  156. return
  157. }
  158. this.setData({
  159. currentChangeDevice
  160. })
  161. this.onPopupState(null, 'visible2', false)
  162. },
  163. onConfirmDate() {
  164. // if(this.data.currentChangeTime.status == 0) {
  165. // wx.showToast({
  166. // title: '当前设备已被预约',
  167. // icon: 'none'
  168. // })
  169. // return
  170. // }
  171. this.setData({
  172. currentChangeTime: this.data.tempTime
  173. })
  174. this.onPopupState(null, 'visible', false)
  175. this.getProjectDevice(this.data.currentChangeTime)
  176. },
  177. /**
  178. * 选择时间
  179. */
  180. onVisibleChange(e) {
  181. console.log(e)
  182. this.setData({
  183. // currentChangeTime: e.detail
  184. tempTime: e.detail
  185. })
  186. },
  187. /**
  188. * 获取详情页数据
  189. */
  190. getProjectInfo(product_id, store_id) {
  191. get(
  192. this.data.source === 'secondaryCard' ? 'v2/api/card/info' : 'api/product/info', this.data.source === 'secondaryCard' ? {
  193. id: this.data.sourceId,
  194. store_id
  195. } : {
  196. store_id,
  197. product_id,
  198. type: this.data.sourceType
  199. },
  200. (res) => {
  201. let content = this.removeCss(res.data.content)
  202. res.data.content = this.removeCss(res.data.content)
  203. WxParse.wxParse('article', 'html', content, this, 5)
  204. this.setData({
  205. projectData: res.data
  206. })
  207. }
  208. )
  209. },
  210. /**
  211. * 获取设备
  212. */
  213. getProjectDevice(time) {
  214. let {
  215. currentChangeTime
  216. } = this.data
  217. get(
  218. 'api/project/device', {
  219. project_id: this.data.projectData.id,
  220. store_id: this.data.projectData.store_id,
  221. order_day: currentChangeTime ? currentChangeTime.order_day : '',
  222. order_time_id: currentChangeTime ?
  223. currentChangeTime.time_id : currentChangeTime
  224. },
  225. (res) => {
  226. this.setData({
  227. deviceList: res.data
  228. },
  229. () => {
  230. this.onPopupState(null, 'visible2', true)
  231. }
  232. )
  233. }
  234. )
  235. },
  236. /**
  237. * 去除富文本图片默认样式
  238. */
  239. removeCss(content) {
  240. let reg = /(style|class)="[^"]+"/gi
  241. let img = /<img[^>]+>/gi
  242. let res
  243. if (img.test(content)) {
  244. res = content.match(img)
  245. for (let i = 0; i < res.length; i++) {
  246. content = content.replace(res[i], res[i].replace(reg, ''))
  247. }
  248. }
  249. // (/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
  250. return content.replace(/\<img/gi, '<img class="richImg" ')
  251. },
  252. // 这里是选择项目
  253. showProjectSelect() {
  254. let toSelectProduct = {}
  255. toSelectProduct.desc = this.data.projectData.tags
  256. toSelectProduct.cover_url = this.data.projectData.banner_urls[0]
  257. toSelectProduct.name = this.data.projectData.name
  258. toSelectProduct.use_time = this.data.projectData.use_time
  259. toSelectProduct.price = this.data.projectData.price
  260. toSelectProduct.id = this.data.projectData.id
  261. console.log(toSelectProduct,'toSelectProduct2222');
  262. this.setData({
  263. toSelectProduct: toSelectProduct,
  264. selectProductIndex: '空',
  265. showProducePup: true
  266. })
  267. this.productV2(toSelectProduct.id)
  268. },
  269. /**
  270. * 获取升级产品列表
  271. * /api/store/info
  272. */
  273. productV2(id) {
  274. console.log(222222);
  275. get(
  276. 'v2/api/product/product', {
  277. id: id,
  278. store_id: this.data.storeId
  279. },
  280. (res) => {
  281. this.setData({
  282. selectProductData: res.data
  283. })
  284. console.log(res)
  285. }
  286. )
  287. },
  288. selectProduct(e) {
  289. let index = e.currentTarget.dataset.index
  290. this.setData({
  291. selectProductIndex: index
  292. })
  293. },
  294. // 关闭产品包
  295. onClosePay() {
  296. this.setData({
  297. showProducePup: false
  298. })
  299. },
  300. submit_bottom: function () {
  301. let toSelectProducts = this.data.toSelectProduct
  302. if (this.data.selectProductIndex == '空') {
  303. toSelectProducts.selectMaterial = ''
  304. } else {
  305. toSelectProducts.selectMaterial = this.data.selectProductData.upgrade[this.data.selectProductIndex]
  306. }
  307. console.log(toSelectProducts)
  308. this.setData({
  309. showProducePup: false,
  310. toSelectProduct: toSelectProducts
  311. })
  312. this.setSubmitCar()
  313. },
  314. setSubmitCar() {
  315. let currentPage = getCurrentPages(); // 获取当前页的数据,
  316. let previousPage = currentPage[currentPage.length - 2]; // 上一页的数据,
  317. console.log(previousPage);
  318. let selectProductCarData =!previousPage?'':previousPage.data.selectProductCar
  319. console.log(previousPage, selectProductCarData, 'selectProductCarData');
  320. console.log(currentPage);
  321. if (!selectProductCarData) {
  322. console.log(11);
  323. let toSelectProduct = wx.getStorageSync('toSelectProduct')
  324. if (!toSelectProduct) {
  325. toSelectProduct = []
  326. } else {
  327. toSelectProduct = JSON.parse(toSelectProduct)
  328. }
  329. toSelectProduct.push(this.data.toSelectProduct)
  330. wx.setStorageSync('toSelectProduct', JSON.stringify(toSelectProduct))
  331. wx.showToast({
  332. title: '成功加入购物车!',
  333. icon: 'none'
  334. })
  335. setTimeout(() => {
  336. wx.switchTab({
  337. url: '/pages/orderBy/orderBy?cat_type=3',
  338. })
  339. }, 1000)
  340. return
  341. }
  342. for (let i = 0; i < selectProductCarData.length; i++) {
  343. if (selectProductCarData[i].id == this.data.toSelectProduct.id) {
  344. wx.showToast({
  345. title: '单项目只能选一次!',
  346. icon: "none"
  347. })
  348. return
  349. }
  350. }
  351. selectProductCarData.push(this.data.toSelectProduct)
  352. previousPage.setData({ //对上一个页面data中的参数进行赋值(上一个页面的数据是在当前页面进行赋值的),就变相实现了向上一个页面传值
  353. selectProductCar: selectProductCarData
  354. })
  355. previousPage.showCarMoney()
  356. wx.showToast({
  357. title: '成功加入购物车!',
  358. icon: 'none'
  359. })
  360. setTimeout(() => {
  361. wx.navigateBack()
  362. }, 1000)
  363. // wx.navigateBack({})
  364. },
  365. /**
  366. * 提交订单
  367. */
  368. addOrder() {
  369. if (this.data.source == 'goods' && this.data.projectData.num == 0) {
  370. wx.showToast({
  371. title: '库存不足',
  372. icon: 'none'
  373. })
  374. return
  375. }
  376. if (this.data.source == 'goods') {
  377. wx.navigateTo({
  378. url: `/pages/goodsOrderConfirm/goodsOrderConfirm?storeId=${this.data.storeId}&productId=${this.data.projectData.id}&isShow=false&source=${this.data.source}&isDirectOrder=true&goods_ids=${this.data.projectData.id}&type=2`,
  379. })
  380. return
  381. }
  382. if (this.data.source == 'project') {
  383. this.showProjectSelect()
  384. return
  385. }
  386. // if (!wx.getStorageSync('token')) {
  387. // wx.reLaunch({
  388. // url: '/pages/login/login'
  389. // })
  390. // return
  391. // }
  392. let {
  393. currentChangeDevice,
  394. currentChangeTime,
  395. projectData,
  396. source
  397. } =
  398. this.data
  399. let _projectData = Object.assign({}, projectData)
  400. delete _projectData.content
  401. let url = `/pages/reserveProject/reserveProject?source=${
  402. this.data.source
  403. }&projectData=${encodeURIComponent(JSON.stringify(_projectData))}`
  404. if (source == 'product') {
  405. url = `/pages/reserveProduct/reserveProduct?source=${
  406. this.data.source
  407. }&productData=${encodeURIComponent(JSON.stringify(_projectData))}`
  408. } else if (['goods', 'cardgoods', 'secondaryCard'].indexOf(source) > -1) {
  409. // 美妆产品、项目卡券产品 直接跳转到确认订单
  410. url = `/pages/orderConfirm/orderConfirm?source=${source}&data=${JSON.stringify(
  411. _projectData
  412. )}&deviceData={}&timeData={}&storeId=${this.data.storeId}`
  413. }
  414. wx.navigateTo({
  415. url: url
  416. })
  417. },
  418. // 美妆产品加入购物车
  419. addGoods(type) {
  420. if(type=='shopping'){
  421. wx.navigateTo({
  422. url: `/pages/goodsOrderConfirm/goodsOrderConfirm?storeId=${this.data.storeId}&productId=${this.data.projectData.id}&isShow=true&source=${this.data.source}`,
  423. })
  424. }else{
  425. let params = {
  426. goods_id: this.data.sourceId,
  427. store_id: this.data.storeId
  428. }
  429. post('v2/api/car/add', params, (res)=>{
  430. if(res.code == 200 ){
  431. wx.showToast({
  432. title: '成功加入购物车!',
  433. icon: 'none'
  434. })
  435. setTimeout(() => {
  436. wx.switchTab({
  437. url: '/pages/shoppingMall/shoppingMall?cat_type=3',
  438. })
  439. }, 1000);
  440. }
  441. })
  442. }
  443. // let params = {
  444. // goods_id: this.data.sourceId,
  445. // store_id: this.data.storeId
  446. // }
  447. // post('v2/api/car/add', params, (res) => {
  448. // if (res.code == 200) {
  449. // if (type == 'shopping') {
  450. // wx.navigateTo({
  451. // url: `/pages/goodsOrderConfirm/goodsOrderConfirm?storeId=${this.data.storeId}&productId=${this.data.projectData.id}&isShow=true&source=${this.data.source}`,
  452. // })
  453. // } else {
  454. // wx.showToast({
  455. // title: '成功加入购物车!',
  456. // icon: 'none'
  457. // })
  458. // setTimeout(() => {
  459. // wx.switchTab({
  460. // url: '/pages/orderBy/orderBy?cat_type=3',
  461. // })
  462. // }, 1000)
  463. // }
  464. // }
  465. // })
  466. }
  467. })