@@ -3,9 +3,9 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
@@ -17,22 +17,22 @@ func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { | |||
req.PvdType = "ordinary" | |||
} | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByOne(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) | |||
list, err := db.GetCouponUserTotalByOne(session, utils.StrToInt(req.Uid), req.PvdType) | |||
if err != nil { | |||
return err | |||
} else if list == nil { | |||
list = &model.CommCouponUserTotal{ | |||
Uid: zhios_tool_utils.StrToInt(req.Uid), | |||
Uid: utils.StrToInt(req.Uid), | |||
CreateAt: time.Now(), | |||
UpdateAt: time.Now(), | |||
PvdType: req.PvdType, | |||
} | |||
} | |||
LeaveCouponAmountValue := list.LeaveCouponAmountValue | |||
list.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.LeaveCouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) | |||
list.LeaveCouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.LeaveCouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2)) | |||
ordType := 4 | |||
if req.Type == "" { | |||
list.CouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.CouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) | |||
list.CouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.CouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2)) | |||
} | |||
if req.Type == "refund" { | |||
ordType = 5 | |||
@@ -65,7 +65,7 @@ func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
return zhios_tool_e.NewErr(400, "失败") | |||
return e.NewErr(400, "失败") | |||
} | |||
return nil | |||
} |
@@ -3,7 +3,7 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"strings" | |||
"xorm.io/xorm" | |||
) | |||
@@ -17,7 +17,7 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
"base_coupon_time": "1440", | |||
} | |||
//查查剩余额度 | |||
userCouponAmount, err := db.GetCouponUserTotalByLeaveSumWithType(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
userCouponAmount, err := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
if err != nil { | |||
return r | |||
} | |||
@@ -32,15 +32,15 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
isCanBuy := 1 | |||
isShowCouponList := 1 | |||
//判断额度够不够 并且有没有开启 | |||
if zhios_tool_utils.StrToFloat64(req.CouponAmount) > userCouponAmount { | |||
if utils.StrToFloat64(req.CouponAmount) > userCouponAmount { | |||
isCanBuy = 0 | |||
} | |||
if zhios_tool_utils.StrToFloat64(req.CouponAmount) == 0 { | |||
if zhios_tool_utils.StrToInt(req.CheckAmount) == 1 { | |||
if utils.StrToFloat64(req.CouponAmount) == 0 { | |||
if utils.StrToInt(req.CheckAmount) == 1 { | |||
isCanBuy = 1 | |||
isShowCouponList = 0 | |||
} | |||
if zhios_tool_utils.StrToInt(req.CheckAmount) == 0 && userCouponAmount == 0 { | |||
if utils.StrToInt(req.CheckAmount) == 0 && userCouponAmount == 0 { | |||
isCanBuy = 0 | |||
isShowCouponList = 1 | |||
} | |||
@@ -58,12 +58,12 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
} | |||
r = map[string]string{ | |||
"user_coupon_amount": zhios_tool_utils.Float64ToStr(userCouponAmount), | |||
"is_show_coupon_list": zhios_tool_utils.IntToStr(isShowCouponList), | |||
"is_can_buy": zhios_tool_utils.IntToStr(isCanBuy), | |||
"base_coupon_time": zhios_tool_utils.IntToStr(baseList.RefundTime), | |||
"user_coupon_amount": utils.Float64ToStr(userCouponAmount), | |||
"is_show_coupon_list": utils.IntToStr(isShowCouponList), | |||
"is_can_buy": utils.IntToStr(isCanBuy), | |||
"base_coupon_time": utils.IntToStr(baseList.RefundTime), | |||
} | |||
if zhios_tool_utils.IntToStr(baseList.RefundTime) == "0" { | |||
if utils.IntToStr(baseList.RefundTime) == "0" { | |||
r["base_coupon_time"] = "1440" | |||
} | |||
return r | |||
@@ -3,9 +3,9 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
@@ -16,31 +16,31 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
r := CommCheck(eg, req) | |||
fmt.Println(r) | |||
if r["is_can_buy"] == "0" && req.IsMustReduce != "1" { | |||
return zhios_tool_e.NewErr(400, "暂不能购买") | |||
return e.NewErr(400, "暂不能购买") | |||
} | |||
if r["is_show_coupon_list"] == "0" && req.IsMustReduce != "1" { | |||
return nil | |||
} | |||
//判断额度够不够 | |||
sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
fmt.Println(sum) | |||
if sum < zhios_tool_utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { | |||
return zhios_tool_e.NewErr(400, "额度不足") | |||
if sum < utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { | |||
return e.NewErr(400, "额度不足") | |||
} | |||
session := eg.NewSession() | |||
defer session.Close() | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) | |||
list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, utils.StrToInt(req.Uid), req.PvdType) | |||
if list == nil || err != nil { | |||
session.Rollback() | |||
return err | |||
} | |||
//负数的记录 | |||
negative, _ := db.GetCouponUserTotalByNegative(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
negative, _ := db.GetCouponUserTotalByNegative(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
//循环出来判断扣钱 | |||
var leaveAmount = zhios_tool_utils.StrToFloat64(req.CouponAmount) | |||
var leaveAmount = utils.StrToFloat64(req.CouponAmount) | |||
num := 0 | |||
one, err := db.CouponUserOrdByGid(session, zhios_tool_utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) | |||
one, err := db.CouponUserOrdByGid(session, utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) | |||
if err != nil { | |||
session.Rollback() | |||
return err | |||
@@ -54,13 +54,13 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
var count = len(*list) | |||
for k, v := range *list { | |||
LeaveCouponAmountValue := v.LeaveCouponAmountValue | |||
if leaveAmount == 0 || (zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 | |||
if leaveAmount == 0 || (utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 | |||
continue | |||
} | |||
//减去负数的金额 | |||
var amount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) | |||
var amount = utils.StrToFloat64(v.LeaveCouponAmountValue) | |||
//当前能使用的额度 | |||
var canUseAmount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) + negative | |||
var canUseAmount = utils.StrToFloat64(v.LeaveCouponAmountValue) + negative | |||
var basicAmount = amount - canUseAmount | |||
if canUseAmount > 0 { | |||
amount = canUseAmount | |||
@@ -81,13 +81,13 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
leaveAmount = leaveAmount - amount | |||
} | |||
leave = leave + basicAmount | |||
v.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(leave) | |||
v.LeaveCouponAmountValue = utils.Float64ToStr(leave) | |||
fmt.Println(v.LeaveCouponAmountValue) | |||
has, err := db.CouponUserTotalByInsertWithUpdate(session, &v) | |||
if (has == 0 && leave != amount) || err != nil { | |||
fmt.Println(err) | |||
session.Rollback() | |||
return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") | |||
return e.NewErr(400, "抵扣失败,暂不能购买!") | |||
} | |||
if reduceAmount != 0 { | |||
@@ -98,7 +98,7 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
OrdType: 3, | |||
UsePvd: v.UsePvd, | |||
PackageId: 0, | |||
CouponAmountValue: zhios_tool_utils.Float64ToStr(reduceAmount), | |||
CouponAmountValue: utils.Float64ToStr(reduceAmount), | |||
PayAmountValue: "", | |||
Discount: "", | |||
PayAmount: req.CouponAmount, | |||
@@ -109,19 +109,19 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
Type: 1, | |||
Gid: req.Gid, | |||
Batch: num, | |||
ExpectedRefundTime: int(time.Now().Unix()) + zhios_tool_utils.StrToInt(r["base_coupon_time"])*60, | |||
ExpectedRefundTime: int(time.Now().Unix()) + utils.StrToInt(r["base_coupon_time"])*60, | |||
GoodsTitle: req.GoodsTitle, | |||
Oid: req.Oid, | |||
BeforeAmout: LeaveCouponAmountValue, | |||
AfterAmout: v.LeaveCouponAmountValue, | |||
} | |||
if req.CreateAt != "" { | |||
tmp.CreateAt = zhios_tool_utils.TimeParseStd(req.CreateAt) | |||
tmp.CreateAt = utils.TimeParseStd(req.CreateAt) | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
session.Rollback() | |||
return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") | |||
return e.NewErr(400, "抵扣失败,暂不能购买!") | |||
} | |||
} | |||
@@ -2,7 +2,7 @@ package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
zhios_tool_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"fmt" | |||
"xorm.io/xorm" | |||
) | |||
@@ -149,7 +149,7 @@ func commWhere(Db *xorm.Engine, uid int, types, pvdType, times string, ordType [ | |||
userOne, _ := UserFindByPhoneOrNickname(Db, keyword) | |||
uids := "0" | |||
if userOne != nil { | |||
uids = zhios_tool_utils.IntToStr(userOne.Uid) | |||
uids = utils.IntToStr(userOne.Uid) | |||
} | |||
dbWhere = dbWhere.And("to_uid = ? and to_uid>0", uids) | |||
} | |||
@@ -2,7 +2,7 @@ package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
zhios_tool_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"xorm.io/xorm" | |||
) | |||
@@ -24,14 +24,14 @@ func GetCouponUserByUid(Db *xorm.Engine, uid string) (*[]model.CommCouponUserTot | |||
func GetCouponUserTotalByLeaveSumWithTypeSession(session *xorm.Session, uid int, pvdType string) (*[]model.CommCouponUserTotal, error) { | |||
var m []model.CommCouponUserTotal | |||
if err := session.Where("uid = " + zhios_tool_utils.IntToStr(uid) + " and leave_coupon_amount_value>=0 and (pvd_type LIKE '%" + pvdType + "%' or pvd_type='ordinary' )").Asc("leave_coupon_amount_value").Find(&m); err != nil { | |||
if err := session.Where("uid = " + utils.IntToStr(uid) + " and leave_coupon_amount_value>=0 and (pvd_type LIKE '%" + pvdType + "%' or pvd_type='ordinary' )").Asc("leave_coupon_amount_value").Find(&m); err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func GetCouponUserTotalByNegative(Db *xorm.Engine, uid int, pvdType, cols string) (float64, error) { | |||
var m model.CommCouponUserTotal | |||
total, err := Db.Where("uid = "+zhios_tool_utils.IntToStr(uid)+" and leave_coupon_amount_value<0 and (pvd_type LIKE '%"+pvdType+"%' or pvd_type='ordinary' )").Sum(m, cols) | |||
total, err := Db.Where("uid = "+utils.IntToStr(uid)+" and leave_coupon_amount_value<0 and (pvd_type LIKE '%"+pvdType+"%' or pvd_type='ordinary' )").Sum(m, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
@@ -2,14 +2,14 @@ package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
zhios_tool_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"fmt" | |||
"xorm.io/xorm" | |||
) | |||
func GetCouponUserTotalByLeaveSumWithType(Db *xorm.Engine, uid int, pvdType, cols string) (float64, error) { | |||
var m model.CommCouponUserTotal | |||
total, err := Db.Where("uid = "+zhios_tool_utils.IntToStr(uid)+" and (pvd_type LIKE '%"+pvdType+"%' or pvd_type='ordinary' )").Sum(m, cols) | |||
total, err := Db.Where("uid = "+utils.IntToStr(uid)+" and (pvd_type LIKE '%"+pvdType+"%' or pvd_type='ordinary' )").Sum(m, cols) | |||
if err != nil { | |||
fmt.Println(err) | |||
return 0, err | |||
@@ -0,0 +1,242 @@ | |||
package e | |||
const ( | |||
// 200 因为部分第三方接口不能返回错误头,因此在此定义部分错误 | |||
ERR_FILE_SAVE = 200001 | |||
// 400 系列 | |||
ERR_BAD_REQUEST = 400000 | |||
ERR_INVALID_ARGS = 400001 | |||
ERR_API_RESPONSE = 400002 | |||
ERR_NO_DATA = 400003 | |||
ERR_MOBILE_NIL = 400004 | |||
ERR_MOBILE_MATH = 400005 | |||
ERR_FILE_EXT = 400006 | |||
ERR_FILE_MAX_SIZE = 400007 | |||
ERR_SIGN = 400008 | |||
ERR_PASSWORD_MATH = 400009 | |||
ERR_PROVIDER_RESPONSE = 400010 | |||
ERR_AES_ENCODE = 400011 | |||
ERR_ADMIN_API = 400012 | |||
ERR_QINIUAPI_RESPONSE = 400013 | |||
ERR_URL_TURNCHAIN = 400014 | |||
// 401 未授权 | |||
ERR_UNAUTHORIZED = 401000 | |||
ERR_NOT_AUTH = 401001 | |||
ERR_SMS_AUTH = 401002 | |||
ERR_TOKEN_AUTH = 401003 | |||
ERR_TOKEN_FORMAT = 401004 | |||
ERR_TOKEN_GEN = 401005 | |||
// 403 禁止 | |||
ERR_FORBIDEN = 403000 | |||
ERR_PLATFORM = 403001 | |||
ERR_MOBILE_EXIST = 403002 | |||
ERR_USER_NO_EXIST = 403003 | |||
ERR_MOBILE_NO_EXIST = 403004 | |||
ERR_FORBIDEN_VALID = 403005 | |||
ERR_RELATE_ERR = 403006 | |||
ERR_REPEAT_RELATE = 403007 | |||
ERR_MOB_FORBIDEN = 403008 | |||
ERR_MOB_SMS_NO_AVA = 403009 | |||
ERR_USER_IS_REG = 403010 | |||
ERR_MASTER_ID = 403011 | |||
ERR_CASH_OUT_TIME = 403012 | |||
ERR_CASH_OUT_FEE = 403013 | |||
ERR_CASH_OUT_USER_NOT_FOUND = 403014 | |||
ERR_CASH_OUT_FAIL = 403015 | |||
ERR_CASH_OUT_TIMES = 403016 | |||
ERR_CASH_OUT_MINI = 403017 | |||
ERR_CASH_OUT_MUT = 403018 | |||
ERR_CASH_OUT_NOT_DECIMAL = 403019 | |||
ERR_CASH_OUT_NOT_DAY_AVA = 403020 | |||
ERR_USER_LEVEL_PAY_CHECK_TASK_NO_DONE = 403021 | |||
ERR_USER_LEVEL_PAY_CHECK_NO_CROSS = 403022 | |||
ERR_USER_LEVEL_ORD_EXP = 403023 | |||
ERR_IS_BIND_THIRDPARTY = 403024 | |||
ERR_USER_LEVEL_UPDATE_CHECK_TASK_NO_DONE = 403025 | |||
ERR_USER_LEVEL_UPDATE_CHECK_NOT_FOUND_ORDER = 403026 | |||
ERR_USER_LEVEL_UPDATE_REPEAT = 403027 | |||
ERR_USER_NO_ACTIVE = 403028 | |||
ERR_USER_IS_BAN = 403029 | |||
ERR_ALIPAY_SETTING = 403030 | |||
ERR_ALIPAY_ORDERTYPE = 403031 | |||
ERR_CLIPBOARD_UNSUP = 403032 | |||
ERR_SYSUNION_CONFIG = 403033 | |||
ERR_WECAHT_MINI = 403034 | |||
ERR_WECAHT_MINI_CACHE = 403035 | |||
ERR_WECAHT_MINI_DECODE = 403036 | |||
ERR_WECHAT_MINI_ACCESSTOKEN = 403037 | |||
ERR_CURRENT_VIP_LEVEL_AUDITING = 403038 | |||
ERR_LEVEL_RENEW_SHOULD_KEEP_CURRENT = 403039 | |||
ERR_LEVEL_UPGRADE_APPLY_AUDITTING = 403040 | |||
ERR_LEVEL_TASK_PAY_TYPE = 403041 | |||
ERR_BALANCE_NOT_ENOUGH = 403042 | |||
ERR_ADMIN_PUSH = 403043 | |||
ERR_PLAN = 403044 | |||
ERR_MOB_CONFIG = 403045 | |||
ERR_BAlANCE_PAY_ORDERTYPE = 403046 | |||
ERR_PHONE_EXISTED = 403047 | |||
ERR_NOT_RESULT = 403048 | |||
ERR_REVIEW = 403049 | |||
ERR_USER_LEVEL_HAS_PAID = 403050 | |||
ERR_USER_BIND_OWN = 403051 | |||
ERR_PARENTUID_ERR = 403052 | |||
ERR_USER_DEL = 403053 | |||
ERR_SEARCH_ERR = 403054 | |||
ERR_LEVEL_REACH_TOP = 403055 | |||
ERR_USER_CHECK_ERR = 403056 | |||
ERR_PASSWORD_ERR = 403057 | |||
ERR_IS_BIND_PDD_ERR = 403058 | |||
ERR_MOB_SMS_NO_SAME = 403059 | |||
ERR_MOB_SMS_NO_EXISTS = 403060 | |||
// 404 | |||
ERR_USER_NOTFOUND = 404001 | |||
ERR_SUP_NOTFOUND = 404002 | |||
ERR_LEVEL_MAP = 404003 | |||
ERR_MOD_NOTFOUND = 404004 | |||
ERR_CLIPBOARD_PARSE = 404005 | |||
ERR_NOT_FAN = 404006 | |||
ERR_USER_LEVEL = 404007 | |||
ERR_LACK_PAY_CFG = 404008 | |||
ERR_NOT_LEVEL_TASK = 404009 | |||
ERR_ITEM_NOT_FOUND = 404010 | |||
ERR_WX_CHECKFILE_NOTFOUND = 404011 | |||
// 429 请求频繁 | |||
ERR_TOO_MANY_REQUESTS = 429000 | |||
// 500 系列 | |||
ERR = 500000 | |||
ERR_UNMARSHAL = 500001 | |||
ERR_UNKNOWN = 500002 | |||
ERR_SMS = 500003 | |||
ERR_ARKID_REGISTER = 500004 | |||
ERR_ARKID_WHITELIST = 500005 | |||
ERR_ARKID_LOGIN = 500006 | |||
ERR_CFG = 500007 | |||
ERR_DB_ORM = 500008 | |||
ERR_CFG_CACHE = 500009 | |||
ERR_ZHIMENG_CONVERT_ERR = 500010 | |||
ERR_ALIPAY_ERR = 500011 | |||
ERR_ALIPAY_ORDER_ERR = 500012 | |||
ERR_PAY_ERR = 500013 | |||
ERR_IS_BIND_THIRDOTHER = 500014 | |||
ERR_IS_BIND_THIRDOTHERWECHAT = 500015 | |||
) | |||
var MsgFlags = map[int]string{ | |||
// 200 | |||
ERR_FILE_SAVE: "文件保存失败", | |||
// 400 | |||
ERR_BAD_REQUEST: "请求失败", | |||
ERR_INVALID_ARGS: "请求参数错误", | |||
ERR_API_RESPONSE: "API错误", | |||
ERR_QINIUAPI_RESPONSE: "七牛请求API错误", | |||
ERR_URL_TURNCHAIN: "转链失败", | |||
ERR_NO_DATA: "暂无数据", | |||
ERR_MOBILE_NIL: "电话号码不能为空", | |||
ERR_MOBILE_MATH: "电话号码输入有误", | |||
ERR_FILE_MAX_SIZE: "文件上传大小超限", | |||
ERR_FILE_EXT: "文件类型不支持", | |||
ERR_SIGN: "签名校验失败", | |||
ERR_PROVIDER_RESPONSE: "提供商接口错误", | |||
ERR_AES_ENCODE: "加解密错误", | |||
ERR_ADMIN_API: "后台接口请求失败", | |||
// 401 | |||
ERR_NOT_AUTH: "请登录后操作", | |||
ERR_SMS_AUTH: "验证码过期或无效", | |||
ERR_UNAUTHORIZED: "验证用户失败", | |||
ERR_TOKEN_FORMAT: "Token格式不对", | |||
ERR_TOKEN_GEN: "生成Token失败", | |||
// 403 | |||
ERR_FORBIDEN: "禁止访问", | |||
ERR_PLATFORM: "平台不支持", | |||
ERR_MOBILE_EXIST: "该号码已注册过", | |||
ERR_USER_NO_EXIST: "用户没有注册或账号密码不正确", | |||
ERR_PASSWORD_ERR: "输入两次密码不一致", | |||
ERR_RELATE_ERR: "推荐人不能是自己的粉丝", | |||
ERR_PARENTUID_ERR: "推荐人不存在", | |||
ERR_TOKEN_AUTH: "登录信息失效,请重新登录", | |||
ERR_MOB_SMS_NO_AVA: "短信余额不足", | |||
ERR_MOB_SMS_NO_SAME: "验证码不一致", | |||
ERR_MOB_SMS_NO_EXISTS: "验证码已失效", | |||
ERR_USER_IS_REG: "用户已注册", | |||
ERR_MASTER_ID: "找不到对应站长的数据库", | |||
ERR_CASH_OUT_TIME: "非可提现时间段", | |||
ERR_CASH_OUT_USER_NOT_FOUND: "收款账号不存在", | |||
ERR_CASH_OUT_FAIL: "提现失败", | |||
ERR_CASH_OUT_FEE: "提现金额必须大于手续费", | |||
ERR_CASH_OUT_TIMES: "当日提现次数已达上线", | |||
ERR_CASH_OUT_MINI: "申请提现金额未达到最低金额要求", | |||
ERR_CASH_OUT_MUT: "申请提现金额未达到整数倍要求", | |||
ERR_CASH_OUT_NOT_DECIMAL: "提现申请金额只能是整数", | |||
ERR_CASH_OUT_NOT_DAY_AVA: "不在可提现日期范围内", | |||
ERR_USER_LEVEL_PAY_CHECK_TASK_NO_DONE: "请先完成其他任务", | |||
ERR_USER_LEVEL_PAY_CHECK_NO_CROSS: "无法跨越升级", | |||
ERR_USER_LEVEL_ORD_EXP: "付费订单已失效", | |||
ERR_IS_BIND_THIRDPARTY: "该用户已经绑定了", | |||
ERR_IS_BIND_THIRDOTHER: "该账号已经被绑定了", | |||
ERR_IS_BIND_THIRDOTHERWECHAT: "该账号已经绑定了其他微信账号", | |||
ERR_USER_LEVEL_UPDATE_CHECK_TASK_NO_DONE: "请完成指定任务", | |||
ERR_USER_LEVEL_UPDATE_CHECK_NOT_FOUND_ORDER: "没有找到对应的订单", | |||
ERR_USER_LEVEL_UPDATE_REPEAT: "不允许重复升级", | |||
ERR_USER_NO_ACTIVE: "账户没激活", | |||
ERR_USER_IS_BAN: "账户已被冻结", | |||
ERR_SYSUNION_CONFIG: "联盟设置错误,请检查配置", | |||
ERR_WECAHT_MINI: "小程序响应错误,请检查小程序配置", | |||
ERR_WECAHT_MINI_CACHE: "获取小程序缓存失败", | |||
ERR_WECAHT_MINI_DECODE: "小程序解密失败", | |||
ERR_WECHAT_MINI_ACCESSTOKEN: "无法获取accesstoekn", | |||
ERR_CURRENT_VIP_LEVEL_AUDITING: "当前等级正在审核中", | |||
ERR_LEVEL_RENEW_SHOULD_KEEP_CURRENT: "续费只能在当前等级续费", | |||
ERR_LEVEL_UPGRADE_APPLY_AUDITTING: "已有申请正在审核中,暂时不能申请", | |||
ERR_LEVEL_TASK_PAY_TYPE: "任务付费类型错误", | |||
ERR_BALANCE_NOT_ENOUGH: "余额不足", | |||
ERR_ADMIN_PUSH: "后台MOB推送错误", | |||
ERR_PLAN: "分拥方案出错", | |||
ERR_MOB_CONFIG: "Mob 配置错误", | |||
ERR_BAlANCE_PAY_ORDERTYPE: "无效余额支付订单类型", | |||
ERR_PHONE_EXISTED: "手机号码已存在", | |||
ERR_NOT_RESULT: "已加载完毕", | |||
ERR_REVIEW: "审核模板错误", | |||
ERR_USER_LEVEL_HAS_PAID: "该等级已经付过款", | |||
ERR_IS_BIND_PDD_ERR: "获取PDD绑定信息失败", | |||
// 404 | |||
ERR_USER_NOTFOUND: "用户不存在", | |||
ERR_USER_DEL: "账号被删除,如有疑问请联系客服", | |||
ERR_SUP_NOTFOUND: "上级用户不存在", | |||
ERR_LEVEL_MAP: "无等级映射关系", | |||
ERR_MOD_NOTFOUND: "没有找到对应模块", | |||
ERR_CLIPBOARD_PARSE: "无法解析剪切板内容", | |||
ERR_NOT_FAN: "没有粉丝", | |||
ERR_CLIPBOARD_UNSUP: "不支持该平台", | |||
ERR_USER_LEVEL: "该等级已不存在", | |||
ERR_LACK_PAY_CFG: "支付配置不完整", | |||
ERR_NOT_LEVEL_TASK: "等级任务查找错误", | |||
ERR_ITEM_NOT_FOUND: "找不到对应商品", | |||
ERR_WX_CHECKFILE_NOTFOUND: "找不到微信校验文件", | |||
ERR_USER_BIND_OWN: "不能填写自己的邀请码", | |||
// 429 | |||
ERR_TOO_MANY_REQUESTS: "请求频繁,请稍后重试", | |||
// 500 内部错误 | |||
ERR: "接口错误", | |||
ERR_SMS: "短信发送出错", | |||
ERR_CFG: "服务器配置错误", | |||
ERR_UNMARSHAL: "JSON解码错误", | |||
ERR_UNKNOWN: "未知错误", | |||
ERR_ARKID_LOGIN: "登录失败", | |||
ERR_MOBILE_NO_EXIST: "该用户未设定手机号", | |||
ERR_FORBIDEN_VALID: "验证码错误", | |||
ERR_CFG_CACHE: "获取配置缓存失败", | |||
ERR_DB_ORM: "数据操作失败", | |||
ERR_REPEAT_RELATE: "重复关联", | |||
ERR_ZHIMENG_CONVERT_ERR: "智盟转链失败", | |||
ERR_MOB_FORBIDEN: "Mob调用失败", | |||
ERR_ALIPAY_ERR: "支付宝参数错误", | |||
ERR_ALIPAY_SETTING: "请在后台正确配置支付宝", | |||
ERR_ALIPAY_ORDERTYPE: "无效支付宝订单类型", | |||
ERR_ALIPAY_ORDER_ERR: "订单创建错误", | |||
ERR_PAY_ERR: "未找到支付方式", | |||
ERR_SEARCH_ERR: "暂无该分类商品", | |||
ERR_LEVEL_REACH_TOP: "已经是最高等级", | |||
ERR_USER_CHECK_ERR: "校验失败", | |||
} |
@@ -0,0 +1,72 @@ | |||
package e | |||
import ( | |||
"fmt" | |||
"path" | |||
"runtime" | |||
) | |||
type E struct { | |||
Code int // 错误码 | |||
msg string // 报错代码 | |||
st string // 堆栈信息 | |||
} | |||
func NewErrCode(code int) error { | |||
if msg, ok := MsgFlags[code]; ok { | |||
return E{code, msg, stack(3)} | |||
} | |||
return E{ERR_UNKNOWN, "unknown", stack(3)} | |||
} | |||
func NewErr(code int, msg string) error { | |||
return E{code, msg, stack(3)} | |||
} | |||
func NewErrf(code int, msg string, args ...interface{}) error { | |||
return E{code, fmt.Sprintf(msg, args), stack(3)} | |||
} | |||
func (e E) Error() string { | |||
return e.msg | |||
} | |||
func stack(skip int) string { | |||
stk := make([]uintptr, 32) | |||
str := "" | |||
l := runtime.Callers(skip, stk[:]) | |||
for i := 0; i < l; i++ { | |||
f := runtime.FuncForPC(stk[i]) | |||
name := f.Name() | |||
file, line := f.FileLine(stk[i]) | |||
str += fmt.Sprintf("\n%-30s[%s:%d]", name, path.Base(file), line) | |||
} | |||
return str | |||
} | |||
// ErrorIsAccountBan is 检查这个账号是否被禁用的错误 | |||
func ErrorIsAccountBan(e error) bool { | |||
err, ok := e.(E) | |||
if ok && err.Code == 403029 { | |||
return true | |||
} | |||
return false | |||
} | |||
// ErrorIsAccountNoActive is 检查这个账号是否被禁用的错误 | |||
func ErrorIsAccountNoActive(e error) bool { | |||
err, ok := e.(E) | |||
if ok && err.Code == 403028 { | |||
return true | |||
} | |||
return false | |||
} | |||
// ErrorIsUserDel is 检查这个账号是否被删除 | |||
func ErrorIsUserDel(e error) bool { | |||
err, ok := e.(E) | |||
if ok && err.Code == 403053 { | |||
return true | |||
} | |||
return false | |||
} |
@@ -0,0 +1,8 @@ | |||
package e | |||
func SetCache(cacheTime int64) map[string]interface{} { | |||
if cacheTime == 0 { | |||
return map[string]interface{}{"cache_time": cacheTime} | |||
} | |||
return map[string]interface{}{"cache_time": cacheTime} | |||
} |
@@ -5,5 +5,4 @@ go 1.15 | |||
require ( | |||
github.com/go-sql-driver/mysql v1.6.0 | |||
xorm.io/xorm v1.3.0 | |||
code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git v1.0.7 // indirect | |||
) |
@@ -0,0 +1,366 @@ | |||
package utils | |||
import ( | |||
"encoding/binary" | |||
"encoding/json" | |||
"fmt" | |||
"math" | |||
"strconv" | |||
"strings" | |||
) | |||
func ToString(raw interface{}, e error) (res string) { | |||
if e != nil { | |||
return "" | |||
} | |||
return AnyToString(raw) | |||
} | |||
func ToInt64(raw interface{}, e error) int64 { | |||
if e != nil { | |||
return 0 | |||
} | |||
return AnyToInt64(raw) | |||
} | |||
func AnyToBool(raw interface{}) bool { | |||
switch i := raw.(type) { | |||
case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32: | |||
return i != 0 | |||
case []byte: | |||
return i != nil | |||
case string: | |||
if i == "false" { | |||
return false | |||
} | |||
return i != "" | |||
case error: | |||
return false | |||
case nil: | |||
return true | |||
} | |||
val := fmt.Sprint(raw) | |||
val = strings.TrimLeft(val, "&") | |||
if strings.TrimLeft(val, "{}") == "" { | |||
return false | |||
} | |||
if strings.TrimLeft(val, "[]") == "" { | |||
return false | |||
} | |||
// ptr type | |||
b, err := json.Marshal(raw) | |||
if err != nil { | |||
return false | |||
} | |||
if strings.TrimLeft(string(b), "\"\"") == "" { | |||
return false | |||
} | |||
if strings.TrimLeft(string(b), "{}") == "" { | |||
return false | |||
} | |||
return true | |||
} | |||
func AnyToInt64(raw interface{}) int64 { | |||
switch i := raw.(type) { | |||
case string: | |||
res, _ := strconv.ParseInt(i, 10, 64) | |||
return res | |||
case []byte: | |||
return BytesToInt64(i) | |||
case int: | |||
return int64(i) | |||
case int64: | |||
return i | |||
case uint: | |||
return int64(i) | |||
case uint8: | |||
return int64(i) | |||
case uint16: | |||
return int64(i) | |||
case uint32: | |||
return int64(i) | |||
case uint64: | |||
return int64(i) | |||
case int8: | |||
return int64(i) | |||
case int16: | |||
return int64(i) | |||
case int32: | |||
return int64(i) | |||
case float32: | |||
return int64(i) | |||
case float64: | |||
return int64(i) | |||
case error: | |||
return 0 | |||
case bool: | |||
if i { | |||
return 1 | |||
} | |||
return 0 | |||
} | |||
return 0 | |||
} | |||
func AnyToString(raw interface{}) string { | |||
switch i := raw.(type) { | |||
case []byte: | |||
return string(i) | |||
case int: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int64: | |||
return strconv.FormatInt(i, 10) | |||
case float32: | |||
return Float64ToStr(float64(i)) | |||
case float64: | |||
return Float64ToStr(i) | |||
case uint: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint8: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint16: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint32: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint64: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int8: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int16: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int32: | |||
return strconv.FormatInt(int64(i), 10) | |||
case string: | |||
return i | |||
case error: | |||
return i.Error() | |||
case bool: | |||
return strconv.FormatBool(i) | |||
} | |||
return fmt.Sprintf("%#v", raw) | |||
} | |||
func AnyToFloat64(raw interface{}) float64 { | |||
switch i := raw.(type) { | |||
case []byte: | |||
f, _ := strconv.ParseFloat(string(i), 64) | |||
return f | |||
case int: | |||
return float64(i) | |||
case int64: | |||
return float64(i) | |||
case float32: | |||
return float64(i) | |||
case float64: | |||
return i | |||
case uint: | |||
return float64(i) | |||
case uint8: | |||
return float64(i) | |||
case uint16: | |||
return float64(i) | |||
case uint32: | |||
return float64(i) | |||
case uint64: | |||
return float64(i) | |||
case int8: | |||
return float64(i) | |||
case int16: | |||
return float64(i) | |||
case int32: | |||
return float64(i) | |||
case string: | |||
f, _ := strconv.ParseFloat(i, 64) | |||
return f | |||
case bool: | |||
if i { | |||
return 1 | |||
} | |||
} | |||
return 0 | |||
} | |||
func ToByte(raw interface{}, e error) []byte { | |||
if e != nil { | |||
return []byte{} | |||
} | |||
switch i := raw.(type) { | |||
case string: | |||
return []byte(i) | |||
case int: | |||
return Int64ToBytes(int64(i)) | |||
case int64: | |||
return Int64ToBytes(i) | |||
case float32: | |||
return Float32ToByte(i) | |||
case float64: | |||
return Float64ToByte(i) | |||
case uint: | |||
return Int64ToBytes(int64(i)) | |||
case uint8: | |||
return Int64ToBytes(int64(i)) | |||
case uint16: | |||
return Int64ToBytes(int64(i)) | |||
case uint32: | |||
return Int64ToBytes(int64(i)) | |||
case uint64: | |||
return Int64ToBytes(int64(i)) | |||
case int8: | |||
return Int64ToBytes(int64(i)) | |||
case int16: | |||
return Int64ToBytes(int64(i)) | |||
case int32: | |||
return Int64ToBytes(int64(i)) | |||
case []byte: | |||
return i | |||
case error: | |||
return []byte(i.Error()) | |||
case bool: | |||
if i { | |||
return []byte("true") | |||
} | |||
return []byte("false") | |||
} | |||
return []byte(fmt.Sprintf("%#v", raw)) | |||
} | |||
func Int64ToBytes(i int64) []byte { | |||
var buf = make([]byte, 8) | |||
binary.BigEndian.PutUint64(buf, uint64(i)) | |||
return buf | |||
} | |||
func BytesToInt64(buf []byte) int64 { | |||
return int64(binary.BigEndian.Uint64(buf)) | |||
} | |||
func StrToInt(s string) int { | |||
res, _ := strconv.Atoi(s) | |||
return res | |||
} | |||
func StrToInt64(s string) int64 { | |||
res, _ := strconv.ParseInt(s, 10, 64) | |||
return res | |||
} | |||
func Float32ToByte(float float32) []byte { | |||
bits := math.Float32bits(float) | |||
bytes := make([]byte, 4) | |||
binary.LittleEndian.PutUint32(bytes, bits) | |||
return bytes | |||
} | |||
func ByteToFloat32(bytes []byte) float32 { | |||
bits := binary.LittleEndian.Uint32(bytes) | |||
return math.Float32frombits(bits) | |||
} | |||
func Float64ToByte(float float64) []byte { | |||
bits := math.Float64bits(float) | |||
bytes := make([]byte, 8) | |||
binary.LittleEndian.PutUint64(bytes, bits) | |||
return bytes | |||
} | |||
func ByteToFloat64(bytes []byte) float64 { | |||
bits := binary.LittleEndian.Uint64(bytes) | |||
return math.Float64frombits(bits) | |||
} | |||
func Float64ToStr(f float64) string { | |||
return strconv.FormatFloat(f, 'f', 2, 64) | |||
} | |||
func Float64ToStrPrec1(f float64) string { | |||
return strconv.FormatFloat(f, 'f', 1, 64) | |||
} | |||
func Float64ToStrByPrec(f float64, prec int) string { | |||
return strconv.FormatFloat(f, 'f', prec, 64) | |||
} | |||
func Float32ToStr(f float32) string { | |||
return Float64ToStr(float64(f)) | |||
} | |||
func StrToFloat64(s string) float64 { | |||
res, err := strconv.ParseFloat(s, 64) | |||
if err != nil { | |||
return 0 | |||
} | |||
return res | |||
} | |||
func StrToFormat(s string, prec int) string { | |||
ex := strings.Split(s, ".") | |||
if len(ex) == 2 { | |||
if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了 | |||
return ex[0] | |||
} | |||
//看取多少位 | |||
str := ex[1] | |||
str1 := str | |||
if prec < len(str) { | |||
str1 = str[0:prec] | |||
} else { | |||
for i := 0; i < prec-len(str); i++ { | |||
str1 += "0" | |||
} | |||
} | |||
if prec > 0 { | |||
return ex[0] + "." + str1 | |||
} else { | |||
return ex[0] | |||
} | |||
} | |||
return s | |||
} | |||
func StrToFloat32(s string) float32 { | |||
res, err := strconv.ParseFloat(s, 32) | |||
if err != nil { | |||
return 0 | |||
} | |||
return float32(res) | |||
} | |||
func StrToBool(s string) bool { | |||
b, _ := strconv.ParseBool(s) | |||
return b | |||
} | |||
func BoolToStr(b bool) string { | |||
if b { | |||
return "true" | |||
} | |||
return "false" | |||
} | |||
func FloatToInt64(f float64) int64 { | |||
return int64(f) | |||
} | |||
func IntToStr(i int) string { | |||
return strconv.Itoa(i) | |||
} | |||
func Int64ToStr(i int64) string { | |||
return strconv.FormatInt(i, 10) | |||
} | |||
func IntToFloat64(i int) float64 { | |||
s := strconv.Itoa(i) | |||
res, err := strconv.ParseFloat(s, 64) | |||
if err != nil { | |||
return 0 | |||
} | |||
return res | |||
} | |||
func Int64ToFloat64(i int64) float64 { | |||
s := strconv.FormatInt(i, 10) | |||
res, err := strconv.ParseFloat(s, 64) | |||
if err != nil { | |||
return 0 | |||
} | |||
return res | |||
} |
@@ -0,0 +1,66 @@ | |||
package utils | |||
import ( | |||
"math" | |||
) | |||
func CouponFormat(data string) string { | |||
switch data { | |||
case "0.00", "0", "": | |||
return "" | |||
default: | |||
return Int64ToStr(FloatToInt64(StrToFloat64(data))) | |||
} | |||
} | |||
func CommissionFormat(data string) string { | |||
if StrToFloat64(data) > 0 { | |||
return data | |||
} | |||
return "" | |||
} | |||
func HideString(src string, hLen int) string { | |||
str := []rune(src) | |||
if hLen == 0 { | |||
hLen = 4 | |||
} | |||
hideStr := "" | |||
for i := 0; i < hLen; i++ { | |||
hideStr += "*" | |||
} | |||
hideLen := len(str) / 2 | |||
showLen := len(str) - hideLen | |||
if hideLen == 0 || showLen == 0 { | |||
return hideStr | |||
} | |||
subLen := showLen / 2 | |||
if subLen == 0 { | |||
return string(str[:showLen]) + hideStr | |||
} | |||
s := string(str[:subLen]) | |||
s += hideStr | |||
s += string(str[len(str)-subLen:]) | |||
return s | |||
} | |||
//SaleCountFormat is 格式化销量 | |||
func SaleCountFormat(s string) string { | |||
if StrToInt(s) > 0 { | |||
if StrToInt(s) >= 10000 { | |||
num := FloatFormat(StrToFloat64(s)/10000, 2) | |||
s = Float64ToStr(num) + "w" | |||
} | |||
return s + "已售" | |||
} | |||
return "" | |||
} | |||
// 小数格式化 | |||
func FloatFormat(f float64, i int) float64 { | |||
if i > 14 { | |||
return f | |||
} | |||
p := math.Pow10(i) | |||
return float64(int64((f+0.000000000000009)*p)) / p | |||
} |
@@ -0,0 +1,208 @@ | |||
package utils | |||
import ( | |||
"errors" | |||
"fmt" | |||
"strconv" | |||
"strings" | |||
"time" | |||
) | |||
func StrToTime(s string) (int64, error) { | |||
// delete all not int characters | |||
if s == "" { | |||
return time.Now().Unix(), nil | |||
} | |||
r := make([]rune, 14) | |||
l := 0 | |||
// 过滤除数字以外的字符 | |||
for _, v := range s { | |||
if '0' <= v && v <= '9' { | |||
r[l] = v | |||
l++ | |||
if l == 14 { | |||
break | |||
} | |||
} | |||
} | |||
for l < 14 { | |||
r[l] = '0' // 补0 | |||
l++ | |||
} | |||
t, err := time.Parse("20060102150405", string(r)) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return t.Unix(), nil | |||
} | |||
func TimeToStr(unixSecTime interface{}, layout ...string) string { | |||
i := AnyToInt64(unixSecTime) | |||
if i == 0 { | |||
return "" | |||
} | |||
f := "2006-01-02 15:04:05" | |||
if len(layout) > 0 { | |||
f = layout[0] | |||
} | |||
return time.Unix(i, 0).Format(f) | |||
} | |||
func FormatNanoUnix() string { | |||
return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1) | |||
} | |||
func TimeParse(format, src string) (time.Time, error) { | |||
return time.ParseInLocation(format, src, time.Local) | |||
} | |||
func TimeParseStd(src string) time.Time { | |||
t, _ := TimeParse("2006-01-02 15:04:05", src) | |||
return t | |||
} | |||
func TimeStdParseUnix(src string) int64 { | |||
t, err := TimeParse("2006-01-02 15:04:05", src) | |||
if err != nil { | |||
return 0 | |||
} | |||
return t.Unix() | |||
} | |||
// 获取一个当前时间 时间间隔 时间戳 | |||
func GetTimeInterval(unit string, amount int) (startTime, endTime int64) { | |||
t := time.Now() | |||
nowTime := t.Unix() | |||
tmpTime := int64(0) | |||
switch unit { | |||
case "years": | |||
tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "months": | |||
tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "days": | |||
tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "hours": | |||
tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix() | |||
} | |||
if amount > 0 { | |||
startTime = nowTime | |||
endTime = tmpTime | |||
} else { | |||
startTime = tmpTime | |||
endTime = nowTime | |||
} | |||
return | |||
} | |||
// 几天前 | |||
func TimeInterval(newTime int) string { | |||
now := time.Now().Unix() | |||
newTime64 := AnyToInt64(newTime) | |||
if newTime64 >= now { | |||
return "刚刚" | |||
} | |||
interval := now - newTime64 | |||
switch { | |||
case interval < 60: | |||
return AnyToString(interval) + "秒前" | |||
case interval < 60*60: | |||
return AnyToString(interval/60) + "分前" | |||
case interval < 60*60*24: | |||
return AnyToString(interval/60/60) + "小时前" | |||
case interval < 60*60*24*30: | |||
return AnyToString(interval/60/60/24) + "天前" | |||
case interval < 60*60*24*30*12: | |||
return AnyToString(interval/60/60/24/30) + "月前" | |||
default: | |||
return AnyToString(interval/60/60/24/30/12) + "年前" | |||
} | |||
} | |||
// 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10 | |||
func HmsToUnix(str string) (int64, error) { | |||
t := time.Now() | |||
arr := strings.Split(str, ":") | |||
if len(arr) < 2 { | |||
return 0, errors.New("Time format error") | |||
} | |||
h, _ := strconv.Atoi(arr[0]) | |||
m, _ := strconv.Atoi(arr[1]) | |||
s := 0 | |||
if len(arr) == 3 { | |||
s, _ = strconv.Atoi(arr[3]) | |||
} | |||
formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s) | |||
res, err := time.ParseInLocation("20060102150405", formatted1, time.Local) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return res.Unix(), nil | |||
} | |||
} | |||
// 获取特定时间范围 | |||
func GetTimeRange(s string) map[string]int64 { | |||
t := time.Now() | |||
var stime, etime time.Time | |||
switch s { | |||
case "today": | |||
stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "yesterday": | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
case "within_seven_days": | |||
// 前6天0点 | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location()) | |||
// 明天 0点 | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "current_month": | |||
stime = GetFirstDateOfMonth(t) | |||
etime = time.Now() | |||
case "last_month": | |||
etime = GetFirstDateOfMonth(t) | |||
monthTimes := TimeStdParseUnix(etime.Format("2006-01-02 15:04:05")) - 86400 | |||
times, _ := UnixToTime(Int64ToStr(monthTimes)) | |||
stime = GetFirstDateOfMonth(times) | |||
} | |||
return map[string]int64{ | |||
"start": stime.Unix(), | |||
"end": etime.Unix(), | |||
} | |||
} | |||
//时间戳转时间格式 | |||
func UnixToTime(e string) (datatime time.Time, err error) { | |||
data, err := strconv.ParseInt(e, 10, 64) | |||
datatime = time.Unix(data, 0) | |||
return | |||
} | |||
//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。 | |||
func GetFirstDateOfMonth(d time.Time) time.Time { | |||
d = d.AddDate(0, 0, -d.Day()+1) | |||
return GetZeroTime(d) | |||
} | |||
//获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。 | |||
func GetLastDateOfMonth(d time.Time) time.Time { | |||
return GetFirstDateOfMonth(d).AddDate(0, 1, -1) | |||
} | |||
//获取某一天的0点时间 | |||
func GetZeroTime(d time.Time) time.Time { | |||
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) | |||
} | |||
//获取当月某天的某个时间的时间 | |||
func GetDayToTime(day, timeStr string) string { | |||
if timeStr == "" { | |||
timeStr = "00:00:00" | |||
} | |||
year := time.Now().Year() | |||
month := time.Now().Format("01") | |||
times := fmt.Sprintf("%s-%s-%s %s", IntToStr(year), month, day, timeStr) | |||
return times | |||
} |