index.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import GoodApi from '../../../services/good'
  2. import Navi from '../../../utils/navi'
  3. const tabList = [
  4. {
  5. label: '商品',
  6. value: 'product',
  7. },
  8. {
  9. label: '灵感',
  10. value: 'inspire',
  11. },
  12. ]
  13. const searchOptions = {
  14. option1: [
  15. {
  16. label: '全部商品',
  17. value: 'all',
  18. },
  19. {
  20. label: '新款商品',
  21. value: 'news',
  22. },
  23. {
  24. label: '收藏商品',
  25. value: 'likes',
  26. },
  27. ],
  28. option2: [
  29. {
  30. label: '默认排序',
  31. value: 'default',
  32. },
  33. {
  34. label: '综合排序',
  35. value: 'avarage',
  36. },
  37. {
  38. label: '销量排序',
  39. value: 'sell',
  40. },
  41. {
  42. label: '最新上架',
  43. value: 'newest',
  44. },
  45. ],
  46. value1: 'all',
  47. value2: 'default',
  48. }
  49. // pages/goods/list/index.ts
  50. Page<
  51. {
  52. isScene: boolean
  53. borderlist: WxOptionLongExtraDto[]
  54. filterOptions: WxGetFilterOptionOutputDto[]
  55. tabList: typeof tabList
  56. searchOptions: typeof searchOptions
  57. filterVisible: boolean
  58. tagvalues: Record<string, any[]>
  59. albumInput: WxGetAlbumInput
  60. albumTotal: number
  61. albums: WxCateAlbumItemDto[]
  62. loadMoreStatus: number
  63. view: 'large' | 'list'
  64. },
  65. {
  66. tempValues: Record<string, any[]>
  67. onBorderChange(
  68. e: WechatMiniprogram.CustomEvent<{ idx: number; item: WxOptionLongExtraDto }>,
  69. ): void
  70. onGoodChange(e: WechatMiniprogram.CustomEvent<{ label: string; value: string }>): void
  71. onSellChange(e: WechatMiniprogram.CustomEvent<{ label: string; value: string }>): void
  72. onFilterClick(): void
  73. onFilterClose(): void
  74. onMove(): void
  75. onSearchReset(): void
  76. onSearchConfirm(): void
  77. load(rest?: boolean): void
  78. loadBorder(): void
  79. loadOptions(): void
  80. onChangeView(): void
  81. }
  82. >({
  83. /**
  84. * 页面的初始数据
  85. */
  86. data: {
  87. isScene: false,
  88. borderlist: [],
  89. filterOptions: [],
  90. tabList,
  91. searchOptions,
  92. filterVisible: false,
  93. tagvalues: {},
  94. albumInput: {
  95. sortkey: 'default',
  96. cateId: -1,
  97. skipCount: 0,
  98. maxResultCount: 10,
  99. },
  100. albumTotal: 0,
  101. albums: [],
  102. loadMoreStatus: 0,
  103. view: 'list',
  104. },
  105. load(rest = false) {
  106. this.setData({
  107. loadMoreStatus: 1,
  108. })
  109. const api = this.data.isScene ? GoodApi.GetCateScene : GoodApi.GetCateAlbum
  110. api(this.data.albumInput).then((rsp) => {
  111. if (rsp.result) {
  112. const result = rsp.result as any
  113. const { items = [] } = result
  114. const nowItems = rest ? items : this.data.albums.concat(items)
  115. this.setData({
  116. albums: nowItems as any,
  117. albumTotal: result.totalCount || 0,
  118. loadMoreStatus: nowItems.length === result.totalCount ? 2 : 0,
  119. })
  120. if (result.name && result.name.length > 0) {
  121. wx.setNavigationBarTitle({
  122. title: `${result.name}挂画`,
  123. })
  124. }
  125. }
  126. })
  127. },
  128. loadBorder() {
  129. GoodApi.GetBorderList().then((rsp) => {
  130. if (rsp.result && rsp.result.length > 0) {
  131. this.setData({
  132. borderlist: [
  133. {
  134. label: '全部',
  135. value: -1,
  136. extra: '/assets/cate/all.png',
  137. } as WxOptionLongExtraDto,
  138. ].concat(rsp.result),
  139. })
  140. }
  141. })
  142. },
  143. loadOptions() {
  144. GoodApi.GetFilterOption().then((rsp) => {
  145. if (rsp.result && rsp.result.length > 0) {
  146. this.setData({
  147. filterOptions: rsp.result,
  148. })
  149. }
  150. })
  151. },
  152. /**
  153. * 生命周期函数--监听页面加载
  154. */
  155. onLoad(query: Record<string, string>) {
  156. const { cate = -1 } = query
  157. this.setData(
  158. {
  159. albumInput: {
  160. ...this.data.albumInput,
  161. cateId: Number(cate),
  162. borderId: -1,
  163. },
  164. },
  165. () => {
  166. this.loadBorder()
  167. this.loadOptions()
  168. this.load(true)
  169. },
  170. )
  171. },
  172. /**
  173. * 生命周期函数--监听页面初次渲染完成
  174. */
  175. onReady() {},
  176. /**
  177. * 生命周期函数--监听页面显示
  178. */
  179. onShow() {
  180. // this.getTabBar().init()
  181. },
  182. /**
  183. * 生命周期函数--监听页面隐藏
  184. */
  185. onHide() {},
  186. /**
  187. * 生命周期函数--监听页面卸载
  188. */
  189. onUnload() {},
  190. /**
  191. * 页面相关事件处理函数--监听用户下拉动作
  192. */
  193. onPullDownRefresh() {
  194. if (this.data.albums.length < this.data.albumTotal) {
  195. this.setData(
  196. {
  197. albumInput: {
  198. ...this.data.albumInput,
  199. skipCount: 0,
  200. },
  201. },
  202. () => {
  203. this.load(true)
  204. },
  205. )
  206. }
  207. },
  208. /**
  209. * 页面上拉触底事件的处理函数
  210. */
  211. onReachBottom() {
  212. console.log(this.data.albums.length, this.data.albumTotal)
  213. if (this.data.loadMoreStatus === 0 && this.data.albums.length < this.data.albumTotal) {
  214. this.setData(
  215. {
  216. albumInput: {
  217. ...this.data.albumInput,
  218. skipCount: this.data.albumInput.skipCount + 10,
  219. },
  220. },
  221. () => {
  222. this.load()
  223. },
  224. )
  225. }
  226. },
  227. /**
  228. * 用户点击右上角分享
  229. */
  230. onShareAppMessage() {},
  231. onBorderChange(e) {
  232. const { item } = e.detail
  233. this.setData(
  234. {
  235. albumInput: {
  236. ...this.data.albumInput,
  237. borderId: item.value,
  238. skipCount: 0,
  239. },
  240. },
  241. () => {
  242. this.load(true)
  243. },
  244. )
  245. },
  246. onGoodChange(e) {
  247. const { value } = e.detail
  248. console.log(value)
  249. },
  250. onSellChange(e) {
  251. const { value } = e.detail
  252. this.setData(
  253. {
  254. 'albumInput.sortkey': value,
  255. },
  256. () => {
  257. this.load(true)
  258. },
  259. )
  260. },
  261. onFilterClick() {
  262. this.setData({
  263. filterVisible: true,
  264. })
  265. },
  266. onChangeView() {
  267. this.setData({
  268. view: this.data.view === 'list' ? 'large' : 'list',
  269. })
  270. },
  271. onFilterClose() {
  272. this.setData({
  273. tagvalues: this.data.tagvalues,
  274. filterVisible: false,
  275. })
  276. },
  277. onMove() {
  278. return false
  279. },
  280. //@ts-ignore
  281. onFilterValueChanged(v: WechatMiniprogram.CustomEvent<{ value: Record<string, any[]> }>) {
  282. this.tempValues = v.detail.value
  283. },
  284. onSearchReset() {
  285. this.setData({
  286. tagvalues: {},
  287. })
  288. this.tempValues = {}
  289. },
  290. onSearchConfirm() {
  291. let tags: number[] = []
  292. const tagvalues = this.tempValues
  293. Object.keys(tagvalues).forEach((s) => {
  294. if (tagvalues[s] && tagvalues[s].length > 0) {
  295. tags = tags.concat(tagvalues[s])
  296. }
  297. })
  298. this.setData(
  299. {
  300. tagvalues: this.tempValues,
  301. filterVisible: false,
  302. albumInput: {
  303. ...this.data.albumInput,
  304. tags: tags.join(','),
  305. },
  306. },
  307. () => {
  308. this.load(true)
  309. },
  310. )
  311. console.log(this.tempValues)
  312. },
  313. gotoGoodsDetail(e: WechatMiniprogram.CustomEvent<{ goods: WxCateAlbumItemDto; index: number }>) {
  314. const { goods } = e.detail
  315. Navi.navigateTo({
  316. url: '/pages/goods/detail/index?id=' + goods.id + '&cateid=' + this.data.albumInput.cateId,
  317. })
  318. console.log('click', e)
  319. },
  320. gosearch() {
  321. console.log('navi')
  322. wx.navigateTo({
  323. url: '/pages/search/search',
  324. })
  325. },
  326. research(keyword: string) {
  327. this.setData(
  328. {
  329. albumInput: {
  330. ...this.data.albumInput,
  331. filterText: keyword,
  332. skipCount: 0,
  333. },
  334. },
  335. () => {
  336. this.load(true)
  337. },
  338. )
  339. },
  340. tabChangeHandle(e: WechatMiniprogram.CustomEvent<{ label: string; value: string }>) {
  341. this.setData(
  342. {
  343. isScene: e.detail.label === '灵感',
  344. },
  345. () => {
  346. this.load(true)
  347. },
  348. )
  349. },
  350. })