彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03

彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03前文再续,之前一篇我们已经配置好了数据库以及模板引擎,现在可以在逻辑层编写具体业务代码了,博客平台和大多数在线平台一样,都是基于用户账号体系来进行操作,所以我们需要针对用户表完成用户账号的CURD操作

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第22天,点击查看活动详情

前文再续,之前一篇我们已经配置好了数据库以及模板引擎,现在可以在逻辑层编写具体业务代码了,博客平台和大多数在线平台一样,都是基于用户账号体系来进行操作,所以我们需要针对用户表完成用户账号的CURD操作。

用户后台模板

首先用户操作逻辑主要在后台展现,所以模板应该单独生成admin文件夹,和前台模板进行逻辑隔离:

cd views  
mkdir admin

随后创建用户管理页面模板user.html:

<!DOCTYPE html>  
<html lang="zh-CN">  
  <head>  
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">  
    <meta name="viewport" content="width=device-width, initial-scale=1">  
    <meta name="applicable-device" content="pc,mobile" />  
  <title>用户管理</title>  
<meta content="index,follow" name="robots">  
<meta content="index,follow" name="GOOGLEBOT">  
<meta content="刘悦" name="Author">  
  
  <meta http-equiv="expires" content="4500"/>  
  
   <link rel="stylesheet" href="../assets/css/style.css" />  
  
   <script src="../assets/js/axios.js"></script>  
    <script src="../assets/js/vue.js"></script>  
  
  
  </head>  
  <body >  
  
    <div id="app">  
  
    <nav class="navbar navbar-inverse navbar-fixed-top">  
      <div class="container">  
        <div class="navbar-header">  
  
            <div class="switch_a nav_swich">  
              
                <div class="react-toggle">  
              <div class="react-toggle-track"><div class="react-toggle-track-check"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx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BqEYKxX3HpmKZEnQnr65X20hqJYaNcLoFOLiJk2LuBbyg7Q0OEn+hm0P3honxFD6rdxYorKpeIoi4YSSvyQHQIbM5t4+YNxLj/OxhVOOE4585qGpjnq+wSx6Q9CtNxTjd5klB+g6Mv36r0+b9cZFi44WYkHdG2ZWb3TtOUOXyVAlKlpGvJIAJ3eBMyfYS5C0qRZGtC85j+4sOasDe9xznPYezhhO/2Q6eP2fSOvYHOjtuQ1a9Q1VKynVDaMc8E0tptdxUsTFpFIYjcZKcbnoaQTNdiqCwNlL4G7oziSqGnT1ALf34vhk4R5zU3qYV9ONp9K88RtouShE68JwaU8dFw5W617shWa9ykeaBIn2hcsvPgL00k45QdTCZuSVcTRNs+8fnyLvooQfR5iujAnR9bxfY2xOVOxFS8SK3Le0l48VyYu1M8HRe5JD8wKPTjYnifaK3Wfn/GChYQ8ZAi6WRzWgqLV5YrsVLnZaVSoXU1g9gOIDwFySiGi+Zdrnzr7J3r+SMuszlcQCRn8lNGcTuSy2jOI7o9mxjZo+vR3ej3tN+ifRSOyUTS0+VMOid93cCubeiy/6TImS0QxRSCq2vxKr45zV+FQnjWH6D2xg+E9EatLcLAdHTgtGGD80D6jM0+aOl4wJgO/f96R2aJKCQ3yvgftRhdFMOpd6oAAAAASUVORK5CYII=" width="16" height="16" role="presentation" style="pointer-events: none;"></div>  
            <div class="react-toggle-track-x"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZ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bajQOpq17F0R9QevNcEhfcU+xvyQQUalGJBSesqOkgPQ4YNyUZL9fSvUPDjoNAwN8/dwFjaczNkc3ptaMud1EIDtGcmXTcefO2cGSvKIFfp/2JIJxlq7xEl3nVPM4fDeIbPkD16/ptNc0bDu7qxbsu0R2JGywWMIjF2ft3tjfloAyQAGXiOn8hrqwbVvMXzaO+QeHXP6nF0wvX74Hf4NGG5GPjSlYoyM3P/0FbCT6zvM/yYoAAAAASUVORK5CYII=" width="16" height="16" role="presentation" style="pointer-events: none;"></div></div><div class="react-toggle-thumb"></div></div>  
    
              </div>  
  
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">  
          <span class="sr-only">菜单</span>  
          <span class="icon-bar"></span>  
          <span class="icon-bar"></span>  
          <span class="icon-bar"></span>  
          </button>  
        </div>  
        <div id="navbar" class="collapse navbar-collapse">  
          <ul class="nav navbar-nav">  
            <li class="index_nav index_index"><a href="/" title='用户管理'>用户管理</a></li>  
            <li class="index_nav index_1"><a href="/l_id_1" title='文章管理'></a></li>  
          </ul>  
  
  
            <div class="react-toggle bigtoggle">  
              <div class="react-toggle-track"><div class="react-toggle-track-check"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmL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diqCwNlL4G7oziSqGnT1ALf34vhk4R5zU3qYV9ONp9K88RtouShE68JwaU8dFw5W617shWa9ykeaBIn2hcsvPgL00k45QdTCZuSVcTRNs+8fnyLvooQfR5iujAnR9bxfY2xOVOxFS8SK3Le0l48VyYu1M8HRe5JD8wKPTjYnifaK3Wfn/GChYQ8ZAi6WRzWgqLV5YrsVLnZaVSoXU1g9gOIDwFySiGi+Zdrnzr7J3r+SMuszlcQCRn8lNGcTuSy2jOI7o9mxjZo+vR3ej3tN+ifRSOyUTS0+VMOid93cCubeiy/6TImS0QxRSCq2vxKr45zV+FQnjWH6D2xg+E9EatLcLAdHTgtGGD80D6jM0+aOl4wJgO/f96R2aJKCQ3yvgftRhdFMOpd6oAAAAASUVORK5CYII=" width="16" height="16" role="presentation" style="pointer-events: none;"></div>  
            <div class="react-toggle-track-x"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8dGlmZjpPcmllbnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgPC9yZGY6UkRGPgo8L3g6eG1wbWV0YT4KTMInWQAABlJJREFUWAm1V3tsFEUcntnXvXu0tBWo1ZZHihBjCEWqkHi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></div><div class="react-toggle-thumb"></div></div>  
  
          <div class="search navbar-right" >  
            <form action="/Index_search" method ="GET" class="search_form" >  
              <input type="search" name="text" class="search_input" placeholder="Search" required="required" >  
            </form>  
          </div>  
  
  
        </div>  
        
      </div>  
    </nav>  
     
    <div class="container">  
    <header>  
      
      
    </header>  
      
    <section>  
      <div class="row">  
        <div class="col-md-8">  
           
            
            <ul class="g-formlist form_li">  
                <li>  
                    <label class="mark">用户名</label>  
                    <div class="write">  
                        <input type="text" id="form-name" class="g-text-entry" placeholder="请输入4-10字符" />  
                        <span class="tip" data-initial="请输入4-10字符"></span>  
                    </div>  
                </li>  
                <li>  
                    <label class="mark">密 码</label>  
                    <div class="write">  
                        <input type="password" id="form-psw" class="g-text-entry" placeholder="请输入6-30字符" />  
                        <span class="tip" data-initial="请输入6-30字符"></span>  
                    </div>  
                </li>  
               
            </ul>  
  
            <button>提交</button>  
  
     
  
       
            
        </div>  
        </div>  
        </section>  
  
    </div>  
  
    </div>  
  
  </body>

模板目录架构如下:

└── views  
 ├── admin │ └── user.html ├── index.html └── test.html 

views根目录模板为前台模板,而admin目录下模板是为后台模板。

同时前端声明username和password变量,分别绑定用户名和密码:

const App = {  
            data() {  
                return {  
                    //用户名 
                    username: "",  
                    //密码 
                    password:""  
                };  
            },  
            created: function() {  
  
                console.log("你好,女神");  
  
            },  
            methods: {  
            },  
        };

接着构造用户添加表单,绑定表单字段:

<ul class="g-formlist form_li">  
                <li>  
                    <label class="mark">用户名</label>  
                    <div class="write">  
                        <input v-model="username" type="text" id="form-name" class="g-text-entry" placeholder="请输入4-10字符" />  
                        <span class="tip" data-initial="请输入4-10字符"></span>  
                    </div>  
                </li>  
                <li>  
                    <label class="mark">密 码</label>  
                    <div class="write">  
                        <input v-model="password" type="password" id="form-psw" class="g-text-entry" placeholder="请输入6-30字符" />  
                        <span class="tip" data-initial="请输入6-30字符"></span>  
                    </div>  
                </li>  
               
            </ul>  
  
            <button @click="submit">提交</button>

这里通过v-model关键字将表单和变量做双向绑定,同时为按钮绑定submit提交方法。

如果愿意,我们也可以针对前端的axios库进行二次封装,增加异步请求方法的复用性:

const myaxios = function (url, type, data = {}) {  
  
return new  
  
    Promise((resolve) => {  
  
        if (type === "get" || type === "delete") {  
  
  
            axios({  
  
                method: type,  
                url: url,  
                params: data  
            }).then((result) => {  
  
                resolve(result.data);  
  
            });  
  
  
        } else {  
  
            const params = new URLSearchParams();  
            for (var key in data) {  
            params.append(key,data[key]);  
            }  
            axios({  
  
                method: type,  
                url: url,  
                data:params  
            }).then((result) => {  
  
                resolve(result.data);  
  
            });  
  
        }  
  
    });  
  
}  
  
app.config.globalProperties.myaxios = myaxios;

这样,我们就可以随时使用this关键字来向后台发起异步请求了。

接着,编写后台视图,将用户后台模板渲染出来:

app.Get("/admin/user/", func(ctx iris.Context) {  
  
		ctx.View("/admin/user.html")  
  
	})

编译后,访问 http://localhost:5000/admin/user/,如图所示:

彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03

用户后台接口

后台接口主要负责接收前端请求的参数,然后根据请求方式类型来决定用户表的操作动作,首先构建添加接口:

app.Post("/admin/user_action/", func(ctx iris.Context) {  
  
		username := ctx.PostValue("username")  
		password := ctx.PostValue("password")  
  
		fmt.Println(username, password)  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg":     "ok",  
		}  
		ctx.JSON(ret)  
  
	})

这里使用Post方式匹配路由/admin/user_action/,随后通过ctx结构体的PostValue函数来获取具体的参数key,然后利用ctx.JSON函数将字典序列化为Json,再返回给前端。

前端则使用之前封装好的myaxios内置方法向后端发起异步请求:

submit:function(){  
  
  
                    this.myaxios("http://localhost:5000/admin/user_action/","post",{"username":this.username,"password":this.password}).then(data => {  
        console.log(data)  
      });  
  
                }

后台返回:

Now listening on: http://localhost:5000 
Application started. Press CTRL+C to shut down.  
19:30:58 app         | admin admin

可以看到,后端打印出了前端请求的用户名和密码,接着就是入库操作:

app.Post("/admin/user_action/", func(ctx iris.Context) {  
  
		username := ctx.PostValue("username")  
		password := ctx.PostValue("password")  
  
		fmt.Println(username, password)  
  
		user := &model.User{Username: username, Password: password}  
		res := db.Create(user)  
  
		fmt.Println(res.Error)  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg":     "ok",  
		}  
		ctx.JSON(ret)  
  
	})

这里初始化结构体变量user后,利用db.Create函数进行入库操作。

随后检查入库结果:

MySQL [irisblog]> select * from user;  
+----+---------------------+---------------------+------------+----------+----------+ 
| id | created_at          | updated_at          | deleted_at | username | password |  
+----+---------------------+---------------------+------------+----------+----------+ 
| 13 | 2022-08-22 19:33:16 | 2022-08-22 19:33:16 | NULL       | admin    | admin    |  
+----+---------------------+---------------------+------------+----------+----------+ 
1 row in set (0.00 sec)

入库操作虽然成功了,但显然,密码不能使用明文,否则不就步CSDN的后尘贻笑大方了吗?

添加md5加密逻辑:

w := md5.New()  
io.WriteString(w, password) //将str写入到w中  
md5str := fmt.Sprintf("%x", w.Sum(nil))

注意导入”crypto/md5″和”io”两个标准库包。

完成代码:

app.Post("/admin/user_action/", func(ctx iris.Context) {  
  
		username := ctx.PostValue("username")  
		password := ctx.PostValue("password")  
  
		fmt.Println(username, password)  
  
		w := md5.New()  
		io.WriteString(w, password) //将str写入到w中 
		md5str := fmt.Sprintf("%x", w.Sum(nil))  
  
		user := &model.User{Username: username, Password: md5str}  
		res := db.Create(user)  
  
		fmt.Println(res.Error)  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg":     "ok",  
		}  
		ctx.JSON(ret)  
  
	})

重新编译后,再次发起请求,检查入库结果:

MySQL [irisblog]> select * from user;  
+----+---------------------+---------------------+------------+----------+----------------------------------+ 
| id | created_at          | updated_at          | deleted_at | username | password                         |  
+----+---------------------+---------------------+------------+----------+----------------------------------+ 
| 16 | 2022-08-22 19:41:40 | 2022-08-22 19:41:40 | NULL       | admin    | 21232f297a57a5a743894a0e4a801fc3 |  
+----+---------------------+---------------------+------------+----------+----------------------------------+ 
1 row in set (0.00 sec)

完成添加逻辑后,可以将用户列表批量查询出来:

app.Get("/admin/userlist/", func(ctx iris.Context) {  
  
		var users []model.User  
		res := db.Find(&users)  
  
		ctx.JSON(res)  
  
	})

注意这里声明一个切片嵌套结构users,切片的每一个元素是用户结构体,接口返回:

{  
Value: [  
{  
ID: 16,  
CreatedAt: "2022-08-22T19:41:40+08:00",  
UpdatedAt: "2022-08-22T19:41:40+08:00",  
DeletedAt: null,  
Username: "admin",  
Password: "21232f297a57a5a743894a0e4a801fc3"  
},  
{  
ID: 17,  
CreatedAt: "2022-08-22T19:48:25+08:00",  
UpdatedAt: "2022-08-22T19:48:25+08:00",  
DeletedAt: null,  
Username: "888123",  
Password: "202cb962ac59075b964b07152d234b70"  
},  
{  
ID: 18,  
CreatedAt: "2022-08-22T19:48:28+08:00",  
UpdatedAt: "2022-08-22T19:48:28+08:00",  
DeletedAt: null,  
Username: "admin123",  
Password: "21232f297a57a5a743894a0e4a801fc3"  
}  
],  
Error: null,  
RowsAffected: 3  
}

随后,前端可以通过异步请求回调赋值将用户列表展示在页面中:

const App = {  
            data() {  
                return {  
                    //用户名 
                    username: "",  
                    //密码 
                    password:"",  
                    //用户列表 
                    userlist:[]  
                };  
            },  
            created: function() {  
  
                console.log("你好,女神");  
  
                this.get_userlist();  
  
            },  
            methods: {  
  
                get_userlist:function(){  
  
  
                    this.myaxios("http://localhost:5000/admin/userlist/","get",).then(data => {  
        console.log(data)  
        this.userlist = data.Value  
      });  
  
  
                },  
                submit:function(){  
  
  
                    this.myaxios("http://localhost:5000/admin/user_action/","post",{"username":this.username,"password":this.password}).then(data => {  
        console.log(data)  
      });  
  
                }  
  
            },  
        };

随后,在页面中渲染userlist变量:

<table class="gridtable">  
  
                <tr>  
  
                    <th>用户id</th>  
                    <th>用户名</th>  
                    <th>添加时间</th>  
                </tr>  
  
                <tr v-for="(item,index) in userlist">  
                    <td>{{ item.ID }}</td>  
                    <td>{{ item.Username }}</td>  
                    <td>{{ item.CreatedAt }}</td>  
                </tr>  
  
  
  
            </table>

请求 http://localhost:5000/admin/user/ 如图所示:

彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03

Vue.js+Iris的前后端联调流程就跑通了,当然有些地方还需要封装,比如md5加密环节,后续登录模块也依然会依赖md5包,项目根目录下建立mytool目录:

mkdir mytool  
cd mytool

将md5加密封装为函数:

package mytool  
  
import (  
	"crypto/md5"  
	"fmt"  
	"io"  
)  
  
func Make_password(password string) string {  
  
	w := md5.New()  
	io.WriteString(w, password) //将str写入到w中 
	md5str := fmt.Sprintf("%x", w.Sum(nil))  
  
	return md5str  
  
}

随后通过包名进行调用:

md5str := mytool.Make_password(password)

结语

至此,前后端分离的用户系统就构建好了,开发效率层面,基于Go lang的Iris框架并不逊色于任何动态语言框架,语法的简明程度有过之而无不及,性能层面更是不遑多让,该项目已开源在Github:github.com/zcxey2911/I… ,与君共觞,和君共勉。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/18411.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注