index.js 13 KB

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