123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- // pages/reserveProduct/reserveProduct.js
- let date = new Date()
- let times = []
- var weekday = new Array(7)
- weekday[0] = '周日'
- weekday[1] = '周一'
- weekday[2] = '周二'
- weekday[3] = '周三'
- weekday[4] = '周四'
- weekday[5] = '周五'
- weekday[6] = '周六'
- for (let i = 0; i < 15; i++) {
- let stamp = date.setDate(date.getDate() + (i == 0 ? 0 : 1))
- let year = new Date(stamp).getFullYear()
- let month =
- new Date(stamp).getMonth() + 1 < 10
- ? '0' + (new Date(stamp).getMonth() + 1)
- : new Date(stamp).getMonth() + 1
- let day =
- new Date(stamp).getDate() < 10
- ? '0' + new Date(stamp).getDate()
- : new Date(stamp).getDate()
- let week = weekday[new Date(stamp).getDay()]
- times.push({
- date: `${year + '-' + month + '-' + day}`,
- day: `${month + '-' + day}`,
- week: week
- })
- }
- // let _hour = ( 10 > date.getHours() ) ? '0' + date.getHours() : date.getHours();
- // let _minute = ( 10 > date.getMinutes() ) ? '0' + date.getMinutes() : date.getMinutes();
- const app = getApp()
- import { get, post } from '../../utils/http'
- import { timestampToDate } from '../../utils/time'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- times,
- time: '',
- currentDateIndex: 0,
- currentSwiperIndex: 0,
- currentTimeIndex: -2,
- currentDeviceIndex: -1,
- projectTime: [],
- productData: {}, // 项目耗材内容
- projectData: {}, // 项目内容
- deviceList: [],
- currentChangeTime: null,
- currentChangeDevice: null,
- source: 'product', // 来源
- projectOrderList: [], // 项目耗材-可用订单列表
- productProjectList: [], // 项目耗材-项目列表
- currentProjectOrderIndex: -1, // 项目耗材-选中的订单下标
- currentProductProjectIndex: -1, // 项目耗材-选中的项目下标
- projectOrderData: {}, // 项目耗材-可用订单内容
- showDateControl: 0, // 展示预约日期控件
- showDeviceControl: 0, // 展示预约设备控件
- allowAddOrder: 0 // 是否允许下单
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- let date = new Date()
- let currentTimestamp = date.getTime() / 1000 + 60 * 2
- let _time = timestampToDate(currentTimestamp, 'H:i')
- let source = options.source || 'product'
- if (options.productData) {
- let productData = decodeURIComponent(options.productData)
- productData = JSON.parse(productData)
- productData.store_address = app.globalData.storeData.belong_region
- this.setData({
- productData: productData,
- time: _time,
- source: source
- })
- if (source == 'product') {
- // 项目耗材预约 - 获取可绑定订单
- this.getProjectOrder()
- }
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {},
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {},
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {},
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {},
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {},
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {},
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {},
- /**
- * 项目耗材预约
- * 获取可以绑定的预约订单
- */
- getProjectOrder() {
- let { productData } = this.data
- get(
- 'api/product/get_project_order',
- {
- product_id: productData.id,
- store_id: productData.store_id
- },
- (res) => {
- // res.data.project_list.map((item) => {
- // // TODO 处理会员价和原价
- // })
- this.setData({
- projectOrderList: res.data.order_list,
- productProjectList: res.data.project_list
- })
- }
- )
- },
- /**
- * 获取可预约时间
- */
- getTime() {
- let {
- productData,
- currentDateIndex,
- projectTime,
- currentSwiperIndex,
- projectData,
- time
- } = this.data
- get(
- 'api/project/time',
- {
- store_id: productData.store_id,
- project_id: projectData.project_id,
- order_day: times[currentDateIndex].date,
- use_time: projectData.use_time
- },
- (res) => {
- projectTime[0] = res.data.day
- projectTime[1] = res.data.night
- let sum = 0
- let defaultTimeIndex = -1
- projectTime.forEach((item, index) => {
- if (item.active == 1) {
- currentSwiperIndex = index
- }
- item.list.forEach((item, index) => {
- item.index = sum
- sum++
- // 默认选中第一个可预约时间(用于后面的默认绑定设备)
- if (item.status == 1 && defaultTimeIndex == -1) {
- defaultTimeIndex = item.index
- }
- })
- })
- this.setData(
- { projectTime, currentSwiperIndex, showDateControl: 1 },
- () => {
- // 默认选中第一个可预约时间
- // if (defaultTimeIndex != -1) {
- // this.onChangeTime({
- // currentTarget: { dataset: { index: defaultTimeIndex } }
- // })
- // }
- this.bindTimeChange({
- detail: { value: time }
- })
- }
- )
- }
- )
- },
- /**
- * 获取项目可预约的设备
- * /api/project/device
- */
- getProjectDevice() {
- let {
- currentTimeIndex,
- time,
- projectTime,
- currentSwiperIndex,
- productData,
- projectData,
- currentDateIndex
- } = this.data
- let order_time_id
- if (currentTimeIndex != -1) {
- projectTime[currentSwiperIndex].list.forEach((item, index) => {
- if (item.index == currentTimeIndex) {
- order_time_id = item.time_id
- }
- })
- } else {
- order_time_id = time
- }
- console.log(productData)
- console.log(order_time_id)
- return new Promise((resolve, reject) => {
- get(
- 'api/project/device',
- {
- store_id: productData.store_id,
- project_id: projectData.project_id,
- order_day: times[currentDateIndex].date,
- order_time_id: order_time_id,
- use_time: projectData.use_time
- },
- (res) => {
- this.setData(
- {
- deviceList: res.data
- },
- () => {
- this.onChangeDevice({ currentTarget: { dataset: { index: 0 } } })
- }
- )
- resolve()
- console.log(res)
- },
- (rej) => {
- this.setData({
- currentTimeIndex: -2,
- currentChangeTime: null,
- currentDeviceIndex: -1,
- currentChangeDevice: null,
- allowAddOrder: 0
- })
- console.log(rej)
- }
- )
- })
- },
- /**
- * 日期选择
- */
- onChangeDate(e) {
- let { currentDateIndex } = this.data
- if (currentDateIndex != e.currentTarget.dataset.index) {
- this.setData(
- {
- currentDateIndex: e.currentTarget.dataset.index,
- currentChangeDevice: null,
- currentChangeTime: null,
- currentTimeIndex: -2,
- currentSwiperIndex: 0,
- currentDeviceIndex: -1,
- deviceList: []
- },
- () => {
- this.getTime()
- }
- )
- }
- },
- /**
- * 时间段切换
- */
- onChangeSwiper(e) {
- this.setData({
- currentSwiperIndex: e.currentTarget.dataset.index
- })
- },
- /**
- * 时间段切换
- */
- changeSwiper(e) {
- this.setData({
- currentSwiperIndex: e.detail.current
- })
- },
- /**
- * 时间选择
- */
- onChangeTime(e) {
- let {
- projectTime,
- currentSwiperIndex,
- currentChangeTime,
- currentTimeIndex
- } = this.data
- projectTime[currentSwiperIndex].list.forEach((item, index) => {
- if (item.index == e.currentTarget.dataset.index) {
- currentChangeTime = item
- }
- })
- if (currentTimeIndex != e.currentTarget.dataset.index) {
- this.setData({
- currentTimeIndex: e.currentTarget.dataset.index,
- currentChangeTime,
- currentChangeDevice: null,
- currentDeviceIndex: -1
- })
- this.getProjectDevice()
- }
- },
- /**
- * 点击自定义时间
- */
- bindTimeChange(e) {
- let { currentDateIndex, currentChangeTime } = this.data
- currentChangeTime = {
- order_day: times[currentDateIndex].date,
- time_id: e.detail.value
- }
- this.setData({
- time: e.detail.value,
- currentTimeIndex: -1,
- currentChangeTime,
- currentChangeDevice: null,
- currentDeviceIndex: -1
- })
- this.getProjectDevice()
- },
- /**
- * 选择设备
- */
- onChangeDevice(e) {
- let { deviceList } = this.data
- this.setData(
- {
- currentDeviceIndex: e.currentTarget.dataset.index,
- currentChangeDevice: deviceList[e.currentTarget.dataset.index]
- },
- () => {
- // 控制确定按钮可点击
- this.setData({ allowAddOrder: 1 })
- }
- )
- },
- /**
- * 项目耗材预约
- * 选择推荐去支付的项目
- */
- selectProductProject(e) {
- let { productData } = this.data
- let item = e.currentTarget.dataset.item
- let currentProductProjectIndex = e.currentTarget.dataset.index
- if (currentProductProjectIndex == this.data.currentProductProjectIndex) {
- currentProductProjectIndex = -1
- this.setData({
- currentProductProjectIndex,
- currentDateIndex: -1,
- currentTimeIndex: -2,
- currentDeviceIndex: -1,
- ['productData.project_id']: -1,
- showDateControl: 0,
- showDeviceControl: 0,
- allowAddOrder: 0
- })
- return
- }
- // 参数字段不一致,对齐其他场景所需参数
- item = Object.assign(
- {
- id: item.project_id,
- name: item.project_name,
- banner_urls: [item.cover_url],
- price: item.original_price,
- store_id: productData.store_id
- },
- item
- )
- // 有效选项
- this.setData(
- {
- currentProductProjectIndex,
- currentDateIndex: 0,
- ['productData.project_id']: item.project_id,
- projectData: item
- },
- () => {
- this.getTime()
- }
- )
- },
- /**
- * 项目耗材预约
- * 选择已支付的项目订单
- */
- selectProjectOrder(e) {
- let { productData } = this.data
- let item = e.currentTarget.dataset.item
- let currentProjectOrderIndex = e.currentTarget.dataset.index
- if (currentProjectOrderIndex == this.data.currentProjectOrderIndex) {
- currentProjectOrderIndex = -1
- this.setData({
- currentProjectOrderIndex,
- allowAddOrder: 0
- })
- return
- }
- // 有效选项
- this.setData({
- currentProjectOrderIndex,
- projectOrderData: item,
- allowAddOrder: 1
- })
- },
- /**
- * 提交订单
- */
- addOrder() {
- let {
- productData,
- projectData,
- projectOrderData,
- currentTimeIndex,
- time,
- currentDateIndex,
- currentChangeTime,
- currentChangeDevice,
- allowAddOrder,
- deviceList
- } = this.data
- if (currentTimeIndex == -1) {
- currentChangeTime = {
- order_day: times[currentDateIndex].date,
- time_id: time,
- order_time: time
- }
- }
- console.log(currentChangeTime)
- if (!allowAddOrder) {
- wx.showToast({
- title: '请先选择使用项目',
- icon: 'none'
- })
- return
- }
- // 选择将升级的产品绑定至项目订单
- if (projectOrderData.order_id) {
- wx.navigateTo({
- url: `/pages/orderConfirm/orderConfirm?source=product_with_project_order&data=${JSON.stringify(
- projectOrderData
- )}&deviceData={}&timeData={}&productData=${JSON.stringify(
- productData
- )}&projectOrderData=${JSON.stringify(projectOrderData)}`
- })
- return
- }
- if (currentChangeTime == null) {
- wx.showToast({
- title: '请选择预约时间',
- icon: 'none'
- })
- } else if (
- new Date(
- `${currentChangeTime.order_day} ${currentChangeTime.order_time}`
- ) <= new Date()
- ) {
- wx.showToast({
- title: '所选时间已超时,请重新选择',
- icon: 'none'
- })
- } else if (currentChangeDevice == null) {
- let _title = '请选择设备'
- if (deviceList.length == 0) {
- _title = '当前所选时间无可用设备'
- }
- wx.showToast({
- title: _title,
- icon: 'none'
- })
- } else {
- wx.navigateTo({
- url: `/pages/orderConfirm/orderConfirm?source=${
- this.data.source
- }&data=${JSON.stringify(projectData)}&deviceData=${JSON.stringify(
- currentChangeDevice
- )}&timeData=${JSON.stringify(
- currentChangeTime
- )}&productData=${JSON.stringify(productData)}`
- })
- }
- }
- })
|