forked from golang/hotime
基础整理
This commit is contained in:
parent
335e8730da
commit
dbf8f91696
18
cache/cache_db.go
vendored
18
cache/cache_db.go
vendored
@ -58,7 +58,7 @@ func (that *CacheDb) initDbTable() {
|
||||
return
|
||||
}
|
||||
|
||||
_, e := that.Db.Exec("CREATE TABLE `" + that.Db.GetPrefix() + "cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
|
||||
_, e := that.Db.Exec("CREATE TABLE `" + that.Db.GetPrefix() + "cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `key` varchar(60) DEFAULT NULL, `value` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8")
|
||||
if e.GetError() == nil {
|
||||
that.isInit = true
|
||||
}
|
||||
@ -74,8 +74,8 @@ func (that *CacheDb) initDbTable() {
|
||||
}
|
||||
_, e := that.Db.Exec(`CREATE TABLE "` + that.Db.GetPrefix() + `cached" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"ckey" TEXT(60),
|
||||
"cvalue" TEXT(2000),
|
||||
"key" TEXT(60),
|
||||
"value" TEXT(2000),
|
||||
"time" integer,
|
||||
"endtime" integer
|
||||
);`)
|
||||
@ -90,7 +90,7 @@ func (that *CacheDb) initDbTable() {
|
||||
//获取Cache键只能为string类型
|
||||
func (that *CacheDb) get(key string) interface{} {
|
||||
|
||||
cached := that.Db.Get("cached", "*", Map{"ckey": key})
|
||||
cached := that.Db.Get("cached", "*", Map{"key": key})
|
||||
|
||||
if cached == nil {
|
||||
return nil
|
||||
@ -103,7 +103,7 @@ func (that *CacheDb) get(key string) interface{} {
|
||||
}
|
||||
|
||||
data := Map{}
|
||||
data.JsonToMap(cached.GetString("cvalue"))
|
||||
data.JsonToMap(cached.GetString("value"))
|
||||
|
||||
return data.Get("data")
|
||||
}
|
||||
@ -113,9 +113,9 @@ func (that *CacheDb) set(key string, value interface{}, tim int64) {
|
||||
|
||||
bte, _ := json.Marshal(Map{"data": value})
|
||||
|
||||
num := that.Db.Update("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"ckey": key})
|
||||
num := that.Db.Update("cached", Map{"value": string(bte), "time": time.Now().UnixNano(), "endtime": tim}, Map{"key": key})
|
||||
if num == int64(0) {
|
||||
that.Db.Insert("cached", Map{"cvalue": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "ckey": key})
|
||||
that.Db.Insert("cached", Map{"value": string(bte), "time": time.Now().UnixNano(), "endtime": tim, "key": key})
|
||||
}
|
||||
|
||||
//随机执行删除命令
|
||||
@ -130,10 +130,10 @@ func (that *CacheDb) delete(key string) {
|
||||
//如果通配删除
|
||||
if del != -1 {
|
||||
key = Substr(key, 0, del)
|
||||
that.Db.Delete("cached", Map{"ckey": key + "%"})
|
||||
that.Db.Delete("cached", Map{"key": key + "%"})
|
||||
|
||||
} else {
|
||||
that.Db.Delete("cached", Map{"ckey": key})
|
||||
that.Db.Delete("cached", Map{"key": key})
|
||||
}
|
||||
}
|
||||
|
||||
|
66
dri/wechat/h5program.go
Normal file
66
dri/wechat/h5program.go
Normal file
@ -0,0 +1,66 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"github.com/silenceper/wechat/v2"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/officialaccount"
|
||||
h5config "github.com/silenceper/wechat/v2/officialaccount/config"
|
||||
"github.com/silenceper/wechat/v2/officialaccount/js"
|
||||
"github.com/silenceper/wechat/v2/officialaccount/oauth"
|
||||
)
|
||||
|
||||
//基于此文档开发
|
||||
//https://github.com/silenceper/wechat/blob/v2/doc/api/officialaccount.md
|
||||
type h5Program struct {
|
||||
Memory *cache.Memory
|
||||
Config *h5config.Config
|
||||
*officialaccount.OfficialAccount
|
||||
weixin *wechat.Wechat //微信登录实例
|
||||
}
|
||||
|
||||
var H5Program = h5Program{}
|
||||
|
||||
// Init 初始化
|
||||
func (that *h5Program) Init(appid string, appsecret string) {
|
||||
that.weixin = wechat.NewWechat()
|
||||
that.Memory = cache.NewMemory()
|
||||
that.Config = &h5config.Config{
|
||||
AppID: appid,
|
||||
AppSecret: appsecret,
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: that.Memory,
|
||||
}
|
||||
that.OfficialAccount = that.weixin.GetOfficialAccount(that.Config)
|
||||
}
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
func (that *h5Program) GetUserInfo(code string) (appid string, resToken oauth.ResAccessToken, userInfo oauth.UserInfo, err error) {
|
||||
auth := that.GetOauth()
|
||||
//weixin.GetOpenPlatform()
|
||||
resToken, err = auth.GetUserAccessToken(code)
|
||||
if err != nil {
|
||||
|
||||
return auth.AppID, resToken, userInfo, err
|
||||
}
|
||||
|
||||
//getUserInfo
|
||||
userInfo, err = auth.GetUserInfo(resToken.AccessToken, resToken.OpenID, "")
|
||||
if err != nil {
|
||||
return auth.AppID, resToken, userInfo, err
|
||||
}
|
||||
|
||||
return auth.AppID, resToken, userInfo, err
|
||||
}
|
||||
|
||||
// GetSignUrl js url签名
|
||||
func (that *h5Program) GetSignUrl(signUrl string) (*js.Config, error) {
|
||||
|
||||
js := that.OfficialAccount.GetJs()
|
||||
cfg1, e := js.GetConfig(signUrl)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
return cfg1, nil
|
||||
}
|
66
dri/wechat/miniprogram.go
Normal file
66
dri/wechat/miniprogram.go
Normal file
@ -0,0 +1,66 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/silenceper/wechat/v2"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/miniprogram"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
||||
)
|
||||
|
||||
type miniProgram struct {
|
||||
Memory *cache.Memory
|
||||
Config *config.Config
|
||||
weixin *wechat.Wechat //微信登录实例
|
||||
*miniprogram.MiniProgram
|
||||
}
|
||||
|
||||
var MiniProgram = miniProgram{}
|
||||
|
||||
// Init 初始化
|
||||
func (that *miniProgram) Init(appid string, appsecret string) {
|
||||
|
||||
that.weixin = wechat.NewWechat()
|
||||
that.Memory = cache.NewMemory()
|
||||
that.Config = &config.Config{
|
||||
AppID: appid,
|
||||
AppSecret: appsecret,
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: that.Memory,
|
||||
}
|
||||
that.MiniProgram = that.weixin.GetMiniProgram(that.Config)
|
||||
}
|
||||
|
||||
func (that *miniProgram) GetBaseUserInfo(code string) (appid string, re auth.ResCode2Session, err error) {
|
||||
appid = that.Config.AppID
|
||||
a := that.GetAuth()
|
||||
re, err = a.Code2Session(code)
|
||||
|
||||
if err != nil {
|
||||
return appid, re, err
|
||||
}
|
||||
|
||||
return appid, re, err
|
||||
}
|
||||
|
||||
func (that *miniProgram) GetPhoneNumber(sessionkey, encryptedData, iv string) (appid string, re *encryptor.PlainData, err error) {
|
||||
appid = that.Config.AppID
|
||||
|
||||
if sessionkey == "" || encryptedData == "" || iv == "" {
|
||||
return appid, re, errors.New("参数不足")
|
||||
}
|
||||
|
||||
eny := that.GetEncryptor()
|
||||
|
||||
re, err = eny.Decrypt(sessionkey, encryptedData, iv)
|
||||
|
||||
if err != nil {
|
||||
return appid, re, err
|
||||
}
|
||||
|
||||
return appid, re, err
|
||||
|
||||
}
|
@ -15,7 +15,6 @@ import (
|
||||
|
||||
// Project 管理端项目
|
||||
var Project = Proj{
|
||||
"user": User,
|
||||
"salesman": Salesman,
|
||||
"lxcx": Lxcx,
|
||||
"wechat": Wechat,
|
||||
|
@ -29,6 +29,7 @@ var Wechat = Ctr{
|
||||
|
||||
},
|
||||
"code": func(that *Context) {
|
||||
|
||||
code := that.Req.FormValue("code")
|
||||
if code == "" {
|
||||
that.Display(3, "缺少code")
|
||||
|
@ -2,7 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
"code.hoteas.com/golang/hotime/dri/ddsms"
|
||||
"code.hoteas.com/golang/hotime/dri/wechat"
|
||||
"code.hoteas.com/golang/hotime/example/app"
|
||||
"code.hoteas.com/golang/hotime/example/salesman"
|
||||
|
||||
//. "code.hoteas.com/golang/hotime/common"
|
||||
"fmt"
|
||||
"time"
|
||||
@ -14,8 +18,13 @@ func main() {
|
||||
//fmt.Println("0123456"[1:7])
|
||||
appIns := Init("config/config.json")
|
||||
app.InitApp(appIns)
|
||||
//a:=Map{}
|
||||
//a.GetBool()
|
||||
//初始化短信
|
||||
ddsms.DDY.Init(appIns.Config.GetString("smsKey"))
|
||||
//初始化公众号配置
|
||||
wechat.H5Program.Init(appIns.Config.GetString("wechatAppID"), appIns.Config.GetString("wechatAppSecret"))
|
||||
//初始化小程序
|
||||
wechat.MiniProgram.Init(appIns.Config.GetString("wechatMiniAppID"), appIns.Config.GetString("wechatMiniAppSecret"))
|
||||
|
||||
appIns.SetConnectListener(func(that *Context) (isFinished bool) {
|
||||
//that.Session("admin_id", 1)
|
||||
|
||||
@ -30,6 +39,7 @@ func main() {
|
||||
// return isSuccess
|
||||
//})
|
||||
appIns.Run(Router{
|
||||
"app": app.Project,
|
||||
"salesman": salesman.Project,
|
||||
"app": app.Project,
|
||||
})
|
||||
}
|
||||
|
63
example/salesman/init.go
Normal file
63
example/salesman/init.go
Normal file
@ -0,0 +1,63 @@
|
||||
package salesman
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"code.hoteas.com/golang/hotime/dri/ddsms"
|
||||
"github.com/silenceper/wechat/v2"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/miniprogram"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||
"github.com/silenceper/wechat/v2/officialaccount"
|
||||
h5config "github.com/silenceper/wechat/v2/officialaccount/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Project 管理端项目
|
||||
var Project = Proj{
|
||||
"salesman": Salesman,
|
||||
"wechat": Wechat,
|
||||
}
|
||||
var weixin *wechat.Wechat //微信登录实例
|
||||
var miniProgram *miniprogram.MiniProgram
|
||||
var h5Program *officialaccount.OfficialAccount
|
||||
|
||||
func InitApp(app *Application) {
|
||||
weixin = wechat.NewWechat()
|
||||
memory := cache.NewMemory()
|
||||
memoryMini := cache.NewMemory()
|
||||
cfg := &config.Config{
|
||||
AppID: app.Config.GetString("wechatMiniAppID"),
|
||||
AppSecret: app.Config.GetString("wechatMiniAppSecret"),
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: memoryMini,
|
||||
}
|
||||
h5cfg := &h5config.Config{
|
||||
AppID: app.Config.GetString("wechatAppID"),
|
||||
AppSecret: app.Config.GetString("wechatAppSecret"),
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: memory,
|
||||
}
|
||||
|
||||
miniProgram = weixin.GetMiniProgram(cfg)
|
||||
h5Program = weixin.GetOfficialAccount(h5cfg)
|
||||
|
||||
ddsms.DDY.Init(app.Config.GetString("smsKey"))
|
||||
}
|
||||
|
||||
//生成随机码的6位md5
|
||||
func getSn() string {
|
||||
x := Rand(8)
|
||||
return Substr(Md5(ObjToStr(int64(x)+time.Now().UnixNano()+int64(Rand(6)))), 0, 6)
|
||||
}
|
||||
|
||||
//生成随机码的4位随机数
|
||||
func getCode() string {
|
||||
//res := ""
|
||||
//for i := 0; i < 4; i++ {
|
||||
res := ObjToStr(RandX(1000, 9999))
|
||||
//}
|
||||
return res
|
||||
}
|
66
example/salesman/salesman.go
Normal file
66
example/salesman/salesman.go
Normal file
@ -0,0 +1,66 @@
|
||||
package salesman
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
)
|
||||
|
||||
var Salesman = Ctr{
|
||||
//用户微信公众号或者小程序登录
|
||||
"login": func(that *Context) {
|
||||
acttoken := that.Req.FormValue("acttoken")
|
||||
retoken := that.Req.FormValue("retoken")
|
||||
appid := that.Req.FormValue("appid")
|
||||
unionid := that.Req.FormValue("unionid")
|
||||
openid := that.Req.FormValue("openid")
|
||||
nickname := that.Req.FormValue("nickname")
|
||||
avatar := that.Req.FormValue("avatar")
|
||||
phone := that.Req.FormValue("phone")
|
||||
Type := ObjToInt(that.Req.FormValue("type"))
|
||||
|
||||
if acttoken == "" || retoken == "" || appid == "" || unionid == "" || openid == "" || nickname == "" || avatar == "" || that.Req.FormValue("type") == "" || phone == "" {
|
||||
that.Display(3, "请求参数异常,请校验参数")
|
||||
return
|
||||
}
|
||||
|
||||
wechat := that.Db.Get("wechat", "*", Map{"AND": Map{"openid": openid, "del_flag": 0}})
|
||||
salesman := that.Db.Get("salesman", "*", Map{"phone": phone})
|
||||
|
||||
if salesman == nil {
|
||||
that.Display(3, "找不到企服商")
|
||||
return
|
||||
}
|
||||
|
||||
if wechat != nil {
|
||||
//有用户直接返回
|
||||
if wechat.GetCeilInt("salesman_id") != 0 && wechat.GetCeilInt64("salesman_id") == salesman.GetInt64("id") {
|
||||
that.Display(0, wechat)
|
||||
return
|
||||
}
|
||||
|
||||
that.Db.Update("wechat", Map{"salesman_id": salesman.GetCeilInt64("id")}, Map{"id": wechat.GetInt64("id")})
|
||||
|
||||
wechat["salesman_id"] = salesman.GetCeilInt64("id")
|
||||
that.Display(0, wechat)
|
||||
return
|
||||
}
|
||||
|
||||
wechat = Map{}
|
||||
wechat["salesman_id"] = salesman.GetCeilInt("id")
|
||||
wechat["acttoken"] = acttoken
|
||||
wechat["retoken"] = retoken
|
||||
wechat["appid"] = appid
|
||||
wechat["unionid"] = unionid
|
||||
wechat["openid"] = openid
|
||||
wechat["nickname"] = nickname
|
||||
wechat["avatar"] = avatar
|
||||
wechat["type"] = Type
|
||||
wechat["id"] = that.Db.Insert("wechat", wechat)
|
||||
if wechat.GetCeilInt("id") == 0 {
|
||||
that.Display(4, "创建用户失败")
|
||||
return
|
||||
}
|
||||
that.Display(0, wechat)
|
||||
|
||||
},
|
||||
}
|
26
example/salesman/sms.go
Normal file
26
example/salesman/sms.go
Normal file
@ -0,0 +1,26 @@
|
||||
package salesman
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
"code.hoteas.com/golang/hotime/dri/ddsms"
|
||||
)
|
||||
|
||||
var Sms = Ctr{
|
||||
//只允许微信验证过的或者登录成功的发送短信
|
||||
"send": func(that *Context) {
|
||||
|
||||
phone := that.Req.FormValue("phone")
|
||||
if len(phone) < 11 {
|
||||
that.Display(3, "手机号格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
code := getCode()
|
||||
that.Session("phone", phone)
|
||||
that.Session("code", code)
|
||||
|
||||
ddsms.DDY.SendYZM(phone, that.Config.GetString("smsLogin"), map[string]string{"code": code})
|
||||
|
||||
that.Display(0, "发送成功")
|
||||
},
|
||||
}
|
115
example/salesman/wechat.go
Normal file
115
example/salesman/wechat.go
Normal file
@ -0,0 +1,115 @@
|
||||
package salesman
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var Wechat = Ctr{
|
||||
"login": func(that *Context) {
|
||||
|
||||
sessionKey := that.Req.FormValue("sessionkey")
|
||||
encryptedData := that.Req.FormValue("encryptedData")
|
||||
iv := that.Req.FormValue("iv")
|
||||
|
||||
if sessionKey == "" || encryptedData == "" || iv == "" {
|
||||
that.Display(3, "参数不足")
|
||||
return
|
||||
}
|
||||
|
||||
eny := miniProgram.GetEncryptor()
|
||||
re, e := eny.Decrypt(sessionKey, encryptedData, iv)
|
||||
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
|
||||
that.Display(0, re.PhoneNumber)
|
||||
|
||||
},
|
||||
"code": func(that *Context) {
|
||||
code := that.Req.FormValue("code")
|
||||
if code == "" {
|
||||
that.Display(3, "缺少code")
|
||||
return
|
||||
}
|
||||
|
||||
a := miniProgram.GetAuth()
|
||||
re, e := a.Code2Session(code)
|
||||
fmt.Println(re)
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
wchat := Map{"openid": re.OpenID,
|
||||
"appid": a.AppID,
|
||||
"state": 0,
|
||||
"sessionkey": re.SessionKey,
|
||||
"unionid": re.UnionID,
|
||||
}
|
||||
//wchat["id"] = that.Db.Insert("wechat", wchat)
|
||||
|
||||
that.Display(0, wchat)
|
||||
return
|
||||
|
||||
},
|
||||
|
||||
////微信注册,0已经完整的注册了,1还没有注册
|
||||
"codeh5": func(that *Context) {
|
||||
|
||||
auth := h5Program.GetOauth()
|
||||
//weixin.GetOpenPlatform()
|
||||
resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
|
||||
if err != nil {
|
||||
that.Display(5, "code错误")
|
||||
return
|
||||
}
|
||||
|
||||
//getUserInfo
|
||||
userInfo, err := auth.GetUserInfo(resToken.AccessToken, resToken.OpenID, "")
|
||||
if err != nil {
|
||||
that.Display(6, "微信个人信息无法获取")
|
||||
return
|
||||
}
|
||||
|
||||
//wechatInfo := ObjToMap(userInfo)
|
||||
wechatInfo := Map{
|
||||
"openid": userInfo.OpenID,
|
||||
"acttoken": resToken.AccessToken,
|
||||
"retoken": resToken.RefreshToken,
|
||||
"appid": that.Config.GetString("wechatAppID"),
|
||||
"unionid": userInfo.Unionid,
|
||||
"nickname": userInfo.Nickname,
|
||||
"avatar": userInfo.HeadImgURL,
|
||||
}
|
||||
|
||||
that.Display(0, wechatInfo)
|
||||
},
|
||||
//网页签名
|
||||
"sign": func(that *Context) {
|
||||
|
||||
if that.Req.FormValue("sign_url") == "" {
|
||||
that.Display(2, "参数不足")
|
||||
return
|
||||
}
|
||||
signUrl := that.Req.FormValue("sign_url")
|
||||
js := h5Program.GetJs()
|
||||
cfg1, e := js.GetConfig(signUrl)
|
||||
if e != nil {
|
||||
that.Display(7, e)
|
||||
return
|
||||
}
|
||||
|
||||
sign := Map{
|
||||
"appId": cfg1.AppID,
|
||||
"timestamp": cfg1.Timestamp,
|
||||
"nonceStr": cfg1.NonceStr,
|
||||
"signature": cfg1.Signature,
|
||||
}
|
||||
|
||||
that.Display(0, sign)
|
||||
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue
Block a user