Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
mintzhao committed Dec 19, 2014
1 parent 79b3c03 commit 793313d
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 28 deletions.
Binary file added .DS_Store
Binary file not shown.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,80 @@
```
* /v1/hongid/register[post]: 绑定昵称&密码
```
http body:
{
tel: "18610889275",
nickname: "sss",
password: "lsjdfoiwejfoj"
}
400: 参数错误
{
code: -201,
message: "sjdfljojfow",
errors: [
{
field: "sss",
message: "sldjfls"
},
{
field: "sss",
message: "sldjfls"
}
]
}
500: 服务器内部错误
200:
{
code: 0,
message: "sfsafasf",
description: "safewfsdf",
}
自定义返回code:
-202:参数错误
-400:系统内部错误
0:一切ok
```
* /v1/hongid/login [post]: 登录
```
http body:
{
username: "18610889275",
password: "lsjdfoiwejfoj"
}
400: 参数错误
{
code: -201,
message: "sjdfljojfow",
errors: [
{
field: "sss",
message: "sldjfls"
},
{
field: "sss",
message: "sldjfls"
}
]
}
500: 服务器内部错误
403:
{
code: 0,
message: "sfsafasf",
description: "safewfsdf",
}
200:
{
id: 3434,
uuid: "asdfsdfsaf",
hongid: "343434",
nickname: "sdfasdf"
}
自定义返回code:
-202:参数错误
-304: 用户名错误
-305: 密码错误
-400:系统内部错误
0:一切ok
```
11 changes: 8 additions & 3 deletions conf/app.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
appname = app-svr
httpport = 8081
httpport = 8082
runmode = dev
autorender = false
copyrequestbody = true
EnableDocs = true

[dev]
hongid_host = 127.0.0.1:8080
hongid_host = "http://127.0.0.1:8081"
hongid_reg_tel = "/v1/members/register/tel"
hongid_info_tel = "/v1/members/tel/"
hongid_info_id = "/v1/members/id/"
app_group = 1

[prod]
hongid_host = 127.0.0.1:8080
hongid_host = "http://127.0.0.1:8081"
app_group = 1
41 changes: 29 additions & 12 deletions controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/validation"
"github.com/globalways/utils_go/errors"
"github.com/globalways/utils_go/smsmgr"
"net/http"
"github.com/astaxie/beego/logs"
"github.com/mreiferson/httpclient"
"io"
"net/http/httptest"
"net/http"
"time"
"io/ioutil"
)

var (
_ context.Context
_ logs.BeeLogger
valid = new(validation.Validation)
sms = smsmgr.NewDefaultSmsManager()
_ context.Context
_ logs.BeeLogger
valid = new(validation.Validation)
sms = smsmgr.NewDefaultSmsManager()
transport = &httpclient.Transport{
ConnectTimeout: 1 * time.Second,
RequestTimeout: 10 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
}
client = &http.Client{Transport: transport}
)

type BaseController struct {
Expand Down Expand Up @@ -64,6 +72,12 @@ func (c *BaseController) renderPng(data []byte) {
c.setHttpBody(data)
}

// http internal error
func (c *BaseController) renderInternalError() {
c.setHttpStatus(http.StatusInternalServerError)
c.renderJson(errors.NewCommonOutRsp(errors.New(errors.CODE_SYS_ERR_BASE)))
}

// set http status
func (c *BaseController) setHttpStatus(status int) {
c.Ctx.Output.SetStatus(status)
Expand Down Expand Up @@ -138,7 +152,7 @@ func (c *BaseController) validation(obj interface{}) {
// generate sms auth code
func (c *BaseController) genSmsAuthCode(tel string) (string, error) {
code, err := sms.GenSmsAuthCode(tel)
beego.BeeLogger.Debug("generate sms auth code: %v", code)
beego.BeeLogger.Debug("generate sms auth code: %v, err: %v", code, err)
return code, err
}

Expand All @@ -147,10 +161,13 @@ func (c *BaseController) varifySmsAuthCode(tel, code string) bool {
return sms.Verify(tel, code)
}

func (c *BaseController) forwardHttp(method, url string, body io.Reader) *httptest.ResponseRecorder {
func (c *BaseController) forwardHttp(method, url string, body io.Reader) (*http.Response, error) {
req, _ := http.NewRequest(method, url, body)
rsp := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(rsp, req)
return client.Do(req)
}

func (c *BaseController) getForwardHttpBody(body io.ReadCloser) []byte {
bodyBytes, _ := ioutil.ReadAll(body)

return rsp
return bodyBytes
}
6 changes: 5 additions & 1 deletion controllers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ package controllers
import "github.com/astaxie/beego"

var (
hongIdUrl = beego.AppConfig.DefaultString(beego.RunMode + "::hongid_host", "127.0.0.1:8080")
hongIdHost = beego.AppConfig.DefaultString(beego.RunMode + "::hongid_host", "http://127.0.0.1:8080")
hongIdRegByTel = beego.AppConfig.DefaultString(beego.RunMode + "::hongid_reg_tel", "/v1/members/register/tel")
hongIdInfoByTel = beego.AppConfig.DefaultString(beego.RunMode + "::hongid_info_tel", "/v1/members/tel/")
hongIdInfoById = beego.AppConfig.DefaultString(beego.RunMode + "::hongid_info_id", "/v1/members/id/")
appUserGroupId = beego.AppConfig.DefaultInt64(beego.RunMode + "::app_group", 1)
)
107 changes: 107 additions & 0 deletions controllers/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2014 [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package controllers

import (
"encoding/json"
"github.com/globalways/utils_go/errors"
"net/http"
hm "github.com/globalways/hongId_models/models"
"github.com/globalways/utils_go/security"
)

type LoginController struct {
BaseController
}

type ReqLogin struct {
UserName string `json:"username"`
PassWord string `json:"password"`
}

// login username is telphone?
func (req *ReqLogin) IsTel() bool {
return true
}

func (req *ReqLogin) IsEmail() bool {
return false
}

func (req *ReqLogin) IsHongId() bool {
return false
}

type RspLogin struct {
Id int64 `json:"id"`
UUID string `json:"uuid"`
HongId string `json:"hongid"`
NickName string `json:"nickname"`
}

// curl -i -H "Content-Type: application/json" -d '{"username": "18610889275", "password": "123456"}' 127.0.0.1:8082/v1/hongid/login
// @router /login [post]
func (c *LoginController) Login() {
reqLogin := new(ReqLogin)
if err := json.Unmarshal(c.getHttpBody(), reqLogin); err != nil {
c.appenWrongParams(errors.NewFieldError("reqBody", err.Error()))
}

// 验证输入参数
c.validation(reqLogin)

// handle http request param
if c.handleParamError() {
return
}

var rsp *http.Response
var err error
switch {
case reqLogin.IsTel():
rsp, err = c.forwardHttp("GET", hongIdHost+hongIdInfoByTel+reqLogin.UserName, nil)
case reqLogin.IsEmail():
case reqLogin.IsHongId():
default:
c.setHttpStatus(http.StatusForbidden)
c.renderJson(errors.NewCommonOutRsp(errors.New(errors.CODE_BISS_ERR_USER_NAME)))
return
}

if err != nil || rsp.StatusCode == http.StatusInternalServerError {
c.renderInternalError()
return
}

member := new(hm.Member)
if err := json.Unmarshal(c.getForwardHttpBody(rsp.Body), member); err != nil {
c.renderInternalError()
return
}

if !security.CompareHashAndPassword(member.PassWord, reqLogin.PassWord) {
c.setHttpStatus(http.StatusForbidden)
c.renderJson(errors.NewCommonOutRsp(errors.New(errors.CODE_BISS_ERR_PASSWORD)))
return
}

rspLogin := new(RspLogin)
rspLogin.Id = member.Id
rspLogin.UUID = member.UUID
rspLogin.HongId = member.HongId
rspLogin.NickName = member.NickName
c.renderJson(rspLogin)
}


Loading

0 comments on commit 793313d

Please sign in to comment.