index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // nova-tourism/components/template2/homestay/index.js
  2. let Parse = getApp().Parse;
  3. const company = getApp().globalData.company
  4. const compute = require("../../../../utils/compute.js");
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. imgUrls: [
  16. 'https://s1.ax1x.com/2023/04/10/ppqifvF.png',
  17. 'https://s1.ax1x.com/2023/04/10/ppqiWgU.png',
  18. 'https://s1.ax1x.com/2023/04/10/ppqiR3T.png'
  19. ],
  20. tabs: [{
  21. title: '推荐',
  22. // icon: 'https://s1.ax1x.com/2023/04/10/ppqEqdH.png'
  23. },
  24. {
  25. title: '价格',
  26. // icon: 'https://s1.ax1x.com/2023/04/10/ppqkh79.png'
  27. },
  28. ],
  29. active: 0,
  30. tabIndex: 0,
  31. latitude: 0,
  32. longitude: 0,
  33. searchVal:'',
  34. loadIndex: 1,
  35. },
  36. lifetimes: {
  37. attached() {
  38. this.getData()
  39. }
  40. },
  41. /**
  42. * 组件的方法列表
  43. */
  44. methods: {
  45. async getData() {
  46. if (this.data.latitude == 0 && this.data.longitude == 0) {
  47. let {
  48. latitude,
  49. longitude
  50. } = await this.getLocation()
  51. this.setData({
  52. latitude,
  53. longitude
  54. })
  55. }
  56. this.getBanner()
  57. this.getRooms()
  58. await this.getCategory()
  59. this.getShopRank(this.data.textItem)
  60. },
  61. getLocation() {
  62. return new Promise((resolve, reject) => {
  63. wx.getLocation({
  64. type: 'gcj02',
  65. success: (res) => {
  66. resolve({
  67. latitude: res.latitude,
  68. longitude: res.longitude
  69. })
  70. },
  71. fail: () => {
  72. resolve({
  73. latitude: 0,
  74. longitude: 0
  75. })
  76. },
  77. complete: () => {}
  78. });
  79. })
  80. },
  81. async getBanner() {
  82. let Banner = new Parse.Query('Banner')
  83. Banner.notEqualTo('isDeleted', "true")
  84. Banner.equalTo('company', company)
  85. Banner.equalTo('isEnabled', "true")
  86. Banner.equalTo('type', 'gourmet')
  87. let banner = await Banner.find()
  88. if (banner && banner.length > 0) {
  89. let listJSON = []
  90. banner.forEach(c => {
  91. listJSON.push(c.toJSON())
  92. })
  93. this.setData({
  94. banner: listJSON
  95. })
  96. }
  97. },
  98. async getShopStore() {
  99. let ShopStore = new Parse.Query('ShopStore')
  100. ShopStore.notEqualTo('isDeleted', "true")
  101. ShopStore.equalTo('company', company)
  102. ShopStore.equalTo('type', "stay")
  103. ShopStore.limit(10)
  104. if (this.data.active == 0) {
  105. ShopStore.descending('isShow')
  106. }
  107. if (this.data.active == 1) {
  108. ShopStore.descending('score')
  109. }
  110. if (this.data.active == 3) {
  111. ShopStore.ascending('perCapita')
  112. }
  113. if (this.data.value) {
  114. ShopStore.contains('storeName', this.data.value)
  115. }
  116. let shopStores = await ShopStore.find()
  117. console.log(shopStores);
  118. if (shopStores) {
  119. let listJSON = []
  120. for (let i = 0; i < shopStores.length; i++) {
  121. let json = shopStores[i].toJSON()
  122. let distances = compute.computeDistance(
  123. this.data.latitude,
  124. this.data.longitude,
  125. json.location.latitude,
  126. json.location.longitude
  127. )
  128. json.distances = distances.toFixed(2)
  129. listJSON.push(json)
  130. }
  131. //按距离排序
  132. if (this.data.active == 2) {
  133. listJSON = listJSON.sort((a, b) => {
  134. return a.distance - b.distance
  135. })
  136. }
  137. this.setData({
  138. list: listJSON
  139. })
  140. }
  141. },
  142. async getRooms () {
  143. console.log('getRooms');
  144. let Room = new Parse.Query('ShopRoom')
  145. Room.equalTo('company', company)
  146. Room.equalTo('isEnabled', true)
  147. if(this.data.searchVal != ''){
  148. Room.contains('name',this.data.searchVal)
  149. }
  150. if (this.data.active == 1) {
  151. Room.ascending('price')
  152. }
  153. if(this.data.active == 0){
  154. Room.equalTo('isRecom',true)
  155. }
  156. Room.skip((this.data.loadIndex - 1) * 10)
  157. Room.select(
  158. 'name',
  159. 'images',
  160. 'price',
  161. 'total',
  162. 'remaining',
  163. 'merber',
  164. 'type',
  165. 'area',
  166. 'tags',
  167. 'address'
  168. )
  169. Room.limit(10)
  170. let rooms = await Room.find()
  171. rooms = rooms.map((room) => room.toJSON())
  172. rooms = this.data.loadIndex == 1 ? rooms : this.data.rooms.concat(rooms)
  173. this.setData({ rooms })
  174. console.log(rooms)
  175. },
  176. async getCategory(){
  177. let cate = new Parse.Query("Category")
  178. cate.notEqualTo('isDeleted', "true")
  179. cate.equalTo("company",company)
  180. cate.equalTo("type",'stayTag')
  181. let cateDate = await cate.find()
  182. let cateList = []
  183. cateDate.forEach(res=>{
  184. let item = res.toJSON()
  185. cateList.push(item)
  186. })
  187. if(cateList.length){
  188. this.setData({
  189. textItem:cateList[0].objectId,
  190. })
  191. }
  192. this.setData({
  193. cateList
  194. })
  195. },
  196. async getShopRank(id) {
  197. let ShopStore = new Parse.Query('ShopStore')
  198. ShopStore.notEqualTo('isDeleted', "true")
  199. ShopStore.equalTo('company', company)
  200. ShopStore.equalTo('type', "stay")
  201. ShopStore.equalTo('category', id)
  202. ShopStore.include("category")
  203. let shopStores = await ShopStore.find()
  204. if (shopStores) {
  205. let rankList = []
  206. for (let i = 0; i < shopStores.length; i++) {
  207. let json = shopStores[i].toJSON()
  208. rankList.push(json)
  209. }
  210. this.setData({
  211. rankList
  212. })
  213. }
  214. },
  215. onTabClick(e) {
  216. let index = e.currentTarget.dataset.index;
  217. this.setData({
  218. tabIndex: index,
  219. active: index
  220. })
  221. this.getRooms()
  222. },
  223. changeText(e) {
  224. let {
  225. id
  226. } = e.currentTarget.dataset
  227. this.setData({
  228. textItem: id
  229. })
  230. this.getShopRank(id)
  231. },
  232. goUrl(e) {
  233. let {
  234. url
  235. } = e.currentTarget.dataset
  236. wx.navigateTo({
  237. url: url,
  238. })
  239. },
  240. }
  241. })