hotime/example/app/user.go

97 lines
2.3 KiB
Go
Raw Normal View History

2022-05-02 07:39:50 +00:00
package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
)
2022-05-03 00:09:25 +00:00
var UserCtr = Ctr{
2022-05-03 01:41:18 +00:00
"test": func(that *Context) {
that.Session("user_id", 1)
that.Session("wechat_id", 1)
that.Display(0, 1)
},
2022-05-02 07:39:50 +00:00
//用户微信公众号或者小程序登录
2022-05-03 00:09:25 +00:00
"info": func(that *Context) {
2022-05-03 07:17:27 +00:00
if that.Session("user_id").Data == nil {
2022-05-03 00:09:25 +00:00
that.Display(2, "没有登录")
2022-05-02 07:39:50 +00:00
return
}
2022-05-03 00:09:25 +00:00
user := that.Db.Get("user", "*", Map{"id": that.Session("user_id").ToCeilInt()})
if user == nil {
that.Display(4, "获取个人信息失败")
return
}
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
delete(user, "password")
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
company := that.Db.Get("company", "id,name", Map{"id": user.GetCeilInt("company_id")})
salesman := that.Db.Get("salesman", "id,name", Map{"id": user.GetCeilInt("salesman_id")})
provider := that.Db.Get("provider", "id,name", Map{"id": user.GetCeilInt("provider_id")})
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
if user != nil {
user["company"] = company
user["salesman"] = salesman
user["provider"] = provider
}
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
that.Display(0, user)
},
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
"edit": func(that *Context) {
2022-05-03 07:17:27 +00:00
if that.Session("user_id").Data == nil {
2022-05-03 00:09:25 +00:00
that.Display(2, "没有登录")
2022-05-02 07:39:50 +00:00
return
}
2022-05-03 00:09:25 +00:00
name := that.Req.FormValue("name")
nickname := that.Req.FormValue("nickname")
sex := that.Req.FormValue("sex")
email := that.Req.FormValue("email")
avatar := that.Req.FormValue("avatar")
address := that.Req.FormValue("address")
phone := that.Req.FormValue("phone") //如果更换手机号则要有新的短信验证码
code := that.Req.FormValue("code")
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
data := Map{"modify_time[#]": "now()"}
if name != "" {
data["name"] = name
}
if nickname != "" {
data["nickname"] = nickname
}
if sex != "" {
data["sex"] = sex
}
if email != "" {
data["email"] = email
}
if avatar != "" {
data["avatar"] = avatar
}
if address != "" {
data["address"] = address
}
if phone != "" {
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
//微信验证成功
if that.Session("wechat_phone").ToStr() == phone {
data["phone"] = phone
} else if code == "" || that.Session("phone").ToStr() != phone || that.Session("code").ToStr() != code {
that.Display(3, "手机短信验证失败")
return
}
data["phone"] = phone
}
2022-05-02 07:39:50 +00:00
2022-05-03 00:09:25 +00:00
re := that.Db.Update("user", data, Map{"id": that.Session("user_id").Data})
if re == 0 {
that.Display(4, "更新失败")
2022-05-02 07:39:50 +00:00
return
}
2022-05-03 07:17:27 +00:00
that.Display(0, "修改成功")
2022-05-02 07:39:50 +00:00
},
}