package order import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/mongodb" "context" "encoding/json" "errors" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "io/ioutil" "jyOrderManager/internal/model" "log" "net/http" "net/url" "time" ) func CopyLink(ctx context.Context, param model.OrderCopyLinkParams) (interface{}, error) { var link string //查询订单信息 orderData, _ := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf("SELECT a.user_id,a.order_code,jod.product_type,jod.filter,b.saler_name,b.saler_Id FROM dataexport_order a INNER JOIN order_sale_record b on(a.order_code = b.ordercode) INNER JOIN jy_order_detail jod on a.order_code = jod.order_code WHERE a.id=%s and jod.status =1", param.Id)) if !orderData.IsEmpty() { order := orderData.Map() filterMap := map[string]interface{}{} if err := json.Unmarshal([]byte((order["filter"]).(string)), &filterMap); err == nil { token := GetSellerToken(mongodb.BsonIdToSId(order["user_id"]), gconv.String(order["saler_name"]), gconv.String(order["saler_Id"]), map[string]interface{}{ "tt": common.IntAll(filterMap["give_cycle"]), "tn": common.IntAll(filterMap["give_type"]), }) productType := common.ObjToString(order["product_type"]) copyHref := make(map[string]string) g.Cfg().MustGet(context.Background(), "copyHref").Scan(©Href) href := fmt.Sprintf("%s/jypay/free/seller/wxPaymentPage?orderCode=%s&token=%s&href=%s", g.Cfg().MustGet(context.Background(), "jyWebDomain").String(), common.ObjToString(order["order_code"]), token, copyHref[productType]) link = href } log.Println("生成链接:", link) //调用短地址服务把长链接转成短链接 pam := url.Values{ "LongUrl": []string{link}, } res, _ := HttpPost(g.Cfg().MustGet(context.Background(), "shortLinkService").String(), pam) if res != nil && len(res) > 0 && common.IntAll(res["err_code"]) == 0 { return map[string]interface{}{ "link": res["data"], }, nil } } return nil, errors.New("复制链接失败") } func GetSellerToken(uId, sName string, sid string, desc map[string]interface{}) (token string) { bytes, _ := json.Marshal(desc) return encrypt.SE.Encode2Hex(fmt.Sprintf("uid=%s&sName=%s&sid=%s&desc=%s&t=%d", uId, sName, sid, string(bytes), time.Now().Unix())) } func HttpPost(path string, param url.Values) (map[string]interface{}, error) { res, err := http.PostForm(path, param) if err != nil { return nil, err } defer res.Body.Close() bs, _ := ioutil.ReadAll(res.Body) resMap := map[string]interface{}{} log.Println("req", param.Encode(), "body:", string(bs), "err", err) err = json.Unmarshal(bs, &resMap) if err != nil { return nil, fmt.Errorf("%s 请求返回内容异常 %s", path, string(bs)) } return resMap, nil }