Commit bf33586b by huangchengyi
parents c755e533 9a5e6eb1
...@@ -31,22 +31,23 @@ gjson 判断某值是否存在 map ...@@ -31,22 +31,23 @@ gjson 判断某值是否存在 map
@param json string 分析json字符串 @param json string 分析json字符串
@param arrk string 分析的json键值转成map @param arrk string 分析的json键值转成map
@param targetk string 目标ID @param targetk string 目标ID
*/ */
func GInArray(json string,arrK string,targetK string) bool{ func GInArray(json string, arrK string, targetK string) bool {
s := gjson.Get(json,arrK).Array() s := gjson.Get(json, arrK).Array()
d := make([]string,0) d := make([]string, 0)
for _,a := range s { for _, a := range s {
c := a.String() c := a.String()
d = append(d,c) d = append(d, c)
} }
return php2go.InArray(targetK,d) return php2go.InArray(targetK, d)
} }
/* /*
sha1加密(tme目前在用) sha1加密(tme目前在用)
@param string query 查询字符串 @param string query 查询字符串
@param string pri_key 加密字符串 @param string pri_key 加密字符串
@return 返回加密后的字符串 @return 返回加密后的字符串
*/ */
func Sha1(query string, pri_key string) string { func Sha1(query string, pri_key string) string {
key := []byte(pri_key) key := []byte(pri_key)
mac := hmac.New(sha1.New, key) mac := hmac.New(sha1.New, key)
...@@ -63,15 +64,15 @@ func Sha1(query string, pri_key string) string { ...@@ -63,15 +64,15 @@ func Sha1(query string, pri_key string) string {
而此函数需要频繁调用,所以需要在调用此函数之前,使用过ShouldBindBodyWith 方法,不然会读取不到。 而此函数需要频繁调用,所以需要在调用此函数之前,使用过ShouldBindBodyWith 方法,不然会读取不到。
具体需了解 ShouldBindBodyWith 源码 和 多次使用shouldbindjsond 的问题 具体需了解 ShouldBindBodyWith 源码 和 多次使用shouldbindjsond 的问题
2.表单提交不需要读body,则正常使用 2.表单提交不需要读body,则正常使用
*/ */
func GetRequestParam(ctx *gin.Context) string{ func GetRequestParam(ctx *gin.Context) string {
ct := ctx.Request.Header.Get("Content-Type") ct := ctx.Request.Header.Get("Content-Type")
switch { switch {
case ct == "application/json": case ct == "application/json":
var BodyJson string var BodyJson string
if cb, ok := ctx.Get(gin.BodyBytesKey); ok { if cb, ok := ctx.Get(gin.BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok { if cbb, ok := cb.([]byte); ok {
BodyJson=string(cbb) BodyJson = string(cbb)
return BodyJson return BodyJson
} }
} }
...@@ -79,14 +80,12 @@ func GetRequestParam(ctx *gin.Context) string{ ...@@ -79,14 +80,12 @@ func GetRequestParam(ctx *gin.Context) string{
default: default:
ctx.Request.ParseForm() ctx.Request.ParseForm()
postForm := ctx.Request.PostForm postForm := ctx.Request.PostForm
formData, _ := json.Marshal(postForm); formData, _ := json.Marshal(postForm)
params := string(formData) params := string(formData)
return params; return params
} }
} }
/** /**
结构复制,从struct1 复制到 struct2,字段类型不一样,那么就不复制那个字段 结构复制,从struct1 复制到 struct2,字段类型不一样,那么就不复制那个字段
@param Starus 说明 @param Starus 说明
...@@ -95,17 +94,17 @@ func GetRequestParam(ctx *gin.Context) string{ ...@@ -95,17 +94,17 @@ func GetRequestParam(ctx *gin.Context) string{
3,struct2 字段为零值,才让struct1 的字段赋值 3,struct2 字段为零值,才让struct1 的字段赋值
*/ */
func CopyStruct(struct1 interface{}, struct2 interface{},Status ...int) { func CopyStruct(struct1 interface{}, struct2 interface{}, Status ...int) {
statusType:=1;//statusType 默认参数 statusType := 1 //statusType 默认参数
for num,opt:=range Status{ for num, opt := range Status {
if(num>0){//只接受一个参数 if num > 0 { //只接受一个参数
break; break
} }
switch num { switch num {
case 0: case 0:
if(opt==2){ if opt == 2 {
statusType=2 statusType = 2
} }
break break
} }
...@@ -116,22 +115,21 @@ func CopyStruct(struct1 interface{}, struct2 interface{},Status ...int) { ...@@ -116,22 +115,21 @@ func CopyStruct(struct1 interface{}, struct2 interface{},Status ...int) {
for i := 0; i < sval.NumField(); i++ { for i := 0; i < sval.NumField(); i++ {
value := sval.Field(i) value := sval.Field(i)
if(statusType==1){ //如果struct1 字段值为零值就不覆盖struct2对应的字段值 if statusType == 1 { //如果struct1 字段值为零值就不覆盖struct2对应的字段值
if(value.IsZero()){ if value.IsZero() {
continue continue
} }
} }
name := sval.Type().Field(i).Name name := sval.Type().Field(i).Name
dvalue := dval.FieldByName(name) dvalue := dval.FieldByName(name)
if statusType == 3 { //struct2字段为零值,才让struct1 的字段赋值,不是零值就跳过此字段的赋值
if(statusType==3){//struct2字段为零值,才让struct1 的字段赋值,不是零值就跳过此字段的赋值 if dvalue.IsZero() == false {
if(dvalue.IsZero()==false){
continue continue
} }
} }
if(dvalue.Kind()!=sval.Field(i).Kind()){//类型不一样不复制 if dvalue.Kind() != sval.Field(i).Kind() { //类型不一样不复制
continue continue
} }
if dvalue.IsValid() == false { //dvalue.IsValid() 与 dvalue.IsZero() 不一样,如果返回fasle是无效的值 if dvalue.IsValid() == false { //dvalue.IsValid() 与 dvalue.IsZero() 不一样,如果返回fasle是无效的值
...@@ -142,84 +140,82 @@ func CopyStruct(struct1 interface{}, struct2 interface{},Status ...int) { ...@@ -142,84 +140,82 @@ func CopyStruct(struct1 interface{}, struct2 interface{},Status ...int) {
} }
} }
/** /**
@author wangsong @author wangsong
合并map,都有值以map1为主,字段类型以map1为主 合并map,都有值以map1为主,字段类型以map1为主
*/ */
func MergeMap(map1 map[string]interface{},map2 map[string]interface{}) map[string]interface{} { func MergeMap(map1 map[string]interface{}, map2 map[string]interface{}) map[string]interface{} {
var newMap map[string]interface{} var newMap map[string]interface{}
newMap=map1//以map1为主,就先将map1给到newMap newMap = map1 //以map1为主,就先将map1给到newMap
//先算mp1 //先算mp1
for key,_:= range map1{ for key, _ := range map1 {
if(!reflect.ValueOf(map1[key]).IsZero()){//如果不为空,就以取map1的值 if !reflect.ValueOf(map1[key]).IsZero() { //如果不为空,就以取map1的值
s:=reflect.ValueOf(map1[key]) s := reflect.ValueOf(map1[key])
if(s.Kind()==reflect.String){ //string if s.Kind() == reflect.String { //string
newMap[key]=gconv.String(map1[key]) newMap[key] = gconv.String(map1[key])
}else{ } else {
newMap[key]=gconv.Int(map1[key]) //int newMap[key] = gconv.Int(map1[key]) //int
} }
delete(map2, key)//删一个重复的map2的key delete(map2, key) //删一个重复的map2的key
}else{//没有数据,取mp2的值,类型要是map1字段的类型 } else { //没有数据,取mp2的值,类型要是map1字段的类型
if _, ok := map2[key]; ok { //map1的key在map2是否存在,不存在就不处理 if _, ok := map2[key]; ok { //map1的key在map2是否存在,不存在就不处理
if(!reflect.ValueOf(map2[key]).IsZero()){ //map2不能为0值才处理 if !reflect.ValueOf(map2[key]).IsZero() { //map2不能为0值才处理
m1 :=reflect.ValueOf(map1[key]) m1 := reflect.ValueOf(map1[key])
if(m1.Kind()==reflect.String){ //string if m1.Kind() == reflect.String { //string
newMap[key]=gconv.String(map2[key]) newMap[key] = gconv.String(map2[key])
}else{ } else {
newMap[key]=gconv.Int(map2[key]) //int newMap[key] = gconv.Int(map2[key]) //int
} }
} }
delete(map2, key)//删一个重复的map2的key delete(map2, key) //删一个重复的map2的key
} }
} }
} }
//处理map2,重复的被删了,剩下的直接添加 //处理map2,重复的被删了,剩下的直接添加
for key,value:= range map2{ for key, value := range map2 {
s:=reflect.ValueOf(value) s := reflect.ValueOf(value)
if(s.Kind()==reflect.String){ //string if s.Kind() == reflect.String { //string
newMap[key]=gconv.String(value) newMap[key] = gconv.String(value)
}else{ } else {
newMap[key]=gconv.Int(value) //int newMap[key] = gconv.Int(value) //int
} }
} }
return newMap return newMap
} }
/* /*
输出header 输出header
*/ */
func PrintDebugHeader(ctx *gin.Context) { func PrintDebugHeader(ctx *gin.Context) {
if ctx.Request.FormValue("flag") == "101" { if ctx.Request.FormValue("flag") == "101" {
ctx.Header("Content-Type", "text/html; charset=utf-8") ctx.Header("Content-Type", "text/html; charset=utf-8")
} }
} }
/* /*
格式化数据直接输出浏览器 格式化数据直接输出浏览器
@parm jsonStr 需要json输出的内容 @parm jsonStr 需要json输出的内容
*/ */
func PrintDebugHtml(ctx *gin.Context,jsonStr interface{}) { func PrintDebugHtml(ctx *gin.Context, jsonStr interface{}) {
if ctx.Request.FormValue("flag") != "101" || jsonStr == "" { if ctx.Request.FormValue("flag") != "101" || jsonStr == "" {
return return
} }
if v, p := jsonStr.(string); p { if v, p := jsonStr.(string); p {
ctx.String(200,"</br></br>-----------"+v) ctx.String(200, "</br></br>-----------"+v)
}else{ } else {
jsonData,err := json.Marshal(jsonStr) jsonData, err := json.Marshal(jsonStr)
if err != nil { if err != nil {
fmt.Println("错误:-----",err) fmt.Println("错误:-----", err)
} }
ctx.String(200,"</br></br>-----------"+string(jsonData)) ctx.String(200, "</br></br>-----------"+string(jsonData))
} }
} }
...@@ -472,8 +468,6 @@ func auth(ctx *gin.Context) bool { ...@@ -472,8 +468,6 @@ func auth(ctx *gin.Context) bool {
return false return false
} }
func CreateAnyTypeSlice(slice interface{}) ([]interface{}, bool) { func CreateAnyTypeSlice(slice interface{}) ([]interface{}, bool) {
val, ok := isSlice(slice) val, ok := isSlice(slice)
...@@ -517,12 +511,13 @@ func StrPadLeft(input string, padLength int, padString string) string { ...@@ -517,12 +511,13 @@ func StrPadLeft(input string, padLength int, padString string) string {
return output[:padLength-len(input)] + input return output[:padLength-len(input)] + input
} }
//移除字符串切片中某個元素 //移除字符串切片中某個元素
func RemoveSliceString(s []string, i string) []string { func RemoveSliceString(s []string, i string) []string {
b := make([]string,0) b := make([]string, 0)
for _,v:=range s{ for _, v := range s {
if v != i{ if v != i {
b= append(b,v) b = append(b, v)
} }
} }
...@@ -530,77 +525,79 @@ func RemoveSliceString(s []string, i string) []string { ...@@ -530,77 +525,79 @@ func RemoveSliceString(s []string, i string) []string {
} }
//map排序 //map排序
func MapSort(mapList map[int]int) []int{ func MapSort(mapList map[int]int) []int {
var( var (
keys []int keys []int
newList []int newList []int
) )
newList = make([]int,0) newList = make([]int, 0)
for _,v:=range mapList{ for _, v := range mapList {
keys = append(keys,v) keys = append(keys, v)
} }
sort.Ints(keys) sort.Ints(keys)
for _,k:=range keys{ for _, k := range keys {
newList = append(newList,mapList[k]) newList = append(newList, mapList[k])
} }
return newList return newList
} }
////////////类型转换///////////////////// ////////////类型转换/////////////////////
func MyInt(str string) int { func MyInt(str string) int {
res,_ := strconv.ParseInt(str,10,64) res, _ := strconv.ParseInt(str, 10, 64)
return int(res) return int(res)
} }
func MyInt8(str string) int8 { func MyInt8(str string) int8 {
res,_ := strconv.ParseInt(str,10,64) res, _ := strconv.ParseInt(str, 10, 64)
return int8(res) return int8(res)
} }
func MyInt16(str string) int16 { func MyInt16(str string) int16 {
res,_ := strconv.ParseInt(str,10,64) res, _ := strconv.ParseInt(str, 10, 64)
return int16(res) return int16(res)
} }
func MyInt64(str string) int64 { func MyInt64(str string) int64 {
res,_ := strconv.ParseInt(str,10,64) res, _ := strconv.ParseInt(str, 10, 64)
return int64(res) return int64(res)
} }
func MyFloat32(str string) float32 { func MyFloat32(str string) float32 {
res,_ := strconv.ParseFloat(str,64) res, _ := strconv.ParseFloat(str, 64)
return float32(res) return float32(res)
} }
func MyFloat64(str string) float64 { func MyFloat64(str string) float64 {
res,_ := strconv.ParseFloat(str,64) res, _ := strconv.ParseFloat(str, 64)
return float64(res) return float64(res)
} }
func MyFloat64ToStr(numb float64) string { func MyFloat64ToStr(numb float64) string {
return strconv.FormatFloat(numb,'f',6,64) return strconv.FormatFloat(numb, 'f', 6, 64)
} }
func MyInt64ToStr(numb int64) string { func MyInt64ToStr(numb int64) string {
return strconv.FormatInt(numb,10) return strconv.FormatInt(numb, 10)
} }
func MyIntToStr(i int) string { func MyIntToStr(i int) string {
return strconv.Itoa(i) return strconv.Itoa(i)
} }
/* /*
四舍五入取多少位 四舍五入取多少位
@param float64 x 目标分析字符串 @param float64 x 目标分析字符串
@param int wei 取小数多少位 @param int wei 取小数多少位
*/ */
func MyRound(x float64,wei int) float64{ func MyRound(x float64, wei int) float64 {
if wei == 0 { if wei == 0 {
return math.Floor(x + 0.5) return math.Floor(x + 0.5)
} }
weis := map[int]float64{ weis := map[int]float64{
1:10, 1: 10,
2:100, 2: 100,
3:1000, 3: 1000,
4:10000, 4: 10000,
5:100000, 5: 100000,
6:1000000, 6: 1000000,
} }
weishu := weis[wei] weishu := weis[wei]
a := math.Floor(x*weishu + 0.5)/weishu a := math.Floor(x*weishu+0.5) / weishu
return a return a
} }
////////////类型转换///////////////////// ////////////类型转换/////////////////////
\ No newline at end of file
...@@ -201,8 +201,6 @@ func (ls *LyService) getSpuList(skuArr map[string]string) (spuList map[string]st ...@@ -201,8 +201,6 @@ func (ls *LyService) getSpuList(skuArr map[string]string) (spuList map[string]st
spuId := gjson.Get(skuStr, "spu_id").String() spuId := gjson.Get(skuStr, "spu_id").String()
spuIds = append(spuIds, spuId) spuIds = append(spuIds, spuId)
} }
redisConn := gredis.Conn("default_r")
defer redisConn.Close()
//批量获取spu详情 //批量获取spu详情
spuList = gredis.Hmget("default_r", "spu", spuIds) spuList = gredis.Hmget("default_r", "spu", spuIds)
return return
......
...@@ -279,6 +279,7 @@ func (ls *LyService) GetCoefficient(sku model.LySku) model.LySku { ...@@ -279,6 +279,7 @@ func (ls *LyService) GetCoefficient(sku model.LySku) model.LySku {
} }
if !hasCoefficient && !hasDefault { if !hasCoefficient && !hasDefault {
logger.Error("%s", "系数获取异常,供应商:"+common.ToString(sku.SupplierId)) logger.Error("%s", "系数获取异常,供应商:"+common.ToString(sku.SupplierId))
sku.Content = "系数获取异常,供应商:"+common.ToString(sku.SupplierId)
return sku return sku
} }
...@@ -339,13 +340,13 @@ func (ls *LyService) GetCoefficient(sku model.LySku) model.LySku { ...@@ -339,13 +340,13 @@ func (ls *LyService) GetCoefficient(sku model.LySku) model.LySku {
} }
} }
} }
if len(data) > 0 {
sku.LadderPrice = data
}
//输出税费到前端 //输出税费到前端
coefficient.Tax = tax coefficient.Tax = tax
sku.Coefficient = coefficient sku.Coefficient = coefficient
} }
if len(data) > 0 {
sku.LadderPrice = data
}
return sku return sku
} }
...@@ -393,19 +394,14 @@ func (ls *LyService) GetIsBuy(sku model.LySku) (isBuy int) { ...@@ -393,19 +394,14 @@ func (ls *LyService) GetIsBuy(sku model.LySku) (isBuy int) {
//合并spu的信息 //合并spu的信息
func (ls *LyService) CombineSup(sku model.LySku, spuStr string) model.LySku { func (ls *LyService) CombineSup(sku model.LySku, spuStr string) model.LySku {
var spu model.Spu sku.UpdateTime = gjson.Get(spuStr,"update_time").Int()
err := json.Unmarshal([]byte(spuStr), &spu) sku.ClassID1 = int(gjson.Get(spuStr,"class_id1").Int())
if err != nil { sku.ClassID2 = int(gjson.Get(spuStr,"class_id2").Int())
return sku sku.SpuName = gjson.Get(spuStr,"spu_name").String()
} sku.SpuBrief =gjson.Get(spuStr,"spu_brief").String()
sku.UpdateTime = spu.UpdateTime sku.SpuDetail = gjson.Get(spuStr,"spu_detail").String()
sku.ClassID1 = spu.ClassID1 sku.Status = int(gjson.Get(spuStr,"status").Int())
sku.ClassID2 = spu.ClassID2 sku.Encap = gjson.Get(spuStr,"encap").String()
sku.SpuName = spu.SpuName
sku.SpuBrief = spu.SpuBrief
sku.SpuDetail = spu.SpuDetail
sku.Status = spu.Status
sku.Encap = spu.Encap
return sku return sku
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment