提交 da48954e 编写于 作者: DCloud_JSON's avatar DCloud_JSON

更新文章阅读记录的数据表为read-news-log

上级 d8a3338e
......@@ -180,7 +180,7 @@
},
{
"path": "uni_modules/uni-news-favorite/pages/uni-news-favorite/list",
"path": "pages/ucenter/read-news-log/read-news-log",
"style": {
"navigationBarTitleText": "阅读记录",
"enablePullDownRefresh": false
......
......@@ -50,7 +50,7 @@
import uniShare from 'uni_modules/uni-share/js_sdk/uni-share.js';
const db = uniCloud.database();
const newsFavoriteTable = db.collection('opendb-news-favorite')
const readNewsLog = db.collection('read-news-log')
import {
mapGetters
} from 'vuex';
......@@ -114,35 +114,26 @@
},
methods: {
setFavorite() {
if (!this.hasLogin) return
newsFavoriteTable.where({
article_id: this.id,
user_id: this.userInfo._id
})
.get()
.then(res => {
let value = {
article_id: this.id,
article_title: this.title,
user_id: this.userInfo._id,
update_date: Date.now()
}
if (res.result.data.length == 0) {
return newsFavoriteTable.add(value)
} else {
return newsFavoriteTable.where({
article_id: this.id,
user_id: this.userInfo._id
})
.update(value)
}
})
.then(res => {
// console.log(res);
})
.catch(err => {
console.log(err);
})
if (!this.hasLogin){
return console.log('未登陆用户');
}
let article_id = this.id,
last_time = Date.now();
console.log({article_id,last_time});
readNewsLog.where(`"article_id" == "${article_id}" && "user_id"==$env.uid`)
.update({last_time})
.then(({result:{updated}}) => {
console.log('updated',updated);
if (!updated) {
readNewsLog.add({article_id}).then(e=>{
console.log(e);
}).catch(err => {
console.log(err);
})
}
}).catch(err => {
console.log(err);
})
},
loadData(data) {
//如果上一页未传递标题过来(如搜索直达详情),则从新闻详情中读取标题
......
<template>
<view class="container">
<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" where="user_id == $env.uid"
orderby="update_date desc" collection="opendb-news-favorite" @load="isLoading == false" @error="isLoading == false" :page-size="10">
orderby="last_time desc" collection="read-news-log,opendb-news-articles" @load="isLoading == false" @error="isLoading == false"
field="article_id.title,article_id._id,last_time" :page-size="10">
<view v-if="data && data.length">
<uni-list>
<uni-list-item v-for="(item, index) in data" :key="index" :clickable="true"
@click="handleItemClick(item)">
<view slot="body">
<text>{{item.article_title || item.article_id}}</text>
<uni-dateformat class="article-date" :date="item.update_date" format="yyyy-MM-dd hh:mm"
<view slot="body">
<text>{{item.article_id[0].title}}</text>
<uni-dateformat class="article-date" :date="item.last_time" format="yyyy-MM-dd hh:mm"
:threshold="[0, 0]" />
</view>
</uni-list-item>
</uni-list>
</view>
<view v-else>
<uni-load-more v-if="!loading" status="noMore"></uni-load-more>
</view>
<uni-load-more v-if="data.length>10" :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
</unicloud-db>
......@@ -48,7 +52,7 @@
},
handleItemClick(item) {
uni.navigateTo({
url: '/pages/list/detail?id=' + item.article_id + '&title=' + (item.article_title || '')
url: '/pages/list/detail?id=' + item.article_id[0]._id + '&title=' + item.article_id[0].title
})
}
}
......
......@@ -70,7 +70,7 @@
//#endif
{
title: '阅读过的文章',
to: '/uni_modules/uni-news-favorite/pages/uni-news-favorite/list',
to: '/pages/ucenter/read-news-log/read-news-log',
},
{
title: '我的积分',
......
......@@ -133,7 +133,7 @@ exports.main = async (event, context) => {
break;
case 'register':
let {
username, password, gender, nickname
username, password, nickname
} = params
if (/^1\d{10}$/.test(username)) {
return {
......@@ -150,7 +150,6 @@ exports.main = async (event, context) => {
res = await uniID.register({
username,
password,
gender,
nickname
});
if (res.code === 0) {
......
{
"bsonType": "object",
"required": ["user_id", "article_id"],
"permission": {
"read": "doc.uid == auth.uid",
"create": "auth.uid != null",
"update": "doc.uid == auth.uid"
//"delete": "doc.uid == auth.uid"
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"article_id": {
"bsonType": "string",
"description": "文章id,参考opendb-news-articles表",
"foreignKey": "opendb-news-articles._id"
},
"user_id": {
"bsonType": "string",
"description": "收藏者id,参考uni-id-users表",
"forceDefaultValue": {
"$env": "uid"
},
"foreignKey": "uni-id-users._id"
},
"last_time": { //设计策略是多次看同一个文章只做一次记录,重复看的文章时间更新为当前时间
"bsonType": "timestamp",
"description": "最后一次看的时间",
"defaultValue": {
"$env": "now"
}
}
}
}
\ No newline at end of file
// 表单校验规则由 schema2code 生成,不建议直接修改校验规则,而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
const validator = {
"avatar": {
"rules": [{
"format": "string"
}],
"label": "头像地址"
},
"gender": {
"rules": [{
"format": "int"
},
{
"range": [{
"text": "未知",
"value": 0
},
{
"text": "",
"value": 1
},
{
"text": "",
"value": 2
}
]
}
],
"defaultValue": 0,
"label": "性别"
},
// "mobile": {
// "rules": [{
// "format": "string"
// },
// {
// "pattern": "^\\+?[0-9-]{3,20}$"
// }
// ],
// "label": "手机号码"
// },
"nickname": {
"rules": [{
required: true,
errorMessage: '请输入昵称',
},
{
minLength: 3,
maxLength: 10,
errorMessage: '昵称长度在 {minLength} 到 {maxLength} 个字符',
}
],
"label": "昵称"
}
}
const enumConverter = {
"gender_valuetotext": {
"0": "未知",
"1": "",
"2": ""
}
}
export {
validator,
enumConverter
}
{
"id": "uni-id-users",
"displayName": "uni-id-users",
"version": "1.0.0",
"description": "",
"keywords": [
"uni-id-users"
],
"repository": "",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"category": [
"uniCloud",
"Admin插件"
],
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [
"uni-dateformat",
"uni-badge",
"uni-icons",
"uni-load-more",
"uni-link",
"uni-forms",
"uni-group",
"uni-list",
"uni-fab",
"uni-datetime-picker",
"uni-data-picker",
"uni-file-picker",
"uni-easyinput",
"uni-data-checkbox"
],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"App": {
"app-vue": "y",
"app-nvue": "u"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "u",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "y",
"百度": "y",
"字节跳动": "y",
"QQ": "y"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
<template>
<view class="uni-container">
<uni-forms ref="form" :value="formData" :rules="rules" validate-trigger="submit" err-show-type="toast">
<uni-forms-item name="avatar">
<template>
<view class="avatar-box">
<image class="avatar-img" :src="formData.avatar || '/static/uni-center/logo.png'" @click="chooseImg" mode="aspectFill"></image>
</view>
</template>
</uni-forms-item>
<!-- <uni-forms-item name="gender" label="性别" required>
<uni-data-checkbox v-model="formData.gender" :localdata="formOptions.gender_localdata" />
</uni-forms-item> -->
<uni-forms-item name="mobile" label="手机号码" v-if="formData.mobile">
<uni-easyinput placeholder="手机号码" :disabled="true" v-model="formData.mobile" trim="both" />
</uni-forms-item>
<uni-forms-item name="nickname" label="昵称" required>
<uni-easyinput placeholder="用户昵称" v-model="formData.nickname" trim="both" />
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" @click="submit">修改</button>
</view>
</uni-forms>
</view>
</template>
<script>
import {
validator
} from '../../js_sdk/validator/uni-id-users.js';
import {
mapMutations,
mapGetters
} from 'vuex';
const db = uniCloud.database();
const dbCollectionName = 'uni-id-users';
function getValidator(fields) {
let reuslt = {}
for (let key in validator) {
if (fields.indexOf(key) > -1) {
reuslt[key] = validator[key]
}
}
return reuslt
}
export default {
data() {
return {
formData: {
"avatar": "",
"gender": '',
"mobile": "",
"nickname": ""
},
formOptions: {
"gender_localdata": [{
"text": "未知",
"value": 0
},{
"text": "",
"value": 1
},
{
"text": "",
"value": 2
}
]
},
rules: {
...getValidator(["avatar", "gender", "nickname"])
}
}
},
computed:{
...mapGetters({
userInfo:'user/info'
})
},
onLoad(e) {
let id = uni.getStorageSync('uni_id_uid') || this.userInfo._id;
this.formDataId = id
this.getDetail(id)
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
...mapMutations({
setUserInfo:'user/login'
}),
// 选择图片
chooseImg(){
uni.chooseImage({
count:1,
success:(res)=> {
// 头像剪裁尺寸
let options = {
width:600,
height:600
}
// 剪裁并上传头像
uni.navigateTo({
url:'/pages/ucenter/userinfo/uploadCutImageToUnicloud?path=' + res.tempFilePaths[0] + `&options=${JSON.stringify(options)}`,
animationType:"fade-in",
events:{
uploadAvatarAfter:({url})=>{
this.formData.avatar = url;
this.submitForm({avatar:url});
}
}
});
}
})
},
/**
* 触发表单提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.submit().then((res) => {
console.log(res);
this.submitForm(res)
}).catch((errors) => {
uni.hideLoading()
})
},
submitForm(value) {
// 使用 clientDB 提交数据
db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
uni.showToast({
icon: 'none',
title: '修改成功'
})
this.setUserInfo(value);
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
}).finally(() => {
uni.hideLoading()
})
},
/**
* 获取表单数据
* @param {Object} id
*/
getDetail(id) {
uni.showLoading({
mask: true
})
db.collection(dbCollectionName).doc(id).field('avatar,gender,mobile,nickname').get().then((res) => {
const data = res.result.data[0]
if (data) {
this.formData = data
}
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
}).finally(() => {
uni.hideLoading()
})
}
}
}
</script>
<style>
.uni-container {
padding: 15px;
}
.uni-input-border,
.uni-textarea-border {
width: 100%;
font-size: 14px;
color: #666;
border: 1px #e5e5e5 solid;
border-radius: 5px;
box-sizing: border-box;
}
.uni-input-border {
padding: 0 10px;
height: 35px;
}
.uni-textarea-border {
padding: 10px;
height: 80px;
}
.uni-button-group {
margin-top: 50px;
display: flex;
justify-content: center;
align-items: center;
}
.uni-button {
width: 184px;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
line-height: 1;
margin: 0;
}
.avatar-box{
width: 700rpx;
height: 200rpx;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
justify-content: center;
align-items: center;
}
.avatar-img{
width: 150rpx;
height: 150rpx;
border-radius: 75rpx;
border: #F8F8F8 solid 3px;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册