projectDetail.js 16 KB

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