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

350 regels
8.2 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 mallList = []
  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. mallList.push(obj)
  87. i++
  88. })
  89. that.setData({
  90. mallList: mallList[0].merchantVoList
  91. })
  92. console.log(that.data.mallList, 'mallList');
  93. } else {
  94. that.setData({
  95. mallList: []
  96. })
  97. console.log(that.data.mallList, 'mallList');
  98. }
  99. if (flag) {
  100. that.dealMallList()
  101. }
  102. tt.hideLoading();
  103. })
  104. },
  105. // 获取适用门店所在的城市list
  106. getCityList(couponChannelId) {
  107. const that = this
  108. Http.get({
  109. url: config.api.couponMerchantCity,
  110. data: {
  111. couponChannelId
  112. }
  113. }).then(res => {
  114. console.log(res, 'res');
  115. const tagArr = Object.keys(res.data)
  116. tagArr.sort(function (a, b) {
  117. if (a < b) {
  118. return -1;
  119. }
  120. if (a > b) {
  121. return 1;
  122. }
  123. return 0;
  124. });
  125. const cityArr = res.data
  126. that.setData({
  127. tagArr,
  128. cityArr
  129. })
  130. console.log(tagArr, 'tagArr');
  131. console.log(cityArr, 'cityArr');
  132. }).catch(err => {
  133. console.log(err, 'err');
  134. })
  135. },
  136. /**
  137. * 获得经纬度
  138. */
  139. getLocation() {
  140. console.log('getLocation');
  141. const that = this;
  142. tt.getLocation({
  143. type: "wgs84",
  144. // isHighAccuracy: true,
  145. success: function (res) {
  146. console.log(res, 'getLocation')
  147. that.setData({
  148. latitude: res.latitude,
  149. longitude: res.longitude,
  150. isLocation: true,
  151. currentCity: res.city
  152. })
  153. if (that.data.isOrder) {
  154. const merOrderId = that.data.merOrderId
  155. that.getCouponMerchant(merOrderId, true)
  156. that.getCityList(merOrderId)
  157. } else {
  158. that.getCouponChannelId(true)
  159. }
  160. },
  161. // 拒绝提供定位权限
  162. fail: error => {
  163. if (that.data.isOrder) {
  164. const merOrderId = that.data.merOrderId
  165. that.getCouponMerchant(merOrderId, false)
  166. that.getCityList(merOrderId)
  167. } else {
  168. that.getCouponChannelId(false)
  169. }
  170. that.setData({
  171. isShop: false,
  172. isLocation: false,
  173. mallList: []
  174. })
  175. tt.hideLoading();
  176. },
  177. complete: finish => {
  178. const city = that.data.currentCity
  179. if (!city) {
  180. tt.showToast({
  181. title: "定位失败!",
  182. icon: "fail"
  183. });
  184. }
  185. }
  186. })
  187. },
  188. dealMallList() {
  189. const that = this
  190. const latitudeNow = that.data.latitude
  191. const longitudeNow = that.data.longitude
  192. const mallLocationArr = []
  193. const mallList = that.data.mallList
  194. mallList.forEach((item, index) => {
  195. const obj = {
  196. latitude: item.latitude || false,
  197. longitude: item.longitude || false,
  198. id: item.id,
  199. index: index
  200. }
  201. mallLocationArr.push(obj)
  202. })
  203. console.log(mallLocationArr, 'mallLocationArr');
  204. const tempArr = mallList
  205. const bloobArr = []
  206. mallLocationArr.forEach((item, index) => {
  207. const distanceData = that.getDistances(latitudeNow, longitudeNow, item.latitude, item.longitude)
  208. tempArr[index].distance_str = distanceData.distance_str
  209. tempArr[index].distanceName = tempArr[index].merchantName + ' ' + distanceData.distance_str
  210. tempArr[index].distance = distanceData.distance
  211. bloobArr.push(tempArr[index])
  212. })
  213. const finalSortMallList = that.bloob(bloobArr)
  214. console.log(finalSortMallList, 'finalSortMallList');
  215. that.setData({
  216. mallList: finalSortMallList
  217. })
  218. },
  219. /**
  220. * @description:根据经纬度计算距离
  221. * @param {*} locationInfo (lat1, lng1, lat2, lng2)
  222. * @return: distanceObj: { distance , distance_str }
  223. */
  224. getDistances(lat1, lng1, lat2, lng2) {
  225. if (lat2 || lng2) {
  226. function rad(num) {
  227. return num * Math.PI / 180.0;
  228. }
  229. var radLat1 = rad(lat1);
  230. var radLat2 = rad(lat2);
  231. var a = radLat1 - radLat2;
  232. var b = rad(lng1) - rad(lng2);
  233. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  234. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  235. s = s * 6378.137;
  236. s = Math.round(s * 10000) / 10000;
  237. var distance = s;
  238. var distance_str = "";
  239. if (parseInt(distance) >= 1) {
  240. distance_str = distance.toFixed(2) + "km";
  241. } else if (!arseInt(distance)) {
  242. return false
  243. } else {
  244. distance_str = (distance * 1000).toFixed(2) + "m";
  245. }
  246. let objData = {
  247. distance: distance,
  248. distance_str: distance_str
  249. }
  250. return objData
  251. } else {
  252. let objData = {
  253. distance: Infinity,
  254. distance_str: ''
  255. }
  256. return objData
  257. }
  258. },
  259. bloob(arr) {
  260. const tempArr = arr
  261. for (let i = 0; i < tempArr.length - 1; i++) {
  262. for (let j = 0; j < tempArr.length - 1 - i; j++) {
  263. if (tempArr[j].distance > tempArr[j + 1].distance) {
  264. let temp = tempArr[j];
  265. tempArr[j] = tempArr[j + 1];
  266. tempArr[j + 1] = temp;
  267. }
  268. }
  269. }
  270. return tempArr
  271. },
  272. selectCity() {
  273. this.setData({
  274. isShop: false
  275. })
  276. },
  277. selectShop() {
  278. this.setData({
  279. isShop: true
  280. })
  281. },
  282. indexBarMove(e) {
  283. },
  284. tagBind(e) {
  285. const that = this
  286. const id = e.target.id
  287. that.setData({
  288. currentTag: id,
  289. toView: id
  290. })
  291. },
  292. goSetShop(e) {
  293. const item = e.currentTarget.dataset.item
  294. console.log(item, 'item');
  295. app.globalData.shopItem = item
  296. tt.navigateBack();
  297. },
  298. goFindShop(e) {
  299. const name = e.currentTarget.dataset.name
  300. this.setData({
  301. currentCity: name,
  302. mallList: [],
  303. isShop: true
  304. })
  305. tt.showLoading({
  306. title: '加载中...',
  307. });
  308. const isLocation = this.data.isLocation
  309. if (this.data.isOrder) {
  310. const merOrderId = this.data.merOrderId
  311. this.getCouponMerchant(merOrderId, isLocation)
  312. this.getCityList(merOrderId)
  313. } else {
  314. this.getCouponChannelId(isLocation)
  315. }
  316. },
  317. })