Skip to content

Commit

Permalink
fix(client_mobile_miniapp): 接口调整,审核中,预计下午恢复使用 🚑 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkvirus committed Feb 15, 2019
1 parent 53b5eae commit 8449e6e
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 48 deletions.
2 changes: 1 addition & 1 deletion client_mobile_miniapp/app.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"pages": [
"pages/index/index",
"pages/classify/classify",
"pages/feedback/feedback",
"pages/read/read",
"pages/search/search",
"pages/classify/classify",
"pages/intro/intro"
],
"window": {
Expand Down
1 change: 1 addition & 0 deletions client_mobile_miniapp/pages/classify/classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Page({
*/
handleSearchClassify: function () {
var that = this
console.log('classify')
// 查询数据
request({
url: api.GET_CLASSIFY,
Expand Down
11 changes: 8 additions & 3 deletions client_mobile_miniapp/pages/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Page({
success (res) {
var result = res.data
if (result.code === '0000' && result.data && result.data.id) {
wx.setStorageSync('user_id', res.id)
that.setData({ userInfo: res })
that.handleSearchShelf(res.id)
wx.setStorageSync('user_id', result.data.id)
that.setData({ userInfo: result.data })
that.handleSearchShelf(result.data.id)
return;
}

Expand All @@ -87,6 +87,11 @@ Page({
* 查询书架里小说
*/
handleSearchShelf: function (userId) {
if (!userId) {
this.setData({ isLoading: false })
return;
}

var that = this
request({
url: api.GET_SHELF,
Expand Down
81 changes: 69 additions & 12 deletions client_mobile_miniapp/pages/read/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,76 @@ Page({
handleShowChapter: function () {
// 当前阅读章节
var { detail, chapter } = this.data
var index = chapter.all.findIndex(item => item.name === detail.title.trim())
var page = chapter.page.find(item => index <= item.end && index >= item.start)
var list = chapter.all.slice(page.start, page.end)
var id = list.find(item => item.name === detail.title.trim()).id

this.handleHideMenus()
this.setData({
isShowChapter: true,
'chapter.isShowPage': false,
'chapter.list': list,
'chapter.defaultPage': page,
'chapter.id': id || '',
})
if (chapter.all.length === 0) { // 查询为空
console.log(11)
var that = this
console.log(that.data.novelUrl)

request({
url: api.GET_CHAPTER,
data: { url: that.data.novelUrl },
}).then(function (res) {
// 拼接分页数据 288 => 2、88,,,,2880 => 28、80
var integer = Math.floor(res.length / 100) // 整数部分
var remainder = res.length % 100 // 余数
var page = []
for (var i = 1; i <= integer; i++) {
var obj = {}
obj.id = String(i)
obj.start = (i - 1) * 100
obj.end = i * 100
obj.desc = `${(i - 1) * 100 + 1}-${i * 100}`
page.push(obj)
}
page.push({
id: String(integer + 1),
desc: `${integer * 100 + 1}-${integer * 100 + remainder}`,
start: integer * 100,
end: integer * 100 + remainder,
})

that.setData({
'chapter.list': res.slice(0, 100),
'chapter.page': page,
'chapter.all': res,
'chapter.defaultPage': page[0],
})

try {
var index = chapter.all.findIndex(item => item.name === detail.title.trim())
var page = chapter.page.find(item => index <= item.end && index >= item.start)
var list = chapter.all.slice(page.start, page.end)
var id = list.find(item => item.name === detail.title.trim()).id
} catch (e) {

}

this.handleHideMenus()
this.setData({
isShowChapter: true,
'chapter.isShowPage': false,
'chapter.list': list,
'chapter.defaultPage': page,
'chapter.id': id || '',
})
})

} else {
var index = chapter.all.findIndex(item => item.name === detail.title.trim())
var page = chapter.page.find(item => index <= item.end && index >= item.start)
var list = chapter.all.slice(page.start, page.end)
var id = list.find(item => item.name === detail.title.trim()).id

this.handleHideMenus()
this.setData({
isShowChapter: true,
'chapter.isShowPage': false,
'chapter.list': list,
'chapter.defaultPage': page,
'chapter.id': id || '',
})
}
},

/**
Expand Down
3 changes: 3 additions & 0 deletions client_mobile_miniapp/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// var apiPrefix = 'https://novel.dkvirus.top/api/test'
var apiPrefix = 'https://novel.dkvirus.top/api/v2'
// var apiPrefix = 'http://localhost:3000/api/test'

module.exports = {
GET_TOKEN: `${apiPrefix}/gysw/oauth/token`, // 获取 token

GET_CLASSIFY: `${apiPrefix}/gysw/novel/classify`, // 查询小说分类
GET_CHAPTER: `${apiPrefix}/gysw/novel/chapter`, // 根据小说url查章节
GET_CONTENT: `${apiPrefix}/gysw/novel/content`, // 根据章节url查内容
Expand Down
56 changes: 44 additions & 12 deletions client_mobile_miniapp/utils/request.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
var api = require('./api.js')

/**
* edit by dkvirus:
* 封装 wx 原生请求方法,统一打印响应日志
*/
function request(opts) {
// 判断 token 是否存在
var token = wx.getStorageSync('token')
if (!token) {
// 请求 token 先
var username = wx.getStorageSync('openId')
wx.request({
url: api.GET_TOKEN,
method: 'POST',
data: {
client_type: 'OPENID',
username,
},
success(res) {
var token = res.data.data.token
wx.setStorageSync('token', token)
request(opts)
},
})
}
if (!opts.url) return;
var { method = 'GET' } = opts
var { url, data = {} } = handleParam(opts)

wx.showNavigationBarLoading()
wx.showLoading()
if (!opts.url) return;
opts.method = opts.method || 'GET'
opts.data = opts.data || {}
var { url, data } = handleParam(opts)

return new Promise(function (resolve, reject) {
wx.request({
url,
data,
method: opts.method,
method,
header: {
Authorization: `Bearer ${token}`,
},
success: function (res) {
console.log(`请求【${opts.method} ${url}】成功,响应数据:%o`, res)
console.log(`请求【${method} ${url}】成功,响应数据:%o`, res)
wx.hideNavigationBarLoading();
wx.hideLoading();
if (res.data.code === '0000') {
resolve(res.data.data)
} else if (res.data.code === '9999' && res.data.message.indexOf('认证') !== -1) {
wx.setStorageSync('token', '')
request(opts).then(function (res2) {
if (res2.data.code === '0000') {
resolve(res2.data.data)
}
})
} else {
reject(res)
}
Expand All @@ -41,19 +72,20 @@ function request(opts) {
* 处理之后返回值 /user/1/stop/2
*/
function handleParam(opts) {
var urlArr = opts.url.split('/')
var data = JSON.parse(JSON.stringify(opts.data))
var { url, data = {} } = opts
var urlArr = url.split('/')
var dataCopy = JSON.parse(JSON.stringify(data))
urlArr = urlArr.map(item => {
if (item.charAt(0) === ':') {
var field = String(item).substring(1)
item = encodeURIComponent(data[field])
delete data[field]
item = encodeURIComponent(dataCopy[field])
delete dataCopy[field]
}
return item
})

var url = urlArr.join('/');
return { url, data };
url = urlArr.join('/');
return { url, data: dataCopy };
}

module.exports = {
Expand Down
61 changes: 41 additions & 20 deletions client_mobile_rn/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ class Home extends React.Component {
],
selectedTab: 'shelf'
};
this.handleGoSearch = this.handleGoSearch.bind(this);
}

// 顶部标题栏
static navigationOptions = {
headerTitle: <Text>书架</Text>,
headerRight: <Image source={require('../images/setting.png')} style={{ width: 40, height: 40 }}></Image>
headerTitle: <Text style={{ marginLeft: 20, fontSize: 18 }}>书架</Text>,
headerRight: (
<View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
<TouchableNativeFeedback onPress={() => this.handleGoSearch()}>
<Image source={require('../images/plus.png')} style={{ width: 40, height: 40 }}></Image>
</TouchableNativeFeedback>

<TouchableNativeFeedback onPress={() => console.log('xxx')}>
<Image source={require('../images/setting.png')} style={{ width: 30, height: 30, marginRight: 20 }}></Image>
</TouchableNativeFeedback>
</View>
)
};

/**
Expand Down Expand Up @@ -62,18 +73,15 @@ class Home extends React.Component {
selectedIcon={require('../images/book-shelf-selected.png')}
selected={true}
>
<View style={styles.container}>
<View style={styles.body}>
<View style={styles.body}>
<View style={styles.bodyWrap}>
<TouchableNativeFeedback onPress={() => this.handleGoRead('https://www.biquge5200.cc/84_84888/152458677.html')}>
<View style={styles.item}>
<Text>都市阴阳师</Text>
<Text>巫九</Text>
</View>
</TouchableNativeFeedback>

<TouchableNativeFeedback onPress={() => this.handleGoSearch()}>
<View style={styles.item}>
<Text>+</Text>
<Image style={styles.cover} source={require('../images/cover.png')}></Image>
<View style={styles.right}>
<Text style={styles.bookname}>都市阴阳师</Text>
<Text>巫九</Text>
</View>
</View>
</TouchableNativeFeedback>
</View>
Expand All @@ -94,14 +102,8 @@ class Home extends React.Component {
}

const styles = StyleSheet.create({
container: {
// backgroundColor: '#ccc',
height: '100%',
},
body: {
width: '100%',
display: 'flex',
alignItems: 'center',
paddingTop: 10,
},
bodyWrap: {
Expand All @@ -110,13 +112,32 @@ const styles = StyleSheet.create({
borderRadius: 4,
paddingVertical: 20,
paddingHorizontal: 10,
display: 'flex',
},
item: {
height: 100,
width: 100,
marginBottom: 20,
borderRadius: 4,
borderWidth: 0.5,
borderBottomWidth: 0.5,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
},
cover: {
height: 90,
width: 70,
marginLeft: 30,
marginRight: 30,
},
right: {
flex: 1,
height: 70,
display: 'flex',
justifyContent: 'space-around',
},
bookname: {
fontSize: 20,
fontWeight: '700',
},
tabbar: {
position: 'absolute',
Expand Down

0 comments on commit 8449e6e

Please sign in to comment.