index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. }
  83. else if (activeIndex == 1) {
  84. this.getPavilionSearch(1);
  85. }
  86. else {
  87. return
  88. }
  89. },
  90. addLike: function (e) {
  91. let { type, id, idx } = e.currentTarget.dataset;
  92. console.log(idx, type, id)
  93. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  94. let exhibitionList = this.data.exhibitionList;
  95. console.log('type', type)
  96. Toast.showToast2('loading');
  97. let loginSessionKey = wx.getStorageSync('token') || "";
  98. // if (loginSessionKey){
  99. request['isCollect']({
  100. loginSessionKey,
  101. exhibitionId: id,
  102. type: Number(type),
  103. }, "post", res => {
  104. if (res.data.code > -1) {
  105. for (let i = 0; i < collectedArr.length; i++) {
  106. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  107. console.log("true")
  108. collectedArr[i] = {
  109. collectedId: id,
  110. status: res.data.data.hasCollect,
  111. }
  112. hasItem = false;
  113. }
  114. }
  115. if (hasItem) {
  116. collectedArr.push({
  117. collectedId: id,
  118. status: res.data.data.hasCollect,
  119. })
  120. }
  121. app.globalData.collectedArr = collectedArr;
  122. app.globalData.collectedChange = true;
  123. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  124. this.setData({
  125. exhibitionList: exhibitionList
  126. })
  127. }
  128. }, err => {
  129. }, complete => {
  130. Toast.hideLoading();
  131. })
  132. // }
  133. console.log(this.data.exhibitionList)
  134. },
  135. getExhibitionSearch:function(page){
  136. let name = this.data.searchText||"";
  137. this.setData({
  138. loading: true
  139. })
  140. request["getExhibitionSearch"]({
  141. name,
  142. page
  143. },"",res=>{
  144. if(res.data.code>-1){
  145. let tempContent = this.data.exhibitionList
  146. ? this.data.exhibitionList
  147. : [];
  148. let { last: lastPage, totalPages, content: exhibitionList } = res.data.data;
  149. this.setData({
  150. currentPage: res.data.data.number+1,
  151. lastPage,
  152. loading: false,
  153. exhibitionList: tempContent.concat(exhibitionList),
  154. totalElements: res.data.data.totalElements
  155. });
  156. console.log(exhibitionList)
  157. wx.stopPullDownRefresh();
  158. console.log(res)
  159. }
  160. },err=>{
  161. }, complete => {
  162. })
  163. },
  164. getPavilionSearch: function (page) {
  165. let name = this.data.searchText || "";
  166. this.setData({
  167. loading: true
  168. })
  169. request["getPavilionSearch"]({
  170. name,
  171. page
  172. }, "", res => {
  173. if (res.data.code > -1) {
  174. let tempContent = this.data.pavilionList
  175. ? this.data.pavilionList
  176. : [];
  177. let { last: lastPage, totalPages, content: pavilionList } = res.data.data;
  178. console.log(res)
  179. this.setData({
  180. currentPage: res.data.data.number+1,
  181. lastPage,
  182. loading: false,
  183. pavilionList: tempContent.concat(pavilionList),
  184. });
  185. console.log(pavilionList)
  186. wx.stopPullDownRefresh();
  187. console.log(res)
  188. }
  189. }, err => {
  190. }, complete => {
  191. })
  192. },
  193. searchByText: function () {
  194. this.loadData()
  195. },
  196. bindInput: function (event) {
  197. var obj = {},
  198. key = event.target.dataset['key'];
  199. // Toast.showToast('success', event.detail.value);
  200. obj[key] = event.detail.value;
  201. this.setData(obj);
  202. console.log(this.data.searchText)
  203. },
  204. loadMore: function () {
  205. const { activeIndex } = this.data;
  206. // const loadDataFn =
  207. // activeIndex == 0 ? this.loadOneToOrderData : this.loadClassroomData;
  208. let loadDataFn;
  209. if ( activeIndex == 0) {
  210. loadDataFn = this.getExhibitionSearch;
  211. }
  212. else if ( activeIndex == 1) {
  213. loadDataFn = this.getPavilionSearch;
  214. }
  215. else {
  216. return
  217. }
  218. if (!this.data.lastPage) {
  219. this.setData({
  220. loading: true
  221. });
  222. console.log(this.data.currentPage + 1)
  223. loadDataFn(this.data.currentPage + 1);
  224. } else {
  225. return;
  226. }
  227. },
  228. onReachBottom: function () {
  229. if (!this.data.loading) {
  230. this.loadMore();
  231. console.log('reach Bottom');
  232. }
  233. },
  234. tabClick: function (e) {
  235. let { activeIndex } = this.data;
  236. this.setData({
  237. loading: true,
  238. activeIndex: e.currentTarget.id
  239. })
  240. this.loadData();
  241. console.log(e.currentTarget.id)
  242. },
  243. /**
  244. * 生命周期函数--监听页面初次渲染完成
  245. */
  246. onReady: function () {
  247. },
  248. /**
  249. * 生命周期函数--监听页面显示
  250. */
  251. onShow: function () {
  252. let { collectedArr, collectedChange } = app.globalData;
  253. let { exhibitionList } = this.data
  254. let isLogin = app.globalData.isLogin;
  255. // this.setData({
  256. // exhibitionList: []
  257. // });
  258. // this.getBanner();
  259. // this.getList(1);
  260. console.log('dwdwasda', isLogin)
  261. this.setData({
  262. isLogin
  263. })
  264. if (collectedChange) {
  265. for (let i = 0; i < exhibitionList.length; i++) {
  266. for (let j = 0; j < collectedArr.length; j++) {
  267. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  268. exhibitionList[i].hasCollect = collectedArr[j].status
  269. if (exhibitionList[i].hasCollect) {
  270. exhibitionList[i].collectionsCount += 1;
  271. }
  272. else {
  273. exhibitionList[i].collectionsCount -= 1;
  274. }
  275. }
  276. }
  277. // console.log(collectedArr.status, collectedArr.collectedId)
  278. }
  279. this.setData({
  280. exhibitionList,
  281. })
  282. }
  283. // app.globalData.clickToSelect = false;
  284. // app.globalData.collectedChange = false;
  285. },
  286. /**
  287. * 生命周期函数--监听页面隐藏
  288. */
  289. onHide: function () {
  290. },
  291. /**
  292. * 生命周期函数--监听页面卸载
  293. */
  294. onUnload: function () {
  295. },
  296. /**
  297. * 页面相关事件处理函数--监听用户下拉动作
  298. */
  299. onPullDownRefresh: function () {
  300. },
  301. /**
  302. * 用户点击右上角分享
  303. */
  304. onShareAppMessage: function () {
  305. }
  306. })