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