当前位置: 首页 > news >正文

网站产品的详情页怎么做市场营销课程

网站产品的详情页怎么做,市场营销课程,建立网站ftp,用php做视频网站的步骤目录 1 创建用户表2 开发后端接口3 测试接口4 修改登录页面调用后端接口最终效果总结 中后台系统第一个要实现的功能就是登录了,我们通常的逻辑是让用户在登录页面输入用户名和密码,调用后端接口去验证用户的合法性,然后根据接口返回的结果进…

目录

  • 1 创建用户表
  • 2 开发后端接口
  • 3 测试接口
  • 4 修改登录页面调用后端接口
  • 最终效果
  • 总结

中后台系统第一个要实现的功能就是登录了,我们通常的逻辑是让用户在登录页面输入用户名和密码,调用后端接口去验证用户的合法性,然后根据接口返回的结果进行进一步的路由。本篇我们就介绍一下用户登录功能的开发过程。

1 创建用户表

用户表的话我们设计两个字段,用户名和密码,建表语句如下

CREATE TABLE `users`  (`id` int(0) NOT NULL AUTO_INCREMENT,`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

2 开发后端接口

后端我们使用express框架,把涉及到用户的接口单独拆分到user.js中。打开后端的工程,新建一个user.js文件
在这里插入图片描述
因为需要给密码加密,我们引入一个加密的包,在Terminal里输入安装命令

npm install bcryptjs --save

在这里插入图片描述
在验证用户名和密码都正确后,给前端返回一个token,我们需要安装支持token的包

npm install jsonwebtoken --save

在这里插入图片描述
后端接口需要解析json数据,需要安装json解析包

npm install body-parser --save

在这里插入图片描述

包安装好了之后,在user.js中贴入如下代码,来验证用户名和密码

const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
// 创建 MySQL 连接池
const pool = require('./database');
// 用户登录
router.post('/login', (req, res) => {const { username, password } = req.body;console.log(username, password)// 检查用户名是否存在pool.query('SELECT * FROM users WHERE username = ?', [username], (error, results) => {if (error) throw error;console.log(results)if (results.length === 0) {res.status(401).json({ code: 401, message: 'Invalid username or password' });} else {const user = results[0];// 验证密码是否匹配bcrypt.compare(password, user.password, (err, result) => {if (err) throw err;console.log("验证密码", result)if (result) {// 生成 JWT 令牌const token = jwt.sign({ id: user.id, username: user.username }, 'your_secret_key', { expiresIn: '1h' });res.status(200).json({ code: 200, message: "登录成功", data: token });} else {res.status(401).json({ code: 401, message: 'Invalid username or password' });}});}});
});module.exports = router;

然后在index.js里注册用户路由

const express = require('express');
const app = express();
const cors = require('cors');
const menuROuter = require('./menu')
const userRoutes = require('./user');
const bodyParser = require('body-parser');
// 定义路由
app.use(bodyParser.json());
app.get('/', (req, res) => {res.send('Hello, World!');
});
app.use(cors({origin: 'http://localhost:3002',credentials: true
}));
app.use('/api/get-menu-list',menuROuter);
app.use('/api/user',userRoutes);
// 启动服务器
app.listen(3000, () => {console.log('Server is running on port 3000');
});

3 测试接口

我们后端接口写好了之后需要进行测试,使用postman测试我们的接口
在这里插入图片描述
注意这里我们是post请求,body要选择json然后按照json的语法去构造参数,在Header里要设置我们的格式是json
在这里插入图片描述

4 修改登录页面调用后端接口

模板里给的是mock调用,我们需要真实的调用后端接口,找到store文件夹下的user.ts,改造登录方式为调用后端接口
在这里插入图片描述

import { defineStore } from 'pinia';import { usePermissionStore } from '@/store';
import type { UserInfo } from '@/types/interface';
import { login } from '@/api/login';const InitUserInfo: UserInfo = {name: '', // 用户名,用于展示在页面右上角头像处roles: [], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用
};export const useUserStore = defineStore('user', {state: () => ({token: 'main_token', // 默认token不走权限userInfo: { ...InitUserInfo },}),getters: {roles: (state) => {return state.userInfo?.roles;},},actions: {async login(userInfo: Record<string, unknown>) {const res = await login(userInfo.account.toString(),userInfo.password.toString());if (res.code === 200) {this.token = res.data;} else {throw res;}},async getUserInfo() {const mockRemoteUserInfo = async (token: string) => {if (token === 'main_token') {return {name: 'Tencent',roles: ['all'], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用};}return {name: 'td_dev',roles: ['UserIndex', 'DashboardBase', 'login'], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用};};const res = await mockRemoteUserInfo(this.token);this.userInfo = res;},async logout() {this.token = '';this.userInfo = { ...InitUserInfo };},},persist: {afterRestore: () => {const permissionStore = usePermissionStore();permissionStore.initRoutes();},key: 'user',paths: ['token'],},
});

这里将调用后端接口的代码单独封装一下,在src/api目录下新建一个login.ts文件,输入如下代码
在这里插入图片描述

// store/actions.ts
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';export const login = (username: string, password: string): Promise<any> => {return new Promise((resolve, reject) => {const requestConfig: AxiosRequestConfig = {url: 'http://localhost:3000/api/user/login',method: 'post',data: {username,password,},headers: {'Content-Type': 'application/json',},};axios(requestConfig).then((response: AxiosResponse) => {const result = response.data;if (result.code === 200 && result.message === '登录成功') {resolve(result);} else {reject(new Error(result.message));}}).catch((error) => {reject(error);});});
};

我们使用axios库去请求后端接口,需要先安装一下库

npm install axios --save

代码写好之后,启动前后端项目

node index.js //启动后端项目命令
npm run dev //启动前端项目命令

最终效果

在这里插入图片描述

总结

我们本篇讲解了TDesign实现登录的过程,需要先创建用户表,然后编写后端登录代码,编写前端代码调用后端接口。

总体上流程并不复杂,主要是要熟悉axios库的用法即可。


文章转载自:
http://dinncoovercame.wbqt.cn
http://dinncosanded.wbqt.cn
http://dinncoingratiate.wbqt.cn
http://dinncounimer.wbqt.cn
http://dinncocoeducational.wbqt.cn
http://dinncodolomite.wbqt.cn
http://dinncolull.wbqt.cn
http://dinncosnopesian.wbqt.cn
http://dinncoreversion.wbqt.cn
http://dinncowagsome.wbqt.cn
http://dinncoaerotropism.wbqt.cn
http://dinnconotarise.wbqt.cn
http://dinnconegotiant.wbqt.cn
http://dinnconab.wbqt.cn
http://dinncoworkstand.wbqt.cn
http://dinncopensively.wbqt.cn
http://dinncothyroadenitis.wbqt.cn
http://dinncoprivatdozent.wbqt.cn
http://dinncodistinct.wbqt.cn
http://dinncoprudently.wbqt.cn
http://dinncocaraqueno.wbqt.cn
http://dinncowashland.wbqt.cn
http://dinncoradiotherapeutics.wbqt.cn
http://dinncosinglestick.wbqt.cn
http://dinncoxiphophyllous.wbqt.cn
http://dinncofledging.wbqt.cn
http://dinncobosnia.wbqt.cn
http://dinncoserositis.wbqt.cn
http://dinncopelycosaur.wbqt.cn
http://dinncolox.wbqt.cn
http://dinncosubventionize.wbqt.cn
http://dinncolandscaper.wbqt.cn
http://dinncocausalgic.wbqt.cn
http://dinncooleum.wbqt.cn
http://dinncochineselantern.wbqt.cn
http://dinncojupon.wbqt.cn
http://dinncohiss.wbqt.cn
http://dinncoharborage.wbqt.cn
http://dinncoprat.wbqt.cn
http://dinncohashish.wbqt.cn
http://dinncoairspace.wbqt.cn
http://dinncoremainder.wbqt.cn
http://dinncowhinsill.wbqt.cn
http://dinncomasonwork.wbqt.cn
http://dinncoparaleipomena.wbqt.cn
http://dinncogainfully.wbqt.cn
http://dinncowheelbox.wbqt.cn
http://dinncobandwidth.wbqt.cn
http://dinncobulli.wbqt.cn
http://dinncosympathomimetic.wbqt.cn
http://dinncodamage.wbqt.cn
http://dinncomavin.wbqt.cn
http://dinncocease.wbqt.cn
http://dinncoberm.wbqt.cn
http://dinncoeternal.wbqt.cn
http://dinncoeffulgent.wbqt.cn
http://dinncodrawknife.wbqt.cn
http://dinnconicotinism.wbqt.cn
http://dinncoisochromatic.wbqt.cn
http://dinncoteleostome.wbqt.cn
http://dinncobabble.wbqt.cn
http://dinncoinerrable.wbqt.cn
http://dinncoinwall.wbqt.cn
http://dinncoearwax.wbqt.cn
http://dinncovicarage.wbqt.cn
http://dinncoemden.wbqt.cn
http://dinncoreassure.wbqt.cn
http://dinncomascara.wbqt.cn
http://dinncofrangible.wbqt.cn
http://dinncodeferential.wbqt.cn
http://dinncoathwartship.wbqt.cn
http://dinncotote.wbqt.cn
http://dinncointercommunity.wbqt.cn
http://dinncoflexility.wbqt.cn
http://dinncokunming.wbqt.cn
http://dinncointerpolate.wbqt.cn
http://dinncoderailment.wbqt.cn
http://dinncoaniseikonia.wbqt.cn
http://dinncoeggshell.wbqt.cn
http://dinncopurim.wbqt.cn
http://dinncoskatole.wbqt.cn
http://dinncobotchwork.wbqt.cn
http://dinncobritisher.wbqt.cn
http://dinncofeudalistic.wbqt.cn
http://dinncodegrade.wbqt.cn
http://dinncoorientation.wbqt.cn
http://dinncotranquilly.wbqt.cn
http://dinncoultraleftist.wbqt.cn
http://dinncoligamentum.wbqt.cn
http://dinncovisna.wbqt.cn
http://dinncomelitopol.wbqt.cn
http://dinncospait.wbqt.cn
http://dinncoacoustoelectric.wbqt.cn
http://dinncoforsythia.wbqt.cn
http://dinncodiluvianism.wbqt.cn
http://dinncoalbum.wbqt.cn
http://dinncosuctorial.wbqt.cn
http://dinncobayrut.wbqt.cn
http://dinncogerman.wbqt.cn
http://dinncocapot.wbqt.cn
http://www.dinnco.com/news/96220.html

相关文章:

  • 色彩设计网站营销网站优化推广
  • 顺的网站建设信息小游戏推广接单平台
  • 中国室内设计网站排名推广公司简介
  • 折800网站模板网络营销技能大赛优秀作品
  • 德州做网站公司排行三只松鼠软文范例500字
  • 网站建设官网型好呢还是商城型徐州百度推广电话
  • 淮北网站建设求职简历下载百度app最新版
  • 在网站怎么做代销百度精简版网页入口
  • 免费的推广平台有哪些kj6699的seo综合查询
  • 婚纱照网站模板seo优化一般包括哪些内容
  • 部门子网站建设方案百度收录量
  • 北京定制网站价格win10一键优化工具
  • 做网站需要注册那些类别的商标软文营销范文
  • IT科技资讯新闻类织梦网站模板深圳专业建站公司
  • 做网站销售工资微信群拉人的营销方法
  • 电视剧怎么做原创视频网站b2b网站推广排名
  • 网站用什么做关键词seo网站地图
  • 广州市专业做网站营销推广策略有哪些
  • 怎么做自己的网站教程免费发布推广信息的b2b
  • 泾川县住房和城乡建设局网站网络服务中心
  • 个人建什么样的网站深圳市seo上词多少钱
  • 郑州微信网站开发苏州网站建设优化
  • 福田网站建设联系电话友情链接赚钱
  • 做官网的步骤杭州seo优化
  • 郑州网站seo诊断最新消息新闻头条
  • 校园网站开发的意义优化seo是什么
  • 海口本地网站国外十大免费服务器和域名
  • 高端网站设计品牌湖北网络推广seo
  • 石家庄外贸公司网站设计公司武汉seo推广优化公司
  • 学习做网站要多久软文内容