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

390 line
9.0 KiB

  1. // c:\Users\56817\Desktop\Working Space\3,TikTok-MiniPro\ttc\pages2\shopSelect\shopSelect.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. isShop: true,
  10. position: 0,
  11. tagArr: [],
  12. cityArr: [],
  13. mallList: [],
  14. currentTag: "",
  15. currentCity: "",
  16. isLocation: false,
  17. isOrder: "",
  18. merOrderId: "",
  19. latitude: "",
  20. longitude: "",
  21. toView: "A"
  22. },
  23. onLoad: function (options) {
  24. tt.showLoading({
  25. title: '加载中...',
  26. })
  27. console.log(options, 'options')
  28. const mallTenantId = tt.getStorageSync('mallTenantId');
  29. if (mallTenantId) {
  30. this.setData({
  31. mallTenantId
  32. })
  33. }
  34. // const tagArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  35. // this.setData({
  36. // tagArr
  37. // })
  38. this.setData({
  39. isOrder: options.isOrder || false,
  40. merOrderId: options.merOrderId || '',
  41. })
  42. this.getLocation()
  43. },
  44. getCouponChannelId(flag) {
  45. const that = this
  46. Http.get({
  47. url: config.api.orderDetail,
  48. data: {
  49. orderId: that.data.merOrderId,
  50. mallTenantId: that.data.mallTenantId || ''
  51. }
  52. }).then(res => {
  53. const couponChannelId = res.data.orders[0].couponChannelId
  54. that.getCouponMerchant(couponChannelId, flag)
  55. that.getCityList(couponChannelId)
  56. }).catch(err => {
  57. })
  58. },
  59. //获取适用门店
  60. getCouponMerchant(couponChannelId, flag) {
  61. console.log(flag, 'flag');
  62. const that = this
  63. Http.get({
  64. url: config.api.couponMerchant,
  65. data: {
  66. couponChannelId,
  67. city: that.data.currentCity,
  68. mallTenantId: that.data.mallTenantId || ''
  69. }
  70. }).then(res => {
  71. const keys = Object.keys(res.data)
  72. if (keys.length > 0) {
  73. const list = []
  74. let i = 0
  75. keys.forEach(item => {
  76. const arr = item.split('|')
  77. const obj = {
  78. tenantId: arr[0],
  79. mallName: arr[1],
  80. merchantVoList: res.data[item],
  81. expand: false
  82. }
  83. if (i == 0) {
  84. obj.expand = true
  85. }
  86. list.push(obj)
  87. i++
  88. })
  89. const mallList = []
  90. // 过滤商管商户
  91. list[0].merchantVoList.forEach(item => {
  92. if (!item.isAdmin) {
  93. mallList.push(item)
  94. }
  95. })
  96. that.setData({
  97. mallList
  98. })
  99. console.log(that.data.mallList, 'mallList');
  100. } else {
  101. that.setData({
  102. mallList: []
  103. })
  104. console.log(that.data.mallList, 'mallList');
  105. }
  106. if (flag) {
  107. that.dealMallList()
  108. }
  109. tt.hideLoading();
  110. })
  111. },
  112. // 获取适用门店所在的城市list
  113. getCityList(couponChannelId) {
  114. tt.showLoading({
  115. title: '加载中...',
  116. })
  117. const that = this
  118. Http.get({
  119. url: config.api.couponMerchantCity,
  120. data: {
  121. couponChannelId
  122. }
  123. }).then(res => {
  124. console.log(res, 'res');
  125. const tagArr = Object.keys(res.data)
  126. tagArr.sort(function (a, b) {
  127. if (a < b) {
  128. return -1;
  129. }
  130. if (a > b) {
  131. return 1;
  132. }
  133. return 0;
  134. });
  135. const cityArr = res.data
  136. that.setData({
  137. tagArr,
  138. cityArr
  139. })
  140. console.log(tagArr, 'tagArr');
  141. console.log(cityArr, 'cityArr');
  142. tt.hideLoading();
  143. }).catch(err => {
  144. console.log(err, 'err');
  145. })
  146. },
  147. /**
  148. * 获得经纬度
  149. */
  150. getLocation() {
  151. console.log('getLocation');
  152. const that = this;
  153. tt.getLocation({
  154. type: "wgs84",
  155. // isHighAccuracy: true,
  156. success: function (res) {
  157. console.log(res, 'getLocation')
  158. that.setData({
  159. latitude: res.latitude,
  160. longitude: res.longitude,
  161. isLocation: true,
  162. currentCity: res.city
  163. })
  164. if (that.data.isOrder) {
  165. const merOrderId = that.data.merOrderId
  166. that.getCouponMerchant(merOrderId, true)
  167. that.getCityList(merOrderId)
  168. } else {
  169. that.getCouponChannelId(true)
  170. }
  171. },
  172. // 拒绝提供定位权限
  173. fail: error => {
  174. if (that.data.isOrder) {
  175. const merOrderId = that.data.merOrderId
  176. that.getCouponMerchant(merOrderId, false)
  177. that.getCityList(merOrderId)
  178. } else {
  179. that.getCouponChannelId(false)
  180. }
  181. that.setData({
  182. isShop: false,
  183. isLocation: false,
  184. mallList: []
  185. })
  186. tt.hideLoading();
  187. },
  188. complete: finish => {
  189. const city = that.data.currentCity
  190. if (!city) {
  191. tt.showToast({
  192. title: "定位失败!",
  193. icon: "fail"
  194. });
  195. }
  196. }
  197. })
  198. },
  199. dealMallList() {
  200. const that = this
  201. const latitudeNow = that.data.latitude
  202. const longitudeNow = that.data.longitude
  203. const mallLocationArr = []
  204. const mallList = that.data.mallList
  205. mallList.forEach((item, index) => {
  206. const obj = {
  207. latitude: item.latitude || false,
  208. longitude: item.longitude || false,
  209. id: item.id,
  210. index: index
  211. }
  212. mallLocationArr.push(obj)
  213. })
  214. console.log(mallLocationArr, 'mallLocationArr');
  215. const tempArr = mallList
  216. const bloobArr = []
  217. mallLocationArr.forEach((item, index) => {
  218. const distanceData = that.getDistances(latitudeNow, longitudeNow, item.latitude, item.longitude)
  219. tempArr[index].distance_str = distanceData.distance_str
  220. tempArr[index].distanceName = tempArr[index].merchantName + ' ' + distanceData.distance_str
  221. tempArr[index].distance = distanceData.distance
  222. bloobArr.push(tempArr[index])
  223. })
  224. const finalSortMallList = that.bloob(bloobArr)
  225. console.log(finalSortMallList, 'finalSortMallList');
  226. that.setData({
  227. mallList: finalSortMallList
  228. })
  229. },
  230. /**
  231. * @description:根据经纬度计算距离
  232. * @param {*} locationInfo (lat1, lng1, lat2, lng2)
  233. * @return: distanceObj: { distance , distance_str }
  234. */
  235. getDistances(lat1, lng1, lat2, lng2) {
  236. if (lat2 || lng2) {
  237. function rad(num) {
  238. return num * Math.PI / 180.0;
  239. }
  240. var radLat1 = rad(lat1);
  241. var radLat2 = rad(lat2);
  242. var a = radLat1 - radLat2;
  243. var b = rad(lng1) - rad(lng2);
  244. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  245. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  246. s = s * 6378.137;
  247. s = Math.round(s * 10000) / 10000;
  248. var distance = s;
  249. var distance_str = "";
  250. if (parseInt(distance) >= 1) {
  251. distance_str = distance.toFixed(2) + "km";
  252. } else if (!arseInt(distance)) {
  253. return false
  254. } else {
  255. distance_str = (distance * 1000).toFixed(2) + "m";
  256. }
  257. let objData = {
  258. distance: distance,
  259. distance_str: distance_str
  260. }
  261. return objData
  262. } else {
  263. let objData = {
  264. distance: Infinity,
  265. distance_str: ''
  266. }
  267. return objData
  268. }
  269. },
  270. bloob(arr) {
  271. const tempArr = arr
  272. for (let i = 0; i < tempArr.length - 1; i++) {
  273. for (let j = 0; j < tempArr.length - 1 - i; j++) {
  274. if (tempArr[j].distance > tempArr[j + 1].distance) {
  275. let temp = tempArr[j];
  276. tempArr[j] = tempArr[j + 1];
  277. tempArr[j + 1] = temp;
  278. }
  279. }
  280. }
  281. return tempArr
  282. },
  283. selectCity() {
  284. this.setData({
  285. isShop: false
  286. })
  287. },
  288. selectShop() {
  289. this.setData({
  290. isShop: true
  291. })
  292. },
  293. indexBarMove(e) {
  294. },
  295. tagBind(e) {
  296. const that = this
  297. const id = e.target.id
  298. that.setData({
  299. currentTag: id,
  300. toView: id
  301. })
  302. tt.vibrateShort()
  303. },
  304. goSetShop(e) {
  305. if (e.target.id == "goMap") return
  306. const item = e.currentTarget.dataset.item
  307. console.log(item, 'item');
  308. app.globalData.shopItem = item
  309. tt.navigateBack();
  310. },
  311. //跳转地图页面
  312. goMap(e) {
  313. const item = e.currentTarget.dataset.item
  314. const latitude = Number(item.latitude)
  315. const longitude = Number(item.longitude)
  316. if (!latitude && !latitude) {
  317. tt.showToast({
  318. title: '抱歉,该门店暂不支持!',
  319. });
  320. return
  321. }
  322. tt.openLocation({
  323. latitude,
  324. longitude,
  325. scale: 18,
  326. success() {
  327. console.log("打开地图成功");
  328. },
  329. fail(err) {
  330. console.log("打开地图失败:", err.errMsg);
  331. },
  332. });
  333. },
  334. goFindShop(e) {
  335. const name = e.currentTarget.dataset.name
  336. this.setData({
  337. currentCity: name,
  338. mallList: [],
  339. isShop: true
  340. })
  341. tt.showLoading({
  342. title: '加载中...',
  343. });
  344. const isLocation = this.data.isLocation
  345. if (this.data.isOrder) {
  346. const merOrderId = this.data.merOrderId
  347. this.getCouponMerchant(merOrderId, isLocation)
  348. this.getCityList(merOrderId)
  349. } else {
  350. this.getCouponChannelId(isLocation)
  351. }
  352. },
  353. })