提交 b005f4c3 编写于 作者: taohebin@dcloud.io's avatar taohebin@dcloud.io

1. websocket替换地址 2. list示例修改

上级 21f4f7c9
...@@ -540,7 +540,7 @@ ...@@ -540,7 +540,7 @@
"path" : "pages/component/list/list", "path" : "pages/component/list/list",
"style" : "style" :
{ {
"navigationBarTitleText": "", "navigationBarTitleText": "列表到详情示例",
"enablePullDownRefresh": false "enablePullDownRefresh": false
} }
...@@ -558,7 +558,7 @@ ...@@ -558,7 +558,7 @@
"path" : "pages/component/list/detail/detail", "path" : "pages/component/list/detail/detail",
"style" : "style" :
{ {
"navigationBarTitleText": "", "navigationBarTitleText": "详情示例",
"enablePullDownRefresh": false "enablePullDownRefresh": false
} }
......
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
title: '连接中...' title: '连接中...'
}) })
uni.connectSocket({ uni.connectSocket({
url: 'ws://192.168.12.106:8080/ws', url: 'ws://websocket.dcloud.net.cn',
success(res) { success(res) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意 // 这里是接口调用成功的回调,不是连接成功的回调,请注意
}, },
......
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
title: '连接中...' title: '连接中...'
}) })
this.socketTask = uni.connectSocket({ this.socketTask = uni.connectSocket({
url: 'ws://192.168.12.106:8080/ws', url: 'ws://websocket.dcloud.net.cn',
success(res) { success(res) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意 // 这里是接口调用成功的回调,不是连接成功的回调,请注意
}, },
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
this.pageVisible = true; this.pageVisible = true;
this.post_id = event["post_id"] ?? ""; this.post_id = event["post_id"] ?? "";
this.cover = event["cover"] ?? ""; this.cover = event["cover"] ?? "";
this.title = event["title"] ?? ""; this.title = event["title"] ?? "";
this.getDetail(); this.getDetail();
}, },
onUnload() { onUnload() {
...@@ -36,11 +36,12 @@ ...@@ -36,11 +36,12 @@
getDetail() { getDetail() {
uni.request({ uni.request({
url: 'https://unidemo.dcloud.net.cn/api/news/36kr/' + this.post_id, url: 'https://unidemo.dcloud.net.cn/api/news/36kr/' + this.post_id,
success: (data) => { success: (data) => {
if (data.statusCode == 200) { if(this.pageVisible){
const result = data.data as UTSJSONObject if (data.statusCode == 200) {
// var htmlString = result["content"].replace(/\\/g, "").replace(/<img/g, "<img style=\"display:none;\""); const result = data.data as UTSJSONObject
this.htmlNodes = result["content"] as string; this.htmlNodes = result["content"] as string;
}
} }
}, },
fail: () => { fail: () => {
......
<template> <template>
<view style="width: 100%;height: 100%;"> <view style="width: 100%;height: 100%;">
<view class="banner"> <view class="banner" @click="bannerClick(banner)">
<image class="banner-img" :src="banner.cover"></image> <image class="banner-img" :src="banner.cover"></image>
<text class="banner-title">{{ banner.title }}</text> <text class="banner-title">{{ banner.title }}</text>
</view> </view>
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
import JSONArray from 'com.alibaba.fastjson.JSONArray'; import JSONArray from 'com.alibaba.fastjson.JSONArray';
type Banner = { type Banner = {
cover: string | null, cover: string | null,
title: string | null, title: string | null,
post_id: string | null
} }
type Item = { type Item = {
author_name: string, author_name: string,
...@@ -50,8 +51,6 @@ ...@@ -50,8 +51,6 @@
banner: {} as Banner, banner: {} as Banner,
listData: [] as Item[], listData: [] as Item[],
last_id: '', last_id: '',
reload: false,
status: 'more',
contentText: { contentText: {
contentdown: '上拉加载更多', contentdown: '上拉加载更多',
contentrefresh: '加载中', contentrefresh: '加载中',
...@@ -63,7 +62,7 @@ ...@@ -63,7 +62,7 @@
onLoad() { onLoad() {
this.pageVisible = true; this.pageVisible = true;
this.getBanner(); this.getBanner();
this.getList(); this.getList();
}, },
onUnload() { onUnload() {
this.pageVisible = false; this.pageVisible = false;
...@@ -76,15 +75,17 @@ ...@@ -76,15 +75,17 @@
uni.request({ uni.request({
url: 'https://unidemo.dcloud.net.cn/api/banner/36kr', url: 'https://unidemo.dcloud.net.cn/api/banner/36kr',
data: data, data: data,
success: data => { success: data => {
// uni.stopPullDownRefresh(); if(this.pageVisible){
this.refresherTriggered = false this.refresherTriggered = false
if (data.statusCode == 200) { if (data.statusCode == 200) {
let result = data.data as UTSJSONObject; let result = data.data as UTSJSONObject;
this.banner = { this.banner = {
cover: result["cover"] as string, cover: result["cover"] as string,
title: result["title"] as string title: result["title"] as string,
} as Banner; post_id: result["post_id"] as string
} as Banner;
}
} }
}, },
fail: (e) => { fail: (e) => {
...@@ -93,32 +94,34 @@ ...@@ -93,32 +94,34 @@
}); });
}, },
getList() { getList() {
let data = { let url = "https://unidemo.dcloud.net.cn/api/news?column=id,post_id,title,author_name,cover,published_at";
column: 'id,post_id,title,author_name,cover,published_at', //需要的字段名 if (this.last_id != "") {
}; const minId = (this.last_id).toInt();
if (this.last_id != "") { const time = new Date().getTime() + '';
//说明已有数据,目前处于上拉加载 const pageSize = 10;
this.status = 'loading'; url = url + "&minId=" + minId + "&time=" + time + "&pageSize=" + pageSize;
data["minId"] = this.last_id; }
data["time"] = new Date().getTime() + '';
data["pageSize"] = 10;
}
uni.request({ uni.request({
url: 'https://unidemo.dcloud.net.cn/api/news', url: url,
data: data, method:"GET",
success: (data) => { success: (data) => {
if (data.statusCode == 200) { if(this.pageVisible){
const result = data.data as UTSJSONObject if (data.statusCode == 200) {
let list = this.setTime(result); const result = data.data as UTSJSONObject
this.listData = this.reload ? list : this.listData.concat(list); let list = this.setTime(result);
this.last_id = list[list.length - 1].id + ""; this.listData = list.concat(this.listData);
this.reload = false; console.log("after ",this.listData.size);
this.refresherTriggered = false this.last_id = listData[0].id + "";
this.refresherTriggered = false;
}
} }
}, },
fail: (res) => { fail: (res) => {
console.log('fail', res); if(this.pageVisible){
this.refresherTriggered = false console.log('fail', res);
this.refresherTriggered = false
}
} }
}); });
}, },
...@@ -130,12 +133,21 @@ ...@@ -130,12 +133,21 @@
uni.navigateTo({ uni.navigateTo({
url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
}); });
},
bannerClick(e:Banner){
const detail = e;
const post_id = detail.post_id;
const cover = detail.cover;
const title = detail.title;
uni.navigateTo({
url: 'pages/component/list/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
});
}, },
setTime(items: UTSJSONObject): Item[] { setTime(items: UTSJSONObject): Item[] {
let newItems = [] as Item[]; let newItems = [] as Item[];
if (items.isJSONArray()) { if (items.isJSONArray()) {
const array = items.toJSONObject() as JSONArray; const array = items.toJSONObject() as JSONArray;
for (let i = 0; i < array.size - 1; i++) { for (let i = array.size - 1; i >= 0; i--) {
const e = array.get(i) as JSONObject; const e = array.get(i) as JSONObject;
newItems.push({ newItems.push({
author_name: e["author_name"] as string, author_name: e["author_name"] as string,
...@@ -150,11 +162,11 @@ ...@@ -150,11 +162,11 @@
return newItems; return newItems;
}, },
onRefresherrefresh() { onRefresherrefresh() {
this.refresherTriggered = true if(this.pageVisible){
this.reload = true; this.refresherTriggered = true
this.last_id = ''; this.getBanner();
this.getBanner(); this.getList();
this.getList(); }
} }
} }
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册