index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // nova-tourism/pages/homestay/homestay-order2/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. import dateServ from '../../../service/date';
  5. const dateF = require("../../../../utils/date")
  6. const real = require("../../../../utils/real")
  7. const uid = Parse.User.current()?.id
  8. const sev = require('../../../service/request')
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. room: {},
  15. startTime: null, // 入住开始时间
  16. endTime: null, // 入住结束时间
  17. roomId: null,
  18. count: 1, // 入住几晚
  19. date: '', //入住区间
  20. date02: '', //入住区间-页面显示
  21. name: '', //住客姓名
  22. mobile: '', //联系电话
  23. activeOrder: null, //创建的订单
  24. tradeNo: null, //订单号
  25. showPayment: false, //显示支付组件
  26. //显示日历选择
  27. show: false,
  28. order: null, //创建的订单-parse类型
  29. formatter: null, //标记被占用日期
  30. scope: null, //日历范围-今日至180天之后
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad: function (options) {
  36. const {
  37. objectId,
  38. date_start,
  39. date_end,
  40. } = options;
  41. console.log(date_start,
  42. date_end, )
  43. let minDate = new Date()
  44. let maxDate = new Date()
  45. maxDate.setDate(maxDate.getDate() + 180)
  46. let scope = {
  47. minDate: minDate.getTime(),
  48. maxDate: maxDate.getTime()
  49. }
  50. this.setData({
  51. startTime: date_start,
  52. endTime: date_end,
  53. roomId: objectId,
  54. scope
  55. })
  56. this.refersh()
  57. },
  58. async refersh() {
  59. await this.getroom()
  60. this.getdate()
  61. },
  62. /** 获取房间信息*/
  63. async getroom() {
  64. let {
  65. startTime,
  66. endTime
  67. } = this.data
  68. let room = new Parse.Query('ShopRoom');
  69. room.include('shop', 'shop.user');
  70. let d = await room.get(this.data.roomId)
  71. let timeList = ['12:00', '14:00']
  72. if (d?.get('open_time')) {
  73. timeList = d?.get('open_time').split("-");
  74. }
  75. console.log(d.toJSON())
  76. startTime = new Date(startTime + ' ' + timeList[0])
  77. endTime = new Date(endTime + ' ' + timeList[1])
  78. await this.setData({
  79. room: d.toJSON(),
  80. startTime,
  81. endTime
  82. })
  83. },
  84. /** 用户入住离店日期,并生成日期范围和价格信息。*/
  85. async getdate() {
  86. let {
  87. startTime,
  88. endTime
  89. } = this.data
  90. var dataAll = dateServ.getDayAll(startTime, endTime);
  91. dataAll.pop()
  92. await this.setData({
  93. date02: `${dateF.formatTime('mm月dd日 入住',startTime)} - ${dateF.formatTime('mm月dd日 离店',endTime)}`,
  94. date: `${this.formatDate(startTime)} - ${this.formatDate(endTime)}`,
  95. count: dataAll.length
  96. })
  97. this.getTotalPrice()
  98. },
  99. /** 开日历*/
  100. async onDisplay() {
  101. let {
  102. roomId
  103. } = this.data
  104. let list = await sev.getRoomOccupiaDate(roomId)
  105. // console.log(list)
  106. let formatter = (day) => {
  107. let year = day.date.getFullYear()
  108. let month = day.date.getMonth()
  109. let date = day.date.getDate()
  110. let isSame = list.findIndex(item => item.year == year && item.month == month && item.date == date)
  111. if (isSame != -1) {
  112. day.topInfo = '被预定'
  113. }
  114. return day
  115. }
  116. this.setData({
  117. formatter,
  118. show: true
  119. });
  120. },
  121. /** 关日历*/
  122. onClose() {
  123. this.setData({
  124. show: false
  125. });
  126. },
  127. /** 选好日期点击完成后*/
  128. async onConfirm(event) {
  129. const [start, end] = event.detail;
  130. start.setHours(12, 0, 0);
  131. end.setHours(14, 0, 0);
  132. let {
  133. roomId
  134. } = this.data
  135. let isFree = await sev.isFree({
  136. from: start,
  137. to: end
  138. }, roomId)
  139. console.log(isFree)
  140. if (!isFree) {
  141. await this.onDisplay()
  142. wx.showToast({
  143. title: '时间段内存在被预定日期,请重新选择',
  144. icon: 'none'
  145. })
  146. } else {
  147. await this.setData({
  148. show: false,
  149. startTime: start,
  150. endTime: end,
  151. });
  152. this.getdate()
  153. }
  154. },
  155. /** 判断是否可创建订单*/
  156. judgeOrder() {
  157. let {
  158. room,
  159. name,
  160. mobile
  161. } = this.data
  162. console.log(name, mobile)
  163. if (!name || !mobile) {
  164. wx.showToast({
  165. title: '住客姓名或联系电话为空',
  166. icon: 'none',
  167. });
  168. return false
  169. }
  170. if (!real.isPoneAvailable(mobile)) {
  171. wx.showToast({
  172. title: '手机号有误',
  173. icon: 'none',
  174. });
  175. return false
  176. }
  177. return true
  178. },
  179. /**创建订单 */
  180. async submitOrder() {
  181. let now = new Date()
  182. tradeNo = "C" +
  183. String(now.getFullYear()) +
  184. (now.getMonth() + 1) +
  185. now.getDate() +
  186. now.getHours() +
  187. now.getMinutes() +
  188. now.getSeconds() +
  189. Math.random().toString().slice(-6);
  190. await this.setData({
  191. tradeNo
  192. })
  193. let {
  194. room,
  195. name,
  196. mobile,
  197. startTime,
  198. endTime,
  199. totalPrice,
  200. order,
  201. tradeNo
  202. } = this.data
  203. console.log(startTime,
  204. endTime, )
  205. let isPass = this.judgeOrder()
  206. if (!isPass) return
  207. this.getTotalPrice()
  208. let o = order
  209. if (!order?.id) {
  210. let now = new Date()
  211. tradeNo = "C" +
  212. String(now.getFullYear()) +
  213. (now.getMonth() + 1) +
  214. now.getDate() +
  215. now.getHours() +
  216. now.getMinutes() +
  217. now.getSeconds() +
  218. Math.random().toString().slice(-6);
  219. let Order = Parse.Object.extend("RoomOrder");
  220. o = new Order()
  221. o.set("company", {
  222. __type: 'Pointer',
  223. className: 'Company',
  224. objectId: company
  225. })
  226. o.set("user", {
  227. __type: 'Pointer',
  228. className: '_User',
  229. objectId: uid
  230. })
  231. o.set("room", {
  232. __type: 'Pointer',
  233. className: 'ShopRoom',
  234. objectId: room?.objectId
  235. })
  236. o.set("shopStore", {
  237. __type: 'Pointer',
  238. className: 'ShopStore',
  239. objectId: room?.shop?.objectId
  240. })
  241. o.set("orderNum", tradeNo);
  242. o.set("merber", room?.merber || 0);
  243. o.set('status', 105) //未支付
  244. }
  245. o.set("name", name);
  246. o.set("mobile", mobile);
  247. o.set("startTime", startTime);
  248. o.set("endTime", endTime);
  249. o.set("price", totalPrice || 0);
  250. try {
  251. order = await o.save()
  252. this.setData({
  253. tradeNo,
  254. order: o,
  255. showPayment: true,
  256. })
  257. wx.showToast({
  258. title: '已创建订单',
  259. icon: 'none'
  260. })
  261. } catch (error) {
  262. wx.showToast({
  263. title: '出错了请检查后重试',
  264. icon: 'error'
  265. })
  266. }
  267. },
  268. /**支付回调 */
  269. async acceptResult(e) {
  270. let {
  271. order,
  272. room
  273. } = this.data
  274. let {
  275. params
  276. } = e.detail
  277. if (params == 'ok') {
  278. wx.showLoading({
  279. title: '加载中',
  280. })
  281. order.set('status', 100)
  282. order.set('isPay', true)
  283. try {
  284. await order.save()
  285. // let userParse = Parse.User.current()
  286. // console.log(userParse?.get('invite'))
  287. // if (!userParse?.get('invite')) {
  288. // let uid = Parse.User.current()?.id
  289. // let a = await sev.bindShop(uid, room.shop?.objectId)
  290. // console.log(a)
  291. // }
  292. setTimeout(() => {
  293. wx.hideLoading()
  294. wx.redirectTo({
  295. url: '/nova-tourism/pages/my/my-order/index?active=0',
  296. })
  297. }, 1000);
  298. } catch (error) {
  299. wx.showToast({
  300. title: '保存订单出错,请联系客服核对订单',
  301. icon: 'none'
  302. })
  303. }
  304. } else {
  305. wx.showToast({
  306. title: '支付失败',
  307. icon: 'none'
  308. })
  309. }
  310. },
  311. /**计算总价 */
  312. getTotalPrice() {
  313. let {
  314. room,
  315. count
  316. } = this.data
  317. let totalPrice = parseFloat(room?.price) * parseFloat(count)
  318. this.setData({
  319. totalPrice: parseFloat(totalPrice.toFixed(2) || 0) || 0
  320. })
  321. },
  322. /**获取日期格式 */
  323. formatDate(date) {
  324. date = new Date(date);
  325. const today = new Date();
  326. const month = date.getMonth() + 1;
  327. const day = date.getDate();
  328. const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  329. const weekDay = weekDays[date.getDay()];
  330. let dateDescription = '';
  331. if (date.toDateString() === today.toDateString()) {
  332. dateDescription = '今天';
  333. } else if (date.toDateString() === new Date(today.getTime() + 86400000).toDateString()) {
  334. dateDescription = '明天';
  335. } else {
  336. dateDescription = weekDay;
  337. }
  338. return `${month}月${day}日 ${dateDescription}`;
  339. },
  340. /** 发送短信*/
  341. formSubmit() {
  342. wx.request({
  343. url: 'https://server.fmode.cn/api/apig/message',
  344. method: "post", //请求方式
  345. data: {
  346. company: 'EbxZUK5lBI',
  347. mobile: '15279240198',
  348. templateId: '28754',
  349. apiName: 'message',
  350. },
  351. success(res) {
  352. console.log(res);
  353. }
  354. })
  355. },
  356. /**
  357. * 生命周期函数--监听页面初次渲染完成
  358. */
  359. onReady: function () {
  360. },
  361. /**
  362. * 生命周期函数--监听页面显示
  363. */
  364. onShow: function () {
  365. },
  366. /**
  367. * 生命周期函数--监听页面隐藏
  368. */
  369. onHide: function () {
  370. },
  371. /**
  372. * 生命周期函数--监听页面卸载
  373. */
  374. onUnload: function () {
  375. },
  376. /**
  377. * 页面相关事件处理函数--监听用户下拉动作
  378. */
  379. onPullDownRefresh: function () {
  380. },
  381. /**
  382. * 页面上拉触底事件的处理函数
  383. */
  384. onReachBottom: function () {
  385. },
  386. /**
  387. * 用户点击右上角分享
  388. */
  389. onShareAppMessage: function () {
  390. },
  391. })