index.js 8.7 KB

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