添加用户操作

func (u *UserController) DoAdd()  {
	username := u.GetString("username")
	password := u.GetString("password")
	age,_ := u.GetInt("age")
	gender,_ := u.GetInt("gender")
	phone := u.GetString("phone")
	addr := u.GetString("addr")
	is_active,_ := u.GetInt("is_active")

	new_password := utils.GetMd5Str(password)
	phone_int64,_ := strconv.ParseInt(phone,10,64)
	o := orm.NewOrm()
	user_data := auth.User{UserName:username,Password:new_password,Age:age,Gender:gender,Phone:phone_int64,Addr:addr,IsActive:is_active}
	_,err := o.Insert(&user_data)


	message_map := map[string]interface{}{}
	if err != nil {  //说明插入数据有问题
		ret1 := fmt.Sprintf("插入数据信息:username:%s|md5_password:%s|age:%d|gender:%d|phone:%s|" +
			"addr:%s;is_active:%d",username,new_password,age,gender,phone,addr,is_active)
		ret := fmt.Sprintf("添加数据出错,错误信息:%v",err)
		logs.Error(ret1)
		logs.Error(ret)
		message_map["code"] = 10001
		message_map["msg"] = "添加数据出错,请重新添加"
		u.Data["json"] = message_map
	}else {
		ret1 := fmt.Sprintf("插入数据成功,数据信息:username:%s|md5_password:%s|age:%d|gender:%d|phone:%s|" +
			"addr:%s;is_active:%d",username,new_password,age,gender,phone,addr,is_active)
		logs.Info(ret1)
		message_map["code"] = 200
		message_map["msg"] = "添加成功"
		u.Data["json"] = message_map
	}

	u.ServeJSON()



}

前端

form.on('submit(add)', function (data) {
                    var username = document.getElementById("username").value;
                    var password = document.getElementById("password").value;
                    var age = document.getElementById("age").value;
                    var gender = $("input[name='gender']:checked").val();
                    var phone = document.getElementById("phone").value;
                    var addr = document.getElementById("addr").value;
                    var is_active = $("input[name='is_active']:checked").val();
                    if (password.length < 6){
                        alert("密码长度不能小于6个字符");
                        return
                    }

                    $.ajax({
                        url:"{{urlfor "UserController.DoAdd"}}",
                        type:"POST",
                        data:{
                            "username":username,
                            "password":password,
                            "age":age,
                            "gender":gender,
                            "phone":phone,
                            "addr":addr,
                            "is_active":is_active
                        },
                        success:function (data) {
                            var code = data.code;
                            if(code == 200){
                                alert(data.msg);
                                setTimeout(reload_list,2000);  // 2s

                            }else {
                                alert(data.msg)
                            }
                        },
                        fail:function (data) {
                            alert(data)
                        }
                    });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值