SCF构建小型服务端并结合uni-app

代码哈士奇 等级 1494 0 1
标签: httpspng

使用腾讯云SCF构建小型服务端并结合uni-app()小程序

我们这里手写了一个nodejs环境下的用户体系 使用了之前写的一个数据库连接插件dmhq-mysql-pool比较垃圾 凑合用 文档地址为 https://github.com/dmhsq/dmhsq-mysql-pool/blob/main/README.md 也使用了md5 npm install js-md5

这里使用邮箱发送验证码 先在本地写好 再上传云函数

配置数据库连接

安装 npm install dmhsq-mysql-pool

新建一个文件db.js

const database = require("dmhsq-mysql-pool");
const configs = {
    host: 'xxxx',
    port: 'xxxx',
    user: 'xxxx',
    password: 'xxxx',
    database: "xxxx"
}
let user = new database(configs).table("user")
let codes = new database(configs).table("email")
module.exports = {
    user,
    codes
};

用户数据表名 user SCF构建小型服务端并结合uni-app

验证码表 名email 由于只用到邮箱验证码 SCF构建小型服务端并结合uni-app

配置邮箱发送模块

这里的user 和 pass 为STMP获取 在各大邮箱的设置可以找到 邮箱转发服务 npm install nodemailer nodemailer文档

const nodemailer = require('nodemailer')

const transporter = nodemailer.createTransport({
    service: 'qq', // no need to set host or port etc.
    auth: {
        user: 'xxxxx@qq.com',
        pass: 'xxxxxxx'
    }
});

const sendCode = async (email,code,time) => {
    let message = {
        from: "验证码<xxxxx@qq.com>",
        to: email,
        subject: "验证码服务",
        html: `<html>
                    <head>
                        <meta charset="utf-8">
                        <title></title>
                    </head>
                    <body>
                        <div>
                            <p style="font-size: 20px;">欢迎您使用,您的验证码为 
                            <span style="color: blue;font-size: 30px;font-weight: 800;">${code}</span> ,有效时间为${time/60}分钟, 请勿泄露并及时验证</p>

                            <div style="margin-top: 50px;"></div>
                            <p style="color: red;font-size: 25px;">请勿回复</p>
                        </div>
                    </body>
                </html>`
    };
    await transporter.sendMail(message)
    return {
            code:0,
            msg:"邮件已发送,如果没有收到,请检查邮箱"
        }
}

module.exports = {sendCode};

编写简单用户体系

const {
    user,
    codes
} = require("./db.js");
const {
    sendCode
} = require("./email.js");
const md5 = require("js-md5")

//注册模块
const sign = async (username, password) => {
    const dfp = password
    password = md5(password);
    let isH = await user.where({username}).get();
    if(isH.data.length>0){
        return {
            code: 5742,
            msg: "用户名已被占用",
        }
    }
    const _id = md5(Math.random().toString(36)).substr(0, 10);
    let res = await user.add({
        username,
        password,
        _id
    }).get();
    let rsp = {
        code: 5741,
        msg: "注册失败",
    }
    if (res.code == 0) {
        let userRes = await login(username, dfp);
        rsp = {
            code: 0,
            msg: "注册成功"
        }
        if (userRes.code == 0) {
            rsp.data = userRes.userInfo
        }
    }
    return rsp;
}

//登陆模块
const login = async (username, password) => {
    password = md5(password)

    let res = await user.where({
        username,
        password
    }).get()
    if (!res.data.length) {
        return {
            code: 9001,
            msg: "用户名或者密码错误"
        }
    } else {
        let token = md5(md5(Math.random().toString(36)) + md5(Math.random().toString(36)));
        const tokenExpired = parseInt(Date.parse(new Date()).toString().substr(0, 10)) + 72000;
        const last_login_time = parseInt(Date.parse(new Date()).toString().substr(0, 10));
        let qres = await user.updata({
            token_expired: tokenExpired,
            token,
            last_login_time
        }).where({username}).get();
        if (qres.code == 0) {
            return {
                code: 0,
                userInfo: {
                    token,
                    tokenExpired,
                    username
                }
            }
        } else {
            return {
                code: 9002,
                msg: "登陆失败",
                data: qres
            }
        }

    }
}

//邮箱发送模块
const sendEmailCode = async (email, type) => {
    const randomStr = '00000' + Math.floor(Math.random() * 1000000)
    const code = randomStr.substring(randomStr.length - 6);
    let time = 3600
    const check_time = parseInt(Date.parse(new Date()).toString().substr(0, 10)) + time;
    let res = {}
    res = await sendCode(email, code, time)
    if (res.code == 0) {
        await codes.add({
            email,
            code,
            check_time,
            state: 0,
            type
        }).get();
    } else {
        res = {
            code: 4046,
            msg: "发送失败"
        }
    }
    return res
}


//验证码校验
const checkCode = async (email, code, type) => {
    let result = await codes.where({
        email,
        code,
        type
    }).sort({
        check_time: "DESC"
    }).get();
    let data = result.data;
    let res = {}
    if (data.length == 0) {
        res = {
            code: 4048,
            msg: "验证码错误"
        }
    } else {
        data = data[0]
        const check_times = parseInt(Date.parse(new Date()).toString().substr(0, 10));
        if (data.state == 0 & data.check_time > check_times) {
            await codes.updata({
                state: 1
            }).where({
                email
            }).get()
            res = {
                code: 0,
                msg: "验证通过"
            }
        } else if (data.check_time < check_times) {
            res = {
                code: 4044,
                msg: "验证码失效"
            }
        } else if (data.state == 1) {
            res = {
                code: 4045,
                msg: "验证码已经验证"
            }
        } else {
            res = {
                code: 4048,
                msg: "验证码错误"
            }
        }
    }
    return res;
}


//邮箱绑定
const bind = async (username, email, code) => {
    const check_code = await checkCode(email, code, "bind");
    const check_user = await user.where({
        username,
        email
    }).get();
    let res = {}
    if (check_user.data.length > 0) {
        res = {
            code: 74174,
            msg: "用户已经绑定邮箱"
        }
    } else {
        if (check_code.code == 0) {
            const datas = await user.updata({
                    email
                }).
                where({
                    username
                }).get();
            if (datas.code == 0) {
                res = {
                    code: 0,
                    msg: "绑定成功"
                }
            }
        }else{
            res = check_code
        }
    }
    return res;
}

//邮箱解除绑定
const unbind = async (username, email, code) => {
    const check_code = await checkCode(email, code, "unbind");
    const check_user = await user.where({
        username,
        email
    }).get();
    let res = {}
    if (check_user.data.length == 0) {
        res = {
            code: 74175,
            msg: "用户还未绑定邮箱"
        }
    } else {
        if (check_code.code == 0) {
            const datas = await user.updata({
                    email: ""
                }).
                where({
                    username
                }).get();
            if (datas.code == 0) {
                res = {
                    code: 0,
                    msg: "解除绑定成功"
                }
            }
        }else{
            res = check_code
        }
    }
    return res;
}

//邮箱校检登录
const checkCodeLogin = async (email, code) => {
    const ress = await checkCode(email, code, "login")
    const isH = await user.where({email}).get();
    if(isH.data.length==0){
        return {
            code:9003,
            msg:"非法邮箱(邮箱未绑定用户)"
        }
    }
    if (ress.code == 0) {
        let token = md5(md5(Math.random().toString(36)) + md5(Math.random().toString(36)));
        const tokenExpired = parseInt(Date.parse(new Date()).toString().substr(0, 10)) + 72000;
        const last_login_time = parseInt(Date.parse(new Date()).toString().substr(0, 10));
        let qres = await user.updata({
            token_expired: tokenExpired,
            token,
            last_login_time
        }).where({
            email
        }).get();
        if (qres.code == 0) {
            res = {
                code: 0,
                userInfo: {
                    token,
                    tokenExpired,
                    email
                }
            }
        } else {
            res = {
                code: 9002,
                msg: "登陆失败",
                data: qres
            }
        }
    }
    return res;
}

//token校检
const checkToken = async (token) => {
    const reqs = await user.where({
        token
    }).get();
    let res = {}
    if (reqs.data.length > 0) {
        const userInfos = reqs.data[0]
        const check_time = userInfos.token_expired;
        const now_time = parseInt(Date.parse(new Date()).toString().substr(0, 10));
        if (check_time > now_time) {
            res = {
                code: 0,
                userInfo: {
                    username: userInfos.username
                }
            }
        } else {
            res = {
                code: 7412,
                msg: "token过期"
            }
        }
    } else {
        res = {
            code: 7417,
            msg: "token非法"
        }
    }
    return res;
}

module.exports = {
    sign,
    login,
    sendEmailCode,
    checkCode,
    bind,
    unbind,
    checkCodeLogin,
    checkToken
}

编写主程序

const userCenter = require("./user.js")

index.main_handler = async (event, context) => {

    let noCheckAction = ['sign', 'checkToken', 'login', 'checkCode', 'loginByEmail', 'emailCode']
    let params = event.queryString;
    let res = {}
    const {
        action
    } = params
    if (noCheckAction.indexOf(action) === -1) {
        if (!params.token) {
            res = {
                code: 401,
                msg: '缺少token'
            }
            return res;
        }else{
            let datas = await userCenter.checkToken(params.token)
            if (datas.code != 0) {
                res = datas
                return res;
            }else{
                params.username = datas.userInfo.username;
            }
        }

    }
    switch (action) {
        case "sign": {
            const {
                username,
                password
            } = params;
            res = await userCenter.sign(username, password);
            break;
        }
        case "login": {
            const {
                username,
                password
            } = params;
            res = await userCenter.login(username, password)
            break;
        }
        case "emailCode": {
            const {
                email,
                type
            } = params;
            res = await userCenter.sendEmailCode(email, type)
            break;
        }
        case "checkCode": {
            const {
                email,
                code,
                type
            } = params;
            res = await userCenter.checkCode(email, code, type)
            break;
        }
        case "bind": {
            const {
                username,
                email,
                code
            } = params;
            res = await userCenter.bind(username, email, code)
            break;
        }
        case "unbind": {
            const {
                username,
                email,
                code
            } = params;
            res = await userCenter.unbind(username, email, code)
            break;
        }
        case "loginByEmail": {
            const {
                email,
                code
            } = params;
            res = await userCenter.checkCodeLogin(email, code)
            break;
        }
        case "checkToken": {
            const {
                token
            } = params;
            res = await userCenter.checkToken(token)
            break;
        }
        default: {
            res = {
                code: 403,
                msg: "非法访问"
            };
            break;
        }
    }


    return res;
}

创建云函数

SCF构建小型服务端并结合uni-app

注意这里的执行方法 SCF构建小型服务端并结合uni-app 选择我们的项目文件夹 SCF构建小型服务端并结合uni-app 上传文件夹

部署

创建触发器

SCF构建小型服务端并结合uni-app SCF构建小型服务端并结合uni-app

点击api名称管理 SCF构建小型服务端并结合uni-app

编辑触发器 SCF构建小型服务端并结合uni-app

关闭集成响应

SCF构建小型服务端并结合uni-app

测试

触发器 拿到请求地址 SCF构建小型服务端并结合uni-app 测试注册 SCF构建小型服务端并结合uni-app

做个小程序

这里使用 uni-app做微信小程序

由于我们只用了 用户模块 那么我们就整合用户模块

页面很简单 登录 注册 邮箱登录 邮箱绑定 邮箱解绑 SCF构建小型服务端并结合uni-app SCF构建小型服务端并结合uni-app 页面代码

<template>
    <view class="content">
        <view v-if="is_us">
            <input v-model="username" placeholder="用户名" />
            <input v-model="password" type="password" placeholder="密码" />
            <text @click="is_us=false">邮箱验证码登录</text>
        <button @click="login()">登录</button>
        <button @click="register()">注册</button>
        </view>
        <view v-if="!is_us">
            <input v-model="email" placeholder="邮箱" />
            <input v-model="code" placeholder="验证码" />
            <text @click="is_us=true">账号密码登录</text>
            <button @click="sendEmail('login')">发送验证码</button>
        <button @click="loginEm()">登录</button>
        </view>
        <view>
            <view>用户名:{{userInfo.username}}</view>
            <view>token过期时间:{{userInfo.tokenExpired | timeDel }}</view>
            <view>token:{{userInfo.token}}</view>
        </view>
        <view v-if="userInfo.token!=''">
            <input v-model="email" placeholder="邮箱" />
            <input v-model="code" placeholder="验证码" />
            <button @click="sendEmail('bind')">发送绑定验证码</button>
            <button @click="sendEmail('unbind')">发送解绑验证码</button>
            <button @click="bindEm()">绑定</button>
            <button @click="unbindEm()">解绑</button>
        </view>
        <view>
            <view>{{userInfoG}}</view>
            <button @click="getUserInfo()">获取信息</button>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                is_us: true,
                username:"",
                password:"",
                email:"",
                code:"",
                userInfo: {
                    username: "未登录",
                    token:"",
                    tokenExpired:""
                },
                userInfoG:{}
            }
        },
        filters:{
            timeDel(val) {
                if(!val){
                    return ""
                }
                var date = new Date(val*1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
                var Y = date.getFullYear() + '-';
                var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
                var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
                var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
                var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                return Y + M + D + h + m + s;
            }
        },
        onLoad() {

        },
        methods: {
            req(action,other){
                return new Promise(resolve=>{
                    uni.request({
                        method:'POST',
                        url:`xxx/userCenter?action=${action}&${other}`,
                        success:res=>{
                            resolve(res.data)
                        }
                    })
                })
            },
            getUserInfo(){
                let tokens = uni.getStorageSync('token')
                this.req('checkToken',`token=${tokens}`).then(res=>{
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                    this.userInfoG = JSON.stringify(res)
                })
            },
            register(){
                this.req('sign',`username=${this.username}&password=${this.password}`).then(res=>{
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                    if(res.code==0){
                        let userInfo = res.data
                        uni.setStorageSync("token",userInfo.token)
                        uni.setStorageSync("tokenExpired",userInfo.tokenExpired)
                        this.userInfo = userInfo
                    }
                })
            },
            login(){
                this.req('login',`username=${this.username}&password=${this.password}`).then(res=>{
                    console.log(res)
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                    if(res.code==0){
                        let userInfo = res.userInfo
                        uni.setStorageSync("token",userInfo.token)
                        uni.setStorageSync("tokenExpired",userInfo.tokenExpired)
                        this.userInfo = userInfo
                    }
                })
            },
            sendEmail(type){
                this.req('emailCode',`email=${this.email}&type=${type}`).then(res=>{
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                })
            },
            loginEm(){
                this.req('loginByEmail',`email=${this.email}&code=${this.code}`).then(res=>{
                    console.log(res)
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                    if(res.code==0){
                        let userInfo = res.userInfo
                        uni.setStorageSync("token",userInfo.token)
                        uni.setStorageSync("tokenExpired",userInfo.tokenExpired)
                        this.userInfo = userInfo
                    }
                })
            },
            bindEm(){
                let tokens = uni.getStorageSync('token')
                this.req('bind',`username=${this.username}&email=${this.email}&code=${this.code}&token=${tokens}`).then(res=>{
                    console.log(res)
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                })
            },
            unbindEm(){
                let tokens = uni.getStorageSync('token')
                this.req('unbind',`username=${this.username}&email=${this.email}&code=${this.code}&token=${tokens}`).then(res=>{
                    console.log(res)
                    uni.showToast({
                        title:res.msg,
                        icon:'none'
                    })
                })
            }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

测试

注册

SCF构建小型服务端并结合uni-app

登录

SCF构建小型服务端并结合uni-app

获取个人信息

SCF构建小型服务端并结合uni-app

绑定/解除绑定邮箱

SCF构建小型服务端并结合uni-app

邮箱验证码登录

没有绑定则邮箱非法 SCF构建小型服务端并结合uni-app

数据库状态

SCF构建小型服务端并结合uni-app SCF构建小型服务端并结合uni-app

收藏
评论区

相关推荐

nodejs环境快速操作mysql数据库
github地址 引入依赖包 npm install dmhsqmysqldb可用于腾讯云SCF 云函数 云开发环境 效果如下 简化了mysql的使用 未经本人允许,禁止转载 安装npm install dmhsqmysqldb 使用示例快速操作mysql 错误处理尚未完善 部分错误参考mysql错误返回的均为Prom
SCF构建小型服务端并结合uni-app
使用腾讯云SCF构建小型服务端并结合uniapp()小程序我们这里手写了一个nodejs环境下的用户体系使用了之前写的一个数据库连接插件dmhqmysqlpool比较垃圾 凑合用文档地址为 也使用了md5 npm install jsmd5这里使用邮箱发送验证码先在本地写好 再上传云函数 配置数据库连接安装 npm install dmhsqmysqlpo
C#和倍福PLC之间的通讯控制
1.新建项目并引用TwinCAT动态链接库 ===================== ![](https://oscimg.oschina.net/oscnet/9aab3c8a6c3624cac52801dcef387fbf8ea.png) ![](https://oscimg.oschina.net/oscnet/59c0fec2d67fb353e
HTTPS
### 最近网站更新为https,于是做个笔记 ### 将域名 [www.domain.com](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.domain.com) 的证书文件1\_www.domain.com\_bundle.crt 、 ### 私钥文件2\_www.doma
HTTPS
楔子 谣言粉碎机前些日子发布的《[用公共WiFi上网会危害银行账户安全吗?](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fwww.guokr.com%2Farticle%2F100110%2F)》,文中介绍了在使用HTTPS进行网络加密传输的一些情况,从回复来看,争议还是有的。随着网络越
PHP对数据库的简单操作
一、运行结果 1、首先创建了一个叫db的数据库,并创建了table1,table2两个表用做演示。 ![](https://img2018.cnblogs.com/blog/1503988/201903/1503988-20190319234004791-1432918411.png)![](https://oscimg.oschina.net/oscn
14个Java并发容器,Java高手都知道!
> 作者:acupt [https://blog.csdn.net/Design407/article/details/100084673](https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fblog.csdn.net%2FDesign407%2Farticle%2Fdetails%2F100
AGU Advances:我们是否算得太多想得太少?
大气科学家Kerry Emanuel在_AGU Advances_ 撰文,反思了理论在大气、海洋和气候科学中的重要性。并就此敲响警钟,认为忽视理论会给我们的科学发展带来危险。 ![](https://oscimg.oschina.net/oscnet/00d3475f1b59741e6e7236928b37f944339.png) ![](h
Flink2020峰会PDF,快手数据治理相关PDF及有赞大数据技术沙龙PDF
**加微信好友并且入群即可获取!** 快手数据治理,数据质量,元数据管理,成本管理及有赞技术沙龙: ![](https://oscimg.oschina.net/oscnet/d271e801-4548-4d9a-8d87-c7cf3a4c9623.png) Flink2020峰会13日: ![](https://os
Https证书申请并自动更新与分发
### 安装acme 我们使用acme来申请免费的https通用域名证书 acme的证书有效期为三个月,但会创建一个定时任务提前一个月来进行续约 安装acme curl https://get.acme.sh | sh cd ~/.acme.sh/ yum -y install socat
NGW,前端新技术赛场:Serverless SSR 技术内幕
▎目录: ---- 1. 前言 2. Serverless 的演进 3. 云函数 SCF 的开发 4. NGW Serverless 同构直出方案 ▎**前言** ------- 最近 Serverless 又火了,有不少业务上云实装了 Serverless 云函数,取得了不错的落地效果,业界也在不断探索 Serverless 更多的落地场景。
Serverless+SCF=打倒服务器,解放程序员
**欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~** > 本文由[云加社区技术沙龙](https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fcloud.tencent.com%2Fdeveloper%2Fuser%2F1184429%3FfromSource%3Dwaitui) 发
svg转png
svg转png网络上常用的方式有两种: 1.直接转base64放到图片src进行显示,测试效果不佳,始终报方法问题。 2.先转canvas,再转为png图,测试效果可以但svg透明背景到了canvas转换会变黑。 推荐使用方法三: 复制链接下载svg转png的js http://p8sv0x8g6.bkt.clouddn.com/saveSvgAs
thunderbird 使用OpenPGP加解密邮件
**一、添加插件Enigmail** ![](http://static.oschina.net/uploads/space/2014/0309/105947_aTHg_921036.png) **二、进行密钥管理,并创建->新密钥对** ![](http://static.oschina.net/uploads/space/2014/0309/110
tomcat从 http转成https,并且去掉端口号
强制使用HTTPS方式访问Tomcat中的相关项目,于是研究了下,现将具体的步骤写下:      主要分2步:让tomcat能使用https--->强制使用https访问  1.让tomcat能使用https    A.在运行命令 JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg R