框架优化
This commit is contained in:
parent
ce515d991a
commit
5945ed64c6
@ -70,7 +70,13 @@ func (that *Application) Run(router Router) {
|
||||
that.Router = Router{}
|
||||
}
|
||||
for k, v := range router {
|
||||
that.Router[k] = v
|
||||
if that.Router[k] == nil {
|
||||
that.Router[k] = v
|
||||
}
|
||||
|
||||
for k1, v1 := range v {
|
||||
that.Router[k][k1] = v1
|
||||
}
|
||||
}
|
||||
//重新设置MethodRouter//直达路由
|
||||
that.MethodRouter = MethodRouter{}
|
||||
|
@ -3,94 +3,21 @@ package app
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"fmt"
|
||||
"github.com/silenceper/wechat/v2"
|
||||
"github.com/silenceper/wechat/v2/officialaccount"
|
||||
|
||||
//"github.com/silenceper/wechat/v2/cache"
|
||||
"gopkg.in/chanxuehong/wechat.v2/mch/core"
|
||||
"gopkg.in/chanxuehong/wechat.v2/mch/pay"
|
||||
"github.com/silenceper/wechat/v2/miniprogram"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Project 管理端项目
|
||||
var Project = Proj{
|
||||
"wechat": Wechat,
|
||||
"wechat": Wechat,
|
||||
"websocket": WebsocketCtr,
|
||||
}
|
||||
var weixin *wechat.Wechat //微信登录实例
|
||||
var officialAccount *officialaccount.OfficialAccount
|
||||
var miniProgram *miniprogram.MiniProgram
|
||||
|
||||
//var appIns = Application{}
|
||||
var wxpayClient *core.Client
|
||||
func init() {
|
||||
|
||||
//生成随机码的4位随机数
|
||||
func getCode() string {
|
||||
//res := ""
|
||||
//for i := 0; i < 4; i++ {
|
||||
res := ObjToStr(RandX(1000, 9999))
|
||||
//}
|
||||
return res
|
||||
}
|
||||
|
||||
//微信获取个人信息
|
||||
//func Weixin(that *Context) *wechat.Wechat {
|
||||
// if weixin == nil {
|
||||
// cache1 := cache.Cache(WechatCache{that.CacheIns})
|
||||
// config := wechat.Config{Cache: cache1, AppID: that.Config.GetString("wechatAppID"), AppSecret: that.Config.GetString("wechatAppSecret")}
|
||||
// weixin = wechat.NewWechat(&config)
|
||||
// }
|
||||
//
|
||||
// return weixin
|
||||
//}
|
||||
|
||||
//微信获取个人信息
|
||||
func WxPayClient(that *Context, sn string, money int64, openid string) Map {
|
||||
if wxpayClient == nil {
|
||||
wxpayClient = core.NewClient(that.Config.GetString("wechatAppID"), that.Config.GetString("wechatChid"), that.Config.GetString("wechatChkey"), nil)
|
||||
}
|
||||
|
||||
res, err := pay.UnifiedOrder2(wxpayClient, &pay.UnifiedOrderRequest{
|
||||
|
||||
// 必选参数
|
||||
Body: "微信充值", // 商品或支付单简要描述
|
||||
OutTradeNo: sn, // 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
|
||||
TotalFee: money, // ObjToInt64(umoney*100), // `xml:"total_fee"` // 订单总金额,单位为分,详见支付金额
|
||||
//SpbillCreateIP string `xml:"spbill_create_ip"` // APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
|
||||
NotifyURL: that.Config.GetString("wechatPayCallBack"), // string `xml:"notify_url"` // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
|
||||
TradeType: "JSAPI", // string `xml:"trade_type"` // 取值如下:JSAPI,NATIVE,APP,详细说明见参数规定
|
||||
|
||||
// 可选参数
|
||||
DeviceInfo: "WEB", // string `xml:"device_info"` // 终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"
|
||||
// NonceStr string `xml:"nonce_str"` // 随机字符串,不长于32位。NOTE: 如果为空则系统会自动生成一个随机字符串。
|
||||
//SignType string `xml:"sign_type"` // 签名类型,默认为MD5,支持HMAC-SHA256和MD5。
|
||||
// Detail:"充值",// string `xml:"detail"` // 商品名称明细列表
|
||||
//Attach string `xml:"attach"` // 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
|
||||
//FeeType string `xml:"fee_type"` // 符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
|
||||
//TimeStart time.Time `xml:"time_start"` // 订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
|
||||
//TimeExpire time.Time `xml:"time_expire"` // 订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则
|
||||
//GoodsTag string `xml:"goods_tag"` // 商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠
|
||||
// ProductId :"CHONGZHI",// string `xml:"product_id"` // trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
|
||||
// LimitPay string `xml:"limit_pay"` // no_credit--指定不能使用信用卡支付
|
||||
OpenId: openid, //user.GetString("wuid"), // string `xml:"openid"` // rade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。
|
||||
// SubOpenId string `xml:"sub_openid"` // trade_type=JSAPI,此参数必传,用户在子商户appid下的唯一标识。openid和sub_openid可以选传其中之一,如果选择传sub_openid,则必须传sub_appid。
|
||||
// SceneInfo string `xml:"scene_info"` // 该字段用于上报支付的场景信息,针对H5支付有以下三种场景,请根据对应场景上报,H5支付不建议在APP端
|
||||
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
wcpay := Map{
|
||||
"appId": that.Config.Get("wechatAppID"),
|
||||
"timeStamp": ObjToStr(time.Now().Unix()),
|
||||
"nonceStr": getSn(),
|
||||
"package": "prepay_id=" + res.PrepayId,
|
||||
"signType": "MD5",
|
||||
}
|
||||
wcpay["paySign"] = core.JsapiSign(that.Config.GetString("wechatAppID"), wcpay.GetString("timeStamp"), wcpay.GetString("nonceStr"), wcpay.GetString("package"), "MD5", that.Config.GetString("wechatChkey"))
|
||||
|
||||
return wcpay
|
||||
}
|
||||
|
||||
//生成随机码的6位md5
|
||||
|
72
example/app/websocket.go
Normal file
72
example/app/websocket.go
Normal file
@ -0,0 +1,72 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"golang.org/x/net/websocket"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WSClient struct {
|
||||
ID string
|
||||
*websocket.Conn
|
||||
time.Time
|
||||
DeadTime time.Time
|
||||
IsDead bool
|
||||
}
|
||||
|
||||
//websocket链接池
|
||||
var WsUserMap = map[string][]*WSClient{}
|
||||
var WSMasterID = ""
|
||||
|
||||
func WsSendMsg(ws *WSClient, data Map) bool {
|
||||
if WsUserMap[ws.ID] == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range WsUserMap[ws.ID] {
|
||||
if v.IsDead || v == ws {
|
||||
//WsUserMap[ws.ID]=WsUserMap[ws.ID][:k]
|
||||
continue
|
||||
}
|
||||
|
||||
str := data.ToJsonString()
|
||||
v.Conn.Write([]byte(str))
|
||||
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var WebsocketCtr = Ctr{
|
||||
"conn": func(this *Context) {
|
||||
id := this.SessionId
|
||||
if WsUserMap[id] == nil {
|
||||
WsUserMap[id] = []*WSClient{}
|
||||
}
|
||||
hdler := websocket.Handler(func(ws *websocket.Conn) {
|
||||
|
||||
client := &WSClient{ID: id, Conn: ws, Time: time.Now(), DeadTime: time.Now(), IsDead: false}
|
||||
|
||||
WsUserMap[id] = append(WsUserMap[id], client)
|
||||
var message string
|
||||
for true {
|
||||
err := websocket.Message.Receive(ws, &message)
|
||||
if err != nil {
|
||||
client.DeadTime = time.Now()
|
||||
client.IsDead = true
|
||||
return
|
||||
}
|
||||
|
||||
data := Map{}
|
||||
data.JsonToMap(message)
|
||||
WsSendMsg(client, data)
|
||||
//switch data.GetString("type") {
|
||||
//
|
||||
//}
|
||||
|
||||
}
|
||||
})
|
||||
hdler.ServeHTTP(this.Resp, this.Req)
|
||||
},
|
||||
}
|
@ -2,50 +2,149 @@ package app
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/cache"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"code.hoteas.com/golang/hotime/dri/download"
|
||||
"fmt"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||
"time"
|
||||
|
||||
//"fmt"
|
||||
"github.com/silenceper/wechat/v2"
|
||||
//"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/officialaccount/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WechatCache struct {
|
||||
CacheIns
|
||||
}
|
||||
|
||||
func (that WechatCache) Get(key string) interface{} {
|
||||
return that.Cache("x" + key).Data
|
||||
//return nil
|
||||
}
|
||||
func (that WechatCache) Set(key string, val interface{}, timeout time.Duration) error {
|
||||
|
||||
that.Cache("x"+key, val, ObjToInt64(timeout.Seconds()))
|
||||
return nil
|
||||
}
|
||||
func (that WechatCache) IsExist(key string) bool {
|
||||
c := that.Cache("x" + key).Data
|
||||
if c != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
func (that WechatCache) Delete(key string) error {
|
||||
that.Cache("x"+key, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
var Wechat = Ctr{
|
||||
"user": func(that *Context) {
|
||||
if that.Session("wechatInfo").Data == nil {
|
||||
that.Display(2, "没有登录")
|
||||
"login": func(that *Context) {
|
||||
nickname := that.Req.FormValue("nickname")
|
||||
avatar := that.Req.FormValue("avatarUrl")
|
||||
encryptedData := that.Req.FormValue("encryptedData")
|
||||
iv := that.Req.FormValue("iv")
|
||||
|
||||
if that.Session("wechat_id").Data == nil {
|
||||
that.Display(2, "还没有获取token")
|
||||
return
|
||||
}
|
||||
|
||||
that.Display(0, that.Session("wechatInfo").ToMap())
|
||||
if nickname == "" || avatar == "" || encryptedData == "" || iv == "" {
|
||||
that.Display(3, "参数不足")
|
||||
return
|
||||
}
|
||||
|
||||
if weixin == nil {
|
||||
weixin = wechat.NewWechat()
|
||||
memory := cache.NewMemory()
|
||||
cfg := &config.Config{
|
||||
AppID: that.Config.GetString("wechatAppID"),
|
||||
AppSecret: that.Config.GetString("wechatAppSecret"),
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: memory,
|
||||
}
|
||||
|
||||
miniProgram = weixin.GetMiniProgram(cfg)
|
||||
}
|
||||
|
||||
wechatId := that.Session("wechat_id").ToCeilInt()
|
||||
|
||||
wchat := that.Db.Get("wechat", "*", Map{"id": wechatId})
|
||||
|
||||
if wchat == nil {
|
||||
that.Display(4, "找不到该用户")
|
||||
return
|
||||
}
|
||||
|
||||
eny := miniProgram.GetEncryptor()
|
||||
re, e := eny.Decrypt(wchat.GetString("retoken"), encryptedData, iv)
|
||||
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
|
||||
admin := that.Db.Get("admin", "*", Map{"phone": re.PhoneNumber})
|
||||
if admin == nil {
|
||||
that.Display(5, "你不是本系统用户,无法使用本系统")
|
||||
return
|
||||
}
|
||||
|
||||
t := time.Now().Unix()
|
||||
path := time.Now().Format(that.Config.GetString("imgPath"))
|
||||
filename := Md5(ObjToStr(t)) + ".jpg"
|
||||
down := download.Down(avatar, that.Config.GetString("tpt")+"/"+path, filename)
|
||||
if down {
|
||||
avatar = path + filename
|
||||
}
|
||||
|
||||
that.Db.Update("wechat", Map{"admin_id": admin.GetCeilInt("id"), "modify_time": t, "avatar": avatar, "nickname": nickname}, Map{"id": wchat.GetCeilInt64("id")})
|
||||
that.Db.Update("admin", Map{"avatar_img": avatar, "modify_time": t}, Map{"id": admin.GetCeilInt64("id")})
|
||||
|
||||
that.Session("admin_id", admin.GetCeilInt64("id"))
|
||||
that.Display(0, "成功!")
|
||||
},
|
||||
"code": func(that *Context) {
|
||||
|
||||
that.Session("admin_id", 1)
|
||||
that.Display(0, Map{"status": 1, "token": that.SessionId})
|
||||
return
|
||||
|
||||
code := that.Req.FormValue("code")
|
||||
if code == "" {
|
||||
that.Display(3, "缺少code")
|
||||
return
|
||||
}
|
||||
if weixin == nil {
|
||||
weixin = wechat.NewWechat()
|
||||
memory := cache.NewMemory()
|
||||
cfg := &config.Config{
|
||||
AppID: that.Config.GetString("wechatAppID"),
|
||||
AppSecret: that.Config.GetString("wechatAppSecret"),
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: memory,
|
||||
}
|
||||
|
||||
miniProgram = weixin.GetMiniProgram(cfg)
|
||||
}
|
||||
//mini := weixin.GetMiniProgram(cfg)
|
||||
a := miniProgram.GetAuth()
|
||||
re, e := a.Code2Session(code)
|
||||
fmt.Println(re)
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
wchat := that.Db.Get("wechat", "*", Map{"openid": re.OpenID})
|
||||
|
||||
if wchat == nil {
|
||||
wchat = Map{"openid": re.OpenID,
|
||||
"appid": a.AppID,
|
||||
"state": 0,
|
||||
"retoken": re.SessionKey,
|
||||
"modify_time": time.Now().Unix(),
|
||||
"create_time": time.Now().Unix(),
|
||||
"unionid": re.UnionID,
|
||||
}
|
||||
wchat["id"] = that.Db.Insert("wechat", wchat)
|
||||
|
||||
} else {
|
||||
wchat["modify_time"] = time.Now().Unix()
|
||||
wchat["retoken"] = re.SessionKey
|
||||
wchat["unionid"] = re.UnionID
|
||||
that.Db.Update("wechat", wchat, Map{"id": wchat.GetCeilInt("id")})
|
||||
}
|
||||
|
||||
that.Session("wechat_id", wchat.GetCeilInt("id"))
|
||||
|
||||
if wchat.GetCeilInt("admin_id") == 0 {
|
||||
that.Display(0, Map{"status": 0, "token": that.SessionId})
|
||||
return
|
||||
}
|
||||
|
||||
that.Session("admin_id", wchat.GetCeilInt("admin_id"))
|
||||
that.Display(0, Map{"status": 1, "token": that.SessionId})
|
||||
return
|
||||
|
||||
},
|
||||
////微信注册,0已经完整的注册了,1还没有注册
|
||||
//"codebase": func(that *Context) {
|
||||
@ -147,45 +246,32 @@ var Wechat = Ctr{
|
||||
// that.Display(0, Map{"type": 1, "token": that.SessionId})
|
||||
//},
|
||||
//网页签名
|
||||
"sign": func(that *Context) {
|
||||
|
||||
if that.Req.FormValue("sign_url") == "" {
|
||||
that.Display(2, "参数不足")
|
||||
return
|
||||
}
|
||||
signUrl := that.Req.FormValue("sign_url")
|
||||
signUrl = "https://hycb.hoteas.com/wxapp"
|
||||
if weixin == nil {
|
||||
weixin = wechat.NewWechat()
|
||||
memory := cache.NewMemory()
|
||||
cfg := &config.Config{
|
||||
AppID: that.Config.GetString("wechatAppID"),
|
||||
AppSecret: that.Config.GetString("wechatAppSecret"),
|
||||
//Token: "xxx",
|
||||
//EncodingAESKey: "xxxx",
|
||||
Cache: memory,
|
||||
}
|
||||
//config := wechat.Config{ AppID: that.Config.GetString("wechatAppID"), AppSecret: that.Config.GetString("wechatAppSecret")}
|
||||
//weixin = wechat.NewWechat()
|
||||
officialAccount = weixin.GetOfficialAccount(cfg)
|
||||
}
|
||||
js := officialAccount.GetJs()
|
||||
//js.GetConfig()
|
||||
//js := weixin.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)
|
||||
|
||||
},
|
||||
//"sign": func(that *Context) {
|
||||
//
|
||||
// if that.Req.FormValue("sign_url") == "" {
|
||||
// that.Display(2, "参数不足")
|
||||
// return
|
||||
// }
|
||||
// signUrl := that.Req.FormValue("sign_url")
|
||||
// signUrl = "https://hycb.hoteas.com/wxapp"
|
||||
//
|
||||
// js := officialAccount.GetJs()
|
||||
// //js.GetConfig()
|
||||
// //js := weixin.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)
|
||||
//
|
||||
//},
|
||||
}
|
||||
|
@ -56,7 +56,7 @@
|
||||
"smsKey": "b0eb4bf0198b9983cffcb85b69fdf4fa",
|
||||
"smsLogin": "【政企超链接】您的验证码为:{code},请在5分钟内使用,切勿将验证码泄露于他人,如非本人操作请忽略。",
|
||||
"tpt": "tpt",
|
||||
"wechatAppID": "wx2edb802f5c3ae1ae",
|
||||
"wechatAppSecret": "4ff97e523c3de6bad47051b568522386",
|
||||
"wechatAppID": "wx6126460a528a20e5",
|
||||
"wechatAppSecret": "1dfdd8079414c795950d71bc92d0f1bd",
|
||||
"wxFilePath": "wxfile/2006/01/02/"
|
||||
}
|
@ -17,14 +17,22 @@ func main() {
|
||||
//a:=Map{}
|
||||
//a.GetBool()
|
||||
appIns.SetConnectListener(func(that *Context) (isFinished bool) {
|
||||
that.Session("admin_id", 1)
|
||||
//if len(that.RouterString) == 3 {
|
||||
// if that.HandlerStr == "/app/hotime/test" {
|
||||
// that.Session("admin_id", 1)
|
||||
// that.Display(0, "开始测试")
|
||||
// return true
|
||||
// }
|
||||
//}
|
||||
//that.Session("admin_id", 1)
|
||||
if len(that.RouterString) == 3 {
|
||||
if that.HandlerStr == "/app/hotime/test" {
|
||||
that.Session("admin_id", 1)
|
||||
that.Display(0, that.SessionId)
|
||||
return true
|
||||
}
|
||||
|
||||
if that.RouterString[1] == "wechat" || that.RouterString[1] == "websocket" {
|
||||
if appIns.MethodRouter[that.HandlerStr] != nil {
|
||||
appIns.MethodRouter[that.HandlerStr](that)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return isFinished
|
||||
})
|
||||
//appIns.Db.Action(func(db db.HoTimeDB) (isSuccess bool) {
|
||||
|
Loading…
Reference in New Issue
Block a user