index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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("merber", 1);
  195. order.set("price", this.data.count * this.data.room.price);
  196. order.set("shopStore", {
  197. __type: 'Pointer',
  198. className: 'ShopStore',
  199. objectId: this.data.room.shop.objectId
  200. });
  201. order.set("company", {
  202. __type: 'Pointer',
  203. className: 'Company',
  204. objectId: company
  205. })
  206. order.set("name", this.data.name);
  207. order.set("mobile", this.data.mobile);
  208. // order.set("merber", this.data.peopleNum);
  209. order = await order.save()
  210. } catch (error) {
  211. console.log(error);
  212. wx.showToast({
  213. title: error.errMsg,
  214. icon: 'none'
  215. })
  216. }
  217. return order
  218. },
  219. //获取住客姓名
  220. getname(e) {
  221. this.setData({
  222. name:e.detail
  223. })
  224. console.log(e.detail);
  225. },
  226. //获取住客电话
  227. getmobile(e) {
  228. const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的
  229. console.log(mobileNumber);
  230. // 正则表达式:匹配中国大陆手机号
  231. const mobilePattern = /^1[3-9]\d{9}$/;
  232. // 验证手机号格式
  233. if (mobilePattern.test(mobileNumber)) {
  234. this.setData({
  235. error_message:'',
  236. mobile:mobileNumber
  237. })
  238. console.log("手机号格式正确");
  239. // 这里可以执行其他操作,比如保存手机号或进行下一步操作
  240. } else {
  241. this.setData({
  242. error_message:'手机号格式不正确'
  243. })
  244. console.log("手机号格式不正确");
  245. // 这里可以提示用户手机号格式不正确
  246. }
  247. },
  248. //点击支付
  249. async submit() {
  250. // if (!this.data.startTime || !this.data.endTime) {
  251. // wx.showToast({
  252. // title: '请选择入住时间',
  253. // icon: 'none',
  254. // })
  255. // return
  256. // }
  257. if (this.data.name.length==0||this.data.mobile.length==0) {
  258. wx.showToast({
  259. title: '请输入住人信息',
  260. icon: 'none',
  261. })
  262. return
  263. }
  264. let count = await this.checkOrderCount()
  265. if (count >= this.data.room.total) {
  266. console.log(count);
  267. wx.showToast({
  268. title: '当前日期,已无剩余房间,请重新选择',
  269. icon: 'none',
  270. })
  271. return
  272. }
  273. let tradeNo = this.getTradeNo() //生成订单编号
  274. let order = await this.setOrder(tradeNo)//生成订单
  275. if (order) {
  276. this.setData({
  277. activeOrder: order,
  278. tradeNo,
  279. showPayment: true,//展示支付组件
  280. })
  281. }
  282. },
  283. // 返回首页
  284. getback(){
  285. wx.navigateTo({
  286. url: '../../../pages/index/index',
  287. });
  288. },
  289. //支付反馈
  290. async acceptResult(e) {
  291. let {
  292. activeOrder
  293. } = this.data
  294. let that = this
  295. let {
  296. params,
  297. no
  298. } = e.detail;
  299. that.setData({
  300. showPayment: false
  301. })
  302. try {
  303. if (params == "ok") {
  304. activeOrder.set("status", 100)
  305. activeOrder.set("isPay", true)
  306. await activeOrder.save()
  307. wx.showToast({
  308. title: '支付成功',
  309. icon: 'none',
  310. duration: 1500,
  311. mask: false,
  312. });
  313. wx.navigateBack({
  314. delta: 1,
  315. })
  316. } else {
  317. wx.showToast({
  318. title: '支付失败,取消订单',
  319. icon: 'none',
  320. duration: 1500,
  321. mask: false,
  322. });
  323. }
  324. } catch (error) {
  325. console.log(error)
  326. wx.showToast({
  327. title: "支付失败",
  328. icon: "error",
  329. duration: 1500,
  330. });
  331. wx.hideLoading()
  332. }
  333. },
  334. /**
  335. * 生命周期函数--监听页面初次渲染完成
  336. */
  337. onReady: function () {
  338. },
  339. /**
  340. * 生命周期函数--监听页面显示
  341. */
  342. onShow: function () {
  343. },
  344. /**
  345. * 生命周期函数--监听页面隐藏
  346. */
  347. onHide: function () {
  348. },
  349. /**
  350. * 生命周期函数--监听页面卸载
  351. */
  352. onUnload: function () {
  353. },
  354. /**
  355. * 页面相关事件处理函数--监听用户下拉动作
  356. */
  357. onPullDownRefresh: function () {
  358. },
  359. /**
  360. * 页面上拉触底事件的处理函数
  361. */
  362. onReachBottom: function () {
  363. },
  364. /**
  365. * 用户点击右上角分享
  366. */
  367. onShareAppMessage: function () {
  368. }
  369. })