package routehb import ( "GoClouds/core/bean/sysBean" "GoClouds/core/utils" "GoClouds/service/userService" "fmt" hbtp "github.com/mgr9525/HyperByte-Transfer-Protocol" gocloud "github.com/mgr9525/go-cloud" ruisUtil "github.com/mgr9525/go-ruisutil" "github.com/sirupsen/logrus" "io/ioutil" "net/http" "net/url" "strings" "time" ) type CommRPC struct{} func (CommRPC) AuthFun() hbtp.AuthFun { return nil } func (CommRPC) ClearCache(c *hbtp.Context, key string) { var err error if key == "" { err = gocloud.CacheFlush() } else { err = gocloud.CacheSet(key, nil) } if err != nil { c.ResString(hbtp.ResStatusErr, "gocloud err:"+err.Error()) return } c.ResString(hbtp.ResStatusOk, "ok") } func (CommRPC) ClearUserCache(c *hbtp.Context, uid string) { gocloud.CacheSet(fmt.Sprintf("uinfo:%s", uid), nil) gocloud.CacheSet(fmt.Sprintf("user-permission:%s", uid), nil) c.ResString(hbtp.ResStatusOk, "ok") } func (CommRPC) CheckPermission(c *hbtp.Context, m *sysBean.CheckPermissionReq) { if m.Uid == "" { c.ResString(hbtp.ResStatusErr, "param err") return } if m.Perm == "" { c.ResString(hbtp.ResStatusErr, "param err") return } c.ResString(hbtp.ResStatusOk, fmt.Sprintf("%t", userService.CheckPermission(m.Uid, m.Perm))) } func (CommRPC) SendSms(c *hbtp.Context, m *sysBean.SendSmsReq) { if m.Phone == "" { c.ResString(hbtp.ResStatusErr, "param err:phone!") return } if m.Ips == "" { c.ResString(hbtp.ResStatusErr, "param err:ips!") return } key1 := fmt.Sprintf("smsSend:%s", m.Phone) key2 := fmt.Sprintf("smsCode:%s", m.Phone) //key3 := fmt.Sprintf("smsSendIP:%s", ips) data, err := gocloud.CacheGet(key1) logrus.Debugf("SendSms get(%s):data=%s ,err=%v", key1, string(data), err) if err == nil { c.ResString(hbtp.ResStatusErr, "1分钟后重试!") return } code := utils.GenValidateCode(6) parm := url.Values{} parm.Set("mobile", m.Phone) parm.Set("param", "code:"+code) parm.Set("tpl_id", gocloud.CloudConf.Custom.GetString("codetmpid")) req, err := http.NewRequest("POST", "http://dingxin.market.alicloudapi.com/dx/sendSms", strings.NewReader(parm.Encode())) if err != nil { c.ResString(hbtp.ResStatusErr, "send req err:"+err.Error()) return } cli := http.Client{Timeout: time.Second * 3} req.Header.Set("Authorization", "APPCODE "+gocloud.CloudConf.Custom.GetString("smskey")) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") //ch.Put(key3, []byte(fmt.Sprintf("%d", time.Now().Unix())), 60) res, err := cli.Do(req) if err != nil { logrus.Debug("sendSms http err:" + err.Error()) c.ResString(hbtp.ResStatusErr, "send do err:"+err.Error()) return } defer res.Body.Close() gocloud.CacheSet(key1, []byte(time.Now().Format(time.RFC3339Nano)), time.Second*60) bts, err := ioutil.ReadAll(res.Body) if err != nil { c.ResString(hbtp.ResStatusErr, "send read err:"+err.Error()) return } mp := ruisUtil.NewMapo(bts) if mp.GetString("return_code") != "00000" { logrus.Debug("return_code err(", res.StatusCode, ":'", parm.Encode(), "'):", string(bts)) c.ResString(hbtp.ResStatusErr, "发送失败:"+mp.GetString("return_code")) return } logrus.Debug("发送短信:", m.Phone, ",IP:", m.Ips, "!!:", string(bts)) gocloud.CacheSet(key2, []byte(code), time.Second*60*5) c.ResString(hbtp.ResStatusOk, "ok") } func (CommRPC) ValidSms(c *hbtp.Context, m *sysBean.VaildSmsReq) { if m.Phone == "" { c.ResString(hbtp.ResStatusErr, "param err:phone!") return } if m.Code == "" { c.ResString(hbtp.ResStatusErr, "param err:code!") return } key := fmt.Sprintf("smsCode:%s", m.Phone) btscode, err := gocloud.CacheGet(key) if err != nil { c.ResString(hbtp.ResStatusErr, "未找到验证码!") return } if m.Code != string(btscode) { c.ResString(hbtp.ResStatusErr, "验证码错误!") return } gocloud.CacheSet(key, nil) c.ResString(hbtp.ResStatusOk, "ok") }