index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. const startTime2 = dateServ.changeDateTime(startTime, '14:00:00')
  147. const endTime2 = dateServ.changeDateTime(endTime, '12: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', [400, 601, 700,800,103])
  153. Order.lessThan("startTime", endTime2);
  154. Order.greaterThan("endTime", startTime2);
  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. const startTime2 = dateServ.changeDateTime(startTime, '14:00:00')
  178. const endTime2 = dateServ.changeDateTime(endTime, '12:00:00')
  179. let currentUser = Parse.User.current()
  180. let Order = Parse.Object.extend("RoomOrder");
  181. let order = new Order()
  182. try {
  183. // order.set("details", this.data.room.details);
  184. order.set("user", {
  185. __type: 'Pointer',
  186. className: '_User',
  187. objectId: currentUser.id
  188. });
  189. order.set("orderNum", tradeNo);
  190. order.set("room", {
  191. __type: 'Pointer',
  192. className: 'ShopRoom',
  193. objectId: this.data.roomId
  194. });
  195. console.log(this.data.startTime, endTime);
  196. order.set("startTime", startTime2);
  197. order.set("endTime", endTime2);
  198. order.set("merber", 1);
  199. order.set("price", this.data.count * this.data.room.price);
  200. order.set("shopStore", {
  201. __type: 'Pointer',
  202. className: 'ShopStore',
  203. objectId: this.data.room.shop.objectId
  204. });
  205. order.set("company", {
  206. __type: 'Pointer',
  207. className: 'Company',
  208. objectId: company
  209. })
  210. order.set("name", this.data.name);
  211. order.set("mobile", this.data.mobile);
  212. // order.set("merber", this.data.peopleNum);
  213. order = await order.save()
  214. } catch (error) {
  215. console.log(error);
  216. wx.showToast({
  217. title: error.errMsg,
  218. icon: 'none'
  219. })
  220. }
  221. return order
  222. },
  223. //获取住客姓名
  224. getname(e) {
  225. this.setData({
  226. name: e.detail.value
  227. })
  228. console.log(this.data.name);
  229. },
  230. //获取住客电话
  231. getmobile(e) {
  232. const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的
  233. console.log(mobileNumber);
  234. // 正则表达式:匹配中国大陆手机号
  235. const mobilePattern = /^1[3-9]\d{9}$/;
  236. // 验证手机号格式
  237. if (mobilePattern.test(mobileNumber)) {
  238. this.setData({
  239. mobile: mobileNumber
  240. })
  241. console.log("手机号格式正确");
  242. // 这里可以执行其他操作,比如保存手机号或进行下一步操作
  243. } else {
  244. wx.showToast({
  245. title: '手机号格式不正确',
  246. icon: 'none',
  247. })
  248. console.log("手机号格式不正确");
  249. // 这里可以提示用户手机号格式不正确
  250. }
  251. },
  252. //点击支付
  253. async submit() {
  254. // if (!this.data.startTime || !this.data.endTime) {
  255. // wx.showToast({
  256. // title: '请选择入住时间',
  257. // icon: 'none',
  258. // })
  259. // return
  260. // }
  261. if (this.data.name.length == 0 || this.data.mobile.length == 0) {
  262. wx.showToast({
  263. title: '请输入住人信息',
  264. icon: 'none',
  265. })
  266. return
  267. }
  268. let count = await this.checkOrderCount()
  269. if (count >= this.data.room.total) {
  270. console.log(count);
  271. wx.showToast({
  272. title: '当前日期,已无剩余房间,请重新选择',
  273. icon: 'none',
  274. })
  275. return
  276. }
  277. let tradeNo = this.getTradeNo() //生成订单编号
  278. let order = await this.setOrder(tradeNo) //生成订单
  279. if (order) {
  280. this.setData({
  281. activeOrder: order,
  282. tradeNo,
  283. showPayment: true, //展示支付组件
  284. })
  285. }
  286. },
  287. // 返回首页
  288. getback() {
  289. wx.navigateTo({
  290. url: '../../../pages/index/index',
  291. });
  292. },
  293. //支付反馈
  294. async acceptResult(e) {
  295. let {
  296. activeOrder
  297. } = this.data
  298. let that = this
  299. let {
  300. params,
  301. no
  302. } = e.detail;
  303. that.setData({
  304. showPayment: false
  305. })
  306. try {
  307. if (params == "ok") {
  308. activeOrder.set("status", 100)
  309. activeOrder.set("isPay", true)
  310. await activeOrder.save()
  311. wx.showToast({
  312. title: '支付成功',
  313. icon: 'none',
  314. duration: 1500,
  315. mask: false,
  316. });
  317. wx.navigateBack({
  318. delta: 1,
  319. })
  320. // this.formSubmit()
  321. } else {
  322. wx.showToast({
  323. title: '支付失败,取消订单',
  324. icon: 'none',
  325. duration: 1500,
  326. mask: false,
  327. });
  328. }
  329. } catch (error) {
  330. console.log(error)
  331. wx.showToast({
  332. title: "支付失败",
  333. icon: "error",
  334. duration: 1500,
  335. });
  336. wx.hideLoading()
  337. }
  338. },
  339. //发送短信
  340. formSubmit() {
  341. wx.request({
  342. url: 'https://server.fmode.cn/api/apig/message',
  343. method: "post", //请求方式
  344. data: { mobile: this.data.room.shop.mobile, company: company, }, //templateId:id,
  345. success(res) {
  346. console.log(res);
  347. }
  348. })
  349. },
  350. /**
  351. * 生命周期函数--监听页面初次渲染完成
  352. */
  353. onReady: function () {
  354. },
  355. /**
  356. * 生命周期函数--监听页面显示
  357. */
  358. onShow: function () {
  359. },
  360. /**
  361. * 生命周期函数--监听页面隐藏
  362. */
  363. onHide: function () {
  364. },
  365. /**
  366. * 生命周期函数--监听页面卸载
  367. */
  368. onUnload: function () {
  369. },
  370. /**
  371. * 页面相关事件处理函数--监听用户下拉动作
  372. */
  373. onPullDownRefresh: function () {
  374. },
  375. /**
  376. * 页面上拉触底事件的处理函数
  377. */
  378. onReachBottom: function () {
  379. },
  380. /**
  381. * 用户点击右上角分享
  382. */
  383. onShareAppMessage: function () {
  384. },
  385. //开日历
  386. onDisplay() {
  387. this.setData({
  388. show: true
  389. });
  390. },
  391. //关日历
  392. onClose() {
  393. this.setData({
  394. show: false
  395. });
  396. },
  397. //选好日期点击完成后
  398. onConfirm(event) {
  399. const [start, end] = event.detail;
  400. this.setData({
  401. show: false,
  402. startTime:start,
  403. endTime:end,
  404. });
  405. this.getdate()
  406. },
  407. })