index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../services/user.js');
  4. import header from './data'
  5. import {
  6. Router
  7. } from '../../utils/router.js'
  8. //获取应用实例
  9. Router({
  10. data: {
  11. currentBanner: 0,
  12. banners: [],
  13. showSearch: false,
  14. activeType: -1,
  15. header: [],
  16. noRecord: '',
  17. hideMask: false,
  18. keyword: '',
  19. url: 'https://www.4dkankan.com/eShopMobile.html?m=',
  20. totalPages: 1,
  21. currentPage: 1,
  22. size: 10,
  23. loading: false,
  24. brandList: []
  25. },
  26. swiperChange(e) {
  27. this.setData({
  28. currentBanner: e.detail.current
  29. })
  30. },
  31. onShareAppMessage: function () {
  32. return {
  33. path: '/pages/index/index'
  34. }
  35. },
  36. getImgHeight() {
  37. const aw = 750
  38. const ah = 480
  39. this.setData({
  40. imgHeight: ah * wx.getSystemInfoSync().windowWidth / aw
  41. })
  42. this.navTop = this.data.imgHeight * 0.87
  43. this.bgTop = this.data.imgHeight * 0.6
  44. this.setData({
  45. navTop: this.bgTop
  46. })
  47. wx.createSelectorQuery().select('#index-nav').boundingClientRect( (rect) => {
  48. console.error(rect.top)
  49. this.setData({
  50. navPosTop: rect.top
  51. })
  52. }).exec()
  53. // console.log(this.navTop, this.bgTop)
  54. },
  55. onPageScroll(e) {
  56. if (e.scrollTop < 0) return;
  57. this.setData({
  58. bgTop: e.scrollTop > this.bgTop ? this.bgTop : e.scrollTop,
  59. bgIsTop: e.scrollTop > this.navTop,
  60. isFixed: e.scrollTop > this.data.navPosTop
  61. })
  62. },
  63. onPullDownRefresh() {
  64. this.setData({
  65. brandList: this.data.brandList.slice(0, 10),
  66. currentPage: 1
  67. });
  68. this.getBrandList(1, false, true);
  69. getApp().onPullDownRefresh()
  70. },
  71. onReachBottom() {
  72. if (!this.data.loading) {
  73. this.loadMore();
  74. console.log('reach down')
  75. }
  76. },
  77. clickSwiper() {
  78. wx.navigateTo({
  79. url: this.data.banners[this.data.currentBanner].redirectUrl,
  80. })
  81. },
  82. loadMore: function () {
  83. if (this.data.currentPage < this.data.totalPages) {
  84. console.log(this.data.currentPage + 1)
  85. this.getBrandList(this.data.currentPage + 1);
  86. } else {
  87. return;
  88. }
  89. },
  90. getBrandList(page, isSearch = false, refresh = false) {
  91. this.setData({
  92. loading: true
  93. })
  94. let tempContent = this.data.brandList ?
  95. this.data.brandList : [];
  96. let {
  97. activeType,
  98. size,
  99. keyword,
  100. } = this.data
  101. util.request(api.IndexList, {
  102. type: activeType,
  103. page,
  104. name: keyword,
  105. size
  106. })
  107. .then(res => {
  108. res.data.data = res.data.data.map(item => {
  109. if (item.pvTotalNum) {
  110. item.pvTotalNum = item.pvTotalNum >= 10000 ?
  111. (
  112. item.pvTotalNum > 990000 ? 99 + 'W+' :
  113. (item.pvTotalNum / 10000).toFixed(1) + 'W'
  114. ) :
  115. item.pvTotalNum
  116. } else {
  117. item.pvTotalNum = 0
  118. }
  119. return item
  120. })
  121. if (isSearch) {
  122. // if (res.data.data.length <= 0) {
  123. // this.setData({
  124. // noRecord: true
  125. // });
  126. // }
  127. // else {
  128. wx.pageScrollTo({
  129. scrollTop: 0
  130. })
  131. this.setData({
  132. brandList: res.data.data,
  133. currentPage: res.data.currentPage,
  134. totalPages: res.data.totalPages,
  135. loading: false,
  136. noRecord: false,
  137. showSearch: false
  138. });
  139. // }
  140. } else if (refresh) {
  141. this.setData({
  142. brandList: res.data.data,
  143. currentPage: res.data.currentPage,
  144. totalPages: res.data.totalPages,
  145. loading: false
  146. });
  147. } else {
  148. this.setData({
  149. brandList: tempContent.concat(res.data.data),
  150. currentPage: res.data.currentPage,
  151. totalPages: res.data.totalPages,
  152. loading: false
  153. });
  154. }
  155. });
  156. },
  157. inputChange: function (e) {
  158. let val = e.detail.value
  159. this.setData({
  160. keyword: val,
  161. noRecord: false
  162. })
  163. this.search()
  164. },
  165. handleScroll(ev) {
  166. this.setData({
  167. isFixed: ev.detail.isFixed
  168. })
  169. },
  170. search: function () {
  171. let {
  172. keyword
  173. } = this.data
  174. this.getBrandList(1, true)
  175. // if (this.brandList.length <= 0) {
  176. // this.setData({
  177. // noRecord: true
  178. // })
  179. // }
  180. // else {
  181. // this.setData({
  182. // activeType: -1,
  183. // noRecord: false,
  184. // showSearch: false
  185. // })
  186. // }
  187. },
  188. inputFocus: function () {
  189. },
  190. gotoCateg() {
  191. wx.navigateTo({
  192. url: '/pages/ncategory/category?id=search',
  193. })
  194. },
  195. tapHeaderBar(e) {
  196. // let { id } = e.currentTarget.dataset
  197. // wx.navigateTo({
  198. // url: '/pages/ncategory/category?id=' + id,
  199. // })
  200. let {
  201. id
  202. } = e.currentTarget.dataset
  203. this.setData({
  204. activeType: id,
  205. showSearch: false,
  206. keyword: '',
  207. brandList: [],
  208. currentPage: 1
  209. })
  210. this.getBrandList(1)
  211. },
  212. tabShow() {
  213. this.setData({
  214. showSearch: true
  215. })
  216. },
  217. tabHide() {
  218. this.setData({
  219. showSearch: false,
  220. noRecord: false,
  221. keyword: ''
  222. })
  223. },
  224. onLoad: async function (options) {
  225. getApp().checkNetStatu();
  226. // this.checkNetStatu()
  227. this.setData({
  228. imgServer: util.imgServer
  229. })
  230. this.getBrandList(1)
  231. this.getBanner()
  232. this.getType()
  233. this.getImgHeight()
  234. // wx.setBackgroundColor({
  235. // backgroundColor: '#ff7701'
  236. // })
  237. },
  238. async getBanner() {
  239. let {
  240. data
  241. } = await util.request(api.HomeBanner)
  242. this.setData({
  243. banners: data
  244. })
  245. },
  246. async getType() {
  247. let {
  248. data
  249. } = await util.request(api.IndexTypeList)
  250. let items = [{
  251. name: '推荐',
  252. id: -1
  253. }]
  254. data.forEach(item => {
  255. Object.keys(item).forEach(key => {
  256. items.push({
  257. name: key,
  258. id: item[key]
  259. })
  260. })
  261. })
  262. this.setData({
  263. header: items
  264. })
  265. },
  266. onReady: function () {
  267. // 页面渲染完成
  268. },
  269. onShow: function (options) {
  270. if (global.type) {
  271. this.tapHeaderBar({
  272. currentTarget: {
  273. dataset: this.data.header[0]
  274. }
  275. })
  276. global.type = null
  277. }
  278. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  279. this.getTabBar().setData({
  280. selected: 0
  281. })
  282. }
  283. getApp().updateCardCount()
  284. // 页面显示
  285. },
  286. onHide: function () {
  287. // 页面隐藏
  288. },
  289. onUnload: function () {
  290. // 页面关闭
  291. },
  292. gotoWV: function (event) {
  293. let {
  294. id
  295. } = event.currentTarget.dataset
  296. wx.navigateTo({
  297. url: `/pages/webview/index?id=${id}`,
  298. // url: `/pages/share/share?id=${id}`,
  299. })
  300. }
  301. })