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. this.setData({
  49. navPosTop: rect.top
  50. })
  51. }).exec()
  52. // console.log(this.navTop, this.bgTop)
  53. },
  54. onPageScroll(e) {
  55. if (e.scrollTop < 0) return;
  56. this.setData({
  57. bgTop: e.scrollTop > this.bgTop ? this.bgTop : e.scrollTop,
  58. bgIsTop: e.scrollTop > this.navTop,
  59. isFixed: e.scrollTop > this.data.navPosTop
  60. })
  61. },
  62. onPullDownRefresh() {
  63. this.setData({
  64. brandList: this.data.brandList.slice(0, 10),
  65. currentPage: 1
  66. });
  67. this.getBrandList(1, false, true);
  68. getApp().onPullDownRefresh()
  69. },
  70. onReachBottom() {
  71. if (!this.data.loading) {
  72. this.loadMore();
  73. console.log('reach down')
  74. }
  75. },
  76. clickSwiper() {
  77. wx.navigateTo({
  78. url: this.data.banners[this.data.currentBanner].redirectUrl,
  79. })
  80. },
  81. loadMore: function () {
  82. if (this.data.currentPage < this.data.totalPages) {
  83. console.log(this.data.currentPage + 1)
  84. this.getBrandList(this.data.currentPage + 1);
  85. } else {
  86. return;
  87. }
  88. },
  89. getBrandList(page, isSearch = false, refresh = false) {
  90. this.setData({
  91. loading: true
  92. })
  93. let tempContent = this.data.brandList ?
  94. this.data.brandList : [];
  95. let {
  96. activeType,
  97. size,
  98. keyword,
  99. } = this.data
  100. util.request(api.IndexList, {
  101. type: activeType,
  102. page,
  103. name: keyword,
  104. size
  105. })
  106. .then(res => {
  107. res.data.data = res.data.data.map(item => {
  108. if (item.pvTotalNum) {
  109. item.pvTotalNum = item.pvTotalNum >= 10000 ?
  110. (
  111. item.pvTotalNum > 990000 ? 99 + 'W+' :
  112. (item.pvTotalNum / 10000).toFixed(1) + 'W'
  113. ) :
  114. item.pvTotalNum
  115. } else {
  116. item.pvTotalNum = 0
  117. }
  118. return item
  119. })
  120. if (isSearch) {
  121. // if (res.data.data.length <= 0) {
  122. // this.setData({
  123. // noRecord: true
  124. // });
  125. // }
  126. // else {
  127. wx.pageScrollTo({
  128. scrollTop: 0
  129. })
  130. this.setData({
  131. brandList: res.data.data,
  132. currentPage: res.data.currentPage,
  133. totalPages: res.data.totalPages,
  134. loading: false,
  135. noRecord: false,
  136. showSearch: false
  137. });
  138. // }
  139. } else if (refresh) {
  140. this.setData({
  141. brandList: res.data.data,
  142. currentPage: res.data.currentPage,
  143. totalPages: res.data.totalPages,
  144. loading: false
  145. });
  146. } else {
  147. this.setData({
  148. brandList: tempContent.concat(res.data.data),
  149. currentPage: res.data.currentPage,
  150. totalPages: res.data.totalPages,
  151. loading: false
  152. });
  153. }
  154. });
  155. },
  156. inputChange: function (e) {
  157. let val = e.detail.value
  158. this.setData({
  159. keyword: val,
  160. noRecord: false
  161. })
  162. this.search()
  163. },
  164. handleScroll(ev) {
  165. this.setData({
  166. isFixed: ev.detail.isFixed
  167. })
  168. },
  169. search: function () {
  170. let {
  171. keyword
  172. } = this.data
  173. this.getBrandList(1, true)
  174. // if (this.brandList.length <= 0) {
  175. // this.setData({
  176. // noRecord: true
  177. // })
  178. // }
  179. // else {
  180. // this.setData({
  181. // activeType: -1,
  182. // noRecord: false,
  183. // showSearch: false
  184. // })
  185. // }
  186. },
  187. inputFocus: function () {
  188. },
  189. gotoCateg() {
  190. wx.navigateTo({
  191. url: '/pages/ncategory/category?id=search',
  192. })
  193. },
  194. tapHeaderBar(e) {
  195. // let { id } = e.currentTarget.dataset
  196. // wx.navigateTo({
  197. // url: '/pages/ncategory/category?id=' + id,
  198. // })
  199. let {
  200. id
  201. } = e.currentTarget.dataset
  202. this.setData({
  203. activeType: id,
  204. showSearch: false,
  205. keyword: '',
  206. brandList: [],
  207. currentPage: 1
  208. })
  209. this.getBrandList(1)
  210. },
  211. tabShow() {
  212. this.setData({
  213. showSearch: true
  214. })
  215. },
  216. tabHide() {
  217. this.setData({
  218. showSearch: false,
  219. noRecord: false,
  220. keyword: ''
  221. })
  222. },
  223. onLoad: async function (options) {
  224. getApp().checkNetStatu();
  225. // this.checkNetStatu()
  226. this.setData({
  227. imgServer: util.imgServer
  228. })
  229. this.getBrandList(1)
  230. this.getBanner()
  231. this.getType()
  232. this.getImgHeight()
  233. // wx.setBackgroundColor({
  234. // backgroundColor: '#ff7701'
  235. // })
  236. },
  237. async getBanner() {
  238. let {
  239. data
  240. } = await util.request(api.HomeBanner)
  241. this.setData({
  242. banners: data
  243. })
  244. },
  245. async getType() {
  246. let {
  247. data
  248. } = await util.request(api.IndexTypeList)
  249. let items = [{
  250. name: '推荐',
  251. id: -1
  252. }]
  253. data.forEach(item => {
  254. Object.keys(item).forEach(key => {
  255. items.push({
  256. name: key,
  257. id: item[key]
  258. })
  259. })
  260. })
  261. this.setData({
  262. header: items
  263. })
  264. },
  265. onReady: function () {
  266. // 页面渲染完成
  267. },
  268. onShow: function (options) {
  269. console.log('*---???????')
  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. })