hcmp/dbop/redisop.go
2024-07-31 15:47:18 +08:00

113 lines
2.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dbop
import (
"encoding/json"
"time"
"github.com/shockliu/logger"
"github.com/go-redis/redis"
//"errors"
)
const (
RD_TRADE_HEAD = "ORDER:"
RD_MACHINE_HEAD = "ZDDevice:"
RD_SESSION_HEAD = "SessionKey:"
RD_SALERSESSION_HEAD = "SalerSessionKey:"
RD_ICCARD_HEAD = "IC:"
RD_SALE_HEAD = "SALE:"
RD_DEVSTAT_HEAD = "MACHINE:"
RD_ALARM_HEAD = "ALARM:"
)
var (
RDb *redis.Client
)
// Redis 预订单状态存储,临时存储,支付后就进入数据库
type OrderInfo struct {
McId string `json:"mcId"` // 机器
TradeNo string `json:"tradeNo"` // 订单
ProdName string `json:"ProdName"` // 产品名称
ProdCode string `json:"ProdCode"` // 产品编码
FavorCode string `json:"FavorCode"` // 口味ID下发制作使用
Amount int `json:"amount"` // 价格
PayType string `json:"payType"` // 支付类型
}
// 初始化连接
func init() {
RDb = redis.NewClient(&redis.Options{
Addr: "47.118.40.174:6379",
Password: "88480288", // no password set
DB: 0, // use default DB
PoolSize: 100, // 连接池大小
})
_, err = RDb.Ping().Result()
if err != nil {
panic("Redis初始化失败")
}
}
/*
// 待支付订单列表 dbop.InsertTrade(McId,tradeNo,data.ProdName,data.amount,"wxpay")
func InsertRedisTrade(McId,tradeNo,Name,code string,amount int,pchan string){
s,_:=json.Marshal(OrderInfo{McId,tradeNo,Name,code,amount,pchan})
RDb.Set(RD_TRADE_HEAD+tradeNo,s,30*time.Minute)
}
*/
/*
// 订单状态修改
func UpdateRedisTrade(tradeNo,status string) bool{
s,err:=RDb.Get(RD_TRADE_HEAD+tradeNo).Result()
if(err==redis.Nil){
return false
}
var data OrderInfo
_ = json.Unmarshal([]byte(s), &data)
data.PayStatus=status
st,_:=json.Marshal(data)
RDb.Set(RD_TRADE_HEAD+tradeNo,string(st),10*time.Minute)
return true
}
*/
// 获取订单支付信息
func GetRedisTradeInfo(TradeNo string) (rst OrderInfo, ok bool) {
s, err := RDb.Get(RD_TRADE_HEAD + TradeNo).Result()
if err != nil {
//if(err==redis.Nil){
logger.Errorf("获取Redis订单信息[%s]失败:%v\n", RD_TRADE_HEAD+TradeNo, err)
return
} // 其它错误未捕获 err!=nil
_ = json.Unmarshal([]byte(s), &rst)
ok = true
return
}
/*
// 获取订单支付状态
func GetRedisTradeStatus(TradeNo string) string{
s,err:=RDb.Get(RD_TRADE_HEAD+TradeNo).Result()
if(err==redis.Nil){
logger.Warnf("Redis no Key\n")
return "NO_EXIST"
} // 其它错误未捕获 err!=nil
var data OrderInfo
_ = json.Unmarshal([]byte(s), &data)
return data.PayStatus
}
*/
func SaveSalerSessionKey(openid, sessionkey string) {
RDb.Set(RD_SALERSESSION_HEAD+openid, sessionkey, 240*time.Hour)
}
func SaveSessionKey(openid, sessionkey string) {
RDb.Set(RD_SESSION_HEAD+openid, sessionkey, 240*time.Hour)
}
func ClearSessionKey(openid string) {
RDb.Del(RD_SESSION_HEAD + openid)
}