index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 {roomId}=this.data
  133. let isFree = await sev.isFree({from:start,to:end},roomId)
  134. console.log(isFree)
  135. if(!isFree){
  136. await this.onDisplay()
  137. wx.showToast({
  138. title: '时间段内存在被预定日期,请重新选择',
  139. icon:'none'
  140. })
  141. }else{
  142. await this.setData({
  143. show: false,
  144. startTime: start,
  145. endTime: end,
  146. });
  147. this.getdate()
  148. }
  149. },
  150. /** 判断是否可创建订单*/
  151. judgeOrder() {
  152. let {
  153. room,
  154. name,
  155. mobile
  156. } = this.data
  157. console.log(name, mobile)
  158. if (!name || !mobile) {
  159. wx.showToast({
  160. title: '住客姓名或联系电话为空',
  161. icon: 'none',
  162. });
  163. return false
  164. }
  165. if (!real.isPoneAvailable(mobile)) {
  166. wx.showToast({
  167. title: '手机号有误',
  168. icon: 'none',
  169. });
  170. return false
  171. }
  172. return true
  173. },
  174. /**创建订单 */
  175. async submitOrder() {
  176. let {
  177. room,
  178. name,
  179. mobile,
  180. startTime,
  181. endTime,
  182. totalPrice,
  183. order,
  184. tradeNo
  185. } = this.data
  186. console.log(startTime,
  187. endTime, )
  188. let isPass = this.judgeOrder()
  189. if (!isPass) return
  190. this.getTotalPrice()
  191. let o = order
  192. if (!order?.id) {
  193. let now = new Date()
  194. tradeNo = "C" +
  195. String(now.getFullYear()) +
  196. (now.getMonth() + 1) +
  197. now.getDate() +
  198. now.getHours() +
  199. now.getMinutes() +
  200. now.getSeconds() +
  201. Math.random().toString().slice(-6);
  202. let Order = Parse.Object.extend("RoomOrder");
  203. o = new Order()
  204. o.set("company", {
  205. __type: 'Pointer',
  206. className: 'Company',
  207. objectId: company
  208. })
  209. o.set("user", {
  210. __type: 'Pointer',
  211. className: '_User',
  212. objectId: uid
  213. })
  214. o.set("room", {
  215. __type: 'Pointer',
  216. className: 'ShopRoom',
  217. objectId: room?.objectId
  218. })
  219. o.set("shopStore", {
  220. __type: 'Pointer',
  221. className: 'ShopStore',
  222. objectId: room?.shop?.objectId
  223. })
  224. o.set("orderNum", tradeNo);
  225. o.set("merber", room?.merber || 0);
  226. o.set('status', 105) //未支付
  227. }
  228. o.set("name", name);
  229. o.set("mobile", mobile);
  230. o.set("startTime", startTime);
  231. o.set("endTime", endTime);
  232. o.set("price", totalPrice || 0);
  233. try {
  234. order = await o.save()
  235. this.setData({
  236. tradeNo,
  237. order: o,
  238. showPayment: true,
  239. })
  240. wx.showToast({
  241. title: '已创建订单',
  242. icon: 'none'
  243. })
  244. } catch (error) {
  245. wx.showToast({
  246. title: '出错了请检查后重试',
  247. icon: 'error'
  248. })
  249. }
  250. },
  251. /**支付回调 */
  252. async acceptResult(e) {
  253. let {
  254. order,room
  255. } = this.data
  256. let {
  257. params
  258. } = e.detail
  259. if (params == 'ok') {
  260. wx.showLoading({
  261. title: '加载中',
  262. })
  263. order.set('status', 100)
  264. order.set('isPay', true)
  265. try {
  266. await order.save()
  267. let userParse = Parse.User.current()
  268. if(!userParse?.get('invite')){
  269. userParse.set('invite',{__type:'Pointer',className:'_User',objectId:room.shop?.user?.objectId})
  270. await userParse.save()
  271. }
  272. setTimeout(() => {
  273. wx.hideLoading()
  274. wx.redirectTo({
  275. url: '/nova-tourism/pages/my/my-order/index?active=0',
  276. })
  277. }, 1000);
  278. } catch (error) {
  279. wx.showToast({
  280. title: '保存订单出错,请联系客服核对订单',
  281. icon: 'none'
  282. })
  283. }
  284. } else {
  285. wx.showToast({
  286. title: '支付失败',
  287. icon: 'none'
  288. })
  289. }
  290. },
  291. /**计算总价 */
  292. getTotalPrice() {
  293. let {
  294. room,
  295. count
  296. } = this.data
  297. let totalPrice = parseFloat(room?.price) * parseFloat(count)
  298. this.setData({
  299. totalPrice: parseFloat(totalPrice.toFixed(2) || 0) || 0
  300. })
  301. },
  302. /**获取日期格式 */
  303. formatDate(date) {
  304. date = new Date(date);
  305. const today = new Date();
  306. const month = date.getMonth() + 1;
  307. const day = date.getDate();
  308. const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  309. const weekDay = weekDays[date.getDay()];
  310. let dateDescription = '';
  311. if (date.toDateString() === today.toDateString()) {
  312. dateDescription = '今天';
  313. } else if (date.toDateString() === new Date(today.getTime() + 86400000).toDateString()) {
  314. dateDescription = '明天';
  315. } else {
  316. dateDescription = weekDay;
  317. }
  318. return `${month}月${day}日 ${dateDescription}`;
  319. },
  320. /** 发送短信*/
  321. formSubmit() {
  322. wx.request({
  323. url: 'https://server.fmode.cn/api/apig/message',
  324. method: "post", //请求方式
  325. data: {
  326. company: 'EbxZUK5lBI',
  327. mobile: '15279240198',
  328. templateId: '28754',
  329. apiName: 'message',
  330. },
  331. success(res) {
  332. console.log(res);
  333. }
  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. })