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

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

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