基础整理

This commit is contained in:
hoteas
2022-05-02 15:42:54 +08:00
parent bdcec2d534
commit 062e849d44
10 changed files with 425 additions and 13 deletions
-1
View File
@@ -15,7 +15,6 @@ import (
// Project 管理端项目
var Project = Proj{
"user": User,
"salesman": Salesman,
"lxcx": Lxcx,
"wechat": Wechat,
+1
View File
@@ -29,6 +29,7 @@ var Wechat = Ctr{
},
"code": func(that *Context) {
code := that.Req.FormValue("code")
if code == "" {
that.Display(3, "缺少code")
+13 -3
View File
@@ -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
View 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
View 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
View 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
View 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)
},
}