index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // pages/yuezhan/search/index.js
  2. const { request, serverName, imgServer } = require('../../../utils/services');
  3. const {
  4. newRequestFns,
  5. newServerName
  6. } = require('../../../utils/newServices.js');
  7. const { Toast } = require('../../../utils/util');
  8. const { defaultImg, noExhibitionImg, noExhibitionHallImg } = require('../../../utils/images');
  9. const app = getApp();
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. tabs: ['展览', '展馆'],
  16. testImg: '../../../imgs/testImg/thumbSmallImg2.jpg',
  17. avatar: '../../../imgs/testImg/1.jpg',
  18. activeIndex:0,
  19. animationData: "",
  20. isLike:false,
  21. loading:false,
  22. currentPage:1,
  23. exhibitionList:[],
  24. pavilionList:[],
  25. totalElements:''
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. this.setData({
  32. searchText: options.searchText,
  33. serverName,
  34. imgServer
  35. });
  36. // 检查 globalData 是否已有位置信息
  37. if (app.globalData.latitude && app.globalData.longitude) {
  38. // 使用 globalData 中的经纬度
  39. this.setData({
  40. latitude: app.globalData.latitude,
  41. longitude: app.globalData.longitude
  42. });
  43. // 处理 city 设置
  44. if (app.globalData.locationName) {
  45. app.globalData.city = app.globalData.locationName;
  46. } else {
  47. app.globalData.city = "北京";
  48. }
  49. this.loadData();
  50. }
  51. // this.getKeywordCode()
  52. this.setData({
  53. noExhibitionImg,
  54. noExhibitionHallImg,
  55. defaultImg
  56. })
  57. },
  58. keywordSearch:function(e){
  59. let { keyword } = e.currentTarget.dataset;
  60. this.setData({
  61. searchText: keyword
  62. })
  63. console.log("s",e)
  64. this.loadData();
  65. },
  66. getKeywordCode:function(){
  67. newRequestFns["getKeywordCode"]({}, "", res => {
  68. if (res.data.code > -1) {
  69. let {data:keyword} = res.data;
  70. this.setData({
  71. keyword
  72. })
  73. wx.stopPullDownRefresh();
  74. console.log(res)
  75. }
  76. }, err => {
  77. }, complete => {
  78. })
  79. },
  80. loadData: function () {
  81. // 通过activeIndex 判断应该load哪部分的数据
  82. const { activeIndex } = this.data;
  83. this.setData({
  84. exhibitionList: [],
  85. pavilionList: []
  86. });
  87. if (activeIndex == 0) {
  88. this.getExhibitionSearch(1);
  89. if (this.data.exhibitionList.length < 5){
  90. this.popular();
  91. }
  92. }
  93. else if (activeIndex == 1) {
  94. this.getPavilionSearch(1);
  95. }
  96. else {
  97. return
  98. }
  99. },
  100. addLike: function (e) {
  101. let { type, id, idx } = e.currentTarget.dataset;
  102. console.log(idx, type, id)
  103. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  104. let exhibitionList = this.data.exhibitionList;
  105. console.log('type', type)
  106. Toast.showToast2('loading');
  107. let loginSessionKey = wx.getStorageSync('token') || "";
  108. // if (loginSessionKey){
  109. newRequestFns['isCollect']({
  110. loginSessionKey,
  111. exhibitionId: id,
  112. type: Number(type),
  113. }, "post", res => {
  114. if (res.data.code > -1) {
  115. for (let i = 0; i < collectedArr.length; i++) {
  116. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  117. console.log("true")
  118. collectedArr[i] = {
  119. collectedId: id,
  120. status: res.data.data.hasCollect,
  121. }
  122. hasItem = false;
  123. }
  124. }
  125. if (hasItem) {
  126. collectedArr.push({
  127. collectedId: id,
  128. status: res.data.data.hasCollect,
  129. })
  130. }
  131. app.globalData.collectedArr = collectedArr;
  132. app.globalData.collectedChange = true;
  133. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  134. this.setData({
  135. exhibitionList: exhibitionList
  136. })
  137. }
  138. }, err => {
  139. }, complete => {
  140. Toast.hideLoading();
  141. })
  142. // }
  143. console.log(this.data.exhibitionList)
  144. },
  145. getExhibitionSearch:function(page){
  146. let name = this.data.searchText||"";
  147. this.setData({
  148. loading: true
  149. })
  150. let { latitude: lat, longitude: lng } = this.data;
  151. newRequestFns["getExhibitionSearch"]({
  152. name,
  153. lat,
  154. lng,
  155. page
  156. },"",res=>{
  157. if(res.data.code>-1){
  158. let tempContent = this.data.exhibitionList
  159. ? this.data.exhibitionList
  160. : [];
  161. let { pageData: exhibitionList, total } = res.data.data;
  162. // 判断是否为最后一页(返回空数组表示没有更多数据)
  163. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  164. if (isLastPage) {
  165. this.setData({
  166. loading: false,
  167. lastPage: true, // 标记为最后一页
  168. totalElements: total || 0
  169. });
  170. wx.stopPullDownRefresh();
  171. return;
  172. }
  173. exhibitionList.forEach((currentValue) => {
  174. currentValue.distance = Math.ceil(currentValue.distance);
  175. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  176. })
  177. // 拼接新数据
  178. let newExhibitionList = tempContent.concat(exhibitionList);
  179. this.setData({
  180. currentPage: page,
  181. lastPage: false, // 有数据时设为false
  182. loading: false,
  183. exhibitionList: newExhibitionList,
  184. totalElements: total
  185. });
  186. console.log(exhibitionList)
  187. wx.stopPullDownRefresh();
  188. console.log(res)
  189. }
  190. },err=>{
  191. }, complete => {
  192. })
  193. },
  194. getPavilionSearch: function (page) {
  195. let name = this.data.searchText || "";
  196. this.setData({
  197. loading: true
  198. })
  199. let { latitude: lat, longitude: lng } = this.data;
  200. newRequestFns["getPavilionSearch"]({
  201. name,
  202. lat,
  203. lng,
  204. page
  205. }, "", res => {
  206. if (res.data.code > -1) {
  207. let tempContent = this.data.pavilionList
  208. ? this.data.pavilionList
  209. : [];
  210. let { pageData: pavilionList, total } = res.data.data;
  211. // 判断是否为最后一页(返回空数组表示没有更多数据)
  212. let isLastPage = !pavilionList || pavilionList.length === 0;
  213. if (isLastPage) {
  214. this.setData({
  215. loading: false,
  216. lastPage: true
  217. });
  218. wx.stopPullDownRefresh();
  219. return;
  220. }
  221. // 拼接新数据
  222. let newPavilionList = tempContent.concat(pavilionList);
  223. this.setData({
  224. currentPage: page,
  225. lastPage: false, // 有数据时设为false
  226. loading: false,
  227. pavilionList: newPavilionList,
  228. });
  229. console.log(pavilionList)
  230. wx.stopPullDownRefresh();
  231. console.log(res)
  232. }
  233. }, err => {
  234. }, complete => {
  235. })
  236. },
  237. searchByText: function () {
  238. this.loadData()
  239. },
  240. bindInput: function (event) {
  241. var obj = {},
  242. key = event.target.dataset['key'];
  243. // Toast.showToast('success', event.detail.value);
  244. obj[key] = event.detail.value;
  245. this.setData(obj);
  246. console.log(this.data.searchText)
  247. },
  248. loadMore: function () {
  249. const { activeIndex } = this.data;
  250. // const loadDataFn =
  251. // activeIndex == 0 ? this.loadOneToOrderData : this.loadClassroomData;
  252. let loadDataFn;
  253. if ( activeIndex == 0) {
  254. loadDataFn = this.getExhibitionSearch;
  255. }
  256. else if ( activeIndex == 1) {
  257. loadDataFn = this.getPavilionSearch;
  258. }
  259. else {
  260. return
  261. }
  262. if (!this.data.lastPage) {
  263. this.setData({
  264. loading: true
  265. });
  266. console.log(this.data.currentPage + 1)
  267. loadDataFn(this.data.currentPage + 1);
  268. } else {
  269. return;
  270. }
  271. },
  272. popular : function(){
  273. let { latitude: lat, longitude: lng } = this.data;
  274. newRequestFns["recommend"]({
  275. lat,
  276. lng,
  277. }, "", res => {
  278. this.setData({
  279. ExhibitionPopular: res.data.data
  280. })
  281. }, err => {
  282. }, complete => {
  283. })
  284. },
  285. onReachBottom: function () {
  286. if (!this.data.loading) {
  287. this.loadMore();
  288. console.log('reach Bottom');
  289. }
  290. },
  291. tabClick: function (e) {
  292. let { activeIndex } = this.data;
  293. this.setData({
  294. loading: true,
  295. activeIndex: e.currentTarget.id
  296. })
  297. this.loadData();
  298. },
  299. /**
  300. * 生命周期函数--监听页面初次渲染完成
  301. */
  302. onReady: function () {
  303. },
  304. /**
  305. * 生命周期函数--监听页面显示
  306. */
  307. onShow: function () {
  308. let { collectedArr, collectedChange } = app.globalData;
  309. let { exhibitionList } = this.data
  310. let isLogin = app.globalData.isLogin;
  311. // this.setData({
  312. // exhibitionList: []
  313. // });
  314. // this.getBanner();
  315. // this.getList(1);
  316. this.setData({
  317. isLogin
  318. })
  319. if (collectedChange) {
  320. for (let i = 0; i < exhibitionList.length; i++) {
  321. for (let j = 0; j < collectedArr.length; j++) {
  322. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  323. exhibitionList[i].hasCollect = collectedArr[j].status
  324. if (exhibitionList[i].hasCollect) {
  325. exhibitionList[i].collectionsCount += 1;
  326. }
  327. else {
  328. exhibitionList[i].collectionsCount -= 1;
  329. }
  330. }
  331. }
  332. // console.log(collectedArr.status, collectedArr.collectedId)
  333. }
  334. this.setData({
  335. exhibitionList,
  336. })
  337. }
  338. // app.globalData.clickToSelect = false;
  339. // app.globalData.collectedChange = false;
  340. },
  341. /**
  342. * 生命周期函数--监听页面隐藏
  343. */
  344. onHide: function () {
  345. },
  346. /**
  347. * 生命周期函数--监听页面卸载
  348. */
  349. onUnload: function () {
  350. },
  351. /**
  352. * 页面相关事件处理函数--监听用户下拉动作
  353. */
  354. onPullDownRefresh: function () {
  355. this.loadData();
  356. },
  357. /**
  358. * 用户点击右上角分享
  359. */
  360. onShareAppMessage: function () {
  361. }
  362. })