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

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