抖音c端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
12 KiB

  1. // c:\Users\Holy-Knight-IX\Desktop\Working Space\4.TikTok-MiniPro\ttc\package2\pages\appointment\appointment.js
  2. let config = require("../../../config/config.js");
  3. let util = require("../../../utils/util");
  4. let Http = require("../../../utils/HttpBasics");
  5. let app = getApp();
  6. const imgurl = require("../../../utils/imgurl");
  7. Page({
  8. data: {
  9. appointmentText: "确认预约",
  10. id: "",
  11. orderId: "",
  12. merOrderId: "",
  13. startTime: '',
  14. endTime: "",
  15. mallTenantId: "",
  16. pickedAddress: "请选择地址",
  17. pickedAddressDetail: "",
  18. pickedDate: "请选择日期",
  19. pickedStartTime: "请选择",
  20. pickedEndTime: "请选择",
  21. reservationMerchantName: "",
  22. describeStr: "",
  23. mallList: [],
  24. merchantId: "",
  25. currentIndex: "",
  26. status: "",
  27. isShowBtns: false,
  28. latitude: "",
  29. longitude: ""
  30. },
  31. onLoad(options) {
  32. console.log(options, 'options')
  33. const mallTenantId = tt.getStorageSync('mallTenantId');
  34. if (mallTenantId) {
  35. this.setData({
  36. mallTenantId
  37. })
  38. }
  39. const date = (new Date()).getTime()
  40. const nowDate = util.timestampToTime(date, 'YYYY-MM-DD')
  41. const startTime = util.timestampToTime(options.startTime * 1, 'YYYY-MM-DD')
  42. console.log(nowDate, startTime, 'time');
  43. const finalStartTime = options.startTime * 1 > date * 1 ? startTime : nowDate
  44. console.log(finalStartTime, 'finalStartTime');
  45. // const mallList = [{
  46. // merchantName: "1家帮汇家政保洁(河池市庆远镇)",
  47. // latitude: 50.277748,
  48. // longitude: 119.996078,
  49. // id: 1
  50. // }, {
  51. // merchantName: "2家帮汇家政保洁(河池市庆远镇)",
  52. // latitude: 35.277748,
  53. // longitude: 119.996078,
  54. // id: 2
  55. // }, {
  56. // merchantName: "3家帮汇家政保洁(河池市庆远镇)",
  57. // id: 3
  58. // }, {
  59. // merchantName: "4家帮汇家政保洁(河池市庆远镇)",
  60. // latitude: 45.277748,
  61. // longitude: 119.996078,
  62. // id: 4
  63. // }]
  64. this.setData({
  65. id: options.id || '',
  66. orderId: options.orderId || '',
  67. merOrderId: options.merOrderId || '',
  68. startTime: finalStartTime,
  69. endTime: options.endTime || '',
  70. })
  71. this.getCouponMerchant(options.orderId)
  72. if (this.data.id) {
  73. this.getDetail(this.data.id)
  74. } else {
  75. this.setData({
  76. isShowBtns: true
  77. })
  78. }
  79. },
  80. setMerchant(e) {
  81. console.log(e.detail.value, 'e');
  82. const index = e.detail.value
  83. this.setData({
  84. currentIndex: index,
  85. merchantId: this.data.mallList[index].id
  86. })
  87. console.log(this.data.currentIndex, this.data.merchantId);
  88. },
  89. /**
  90. * 获得经纬度
  91. */
  92. getLocation() {
  93. console.log('getLocation');
  94. let that = this;
  95. tt.getLocation({
  96. type: "wgs84",
  97. // isHighAccuracy: true,
  98. success: function (res) {
  99. console.log(res, 'getLocation')
  100. that.setData({
  101. latitude: res.latitude,
  102. longitude: res.longitude,
  103. })
  104. const latitudeNow = res.latitude
  105. const longitudeNow = res.longitude
  106. const mallLocationArr = []
  107. const mallList = that.data.mallList
  108. mallList.forEach((item, index) => {
  109. const obj = {
  110. latitude: item.latitude || false,
  111. longitude: item.longitude || false,
  112. id: item.id,
  113. index: index
  114. }
  115. mallLocationArr.push(obj)
  116. })
  117. console.log(mallLocationArr, 'mallLocationArr');
  118. const tempArr = mallList
  119. const bloobArr = []
  120. mallLocationArr.forEach((item, index) => {
  121. const distanceData = that.getDistances(latitudeNow, longitudeNow, item.latitude, item.longitude)
  122. tempArr[index].distance_str = distanceData.distance_str
  123. tempArr[index].distanceName = tempArr[index].merchantName + ' ' + distanceData.distance_str
  124. tempArr[index].distance = distanceData.distance
  125. bloobArr.push(tempArr[index])
  126. })
  127. const finalSortMallList = that.bloob(bloobArr)
  128. console.log(finalSortMallList, 'finalSortMallList');
  129. that.setData({
  130. mallList: finalSortMallList
  131. })
  132. },
  133. // 拒绝提供定位权限
  134. fail: error => {
  135. console.log(error, 'error');
  136. const mallList = that.data.mallList
  137. mallList.forEach(item => {
  138. item.distanceName = item.merchantName
  139. })
  140. that.setData({
  141. mallList
  142. })
  143. },
  144. complete: finish => {
  145. }
  146. })
  147. },
  148. getCouponMerchant() { //获取适用门店
  149. const that = this
  150. Http.get({
  151. url: config.api.orderDetail,
  152. data: {
  153. orderId: that.data.merOrderId,
  154. mallTenantId: that.data.mallTenantId || ''
  155. }
  156. }).then(res => {
  157. const couponChannelId = res.data.orders[0].couponChannelId
  158. Http.get({
  159. url: config.api.couponMerchant,
  160. data: {
  161. couponChannelId,
  162. mallTenantId: that.data.mallTenantId || ''
  163. }
  164. }).then(res => {
  165. const keys = Object.keys(res.data)
  166. const mallList = []
  167. let i = 0
  168. keys.forEach(item => {
  169. const arr = item.split('|')
  170. const obj = {
  171. tenantId: arr[0],
  172. mallName: arr[1],
  173. merchantVoList: res.data[item],
  174. expand: false
  175. }
  176. if (i == 0) {
  177. obj.expand = true
  178. }
  179. mallList.push(obj)
  180. i++
  181. })
  182. that.setData({
  183. mallList: mallList[0].merchantVoList
  184. })
  185. console.log(that.data.mallList, 'mallList');
  186. that.getLocation()
  187. })
  188. }).catch(err => {
  189. })
  190. },
  191. chooseAddress() {
  192. if (!this.data.isShowBtns) return
  193. let that = this;
  194. tt.chooseLocation({
  195. success: function (res) {
  196. that.setData({
  197. pickedAddress: res.address,
  198. })
  199. },
  200. fail: function (error) {
  201. console.log(error)
  202. },
  203. complete: function (data) {}
  204. })
  205. },
  206. chooseAddressDetail() {
  207. this.setData({
  208. pickedAddressDetailFlag: false
  209. })
  210. },
  211. chooseDate(e) {
  212. console.log(e.detail.value, 'e');
  213. this.setData({
  214. pickedDate: e.detail.value
  215. })
  216. },
  217. chooseStartTime(e) {
  218. console.log(e.detail.value, 'e');
  219. this.setData({
  220. pickedStartTime: e.detail.value
  221. })
  222. },
  223. chooseEndTime(e) {
  224. console.log(e.detail.value, 'e');
  225. this.setData({
  226. pickedEndTime: e.detail.value
  227. })
  228. },
  229. inputAddressDetail(e) {
  230. this.setData({
  231. pickedAddressDetail: e.detail.value
  232. })
  233. },
  234. describing(e) {
  235. this.setData({
  236. describeStr: e.detail.value
  237. })
  238. },
  239. confirm() {
  240. const tempData = this.data
  241. if (tempData.pickedAddress == '请选择地址') {
  242. tt.showToast({
  243. title: '请选择地址!',
  244. icon: 'fail'
  245. });
  246. return
  247. }
  248. if (tempData.pickedDate == '请选择日期') {
  249. tt.showToast({
  250. title: '请请选择日期!',
  251. icon: 'fail'
  252. });
  253. return
  254. }
  255. if (tempData.pickedStartTime == '请选择') {
  256. tt.showToast({
  257. title: '请起始时间!',
  258. icon: 'fail'
  259. });
  260. return
  261. }
  262. if (tempData.pickedEndTime == '请选择') {
  263. tt.showToast({
  264. title: '请结束时间!',
  265. icon: 'fail'
  266. });
  267. return
  268. }
  269. const data = {
  270. couponOrderId: tempData.orderId,
  271. userAddress: tempData.pickedAddress,
  272. detailedAddress: tempData.pickedAddressDetail,
  273. startDate: tempData.pickedDate + " " + tempData.pickedStartTime + ":00",
  274. endDate: tempData.pickedDate + " " + tempData.pickedEndTime + ":00",
  275. describeStr: tempData.describeStr,
  276. mallTenantId: tempData.mallTenantId || '',
  277. reservationMerchantId: tempData.merchantId
  278. }
  279. if (this.data.id) {
  280. data.id = this.data.id
  281. }
  282. if (this.data.status == 1) {
  283. data.status = 0
  284. }
  285. console.log(data, 'data');
  286. this.saveOrderReservation(data)
  287. },
  288. cancel() {
  289. tt.showModal({
  290. title: "取消预约",
  291. content: "确定要取消预约吗?",
  292. showCancel: true,
  293. success: (res) => {
  294. if (res.confirm) {
  295. this.cancelReservation(this.data.id)
  296. }
  297. if (res.cancel) {
  298. return
  299. }
  300. },
  301. fail: (res) => {
  302. },
  303. });
  304. },
  305. cancelReservation(id) {
  306. const that = this
  307. Http.post({
  308. url: config.api.cancelReservation,
  309. data: {
  310. id,
  311. mallTenantId: this.data.mallTenantId || ''
  312. }
  313. }).then(res => {
  314. console.log(res, 'cancelReservation');
  315. tt.showToast({
  316. title: '取消成功!',
  317. icon: 'success',
  318. duration: 2000,
  319. success: (res) => {
  320. setTimeout(() => {
  321. tt.navigateBack();
  322. }, 1500);
  323. },
  324. fail: (res) => {
  325. },
  326. });
  327. }).catch(err => {
  328. console.log(err, 'cancelReservation');
  329. tt.showToast({
  330. title: err.message,
  331. icon: 'none'
  332. });
  333. })
  334. },
  335. saveOrderReservation(data) {
  336. Http.post({
  337. url: config.api.saveOrderReservation,
  338. data
  339. }).then(res => {
  340. console.log(res, 'saveOrderReservation');
  341. tt.showToast({
  342. title: '预约成功!',
  343. icon: 'success',
  344. duration: 2000,
  345. success: (res) => {
  346. setTimeout(() => {
  347. tt.navigateBack();
  348. }, 1500);
  349. },
  350. fail: (res) => {
  351. },
  352. });
  353. }).catch(err => {
  354. console.log(err, 'saveOrderReservation');
  355. tt.showToast({
  356. title: err.message,
  357. icon: 'none'
  358. });
  359. })
  360. },
  361. getDetail(id) {
  362. const that = this
  363. Http.get({
  364. url: config.api.getOrderReservationDetail,
  365. data: {
  366. id,
  367. mallTenantId: this.data.mallTenantId || ''
  368. }
  369. }).then(res => {
  370. console.log(res, 'getOrderReservationDetail');
  371. const status = res.data.status
  372. that.setData({
  373. pickedAddress: res.data.userAddress || '请选择地址',
  374. pickedAddressDetail: res.data.detailedAddress,
  375. pickedDate: util.timestampToTime(res.data.startDate, 'YYYY-MM-DD'),
  376. pickedStartTime: util.timestampToTime(res.data.startDate, 'hh:mm'),
  377. pickedEndTime: util.timestampToTime(res.data.endDate, 'hh:mm'),
  378. describeStr: res.data.describeStr,
  379. status,
  380. appointmentText: res.data.status == 1 ? "重新预约" : "修改预约",
  381. merchantId: res.data.reservationMerchantId,
  382. reservationMerchantName: res.data.reservationMerchantName
  383. })
  384. if (status == 0 || status == 1 || status == 2 || status == 3) {
  385. that.setData({
  386. isShowBtns: true
  387. })
  388. }
  389. }).catch(err => {
  390. console.log(err, 'getOrderReservationDetail');
  391. })
  392. },
  393. /**
  394. * @description:根据经纬度计算距离
  395. * @param {*} locationInfo (lat1, lng1, lat2, lng2)
  396. * @return: distanceObj: { distance , distance_str }
  397. */
  398. getDistances(lat1, lng1, lat2, lng2) {
  399. if (lat2 || lng2) {
  400. function rad(num) {
  401. return num * Math.PI / 180.0;
  402. }
  403. var radLat1 = rad(lat1);
  404. var radLat2 = rad(lat2);
  405. var a = radLat1 - radLat2;
  406. var b = rad(lng1) - rad(lng2);
  407. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  408. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  409. s = s * 6378.137;
  410. s = Math.round(s * 10000) / 10000;
  411. var distance = s;
  412. var distance_str = "";
  413. if (parseInt(distance) >= 1) {
  414. distance_str = distance.toFixed(2) + "km";
  415. } else if (!arseInt(distance)) {
  416. return false
  417. } else {
  418. distance_str = (distance * 1000).toFixed(2) + "m";
  419. }
  420. let objData = {
  421. distance: distance,
  422. distance_str: distance_str
  423. }
  424. return objData
  425. } else {
  426. let objData = {
  427. distance: Infinity,
  428. distance_str: ''
  429. }
  430. return objData
  431. }
  432. },
  433. bloob(arr) {
  434. const tempArr = arr
  435. for (let i = 0; i < tempArr.length - 1; i++) {
  436. for (let j = 0; j < tempArr.length - 1 - i; j++) {
  437. if (tempArr[j].distance > tempArr[j + 1].distance) {
  438. let temp = tempArr[j];
  439. tempArr[j] = tempArr[j + 1];
  440. tempArr[j + 1] = temp;
  441. }
  442. }
  443. }
  444. return tempArr
  445. },
  446. onShow() {
  447. }
  448. })