house.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // pages/index/house/house.js
  2. import HTTP, { GetGardenHouse } from "./../../../../requestFn/Api"
  3. const app = getApp();
  4. var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
  5. var qqmapsdk = new QQMapWX({
  6. key: 'ZZFBZ-RK6L6-FPZS5-E3MBZ-63LS2-7WBJE' // 必填
  7. });
  8. Page({
  9. data: {
  10. navbarActiveIndex: 0,
  11. navbarTitle: [
  12. "租房",
  13. "二手房",
  14. "小区"
  15. ],
  16. hiddenName: {
  17. rent: false,
  18. old: true,
  19. garden: true
  20. },
  21. chooseItems: {
  22. 0: [{},
  23. {
  24. name: '床',
  25. value: '1',
  26. selected: false
  27. }, {
  28. name: '天然气',
  29. value: '2',
  30. selected: false
  31. }, {
  32. name: '暖气',
  33. value: '3',
  34. selected: false
  35. }, {
  36. name: '宽带',
  37. value: '4',
  38. selected: false
  39. }, {
  40. name: '冰箱',
  41. value: '5',
  42. selected: false
  43. }, {
  44. name: '衣柜',
  45. value: '6',
  46. selected: false
  47. }, {
  48. name: '沙发',
  49. value: '7',
  50. selected: false
  51. }, {
  52. name: '空调',
  53. value: '8',
  54. selected: false
  55. }, {
  56. name: '电视',
  57. value: '9',
  58. selected: false
  59. }, {
  60. name: '热水器',
  61. value: '10',
  62. selected: false
  63. }, {
  64. name: '洗衣机',
  65. value: '11',
  66. selected: false
  67. },
  68. ],
  69. 1: [{},
  70. {
  71. name: '智能门锁',
  72. value: '1',
  73. selected: false
  74. }, {
  75. name: 'WIFI',
  76. value: '2',
  77. selected: false
  78. }, {
  79. name: '近地铁',
  80. value: '3',
  81. selected: false
  82. }, {
  83. name: '停车场',
  84. value: '4',
  85. selected: false
  86. }, {
  87. name: '独立卫生间',
  88. value: '5',
  89. selected: false
  90. }, {
  91. name: '私人阳台',
  92. value: '6',
  93. selected: false
  94. }, {
  95. name: '首次出租',
  96. value: '7',
  97. selected: false
  98. },
  99. ],
  100. },
  101. house_list: [],
  102. garden_list:[],
  103. page: 1,
  104. garden_page:1,
  105. page_size: 10,
  106. has_more: true,
  107. garden_more:true
  108. },
  109. onShow(options) {
  110. const that = this;
  111. const {
  112. city_code
  113. } = app.globalData;
  114. if (city_code) {
  115. this.setData({
  116. city_code
  117. })
  118. this.init();
  119. } else {
  120. wx.getSetting({
  121. success(res) {
  122. if (res.authSetting['scope.userFuzzyLocation'] === true) {
  123. that.getLocation();
  124. } else if (res.authSetting['scope.userFuzzyLocation'] === undefined) {
  125. that.locationScope();
  126. } else if (res.authSetting['scope.userFuzzyLocation'] !== undefined && res.authSetting['scope.userFuzzyLocation'] !== true) {
  127. wx.showModal({
  128. title: '是否授权当前位置',
  129. content: '需要获取您的地理位置,请确认授权,否则无法使用相关功能!',
  130. success: res => {
  131. //如果点击取消则显示授权失败
  132. if (res.cancel) {
  133. wx.showToast({
  134. title: '您已拒绝授权!',
  135. icon: 'none'
  136. })
  137. that.setData({
  138. city_name: "请选择"
  139. })
  140. }
  141. //如果点击确定会打开授权页请求二次授权
  142. else if (res.confirm) {
  143. wx.openSetting({
  144. success: dataAu => {
  145. if (dataAu.authSetting['scope.userFuzzyLocation'] === true) {
  146. wx.showToast({
  147. title: '授权成功',
  148. icon: 'none'
  149. })
  150. //再次授权,调用getLocationt的API
  151. that.locationScope()
  152. } else {
  153. wx.showToast({
  154. title: '授权失败',
  155. icon: 'none'
  156. })
  157. }
  158. }
  159. })
  160. }
  161. }
  162. })
  163. }
  164. },
  165. fail(err) {
  166. wx.showModal({
  167. title: '提示',
  168. content: '未知错误,请重新打开小程序!'
  169. })
  170. }
  171. })
  172. }
  173. },
  174. locationScope() {
  175. const that = this;
  176. wx.authorize({
  177. scope: 'scope.userFuzzyLocation',
  178. success(res) {
  179. if (res.errMsg == "authorize:ok") {
  180. that.getLocation();
  181. }
  182. },
  183. fail(e) {}
  184. })
  185. },
  186. getLocation() {
  187. const that = this;
  188. wx.getFuzzyLocation({
  189. success(res) {
  190. const {
  191. latitude,
  192. longitude
  193. } = res;
  194. app.globalData.longitude = longitude;
  195. app.globalData.latitude = latitude;
  196. that.setData({
  197. longitude,
  198. latitude
  199. })
  200. that.getUserLocation();
  201. },
  202. })
  203. },
  204. getUserLocation() {
  205. let vm = this
  206. qqmapsdk.reverseGeocoder({ //逆地址解析(经纬度 ==> 坐标位置)
  207. location: {
  208. latitude: vm.data.latitude,
  209. longitude: vm.data.longitude
  210. },
  211. success(res) {
  212. var {
  213. result: {
  214. ad_info: {
  215. city
  216. }
  217. }
  218. } = res;
  219. app.globalData.city_name = city;
  220. vm.setData({
  221. city_name: city
  222. })
  223. vm.GetCityCode();
  224. }
  225. })
  226. },
  227. async GetCityCode() {
  228. const {
  229. city_name
  230. } = this.data;
  231. const data = await HTTP.GetCityCode({
  232. city_name
  233. });
  234. app.globalData.city_code = data.code;
  235. this.setData({
  236. city_code: data.code
  237. })
  238. this.init();
  239. },
  240. goRent_house() {
  241. wx.navigateTo({
  242. url: './renthouse/renthouse',
  243. })
  244. },
  245. goRelease() {
  246. wx.navigateTo({
  247. url: './myhouse/myhouse',
  248. })
  249. },
  250. goAddRenthouse() {
  251. wx.navigateTo({
  252. url: './addrent/addrent',
  253. })
  254. },
  255. init() {
  256. this.loadHomePage();
  257. },
  258. loadHomePage() {
  259. wx.stopPullDownRefresh();
  260. this.setData({
  261. pageLoading: true,
  262. page: 1,
  263. garden_page:1,
  264. garden_more:true,
  265. has_more: true
  266. });
  267. this.getRenthouse(true, );
  268. this.GetGardenHouse(true);
  269. },
  270. onNavBarTap: function (event) {
  271. const that = this;
  272. let navbarTapIndex = event.currentTarget.dataset.navbarIndex
  273. that.setData({
  274. navbarActiveIndex: navbarTapIndex
  275. })
  276. if (that.data.navbarActiveIndex == 0) {
  277. that.setData({
  278. hiddenName: {
  279. rent: false,
  280. old: true,
  281. garden: true
  282. }
  283. })
  284. } else if (that.data.navbarActiveIndex == 1) {
  285. that.setData({
  286. hiddenName: {
  287. rent: true,
  288. old: false,
  289. garden: true
  290. }
  291. })
  292. } else {
  293. that.setData({
  294. hiddenName: {
  295. rent: true,
  296. old: true,
  297. garden: false
  298. },
  299. })
  300. }
  301. },
  302. async getRenthouse(fresh = false, ) {
  303. if (fresh) {
  304. wx.pageScrollTo({
  305. scrollTop: 0,
  306. });
  307. }
  308. this.setData({
  309. goodsListLoadStatus: 1
  310. });
  311. let {
  312. house_list,
  313. page,
  314. page_size,
  315. city_code
  316. } = this.data;
  317. try {
  318. let res = await HTTP.RentHouse({
  319. page,
  320. page_size,
  321. city_code
  322. })
  323. this.setData({
  324. house_list: fresh ? res.list : house_list.concat(res.list),
  325. pageLoading: false,
  326. page: ++page,
  327. has_more: res.list.length === page_size
  328. })
  329. } catch (e) {
  330. this.setData({
  331. goodsListLoadStatus: 3
  332. });
  333. }
  334. },
  335. async GetGardenHouse(fresh = false,){
  336. if (fresh) {
  337. wx.pageScrollTo({
  338. scrollTop: 0,
  339. });
  340. }
  341. this.setData({
  342. goodsListLoadStatus: 1
  343. });
  344. let {
  345. garden_list,
  346. garden_page,
  347. page_size,
  348. city_code
  349. } = this.data;
  350. try {
  351. let res = await HTTP.GetGardenList({
  352. page:garden_page,
  353. page_size,
  354. city_code
  355. })
  356. this.setData({
  357. garden_list: fresh ? res.list : garden_list.concat(res.list),
  358. pageLoading: false,
  359. garden_page: ++garden_page,
  360. garden_more: res.list.length === page_size
  361. })
  362. } catch (e) {
  363. this.setData({
  364. goodsListLoadStatus: 3
  365. });
  366. }
  367. },
  368. onReachBottom() {
  369. if (!this.data.hiddenName.rent) {
  370. if (!this.data.has_more) {
  371. wx.showToast({
  372. title: '没有更多数据了',
  373. icon: 'none'
  374. })
  375. return console.log('没有更多数据了');
  376. }
  377. this.getRenthouse(false);
  378. }else if(!this.data.hiddenName.garden){
  379. if (!this.data.garden_more) {
  380. wx.showToast({
  381. title: '没有更多数据了',
  382. icon: 'none'
  383. })
  384. return console.log('没有更多数据了');
  385. }
  386. this.GetGardenHouse(false);
  387. }
  388. },
  389. goHouseDetail(e) {
  390. const {
  391. id
  392. } = e.currentTarget;
  393. const {
  394. house_list
  395. } = this.data;
  396. const data = house_list.filter(item => item.id == id);
  397. wx.navigateTo({
  398. url: './detail/detail',
  399. success: function (res) {
  400. res.eventChannel.emit('acceptDataFromOpenerPage', {
  401. data
  402. })
  403. }
  404. })
  405. },
  406. goFindGarden() {
  407. wx.navigateTo({
  408. url: './findgarden/findgarden',
  409. })
  410. },
  411. onPullDownRefresh: function () {
  412. app.onRefresh();
  413. this.init();
  414. },
  415. })