抖音c端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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