Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
add api/api.js, utils/constants.js and use es6
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Nov 12, 2016
1 parent b68caa5 commit e3e30ab
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 357 deletions.
62 changes: 62 additions & 0 deletions 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
}
4 changes: 1 addition & 3 deletions app.js
@@ -1,3 +1 @@
App({

})
App({})
21 changes: 9 additions & 12 deletions 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 })
}
}
})
Expand Down
70 changes: 30 additions & 40 deletions pages/index/index.js
@@ -1,65 +1,55 @@
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({
current: volsLength
})
wx.navigateTo({
url: '../history/history?page=index',
success: function () {
that.setData({
success: () => {
this.setData({
current: volsLength - 1
})
}
Expand Down
33 changes: 12 additions & 21 deletions 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
})
Expand Down
78 changes: 33 additions & 45 deletions 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(/<br>/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 })
}
})
3 changes: 2 additions & 1 deletion pages/music/detail/detail.wxml
@@ -1,3 +1,4 @@
<view class="container">
<include src="../templates/musicItem.wxml"/>
<import src="../templates/musicItem.wxml"/>
<template is="music-item" data="{{item: detail}}"/>
</view>
@@ -1,9 +1,9 @@
<view class="music-about">
<view class="music-content-header">
<text class="content-label">歌曲信息</text>
<view class="content-links">
<image class="link" src="../../../image/music_story_default.png" mode="aspectFill" bindtap="showStory"></image>
<image class="link" src="../../../image/music_lyric_default.png" mode="aspectFill" bindtap="showLyric"></image>
<view class="content-links" data-id="{{item.id}}" bindtap="switchContent">
<image class="link" src="../../../image/music_story_default.png" mode="aspectFill" data-type="story"></image>
<image class="link" src="../../../image/music_lyric_default.png" mode="aspectFill" data-type="lyric"></image>
<image class="link" src="../../../image/music_about_selected.png" mode="aspectFill"></image>
</view>
</view>
Expand Down
@@ -1,10 +1,10 @@
<view class="music-lyric">
<view class="music-content-header">
<text class="content-label">歌词</text>
<view class="content-links">
<image class="link" src="../../../image/music_story_default.png" mode="aspectFill" bindtap="showStory"></image>
<view class="content-links" data-id="{{item.id}}" bindtap="switchContent">
<image class="link" src="../../../image/music_story_default.png" mode="aspectFill" data-type="story"></image>
<image class="link" src="../../../image/music_lyric_selected.png" mode="aspectFill"></image>
<image class="link" src="../../../image/music_about_default.png" mode="aspectFill" bindtap="showAbout"></image>
<image class="link" src="../../../image/music_about_default.png" mode="aspectFill" data-type="about"></image>
</view>
</view>
<view class="music-content-body">
Expand Down

0 comments on commit e3e30ab

Please sign in to comment.