抖音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.

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