index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //index.js
  2. //获取应用实例
  3. const { request, serverName } = require('../../utils/services');
  4. const { Toast } = require('../../utils/util.js');
  5. const { defaultImg, noExhibitionImg } = require('../../utils/images');
  6. const WxParse = require('../../common/component/wxParse/wxParse.js');
  7. const QQMapWX = require('../../common/component/mapSDK/qqmap-wx-jssdk.min.js');
  8. var qqmapsdk = "";
  9. const app = getApp();
  10. Page({
  11. data: {
  12. navItem: [{ title: '', tag: 5 }, { title: '附近', tag: "" }, { title: '热门', tag: 0 }, { title: '展览中', tag: 4 }, { title: '即将开始', tag: 2 }, { title: '已结束', tag: 6 }],
  13. indicatorDots: true,
  14. autoplay: true,
  15. interval: 5000,
  16. duration: 1000,
  17. activeIndex: 0,
  18. locationName: "珠海",
  19. animationData: {},
  20. isLike: false,
  21. showConfirm: false,
  22. tag: "",
  23. exhibitionList: [],
  24. commodityImgs: [
  25. '../../imgs/testImg/thumbSmallImg.jpg',
  26. '../../imgs/testImg/thumbSmallImg.jpg',
  27. ],
  28. type: 5,
  29. testImg: '../../imgs/testImg/fdkz.png',
  30. getLocationBtn: false
  31. },
  32. to_search_exhibition: function () {
  33. wx.navigateTo({
  34. url: '../yuezhan/search/index',
  35. success: function (res) { },
  36. fail: function (res) { },
  37. complete: function (res) { },
  38. })
  39. },
  40. onLoad: function () {
  41. this.getList(1)
  42. qqmapsdk = new QQMapWX({
  43. key: '2Z3BZ-H7EWO-F4YWX-SG5JF-2VOK2-S2FUB'
  44. });
  45. this.getLocationName();
  46. var animation = wx.createAnimation({
  47. duration: 400,
  48. timingFunction: 'ease-in-out',
  49. })
  50. this.animation = animation
  51. animation.scale(1.5, 1.5).step();
  52. animation.scale(1, 1).step();
  53. this.setData({
  54. serverName,
  55. animationData: animation.export(),
  56. noExhibitionImg,
  57. defaultImg
  58. })
  59. },
  60. onShareAppMessage: function () {
  61. },
  62. onShow: function () {
  63. let { city, clickToSelect, collectedArr, collectedChange } = app.globalData;
  64. let { activeIndex, exhibitionList } = this.data
  65. if (clickToSelect){
  66. // console.log(city)
  67. this.setData({
  68. locationName: city || "珠海",
  69. exhibitionList: [],
  70. activeIndex:0,
  71. type: 5
  72. });
  73. if (activeIndex == 1) {
  74. this.getNearByList(1);
  75. }
  76. else {
  77. this.getList(1);
  78. }
  79. }
  80. if (collectedChange){
  81. for (let i = 0; i < exhibitionList.length; i++ ){
  82. for (let j = 0; j < collectedArr.length;j++){
  83. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  84. exhibitionList[i].hasCollect = collectedArr[j].status
  85. if (exhibitionList[i].hasCollect) {
  86. exhibitionList[i].collectionsCount += 1;
  87. }
  88. else {
  89. exhibitionList[i].collectionsCount -= 1;
  90. }
  91. if (exhibitionList[i].collectionsCount < 0) {
  92. exhibitionList[i].collectionsCount = 0
  93. }
  94. }
  95. }
  96. // console.log(collectedArr.status, collectedArr.collectedId)
  97. }
  98. this.setData({
  99. exhibitionList
  100. })
  101. }
  102. app.globalData.clickToSelect = false;
  103. // app.globalData.collectedChange = false;
  104. },
  105. addLike: function (e) {
  106. let { type, id, idx } = e.currentTarget.dataset;
  107. console.log(idx, type, id)
  108. let exhibitionList = this.data.exhibitionList;
  109. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  110. console.log('type', type)
  111. Toast.showToast2('loading');
  112. let loginSessionKey = wx.getStorageSync('token') || "";
  113. // if (loginSessionKey){
  114. request['isCollect']({
  115. loginSessionKey,
  116. exhibitionId: id,
  117. type: Number(type),
  118. }, "post", res => {
  119. if (res.data.code > -1) {
  120. for (let i = 0; i < collectedArr.length; i++) {
  121. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  122. console.log("true")
  123. collectedArr[i] = {
  124. collectedId: id,
  125. status: res.data.data.hasCollect,
  126. }
  127. hasItem = false;
  128. }
  129. }
  130. if (hasItem) {
  131. collectedArr.push({
  132. collectedId: id,
  133. status: res.data.data.hasCollect,
  134. })
  135. }
  136. app.globalData.collectedArr = collectedArr;
  137. app.globalData.collectedChange = true;
  138. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  139. this.setData({
  140. exhibitionList: exhibitionList
  141. })
  142. }
  143. }, err => {
  144. }, complete => {
  145. Toast.hideLoading();
  146. })
  147. // }
  148. console.log(this.data.exhibitionList)
  149. },
  150. getLocationName: function () {
  151. // 调用接口
  152. wx.getLocation({
  153. type: 'wgs84',
  154. success: (res) => {
  155. this.setData({
  156. latitude: res.latitude,
  157. longitude: res.longitude
  158. })
  159. //2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析
  160. qqmapsdk.reverseGeocoder({
  161. location: {
  162. latitude: res.latitude,
  163. longitude: res.longitude
  164. },
  165. success: (res) => {
  166. let { city: locationName } = res.result.address_component;
  167. locationName = locationName.substring(0, 2);
  168. console.log(locationName);
  169. app.globalData.city = locationName;
  170. this.setData({
  171. locationName
  172. })
  173. },
  174. fail: function (res) {
  175. console.log(res)
  176. this.setData({
  177. locationName: ""
  178. })
  179. },
  180. complete: function (res) {
  181. console.log(res);
  182. }
  183. });
  184. }
  185. })
  186. },
  187. getList: function (page) {
  188. let { type, locationName } = this.data;
  189. let loginSessionKey = wx.getStorageSync("token");
  190. console.log(loginSessionKey)
  191. console.log(type, locationName);
  192. this.setData({
  193. loading: true
  194. })
  195. request["getExhibitionList"]({
  196. page: page,
  197. type: type,
  198. loginSessionKey,
  199. city: locationName
  200. }, '', res => {
  201. let tempContent = this.data.exhibitionList
  202. ? this.data.exhibitionList
  203. : [];
  204. let { last: lastPage, totalPages, totalElements, content: exhibitionList } = res.data.data;
  205. let { openTime } = exhibitionList;
  206. console.log(res)
  207. this.setData({
  208. currentPage: res.data.data.number + 1,
  209. lastPage,
  210. loading: false,
  211. exhibitionList: tempContent.concat(exhibitionList),
  212. });
  213. console.log(exhibitionList)
  214. wx.stopPullDownRefresh();
  215. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  216. }, err => {
  217. }, complete => {
  218. })
  219. },
  220. getNearByList: function (page) {
  221. let { type, locationName, latitude, longitude } = this.data;
  222. let loginSessionKey = wx.getStorageSync("token");
  223. wx.getSetting({
  224. success:res=>{
  225. if (!res.authSetting['scope.userLocation']) {
  226. Toast.showToast('tip', "无法获取用户位置", () => {
  227. return;
  228. })
  229. this.setData({
  230. getLocationBtn: true
  231. })
  232. }
  233. else {
  234. this.setData({
  235. loading: true,
  236. getLocationBtn: false
  237. })
  238. if(!longitude&&!latitude){
  239. wx.getLocation({
  240. success: res => {
  241. let longitude = (this.longitude = res.longitude);
  242. let latitude = (this.latitude = res.latitude);
  243. console.log("dqwdwd6w5d465ad4w")
  244. request["getNearByList"]({
  245. page: page,
  246. loginSessionKey,
  247. city: locationName,
  248. lng: longitude,
  249. lat: latitude
  250. }, '', res => {
  251. let tempContent = this.data.exhibitionList
  252. ? this.data.exhibitionList
  253. : [];
  254. let { last: lastPage, totalPages, content: exhibitionList } = res.data.data;
  255. let { openTime } = exhibitionList;
  256. console.log(res)
  257. this.setData({
  258. currentPage: res.data.data.number + 1,
  259. lastPage,
  260. loading: false,
  261. longitude,
  262. latitude,
  263. exhibitionList: tempContent.concat(exhibitionList),
  264. });
  265. console.log(exhibitionList)
  266. wx.stopPullDownRefresh();
  267. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  268. }, err => {
  269. }, complete => {
  270. })
  271. },
  272. })
  273. }else{
  274. request["getNearByList"]({
  275. page: page,
  276. loginSessionKey,
  277. city: locationName,
  278. lng: longitude,
  279. lat: latitude
  280. }, '', res => {
  281. let tempContent = this.data.exhibitionList
  282. ? this.data.exhibitionList
  283. : [];
  284. let { last: lastPage, totalPages, content: exhibitionList } = res.data.data;
  285. let { openTime } = exhibitionList;
  286. console.log(res)
  287. this.setData({
  288. currentPage: res.data.data.number + 1,
  289. lastPage,
  290. loading: false,
  291. exhibitionList: tempContent.concat(exhibitionList),
  292. });
  293. console.log(exhibitionList)
  294. wx.stopPullDownRefresh();
  295. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  296. }, err => {
  297. }, complete => {
  298. })
  299. }
  300. }
  301. }
  302. })
  303. },
  304. closeDialog: function () {
  305. this.setData({
  306. showConfirm: false
  307. })
  308. },
  309. loadMore: function () {
  310. let { activeIndex } = this.data
  311. if (!this.data.lastPage) {
  312. console.log(this.data.currentPage + 1)
  313. if (activeIndex == 1) {
  314. this.getNearByList(this.data.currentPage + 1);
  315. }
  316. else {
  317. this.getList(this.data.currentPage + 1);
  318. }
  319. } else {
  320. return;
  321. }
  322. },
  323. onReachBottom: function () {
  324. if (!this.data.loading) {
  325. this.loadMore();
  326. console.log('reach Bottom');
  327. }
  328. },
  329. tabClick: function (e) {
  330. // console.log(e.currentTarget.dataset)
  331. this.setData({
  332. exhibitionList: [],
  333. activeIndex: e.currentTarget.id,
  334. type: e.currentTarget.dataset.tag,
  335. currentPage: 1
  336. });
  337. if (e.currentTarget.id == 1) {
  338. this.getNearByList(1);
  339. }
  340. else {
  341. this.getList(1);
  342. }
  343. },
  344. to_search: function () {
  345. wx.navigateTo({
  346. url: './search/index',
  347. success: function (res) { },
  348. fail: function (res) { },
  349. complete: function (res) { },
  350. })
  351. }
  352. })