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

新增注册成功后的逻辑

上级 03468546
module.exports = {
"h5":{
"url":"https://static-76ce2c5e-31c7-4d81-8fcf-ed1541ecbc6e.bspapp.com" ,// 前端网页托管的域名
"openApp":{
"url":"https://static-76ce2c5e-31c7-4d81-8fcf-ed1541ecbc6e.bspapp.com",// 前端网页托管的域名
"openApp":{ // 在h5端全局悬浮引导用户下载app的功能 更多自定义要求在/common/openApp.js中修改
"openUrl":'https://sj.qq.com/myapp/detail.htm?apkName=com.tencent.android.qqdownloader&info=6646FD239A6EBA9E2DEE5DFC7E18D867',
"appname": 'base-app',
"logo": './static/logo.png',
......
......@@ -10,15 +10,14 @@ export default function() {
`<img src="${CONFIG_OPEN.logo}" style="width: 2rem;height: 2rem;border-radius: 3px;">` : '';
let openApp = document.createElement("div");
openApp.id = 'openApp';
openApp.style =
'position: fixed;top: 0;left: 0;right: 0;z-index: 999;width: 100%;height: 45px;display: flex;flex-direction: row;justify-content: space-between;align-items: center;box-sizing: border-box;padding: 0 0.5rem;background-color: #FFFFFF;'
'position: fixed;background:#FFFFFF;box-shadow: #eeeeee 1px 1px 9px; ;top: 0;left: 0;right: 0;z-index: 999;width: 100%;height: 45px;display: flex;flex-direction: row;justify-content: space-between;align-items: center;box-sizing: border-box;padding: 0 0.5rem;'
openApp.innerHTML = `
<div style="display: flex;flex-direction: row;justify-content: flex-start;align-items: center;">
${openLogo}
<div style="padding-left: 0.3rem;font-size: 0.25rem;">${CONFIG_OPEN.appname || ''}</div>
<div style="padding-left: 0.3rem;font-size: 12px;">${CONFIG_OPEN.appname || ''}</div>
</div>
<div class="openBtn" style="padding: 5px;font-size: 0.3rem;border-radius: 2px;border: 1px solid #007AFF;color: #007AFF;">下载app</div>
<div class="openBtn" style="padding: 5px;font-size:12px;border-radius: 2px;border: 1px solid #007AFF;color: #007AFF;">下载app</div>
`;
document.body.insertBefore(openApp, document.body.firstChild);
document.body.style = 'height:calc(100% - 45px); margin-top:45px;';
......
......@@ -330,6 +330,6 @@
/* #ifdef APP-NVUE */
border-radius: 10rpx;
/* #endif */
background-color: #DD524D;
background-color: #DD524D;
}
</style>
{
"bsonType": "object",
"permission": {
"read": "doc.is_reply == false",
"create": true,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"user_id": {
"bsonType": "string",
"description": "留言反馈用户ID\/回复留言用户ID,参考uni-id-users表",
"foreignKey": "uni-id-users._id",
"forceDefaultValue": {
"$env": "uid"
}
},
"title": {
"description": "反馈标题(用于列表展示)"
},
"ip": {
"forceDefaultValue": {
"$env": "clientIP"
}
},
"create_date": {
"bsonType": "timestamp",
"description": "留言时间\/回复留言时间",
"forceDefaultValue": {
"$env": "now"
}
},
"content": {
"bsonType": "string",
"description": "留言内容\/回复内容",
"trim": "right"
},
"imgs": {
"bsonType": "array",
"description": "图片列表"
},
"is_reply": {
"bsonType": "bool",
"description": "是否是回复类型"
},
"feedback_id": {
"bsonType": "string",
"description": "被回复留言ID"
},
"contact": {
"bsonType": "string",
"description": "联系人",
"trim": "both"
},
"mobile": {
"bsonType": "string",
"description": "联系电话",
"trim": "both"
},
"reply_count": {
"bsonType": "int",
"description": "被回复条数"
}
}
}
......@@ -3,23 +3,23 @@ let uniID = require('uni-id')
const uniCaptcha = require('uni-captcha')
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
/**
* UNI_WYQ:这里的uniID换成新的,保证多人访问不会冲突
*/
uniID = uniID.createInstance({
context
})
//event为客户端上传的参数
console.log('event : ' + JSON.stringify(event))
let params = event.params || {}
exports.main = async (event, context) => {
/**
* UNI_WYQ:这里的uniID换成新的,保证多人访问不会冲突
*/
uniID = uniID.createInstance({
context
})
//event为客户端上传的参数
console.log('event : ' + JSON.stringify(event))
let params = event.params || {}
//防止黑客恶意破解登陆,连续登陆失败一定次数后,需要用户提供验证码
const getNeedCaptcha = async () => {
const getNeedCaptcha = async () => {
//当用户最近“2小时内(recordDate)”登陆失败达到2次(recordSize)时。要求用户提交验证码
const now = Date.now(),
recordDate = 120 * 60 * 1000,
recordSize = 2;
const now = Date.now(),
recordDate = 120 * 60 * 1000,
recordSize = 2;
const uniIdLogCollection = db.collection('uni-id-log')
let recentRecord = await uniIdLogCollection.where({
deviceId: params.deviceId || context.DEVICEID,
......@@ -31,14 +31,26 @@ exports.main = async (event, context) => {
.get();
return recentRecord.data.filter(item => item.state === 0).length === recordSize;
}
//注册成功后为用户执行相关操作,如创建该用户的积分表等
function registerSuccess(uid) {
await db.collection('uni-id-scores').add({
user_id: uid,
score: 1,
type: 1,
balance: 1,
comment: "",
create_date: Date.now()
})
}
//设置某些模块不需要token(也就是登陆成功后)才能操作,如果需要token就获取当前操作账户的uid
let noCheckAction = [
'register', 'checkToken','login', 'logout', 'sendSmsCode',
'createCaptcha', 'verifyCaptcha','refreshCaptcha', 'inviteLogin',
'login_by_weixin','login_by_univerify','login_by_apple','loginBySms','resetPwdBySmsCode'
]
let payload;
'register', 'checkToken', 'login', 'logout', 'sendSmsCode',
'createCaptcha', 'verifyCaptcha', 'refreshCaptcha', 'inviteLogin',
'login_by_weixin', 'login_by_univerify', 'login_by_apple', 'loginBySms', 'resetPwdBySmsCode'
]
let payload;
console.log(event.action);
if (!noCheckAction.includes(event.action)) {
if (!event.uniIdToken) {
......@@ -46,93 +58,117 @@ exports.main = async (event, context) => {
code: 403,
msg: '缺少token'
}
}
}
payload = await uniID.checkToken(event.uniIdToken)
if (payload.code && payload.code > 0) {
return payload
}
params.uid = payload.uid
}
//记录成功登陆的日志
const loginLog = async (res = {}, type = 'login') => {
const now = Date.now()
const uniIdLogCollection = db.collection('uni-id-log')
let logData = {
deviceId: params.deviceId || context.DEVICEID,
ip: params.ip || context.CLIENTIP,
type,
ua: context.CLIENTUA,
create_date: now
};
Object.assign(logData,
res.code === 0 ? {
user_id: res.uid,
state: 1
} : {
state: 0
})
return uniIdLogCollection.add(logData)
}
//记录成功登陆的日志
const loginLog = async (res = {}, type = 'login') => {
const now = Date.now()
const uniIdLogCollection = db.collection('uni-id-log')
let logData = {
deviceId: params.deviceId || context.DEVICEID,
ip: params.ip || context.CLIENTIP,
type,
ua: context.CLIENTUA,
create_date: now
};
Object.assign(logData,
res.code === 0 ? {
user_id: res.uid,
state: 1
} : {
state: 0
})
return uniIdLogCollection.add(logData)
}
let res = {}
switch (event.action) {
case 'register':
let {username,password,gender,nickname} = params
if(/^1\d{10}$/.test(username)){
return {
code: 401,
msg: '用户名不能是手机号'
}
};
if(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(username)){
return {
code: 401,
msg: '用户名不能是邮箱'
}
}
res = await uniID.register({username,password,gender,nickname,password});
case 'register':
let {
username, password, gender, nickname
} = params
if (/^1\d{10}$/.test(username)) {
return {
code: 401,
msg: '用户名不能是手机号'
}
};
if (/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(username)) {
return {
code: 401,
msg: '用户名不能是邮箱'
}
}
res = await uniID.register({
username,
password,
gender,
nickname,
password
});
if (res.code === 0) {
registerSuccess(res.uid)
}
break;
case 'login':
let passed = false;
let needCaptcha = await getNeedCaptcha();
if (needCaptcha) {
res = await uniCaptcha.verify(params)
if (res.code === 0) passed = true;
}
if (!needCaptcha || passed) {
res = await uniID.login({
...params,
queryField: ['username', 'email', 'mobile']
res = await uniID.login({
...params,
queryField: ['username', 'email', 'mobile']
});
await loginLog(res);
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
needCaptcha = await getNeedCaptcha();
}
res.needCaptcha = needCaptcha;
break;
case 'login_by_weixin':
res = await uniID.loginByWeixin(params);
await uniID.updateUser({
uid: params.uid,
username:"微信用户"
break;
case 'login_by_weixin':
res = await uniID.loginByWeixin(params);
await uniID.updateUser({
uid: res.uid,
username: "微信用户"
});
res.userInfo.username = "微信用户"
loginLog(res)
break;
case 'login_by_univerify':
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
res.userInfo.username = "微信用户"
loginLog(res)
break;
case 'login_by_univerify':
res = await uniID.loginByUniverify(params)
break;
case 'login_by_apple':
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
break;
case 'login_by_apple':
res = await uniID.loginByApple(params)
loginLog(res)
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
loginLog(res)
break;
case 'checkToken':
res = await uniID.checkToken(event.uniIdToken);
......@@ -166,7 +202,10 @@ exports.main = async (event, context) => {
code,
type: params.type,
templateId
})
})
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
break;
case 'loginBySms':
if (!params.code) {
......@@ -181,7 +220,10 @@ exports.main = async (event, context) => {
msg: '手机号码填写错误'
}
}
res = await uniID.loginBySms(params)
res = await uniID.loginBySms(params)
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
loginLog(res)
break;
case 'inviteLogin':
......@@ -194,28 +236,34 @@ exports.main = async (event, context) => {
res = await uniID.loginBySms({
...params,
type: 'register'
})
break;
case 'resetPwdBySmsCode':
if (!params.code) {
return {
code: 500,
msg: '请填写验证码'
}
}
if (!/^1\d{10}$/.test(params.mobile)) {
return {
code: 500,
msg: '手机号码填写错误'
}
}
let loginBySmsRes = await uniID.loginBySms(params)
console.log(loginBySmsRes);
if(loginBySmsRes.code === 0){
res = await uniID.resetPwd({password:params.password,"uid":loginBySmsRes.uid})
}else{
return loginBySmsRes
}
})
if (res.code == 0 && res.type == 'register') {
registerSuccess(res.uid)
}
break;
case 'resetPwdBySmsCode':
if (!params.code) {
return {
code: 500,
msg: '请填写验证码'
}
}
if (!/^1\d{10}$/.test(params.mobile)) {
return {
code: 500,
msg: '手机号码填写错误'
}
}
let loginBySmsRes = await uniID.loginBySms(params)
console.log(loginBySmsRes);
if (loginBySmsRes.code === 0) {
res = await uniID.resetPwd({
password: params.password,
"uid": loginBySmsRes.uid
})
} else {
return loginBySmsRes
}
break;
case 'getInviteCode':
res = await uniID.getUserInfo({
......@@ -241,7 +289,7 @@ exports.main = async (event, context) => {
break;
case 'refreshCaptcha':
res = await uniCaptcha.refresh(params)
break;
break;
default:
res = {
code: 403,
......
<template>
<view class="uni-container">
<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
<uni-forms-item name="article_id" label="" required>
<uni-easyinput placeholder="文章id,参考opendb-news-articles表" v-model="formData.article_id" />
</uni-forms-item>
<uni-forms-item name="create_date" label="">
<uni-datetime-picker return-type="timestamp" :value="formData.create_date" />
</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/opendb-news-favorite.js';
const db = uniCloud.database();
const dbCollectionName = 'opendb-news-favorite';
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: {
"article_id": "",
"create_date": null
},
formOptions: {},
rules: {
...getValidator(["article_id","create_date"])
}
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 触发表单提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.submit().then((res) => {
this.submitForm(res)
}).catch((errors) => {
uni.hideLoading()
})
},
submitForm(value) {
// 使用 clientDB 提交数据
db.collection(dbCollectionName).add(value).then((res) => {
uni.showToast({
icon: 'none',
title: '新增成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).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;
}
.uni-button {
width: 184px;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
line-height: 1;
margin: 0;
}
</style>
<template>
<view class="container">
<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="options" collection="opendb-news-favorite" field="article_id,create_date" :where="queryWhere" :getone="true" :manual="true">
<view v-if="error">{{error.message}}</view>
<view v-else-if="loading">
<uni-load-more :contentText="loadMore" status="loading"></uni-load-more>
</view>
<view v-else-if="data">
<view>
<text>article_id:</text>
<text>{{data.article_id}}</text>
</view>
<view>
<text>create_date:</text>
<uni-dateformat :date="data.create_date" :threshold="[0, 0]" />
</view>
</view>
</unicloud-db>
<view class="btns">
<button type="primary" @click="handleUpdate">修改</button>
<button type="warn" class="btn-delete" @click="handleDelete">删除</button>
</view>
</view>
</template>
<script>
// 由schema2code生成,包含校验规则和enum静态数据
import { enumConverter } from '../../js_sdk/validator/opendb-news-favorite.js';
export default {
data() {
return {
queryWhere: '',
loadMore: {
contentdown: '',
contentrefresh: '',
contentnomore: ''
},
options: {
// 将scheme enum 属性静态数据中的value转成text
...enumConverter
}
}
},
onLoad(e) {
this._id = e.id
},
onReady() {
if (this._id) {
this.queryWhere = '_id=="' + this._id + '"'
}
},
methods: {
handleUpdate() {
// 打开修改页面
uni.navigateTo({
url: './edit?id=' + this._id,
events: {
// 监听修改页面成功修改数据后, 刷新当前页面数据
refreshData: () => {
this.$refs.udb.loadData({
clear: true
})
}
}
})
},
handleDelete() {
this.$refs.udb.remove(this._id, {
success: (res) => {
// 删除数据成功后跳转到list页面
uni.navigateTo({
url: './list'
})
}
})
}
}
}
</script>
<style>
.container {
padding: 10px;
}
.btns {
margin-top: 10px;
display: flex;
flex-direction: row;
}
.btns button {
flex: 1;
}
.btn-delete {
margin-left: 10px;
}
</style>
<template>
<view class="uni-container">
<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
<uni-forms-item name="article_id" label="" required>
<uni-easyinput placeholder="文章id,参考opendb-news-articles表" v-model="formData.article_id" />
</uni-forms-item>
<uni-forms-item name="create_date" label="">
<uni-datetime-picker return-type="timestamp" :value="formData.create_date" />
</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/opendb-news-favorite.js';
const db = uniCloud.database();
const dbCollectionName = 'opendb-news-favorite';
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: {
"article_id": "",
"create_date": null
},
formOptions: {},
rules: {
...getValidator(["article_id","create_date"])
}
}
},
onLoad(e) {
const id = e.id
this.formDataId = id
this.getDetail(id)
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 触发表单提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.submit().then((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.getOpenerEventChannel().emit('refreshData')
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('article_id').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;
}
.uni-button {
width: 184px;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
line-height: 1;
margin: 0;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册