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.
165 lines
4.5 KiB
165 lines
4.5 KiB
package controller
|
|
|
|
import (
|
|
"GoClouds/core/utils"
|
|
"GoClouds/service/userService"
|
|
"GoClouds/webs/sys/app"
|
|
"fmt"
|
|
"github.com/donnie4w/go-logger/logger"
|
|
"github.com/go-macaron/cache"
|
|
gocloud "github.com/mgr9525/go-cloud"
|
|
ruisUtil "github.com/mgr9525/go-ruisutil"
|
|
"gopkg.in/macaron.v1"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type CommController struct{}
|
|
|
|
func (e *CommController) GetPath() string {
|
|
return "/comm"
|
|
}
|
|
func (e *CommController) Routes() {
|
|
gocloud.Web.Any("/clearCache", e.clearCache)
|
|
gocloud.Web.Any("/clearUserCache", e.clearUserCache)
|
|
gocloud.Web.Any("/getUserPerms", e.getUserPerms)
|
|
gocloud.Web.Any("/checkPermission", e.checkPermission)
|
|
gocloud.Web.Any("/sendSms", e.sendSms)
|
|
gocloud.Web.Any("/vaildSms", e.vaildSms)
|
|
}
|
|
func (e *CommController) Mid() []macaron.Handler {
|
|
return []macaron.Handler{gocloud.AccessAllowFun}
|
|
}
|
|
|
|
func (e *CommController) clearCache(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
key := ctj.GetString("key")
|
|
if len(key) > 0 {
|
|
ch.Delete(key)
|
|
} else {
|
|
ch.Flush()
|
|
}
|
|
c.PlainText(200, []byte("ok"))
|
|
}
|
|
func (e *CommController) clearUserCache(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
uid := ctj.GetString("uid")
|
|
if len(uid) <= 0 {
|
|
c.PlainText(500, []byte("param err:uid!"))
|
|
return
|
|
}
|
|
ch.Delete(fmt.Sprintf("uinfo:%s", uid))
|
|
ch.Delete(fmt.Sprintf("user-permission:%s", uid))
|
|
c.PlainText(200, []byte("ok"))
|
|
}
|
|
func (e *CommController) checkPermission(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
uid := ctj.GetString("uid")
|
|
perm := ctj.GetString("perm")
|
|
|
|
if len(uid) <= 0 {
|
|
c.PlainText(500, []byte("param err:uid!"))
|
|
return
|
|
}
|
|
if len(perm) <= 0 {
|
|
c.PlainText(500, []byte("param err:perm!"))
|
|
return
|
|
}
|
|
|
|
c.PlainText(200, []byte(fmt.Sprintf("%t", userService.CheckPermission(uid, perm, ch))))
|
|
}
|
|
func (e *CommController) getUserPerms(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
uid := ctj.GetString("uid")
|
|
|
|
if len(uid) <= 0 {
|
|
c.PlainText(500, []byte("param err:uid!"))
|
|
return
|
|
}
|
|
|
|
c.JSON(200, userService.FindUserPermissionCache(uid, ch))
|
|
}
|
|
func (e *CommController) sendSms(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
phone := ctj.GetString("phone")
|
|
ips := ctj.GetString("ips")
|
|
if len(phone) <= 0 {
|
|
c.PlainText(500, []byte("param err:phone!"))
|
|
return
|
|
}
|
|
if len(ips) <= 0 {
|
|
c.PlainText(500, []byte("param err:ips!"))
|
|
return
|
|
}
|
|
|
|
key1 := fmt.Sprintf("smsSend:%s", phone)
|
|
key2 := fmt.Sprintf("smsCode:%s", phone)
|
|
//key3 := fmt.Sprintf("smsSendIP:%s", ips)
|
|
if ch.Get(key1) != nil /*||ch.Get(key3) != nil*/ {
|
|
c.PlainText(511, []byte("1分钟后重试!"))
|
|
return
|
|
}
|
|
|
|
code := utils.GenValidateCode(6)
|
|
|
|
parm := url.Values{}
|
|
parm.Set("mobile", phone)
|
|
parm.Set("param", "code:"+code)
|
|
parm.Set("tpl_id", app.Conf.Codetmpid)
|
|
req, err := http.NewRequest("POST", "http://dingxin.market.alicloudapi.com/dx/sendSms", strings.NewReader(parm.Encode()))
|
|
if err != nil {
|
|
c.PlainText(512, []byte("服务器错误!"))
|
|
return
|
|
}
|
|
cli := http.Client{Timeout: time.Second * 3}
|
|
req.Header.Set("Authorization", "APPCODE "+app.Conf.Smskey)
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
ch.Put(key1, []byte(fmt.Sprintf("%d", time.Now().Unix())), 60)
|
|
//ch.Put(key3, []byte(fmt.Sprintf("%d", time.Now().Unix())), 60)
|
|
res, err := cli.Do(req)
|
|
if err != nil {
|
|
logger.Info("sendSms http err:" + err.Error())
|
|
c.PlainText(513, []byte("服务器错误!"))
|
|
return
|
|
}
|
|
defer res.Body.Close()
|
|
bts, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
c.PlainText(514, []byte("服务器错误!"))
|
|
return
|
|
}
|
|
mp := ruisUtil.NewMapo(bts)
|
|
if mp.GetString("return_code") != "00000" {
|
|
logger.Info("return_code err(", res.StatusCode, ":'", parm.Encode(), "'):", string(bts))
|
|
c.PlainText(521, []byte("发送失败:"+mp.GetString("return_code")))
|
|
return
|
|
}
|
|
|
|
logger.Info("发送短信:", phone, ",IP:", ips, "!!:", string(bts))
|
|
ch.Put(key2, []byte(code), 60*5)
|
|
c.PlainText(200, []byte("ok"))
|
|
}
|
|
func (e *CommController) vaildSms(c *macaron.Context, ch cache.Cache, ctj gocloud.ContJSON) {
|
|
phone := ctj.GetString("phone")
|
|
code := ctj.GetString("code")
|
|
if len(phone) <= 0 {
|
|
c.PlainText(500, []byte("param err:phone!"))
|
|
return
|
|
}
|
|
if len(code) <= 0 {
|
|
c.PlainText(500, []byte("param err:code!"))
|
|
return
|
|
}
|
|
|
|
key := fmt.Sprintf("smsCode:%s", phone)
|
|
btscode := ch.Get(key)
|
|
if btscode == nil {
|
|
c.PlainText(511, []byte("未找到验证码!"))
|
|
return
|
|
}
|
|
if code != string(btscode.([]byte)) {
|
|
c.PlainText(512, []byte("验证码错误!"))
|
|
return
|
|
}
|
|
|
|
ch.Delete(key)
|
|
c.PlainText(200, []byte("ok"))
|
|
}
|