You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
364 lines
8.2 KiB
364 lines
8.2 KiB
package usercenter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"regexp"
|
|
"src/module/tencent"
|
|
|
|
"github.com/towgo/towgo/dao/basedboperat"
|
|
"github.com/towgo/towgo/towgo"
|
|
)
|
|
|
|
func InitManageApi() {
|
|
|
|
//初始化API加载器
|
|
initLoader()
|
|
|
|
//注册JSON-RPC服务处理器method路由
|
|
//账户登录 F
|
|
|
|
towgo.SetFunc(_methodHead+"/user/loginOrRegByMobile", userLoginOrRegByMobile)
|
|
|
|
//获取自己的账户信息
|
|
towgo.SetFunc(_methodHead+"/user/myinfo", userMyinfo)
|
|
|
|
//账户注销 F
|
|
towgo.SetFunc(_methodHead+"/user/logoff", userLogoff)
|
|
|
|
//账户注册移动用户注册
|
|
towgo.SetFunc(_methodHead+"/user/regByMobile", userRegByMobile)
|
|
|
|
//获取注册短信验证码
|
|
towgo.SetFunc(_methodHead+"/user/getRegSMSVerificationCode", getRegSMSVerificationCode)
|
|
|
|
//修改密码 F
|
|
towgo.SetFunc(_methodHead+"/user/changepassword", userChangepassword)
|
|
|
|
}
|
|
|
|
func isPhoneNumber(input string) bool {
|
|
// 中国手机号码正则表达式
|
|
// 13[0-9], 14[5,7,9], 15[0-3,5-9], 16[6], 17[0-8], 18[0-9], 19[1,8,9]
|
|
phoneNumberPattern := `^1([38][0-9]|14[579]|5[^4]|6[6]|7[0-8]|9[189])\d{8}$`
|
|
reg := regexp.MustCompile(phoneNumberPattern)
|
|
return reg.MatchString(input)
|
|
}
|
|
|
|
func getRegSMSVerificationCode(rpcConn towgo.JsonRpcConnection) {
|
|
var params struct {
|
|
Mobile string `json:"mobile"`
|
|
}
|
|
rpcConn.ReadParams(¶ms)
|
|
|
|
if !isPhoneNumber(params.Mobile) {
|
|
rpcConn.WriteError(500, "手机号码非法")
|
|
return
|
|
}
|
|
|
|
code, err := StoreRegSMSVerificationCode(params.Mobile)
|
|
if err != nil {
|
|
rpcConn.WriteError(500, err.Error())
|
|
return
|
|
}
|
|
|
|
err = tencent.SendSMSVerificationCode("2030693", "蕊鑫信息科技", params.Mobile, code)
|
|
if err != nil {
|
|
rpcConn.WriteError(500, err.Error())
|
|
return
|
|
}
|
|
|
|
rpcConn.WriteResult("验证码已经发送,请查收")
|
|
|
|
}
|
|
|
|
// 注册用户
|
|
func userRegByMobile(rpcConn towgo.JsonRpcConnection) {
|
|
result := map[string]interface{}{} //初始化结果参数
|
|
|
|
var params struct {
|
|
Mobile string `json:"mobile"`
|
|
VerificationCode string `json:"verification_code"`
|
|
}
|
|
|
|
rpcConn.ReadParams(¶ms)
|
|
|
|
if params.Mobile == "" {
|
|
rpcConn.WriteError(500, "手机号码不能为空")
|
|
return
|
|
}
|
|
if params.VerificationCode == "" {
|
|
rpcConn.WriteError(500, "验证码不能为空")
|
|
return
|
|
}
|
|
|
|
if !RegSMSVerification(params.Mobile, params.VerificationCode) {
|
|
rpcConn.WriteError(500, "验证码错误")
|
|
return
|
|
}
|
|
|
|
var user User
|
|
user.Username = params.Mobile
|
|
user.Password = randCharNumber(8)
|
|
|
|
/*
|
|
user := user{}
|
|
user.Nickname = jsonObj.Params.Nickname
|
|
user.Email = jsonObj.Params.Email
|
|
*/
|
|
Err := user.Reg(user.Username, user.Password)
|
|
if Err != nil {
|
|
rpcConn.WriteError(500, Err.Error())
|
|
return
|
|
}
|
|
|
|
//拼装结果返回
|
|
result["id"] = user.ID
|
|
result["username"] = user.Username
|
|
rpcConn.WriteResult(result)
|
|
}
|
|
|
|
// 用户登陆
|
|
func userLogin(rpcConn towgo.JsonRpcConnection) {
|
|
result := map[string]interface{}{} //初始化结果参数
|
|
var err error
|
|
|
|
rpcResponse := rpcConn.GetRpcResponse()
|
|
jsonObj := struct {
|
|
Params struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
} `json:"params"`
|
|
}{}
|
|
err = json.Unmarshal([]byte(rpcConn.Read()), &jsonObj)
|
|
if err != nil {
|
|
rpcResponse.Error.Set(1, err.Error())
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
if jsonObj.Params.Username == "" {
|
|
rpcResponse.Error.Set(1001, "")
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
if jsonObj.Params.Password == "" {
|
|
rpcResponse.Error.Set(1002, "")
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
|
|
user := User{}
|
|
loginErr := user.Login(jsonObj.Params.Username, jsonObj.Params.Password)
|
|
|
|
if loginErr != nil { //模型层登陆成功
|
|
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录失败! 错误信息:%s", user.Username, rpcConn.GetRemoteAddr(), loginErr.Error()))
|
|
rpcResponse.Error.Set(1, "用户名或密码错误")
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
|
|
result["id"] = user.ID
|
|
result["username"] = user.Username
|
|
|
|
result["token"] = user.UserToken.TokenKey
|
|
|
|
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录成功!", user.Username, rpcConn.GetRemoteAddr()))
|
|
rpcConn.WriteResult(result)
|
|
|
|
}
|
|
|
|
// 用户手机验证码登陆
|
|
func userLoginOrRegByMobile(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
var params struct {
|
|
Mobile string `json:"mobile"`
|
|
VerificationCode string `json:"verification_code"`
|
|
}
|
|
|
|
rpcConn.ReadParams(¶ms)
|
|
|
|
if params.Mobile == "" {
|
|
rpcConn.WriteError(500, "手机号码不能为空")
|
|
return
|
|
}
|
|
if params.VerificationCode == "" {
|
|
rpcConn.WriteError(500, "验证码不能为空")
|
|
return
|
|
}
|
|
|
|
if !RegSMSVerification(params.Mobile, params.VerificationCode) {
|
|
rpcConn.WriteError(500, "验证码错误")
|
|
return
|
|
}
|
|
|
|
//验证通过
|
|
|
|
user := User{}
|
|
|
|
basedboperat.Get(&user, nil, "username = ?", params.Mobile)
|
|
|
|
if user.ID > 0 {
|
|
loginErr := user.LoginNoAuth(params.Mobile)
|
|
|
|
if loginErr != nil { //模型层登陆成功
|
|
log.Print(loginErr.Error())
|
|
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录失败! 错误信息:%s", user.Username, rpcConn.GetRemoteAddr(), loginErr.Error()))
|
|
rpcConn.WriteError(500, "用户名或密码错误")
|
|
return
|
|
}
|
|
|
|
user.Password = ""
|
|
user.Salt = ""
|
|
|
|
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录成功!", user.Username, rpcConn.GetRemoteAddr()))
|
|
rpcConn.WriteResult(user)
|
|
return
|
|
}
|
|
|
|
user.Username = params.Mobile
|
|
user.Password = randCharNumber(8)
|
|
|
|
Err := user.Reg(user.Username, user.Password)
|
|
if Err != nil {
|
|
rpcConn.WriteError(500, Err.Error())
|
|
return
|
|
}
|
|
|
|
user.Password = ""
|
|
user.Salt = ""
|
|
rpcConn.WriteResult(user)
|
|
|
|
}
|
|
|
|
// token check
|
|
func userTokenCheck(rpcConn towgo.JsonRpcConnection) {
|
|
result := map[string]interface{}{} //初始化结果参数
|
|
var err error
|
|
|
|
rpcResponse := rpcConn.GetRpcResponse()
|
|
jsonObj := struct {
|
|
Session string `json:"session"`
|
|
Params struct {
|
|
Username string `json:"username"`
|
|
Userid int `json:"userid"`
|
|
Token string `json:"token"`
|
|
} `json:"params"`
|
|
}{}
|
|
err = json.Unmarshal([]byte(rpcConn.Read()), &jsonObj)
|
|
if err != nil {
|
|
rpcResponse.Error.Set(1, err.Error())
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
|
|
var user *User
|
|
user, err = user.LoginByToken(jsonObj.Params.Token)
|
|
if err != nil {
|
|
result["valid"] = false
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
if user.ID == 0 {
|
|
result["valid"] = false
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
if !user.UserToken.Valid() {
|
|
result["valid"] = false
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
result["valid"] = true
|
|
rpcConn.WriteResult(result)
|
|
}
|
|
|
|
// 用户注销
|
|
func userLogoff(rpcConn towgo.JsonRpcConnection) {
|
|
//result := map[string]interface{}{} //初始化结果参数
|
|
var err error
|
|
|
|
rpcResponse := rpcConn.GetRpcResponse()
|
|
jsonObj := struct {
|
|
Session string `json:"session"`
|
|
}{}
|
|
err = json.Unmarshal([]byte(rpcConn.Read()), &jsonObj)
|
|
if err != nil {
|
|
rpcResponse.Error.Set(1, err.Error())
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
|
|
var user *User
|
|
user, err = user.LoginByToken(jsonObj.Session)
|
|
if err != nil {
|
|
rpcConn.WriteResult(map[string]string{"success": "ok"})
|
|
return
|
|
}
|
|
if user.ID > 0 {
|
|
user.Logoff()
|
|
}
|
|
|
|
rpcConn.WriteResult(map[string]string{"success": "ok"})
|
|
|
|
}
|
|
|
|
func userMyinfo(rpcConn towgo.JsonRpcConnection) {
|
|
userSession, err := LoginByToken(rpcConn.GetRpcRequest().Session)
|
|
userSession.Token = rpcConn.GetRpcRequest().Session
|
|
if err != nil {
|
|
rpcConn.GetRpcResponse().Error.Set(401, err.Error())
|
|
rpcConn.Write()
|
|
return
|
|
}
|
|
rpcConn.WriteResult(userSession)
|
|
}
|
|
|
|
func userChangepassword(rpcConn towgo.JsonRpcConnection) {
|
|
result := map[string]interface{}{} //初始化结果参数
|
|
var err error
|
|
|
|
rpcResponse := rpcConn.GetRpcResponse()
|
|
jsonObj := struct {
|
|
Session string `json:"session"`
|
|
Params struct {
|
|
Oldpassword string `json:"oldpassword"`
|
|
Newpassword string `json:"newpassword"`
|
|
} `json:"params"`
|
|
}{}
|
|
err = json.Unmarshal([]byte(rpcConn.Read()), &jsonObj)
|
|
|
|
if err != nil {
|
|
rpcResponse.Error.Set(1, err.Error())
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
var user *User
|
|
user, err = user.LoginByToken(jsonObj.Session)
|
|
if err != nil {
|
|
rpcResponse.Error.Set(401, err.Error())
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
if user.ID == 0 {
|
|
rpcResponse.Error.Set(1003, "")
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
err = user.Changepassword(jsonObj.Params.Oldpassword, jsonObj.Params.Newpassword)
|
|
if err != nil {
|
|
rpcResponse.Error.Set(1, err.Error())
|
|
rpcConn.WriteResult(result)
|
|
return
|
|
}
|
|
|
|
rpcConn.WriteResult(struct {
|
|
Success string `json:"success"`
|
|
}{Success: "ok"})
|
|
|
|
}
|