index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // nova-tourism/pages/homestay/homestay-order2/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. import dataSource from '../../../service/data';
  5. import dateServ from '../../../service/date';
  6. const rechText = require('../../../../utils/rech-text')
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. //屏幕高度
  13. statusBarHeight: 0, // 状态栏高度
  14. screenHeight: 0, // 屏幕高度
  15. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  16. bottomNavHeight: 0, // 底部导航栏高度
  17. contentHeight: 0, // 可用内容高度
  18. room: {},
  19. startTime: null, // 入住开始时间
  20. endTime: null, // 入住结束时间
  21. roomId: null,
  22. count: 1, // 入住几晚
  23. date: '', //入住区间
  24. date2: '',
  25. name: '',
  26. mobile: '',
  27. activeOrder: null, //创建的订单
  28. tradeNo: null, //订单号
  29. showPayment: false, //显示支付组件
  30. //显示日历选择
  31. show:false,
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. const systemInfo = wx.getSystemInfoSync();
  38. const statusBarHeight = systemInfo.statusBarHeight || 0;
  39. const screenHeight = systemInfo.screenHeight || 0;
  40. const custom = wx.getMenuButtonBoundingClientRect();
  41. const customHeight = custom.height + 10 + 2 || 0;
  42. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  43. const contentHeight = (screenHeight - bottomNavHeight -5- statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  44. this.setData({
  45. contentHeight
  46. });
  47. const {
  48. objectId,
  49. date_start,
  50. date_end,
  51. } = options;
  52. const date_start1 = decodeURIComponent(date_start);
  53. const date_end1 = decodeURIComponent(date_end);
  54. this.setData({
  55. startTime: date_start1,
  56. endTime: date_end1,
  57. roomId: objectId
  58. })
  59. console.log('123', date_start1, date_end1);
  60. this.getroom()
  61. this.getdate()
  62. },
  63. //计算用户选择的入住和离店日期,并生成相应的日期范围和价格信息。
  64. getdate() {
  65. // let {
  66. // startTime,
  67. // endTime
  68. // } = this.formatTimeArea(this.data.start, this.data.end)
  69. // console.log('开始时间', this.data.start, '结束时间', this.data.end);
  70. // console.log('开始时间', startTime, '结束时间', endTime);
  71. var dataAll = dateServ.getDayAll(this.data.startTime, this.data.endTime);
  72. dataAll.pop(); // 最后一天为退房,不算在内
  73. console.log('dataAll', dataAll);
  74. // console.log(this.formatDate(startTime), this.formatDate(endTime))
  75. let dateStr = this.formatDate(this.data.startTime) + '-' + this.formatDate(this.data.endTime)
  76. console.log('dateStr', dateStr)
  77. this.setData({
  78. // showDate: false,
  79. date: `${this.formatDate(this.data.startTime)} - ${this.formatDate(this.data.endTime)}`,
  80. date2: `${this.formatDate2(this.data.startTime,this.data.endTime)}`,
  81. // priceInfoArr: dataAll,
  82. count: dataAll.length
  83. })
  84. console.log('天数', this.data.count);
  85. },
  86. //修改时间格式
  87. formatDate(date) {
  88. date = new Date(date);
  89. // 获取当前日期
  90. const today = new Date();
  91. // 格式化日期为 "月/日"
  92. const month = date.getMonth() + 1; // 月份从0开始,所以要加1
  93. const day = date.getDate();
  94. // 获取星期几
  95. const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  96. const weekDay = weekDays[date.getDay()]; // 获取星期几的中文
  97. // 判断是今天还是明天
  98. let dateDescription = '';
  99. if (date.toDateString() === today.toDateString()) {
  100. dateDescription = '今天';
  101. } else if (date.toDateString() === new Date(today.getTime() + 86400000).toDateString()) {
  102. dateDescription = '明天';
  103. } else {
  104. dateDescription = weekDay; // 其他情况返回星期几
  105. }
  106. // 返回格式化的字符串
  107. return `${month}月${day}日 ${dateDescription}`;
  108. },
  109. formatDate2(date1, date2) {
  110. date1 = new Date(date1);
  111. date2 = new Date(date2);
  112. // 获取年份、月份和日期
  113. const year1 = date1.getFullYear();
  114. const month1 = date1.getMonth() + 1; // 月份从0开始,所以要加1
  115. const day1 = date1.getDate();
  116. const year2 = date2.getFullYear();
  117. const month2 = date2.getMonth() + 1; // 月份从0开始,所以要加1
  118. const day2 = date2.getDate();
  119. // 返回格式化的字符串
  120. return `${year1}年${month1}月${day1}日入住 - ${year2}年${month2}月${day2}日离店`;
  121. },
  122. //获取房间信息
  123. async getroom() {
  124. let room = new Parse.Query('ShopRoom');
  125. room.equalTo('company', company);
  126. room.equalTo('objectId', this.data.roomId);
  127. room.equalTo('isEnabled', 'true');
  128. room.include('shop');
  129. room.notEqualTo('isDeleted', 'true');
  130. let room2 = await room.find();
  131. let roomList = room2.map(item => item.toJSON());
  132. this.setData({
  133. room: roomList[0]
  134. })
  135. console.log('房间', this.data.room);
  136. },
  137. //计算选择日期的房间数量
  138. async checkOrderCount() {
  139. // start1 end1 现在选择
  140. // start2 end2 已有订单时间
  141. // start1 < start2 end1 < end2
  142. // start1 > start2 end1 > end2
  143. // start1 = start2 end1 = end2
  144. const startTime = new Date(this.data.startTime);
  145. const endTime = new Date(this.data.endTime);
  146. let now = dateServ.changeDateTime(new Date(), '14:00:00')
  147. let Order = new Parse.Query("RoomOrder")
  148. Order.equalTo("room", this.data.roomId)
  149. Order.equalTo("company", company)
  150. Order.exists("status")
  151. Order.notContainedIn('status', [100, 400, 601, 700])
  152. Order.greaterThanOrEqualTo("startTime", endTime) //20 23 21 22
  153. Order.lessThanOrEqualTo("endTime", startTime)
  154. Order.select("startTime", "endTime")
  155. let count = await Order.count()
  156. console.log('count', count);
  157. return count
  158. },
  159. //生成订单号
  160. getTradeNo() {
  161. let now = new Date()
  162. let tradeNo = "C" +
  163. String(now.getFullYear()) +
  164. (now.getMonth() + 1) +
  165. now.getDate() +
  166. now.getHours() +
  167. now.getMinutes() +
  168. now.getSeconds() +
  169. Math.random().toString().slice(-6); //生成六位随机数
  170. return tradeNo
  171. },
  172. // 创建待支付订单
  173. async setOrder(tradeNo) {
  174. const startTime = new Date(this.data.startTime);
  175. const endTime = new Date(this.data.endTime);
  176. let currentUser = Parse.User.current()
  177. let Order = Parse.Object.extend("RoomOrder");
  178. let order = new Order()
  179. try {
  180. // order.set("details", this.data.room.details);
  181. order.set("user", {
  182. __type: 'Pointer',
  183. className: '_User',
  184. objectId: currentUser.id
  185. });
  186. order.set("orderNum", tradeNo);
  187. order.set("room", {
  188. __type: 'Pointer',
  189. className: 'ShopRoom',
  190. objectId: this.data.roomId
  191. });
  192. console.log(this.data.startTime, endTime);
  193. order.set("startTime", startTime);
  194. order.set("endTime", endTime);
  195. order.set("merber", 1);
  196. order.set("price", this.data.count * this.data.room.price);
  197. order.set("shopStore", {
  198. __type: 'Pointer',
  199. className: 'ShopStore',
  200. objectId: this.data.room.shop.objectId
  201. });
  202. order.set("company", {
  203. __type: 'Pointer',
  204. className: 'Company',
  205. objectId: company
  206. })
  207. order.set("name", this.data.name);
  208. order.set("mobile", this.data.mobile);
  209. // order.set("merber", this.data.peopleNum);
  210. order = await order.save()
  211. } catch (error) {
  212. console.log(error);
  213. wx.showToast({
  214. title: error.errMsg,
  215. icon: 'none'
  216. })
  217. }
  218. return order
  219. },
  220. //获取住客姓名
  221. getname(e) {
  222. this.setData({
  223. name: e.detail.value
  224. })
  225. console.log(this.data.name);
  226. },
  227. //获取住客电话
  228. getmobile(e) {
  229. const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的
  230. console.log(mobileNumber);
  231. // 正则表达式:匹配中国大陆手机号
  232. const mobilePattern = /^1[3-9]\d{9}$/;
  233. // 验证手机号格式
  234. if (mobilePattern.test(mobileNumber)) {
  235. this.setData({
  236. mobile: mobileNumber
  237. })
  238. console.log("手机号格式正确");
  239. // 这里可以执行其他操作,比如保存手机号或进行下一步操作
  240. } else {
  241. wx.showToast({
  242. title: '手机号格式不正确',
  243. icon: 'none',
  244. })
  245. console.log("手机号格式不正确");
  246. // 这里可以提示用户手机号格式不正确
  247. }
  248. },
  249. //点击支付
  250. async submit() {
  251. // if (!this.data.startTime || !this.data.endTime) {
  252. // wx.showToast({
  253. // title: '请选择入住时间',
  254. // icon: 'none',
  255. // })
  256. // return
  257. // }
  258. if (this.data.name.length == 0 || this.data.mobile.length == 0) {
  259. wx.showToast({
  260. title: '请输入住人信息',
  261. icon: 'none',
  262. })
  263. return
  264. }
  265. let count = await this.checkOrderCount()
  266. if (count >= this.data.room.total) {
  267. console.log(count);
  268. wx.showToast({
  269. title: '当前日期,已无剩余房间,请重新选择',
  270. icon: 'none',
  271. })
  272. return
  273. }
  274. let tradeNo = this.getTradeNo() //生成订单编号
  275. let order = await this.setOrder(tradeNo) //生成订单
  276. if (order) {
  277. this.setData({
  278. activeOrder: order,
  279. tradeNo,
  280. showPayment: true, //展示支付组件
  281. })
  282. }
  283. },
  284. // 返回首页
  285. getback() {
  286. wx.navigateTo({
  287. url: '../../../pages/index/index',
  288. });
  289. },
  290. //支付反馈
  291. async acceptResult(e) {
  292. let {
  293. activeOrder
  294. } = this.data
  295. let that = this
  296. let {
  297. params,
  298. no
  299. } = e.detail;
  300. that.setData({
  301. showPayment: false
  302. })
  303. try {
  304. if (params == "ok") {
  305. activeOrder.set("status", 100)
  306. activeOrder.set("isPay", true)
  307. await activeOrder.save()
  308. wx.showToast({
  309. title: '支付成功',
  310. icon: 'none',
  311. duration: 1500,
  312. mask: false,
  313. });
  314. wx.navigateBack({
  315. delta: 1,
  316. })
  317. } else {
  318. wx.showToast({
  319. title: '支付失败,取消订单',
  320. icon: 'none',
  321. duration: 1500,
  322. mask: false,
  323. });
  324. }
  325. } catch (error) {
  326. console.log(error)
  327. wx.showToast({
  328. title: "支付失败",
  329. icon: "error",
  330. duration: 1500,
  331. });
  332. wx.hideLoading()
  333. }
  334. },
  335. /**
  336. * 生命周期函数--监听页面初次渲染完成
  337. */
  338. onReady: function () {
  339. },
  340. /**
  341. * 生命周期函数--监听页面显示
  342. */
  343. onShow: function () {
  344. },
  345. /**
  346. * 生命周期函数--监听页面隐藏
  347. */
  348. onHide: function () {
  349. },
  350. /**
  351. * 生命周期函数--监听页面卸载
  352. */
  353. onUnload: function () {
  354. },
  355. /**
  356. * 页面相关事件处理函数--监听用户下拉动作
  357. */
  358. onPullDownRefresh: function () {
  359. },
  360. /**
  361. * 页面上拉触底事件的处理函数
  362. */
  363. onReachBottom: function () {
  364. },
  365. /**
  366. * 用户点击右上角分享
  367. */
  368. onShareAppMessage: function () {
  369. },
  370. //开日历
  371. onDisplay() {
  372. this.setData({
  373. show: true
  374. });
  375. },
  376. //关日历
  377. onClose() {
  378. this.setData({
  379. show: false
  380. });
  381. },
  382. //选好日期点击完成后
  383. onConfirm(event) {
  384. const [start, end] = event.detail;
  385. this.setData({
  386. show: false,
  387. startTime:start,
  388. endTime:end,
  389. });
  390. this.getdate()
  391. },
  392. })