index.ts 7.9 KB

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