框架优化
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
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"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Project 管理端项目
|
||||
var Project = Proj{
|
||||
"wechat": Wechat,
|
||||
}
|
||||
var weixin *wechat.Wechat //微信登录实例
|
||||
var officialAccount *officialaccount.OfficialAccount
|
||||
|
||||
//var appIns = Application{}
|
||||
var wxpayClient *core.Client
|
||||
|
||||
//生成随机码的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
|
||||
func getSn() string {
|
||||
x := Rand(8)
|
||||
return Substr(Md5(ObjToStr(int64(x)+time.Now().UnixNano()+int64(Rand(6)))), 0, 6)
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/cache"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
|
||||
//"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, "没有登录")
|
||||
}
|
||||
|
||||
that.Display(0, that.Session("wechatInfo").ToMap())
|
||||
},
|
||||
////微信注册,0已经完整的注册了,1还没有注册
|
||||
//"codebase": func(that *Context) {
|
||||
// wx := Weixin(that)
|
||||
// auth := wx.GetOauth()
|
||||
// resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
|
||||
// if err != nil {
|
||||
// that.Display(6, "code错误")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// //判断用户是否已经注册
|
||||
// user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID})
|
||||
// if user != nil {
|
||||
// that.Session("user_id", user.Get("id"))
|
||||
// that.Display(0, 0)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// user_id := that.Db.Insert("user", Map{"time": time.Now().Unix(), "state": 2})
|
||||
// if user_id == 0 {
|
||||
// that.Display(4, "创建用户失败")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// wid := that.Db.Insert("wechat", Map{"openid": resToken.OpenID, "appid": that.Config.GetString("wechatAppID"), "state": 1, "user_id": user_id})
|
||||
// if wid == 0 {
|
||||
// that.Display(4, "关联微信失败!")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// that.Session("user_id", user.Get("id"))
|
||||
//
|
||||
// that.Display(0, 1)
|
||||
//},
|
||||
////微信注册,0已经完整的注册了,1还没有注册
|
||||
//"code": func(that *Context) {
|
||||
//
|
||||
// orgId := ObjToInt(that.Req.FormValue("org_id"))
|
||||
// //if orgId==0{
|
||||
// // that.Display(3, "缺少组织id")
|
||||
// // return
|
||||
// //}
|
||||
//
|
||||
// wx := Weixin(that)
|
||||
// auth := wx.GetOauth()
|
||||
// resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
|
||||
// if err != nil {
|
||||
// that.Display(5, "code错误")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// //判断用户是否已经注册
|
||||
// user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID})
|
||||
// if user != nil && user.GetCeilInt("id") != 0 && user.GetCeilInt("state") == 0 {
|
||||
// that.Session("user_id", user.Get("id"))
|
||||
// that.Display(0, Map{"type": 0, "token": that.SessionId})
|
||||
// return
|
||||
// }
|
||||
// //getUserInfo
|
||||
// userInfo, err := auth.GetUserInfo(resToken.AccessToken, resToken.OpenID)
|
||||
// if err != nil {
|
||||
// that.Display(6, "微信个人信息无法获取")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// //wechatInfo := ObjToMap(userInfo)
|
||||
// t := time.Now().Unix()
|
||||
// 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,
|
||||
// }
|
||||
// if orgId != 0 {
|
||||
// wechatInfo["org_id"] = orgId
|
||||
// wechatInfo["status"] = 0
|
||||
// }
|
||||
//
|
||||
// wechatDb := that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")})
|
||||
//
|
||||
// if wechatDb != nil {
|
||||
//
|
||||
// that.Db.Update("wechat", wechatInfo, Map{"id": wechatDb.GetCeilInt("id")})
|
||||
// //userInfo["wid"]=wechatDb.GetCeilInt("wid")
|
||||
// } else {
|
||||
// wechatInfo["create_time"] = t
|
||||
// wechatInfo["id"] = that.Db.Insert("wechat", wechatInfo)
|
||||
// }
|
||||
// wechatDb = that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")})
|
||||
//
|
||||
// that.Session("wechatInfo", wechatDb)
|
||||
// fmt.Println(wechatDb)
|
||||
// fmt.Println(that.Session("wechatInfo"))
|
||||
// //that.Display(0, 1)
|
||||
// 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)
|
||||
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user