diff --git a/api/api.js b/api/api.js new file mode 100644 index 0000000..ff61303 --- /dev/null +++ b/api/api.js @@ -0,0 +1,62 @@ +const host = 'http://v3.wufazhuce.com:8000' +const wxRequest = (params, url) => { + wx.showToast({ + title: '加载中', + icon: 'loading' + }) + wx.request({ + url: url, + method: params.method || 'GET', + data: params.data || {}, + header: { + 'Content-Type': 'application/json' + }, + success: (res) => { + params.success && params.success(res) + wx.hideToast() + }, + fail: (res) => { + params.fail && params.fail(res) + }, + complete: (res) => { + params.complete && params.complete(res) + } + }) +} + +// Index +const getVolById = (params) => wxRequest(params, host + '/api/hp/detail/' + params.query.id) +const getVolIdList = (params) => wxRequest(params, host + '/api/hp/idlist/0') +const getVolsByMonth = (params) => wxRequest(params, host + '/api/hp/bymonth/' + params.query.month) +const getVolDetailById = (params) => wxRequest(params, host + '/api/hp/detail/' + params.query.id) + +// Reading +const getCarousel = (params) => wxRequest(params, host + '/api/reading/carousel') +const getLastArticles = (params) => wxRequest(params, host + '/api/reading/index') +const getEssayById = (params) => wxRequest(params, host + '/api/essay/' + params.query.id) +const getSerialById = (params) => wxRequest(params, host + '/api/serialcontent/' + params.query.id) +const getQuestionById = (params) => wxRequest(params, host + '/api/question/' + params.query.id) +const getArticlesByMonth = (params) => { + wxRequest(params, host + '/api/' + params.query.type + '/bymonth/' + params.query.month) +} + +// Music +const getMusicIdList = (params) => wxRequest(params, host + '/api/music/idlist/0') +const getMusicsByMonth = (params) => wxRequest(params, host + '/api/music/bymonth/' + params.query.month) +const getMusicDetailById = (params) => wxRequest(params, host + '/api/music/detail/' + params.query.id) + +module.exports = { + getVolById, + getVolIdList, + getVolsByMonth, + getVolDetailById, + getCarousel, + getLastArticles, + getEssayById, + getSerialById, + getQuestionById, + getArticlesByMonth, + getMusicIdList, + getMusicsByMonth, + getMusicDetailById +} diff --git a/app.js b/app.js index 618e1d2..bca1d70 100644 --- a/app.js +++ b/app.js @@ -1,3 +1 @@ -App({ - -}) \ No newline at end of file +App({}) \ No newline at end of file diff --git a/pages/index/detail/detail.js b/pages/index/detail/detail.js index 2fa70be..5346220 100644 --- a/pages/index/detail/detail.js +++ b/pages/index/detail/detail.js @@ -1,23 +1,20 @@ -var util = require('../../../utils/util.js') +import api from '../../../api/api.js' +import util from '../../../utils/util.js' + Page({ data: { detail: [] }, onLoad: function (options) { - var that = this - - wx.request({ - url: 'http://v3.wufazhuce.com:8000/api/hp/detail/' + options.id, - header: { - 'Content-Type': 'application/json' + api.getVolDetailById({ + query: { + id: options.id }, - success: function (res) { + success: (res) => { if (res.data.res === 0) { - var detail = res.data.data + let detail = res.data.data detail.hp_makettime = util.formatMakettime(detail.hp_makettime) - that.setData({ - detail: detail - }) + this.setData({ detail }) } } }) diff --git a/pages/index/index.js b/pages/index/index.js index 017fe8b..f4eeabe 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,56 +1,46 @@ -var util = require('../../utils/util.js') +import api from '../../api/api.js' +import util from '../../utils/util.js' + Page({ data: { vols: [], current: 0 }, onLoad: function () { - var that = this - wx.request({ - url: 'http://v3.wufazhuce.com:8000/api/hp/idlist/0', - header: { - 'Content-Type': 'application/json' - }, - success: function(res) { + api.getVolIdList({ + success: (res) => { if (res.data.res === 0) { - var idList = res.data.data.slice(0, 9) - idList.map(function (id) { - that.getVols(id) - }) + let idList = res.data.data + this.getVols(idList) } } }) }, - getVols: function (id) { - var that = this - wx.request({ - url: 'http://v3.wufazhuce.com:8000/api/hp/detail/' + id, - header: { - 'Content-Type': 'application/json' - }, - success: function(res) { - if (res.data.res === 0) { - var vol = res.data.data - var vols = that.data.vols - - vol.date = new Date(vol.hp_makettime) - vol.hp_makettime = util.formatMakettime(vol.hp_makettime) - vols.push(vol) - vols.sort(function (a, b) { - return b.date - a.date - }) + getVols: function (idList) { + let vols = this.data.vols - that.setData({ - vols: vols - }) + if (idList.length > 0) { + api.getVolById({ + query: { + id: idList.shift() + }, + success: (res) => { + if (res.data.res === 0) { + let vol = res.data.data + + vol.hp_makettime = util.formatMakettime(vol.hp_makettime) + vols.push(vol) + } + this.getVols(idList) } - } - }) + }) + } else { + this.setData({ vols }) + } }, handleChange: function (e) { - var that = this - var current = e.detail.current - var volsLength = this.data.vols.length + let current = e.detail.current + let volsLength = this.data.vols.length if (current === volsLength) { this.setData({ @@ -58,8 +48,8 @@ Page({ }) wx.navigateTo({ url: '../history/history?page=index', - success: function () { - that.setData({ + success: () => { + this.setData({ current: volsLength - 1 }) } diff --git a/pages/index/monthly/monthly.js b/pages/index/monthly/monthly.js index c40c104..da45a27 100644 --- a/pages/index/monthly/monthly.js +++ b/pages/index/monthly/monthly.js @@ -1,38 +1,29 @@ -var util = require('../../../utils/util.js') +import api from '../../../api/api.js' +import util from '../../../utils/util.js' + Page({ data: { monthly: [] }, onLoad: function (options) { - var that = this - - wx.showToast({ - title: '加载中', - icon: 'loading' - }) - wx.request({ - url: 'http://v3.wufazhuce.com:8000/api/hp/bymonth/' + options.month, - header: { - 'Content-Type': 'application/json' + api.getVolsByMonth({ + query: { + month: options.month }, - success: function (res) { + success: (res) => { if (res.data.res === 0) { - var monthly = res.data.data + let monthly = res.data.data - monthly.map(function (vol) { - vol.date = new Date(vol.hp_makettime) + monthly.map((vol) => { vol.hp_makettime = util.formatMakettime(vol.hp_makettime) }) - that.setData({ - monthly: monthly - }) - wx.hideToast() + this.setData({ monthly }) } } - }) + }) }, handleTap: function (e) { - var id = e.currentTarget.dataset.id + let id = e.currentTarget.dataset.id wx.navigateTo({ url: '../detail/detail?id=' + id }) diff --git a/pages/music/detail/detail.js b/pages/music/detail/detail.js index d06d091..f3e519e 100644 --- a/pages/music/detail/detail.js +++ b/pages/music/detail/detail.js @@ -1,75 +1,63 @@ -var util = require('../../../utils/util.js') +import { + MUSIC_PALY_IMG, + MUSIC_PAUSE_IMG +} from '../../../utils/constants.js' +import api from '../../../api/api.js' +import util from '../../../utils/util.js' + Page({ data: { - item: [], - playing: false, - playImg: '../../../image/music_play.png', - content: 'story' + detail: [], + playing: false }, onLoad: function (options) { - var that = this - - wx.request({ - url: 'http://v3.wufazhuce.com:8000/api/music/detail/' + options.id, - header: { - 'Content-Type': 'application/json' + api.getMusicDetailById({ + query: { + id: options.id }, - success: function (res) { + success: (res) => { if (res.data.res === 0) { - var item = res.data.data + let detail = res.data.data + + detail.playImg = MUSIC_PALY_IMG + detail.contentType = 'story' + detail.story = util.filterHTML(detail.story) + detail.maketime = util.formatMakettime(detail.maketime) - item.story = item.story.replace(/
/g,"") - item.maketime = util.formatMakettime(item.maketime) - that.setData({ - item: item - }) + this.setData({ detail }) } } }) }, togglePlay: function (e) { - var music = this.data.item - var playing = this.data.playing + let detail = this.data.detail + let playing = this.data.playing if (!playing) { - var playImg = '../../../image/music_pause.png' - this.playMusic(music) + detail.playImg = MUSIC_PAUSE_IMG + this.playMusic(detail) } else { - var playImg = '../../../image/music_play.png' + detail.playImg = MUSIC_PALY_IMG this.pauseMusic() } playing = !playing - this.setData({ - playing: playing, - playImg: playImg - }) + this.setData({ detail, playing }) }, playMusic: function (music) { wx.playBackgroundAudio({ dataUrl: music.music_id, - title: music.title, - fail: function () { - wx.showToast({ title: '播放失败' }) - } + title: music.title }) }, pauseMusic: function () { wx.pauseBackgroundAudio() }, - showStory: function () { - this.setData({ - content: 'story' - }) - }, - showLyric: function () { - this.setData({ - content: 'lyric' - }) - }, - showAbout: function () { - this.setData({ - content: 'about' - }) + switchContent: function (e) { + let type = e.target.dataset.type + let detail = this.data.detail + + detail.contentType = type + this.setData({ detail }) } }) \ No newline at end of file diff --git a/pages/music/detail/detail.wxml b/pages/music/detail/detail.wxml index ce610f1..74f02cd 100644 --- a/pages/music/detail/detail.wxml +++ b/pages/music/detail/detail.wxml @@ -1,3 +1,4 @@ - + +