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