diff --git a/service/wnfuwu/api.go b/service/wnfuwu/api.go new file mode 100644 index 00000000..ec52280a --- /dev/null +++ b/service/wnfuwu/api.go @@ -0,0 +1 @@ +package wnfuwu diff --git a/service/wnfuwu/check.go b/service/wnfuwu/check.go new file mode 100644 index 00000000..9578372e --- /dev/null +++ b/service/wnfuwu/check.go @@ -0,0 +1,52 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type CheckResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data struct { + OrderNumber string `json:"order_number"` // 系统定单号 + OutTradeNum string `json:"out_trade_num"` // 商户订单号 + CreateTime string `json:"create_time"` // 下单时间 + Mobile string `json:"mobile"` // 手机号 + ProductId string `json:"product_id"` // 产品ID + ChargeAmount float64 `json:"charge_amount"` // 充值成功面额 + ChargeKami string `json:"charge_kami"` // 卡密流水 + State string `json:"state"` // 充值状态:-1取消,0充值中 ,1充值成功,2充值失败,3部分成功 + } `json:"data,omitempty"` +} + +type CheckResult struct { + Result CheckResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newCheckResult(result CheckResponse, body []byte, http gorequest.Response) *CheckResult { + return &CheckResult{Result: result, Body: body, Http: http} +} + +// Check 自发查询订单状态 +// https://www.showdoc.com.cn/dyr/9227006175502841 +func (c *Client) Check(ctx context.Context, outTradeNums string, notMustParams ...gorequest.Params) (*CheckResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + if outTradeNums != "" { + params.Set("out_trade_nums", outTradeNums) + } + // 请求 + request, err := c.request(ctx, apiUrl+"/index/check", params) + if err != nil { + return newCheckResult(CheckResponse{}, request.ResponseBody, request), err + } + // 定义 + var response CheckResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newCheckResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/client.go b/service/wnfuwu/client.go new file mode 100644 index 00000000..f4dc8e4c --- /dev/null +++ b/service/wnfuwu/client.go @@ -0,0 +1,38 @@ +package wnfuwu + +import ( + "github.com/dtapps/go-library/utils/golog" + "github.com/dtapps/go-library/utils/gorequest" +) + +// ClientConfig 实例配置 +type ClientConfig struct { + UserId int64 // 商户ID + ApiKey string // 秘钥 +} + +// Client 实例 +type Client struct { + requestClient *gorequest.App // 请求服务 + config struct { + userId int64 // 商户ID + apiKey string // 秘钥 + } + log struct { + status bool // 状态 + client *golog.ApiClient // 日志服务 + } +} + +// NewClient 创建实例化 +func NewClient(config *ClientConfig) (*Client, error) { + + c := &Client{} + + c.config.userId = config.UserId + c.config.apiKey = config.ApiKey + + c.requestClient = gorequest.NewHttp() + + return c, nil +} diff --git a/service/wnfuwu/config.go b/service/wnfuwu/config.go new file mode 100644 index 00000000..6f32d19a --- /dev/null +++ b/service/wnfuwu/config.go @@ -0,0 +1,19 @@ +package wnfuwu + +import "github.com/dtapps/go-library/utils/golog" + +// ConfigApp 配置 +func (c *Client) ConfigApp(userId int64, apiKey string) *Client { + c.config.userId = userId + c.config.apiKey = apiKey + return c +} + +// ConfigApiClientFun 日志配置 +func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) { + apiClient := apiClientFun() + if apiClient != nil { + c.log.client = apiClient + c.log.status = true + } +} diff --git a/service/wnfuwu/const.go b/service/wnfuwu/const.go new file mode 100644 index 00000000..18068a63 --- /dev/null +++ b/service/wnfuwu/const.go @@ -0,0 +1,9 @@ +package wnfuwu + +const ( + apiUrl = "http://wn.wnfuwu.cn/yrapi.php" +) + +const ( + LogTable = "wnfuwu" +) diff --git a/service/wnfuwu/elecity.go b/service/wnfuwu/elecity.go new file mode 100644 index 00000000..924d6fc9 --- /dev/null +++ b/service/wnfuwu/elecity.go @@ -0,0 +1,52 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type ElecityResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data []struct { + Id int64 `json:"id,omitempty"` + CityName string `json:"city_name"` // 地区名称 + Sort int64 `json:"sort"` // 排序 + Initial string `json:"initial"` // 首字母 + NeedYtype int64 `json:"need_ytype"` // 是否三要素认证 + NeedCity int64 `json:"need_city"` // 是否需要选择城市(当此开关打开以后才有下面的城市列表) + City []struct { + Id int64 `json:"id,omitempty"` + CityName string `json:"city_name"` // 城市名称 + Initial string `json:"initial"` // 首字母 + } `json:"city"` // 支持的地级市 + } `json:"data,omitempty"` +} + +type ElecityResult struct { + Result ElecityResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newElecityResult(result ElecityResponse, body []byte, http gorequest.Response) *ElecityResult { + return &ElecityResult{Result: result, Body: body, Http: http} +} + +// Elecity 电费支持地区查询 +// https://www.showdoc.com.cn/dyr/9227008514209156 +func (c *Client) Elecity(ctx context.Context, notMustParams ...gorequest.Params) (*ElecityResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + // 请求 + request, err := c.request(ctx, apiUrl+"/index/elecity", params) + if err != nil { + return newElecityResult(ElecityResponse{}, request.ResponseBody, request), err + } + // 定义 + var response ElecityResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newElecityResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/get.go b/service/wnfuwu/get.go new file mode 100644 index 00000000..b32ad55c --- /dev/null +++ b/service/wnfuwu/get.go @@ -0,0 +1,15 @@ +package wnfuwu + +import "github.com/dtapps/go-library/utils/golog" + +func (c *Client) GetUserId() int64 { + return c.config.userId +} + +func (c *Client) GetApiKey() string { + return c.config.apiKey +} + +func (c *Client) GetLog() *golog.ApiClient { + return c.log.client +} diff --git a/service/wnfuwu/price.go b/service/wnfuwu/price.go new file mode 100644 index 00000000..b0d56a34 --- /dev/null +++ b/service/wnfuwu/price.go @@ -0,0 +1,55 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type PriceResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data struct { + Name string `json:"name"` // 产品名称 + Desc string `json:"desc"` // 产品说明 + ApiOpen string `json:"api_open"` // 自动充值 + Isp string `json:"isp"` // 运营商集合(话费、流量有效),1移动,2电信,3联通,4虚拟 + YsTag string `json:"ys_tag"` // 标签 + Price string `json:"price"` // 价格,下单扣费金额 + YPrice string `json:"y_price"` // 原价 + MaxPrice string `json:"max_price"` // 封顶价格 + Type string `json:"type"` // 产品类型ID + CateName string `json:"cate_name"` // 产品分类名称 + TypeName string `json:"type_name"` // 产品类型名称 + } `json:"data,omitempty"` +} + +type PriceResult struct { + Result PriceResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newPriceResult(result PriceResponse, body []byte, http gorequest.Response) *PriceResult { + return &PriceResult{Result: result, Body: body, Http: http} +} + +// Price 产品ID查询【新增】 +// https://www.showdoc.com.cn/dyr/9757701226597233 +func (c *Client) Price(ctx context.Context, id int64, notMustParams ...gorequest.Params) (*PriceResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + if id != 0 { + params.Set("id", id) + } + // 请求 + request, err := c.request(ctx, apiUrl+"/index/price", params) + if err != nil { + return newPriceResult(PriceResponse{}, request.ResponseBody, request), err + } + // 定义 + var response PriceResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newPriceResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/product.go b/service/wnfuwu/product.go new file mode 100644 index 00000000..07d92ad7 --- /dev/null +++ b/service/wnfuwu/product.go @@ -0,0 +1,77 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type ProductResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data []struct { + Id int64 `json:"id"` // 分类ID + Cate string `json:"cate"` // 分类名称 + Sort int64 `json:"sort"` // 排序 + Type int64 `json:"type"` // 产品类型ID + Products []struct { + Id int64 `json:"id"` // 产品ID,下单报文中用此参数 + Name string `json:"name"` // 产品名称 + Yname string `json:"yname,omitempty"` + Desc string `json:"desc"` // 产品说明 + ApiOpen int64 `json:"api_open"` // 自动充值 + Isp string `json:"isp"` // 运营商集合(话费、流量有效),1移动,2电信,3联通,4虚拟 + YsTag string `json:"ys_tag"` // 标签 + Price string `json:"price"` // 价格,下单扣费金额 + ShowStyle int64 `json:"show_style,omitempty"` + CateId int64 `json:"cate_id,omitempty"` + DelayApi string `json:"delay_api,omitempty"` + YPrice float64 `json:"y_price"` // 原价 + MaxPrice string `json:"max_price"` // 封顶价格 + Type int64 `json:"type"` // 产品类型ID + AllowPro string `json:"allow_pro,omitempty"` + AllowCity string `json:"allow_city,omitempty"` + ForbidPro string `json:"forbid_pro,omitempty"` + ForbidCity string `json:"forbid_city,omitempty"` + JmapiId int64 `json:"jmapi_id,omitempty"` + JmapiParamId int64 `json:"jmapi_param_id,omitempty"` + IsJiema int64 `json:"is_jiema,omitempty"` + CateName string `json:"cate_name"` // 产品分类名称 + TypeName string `json:"type_name"` // 产品类型名称 + TypecId int64 `json:"typec_id,omitempty"` + } `json:"products"` // 产品列表 + } `json:"data,omitempty"` +} + +type ProductResult struct { + Result ProductResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newProductResult(result ProductResponse, body []byte, http gorequest.Response) *ProductResult { + return &ProductResult{Result: result, Body: body, Http: http} +} + +// Product 获取产品 +// https://www.showdoc.com.cn/dyr/9227005691961526 +func (c *Client) Product(ctx context.Context, Type, cateId int64, notMustParams ...gorequest.Params) (*ProductResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + if Type != 0 { + params.Set("type", Type) + } + if cateId != 0 { + params.Set("cate_id", cateId) + } + // 请求 + request, err := c.request(ctx, apiUrl+"/index/product", params) + if err != nil { + return newProductResult(ProductResponse{}, request.ResponseBody, request), err + } + // 定义 + var response ProductResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newProductResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/recharge.go b/service/wnfuwu/recharge.go new file mode 100644 index 00000000..8ac4033c --- /dev/null +++ b/service/wnfuwu/recharge.go @@ -0,0 +1,47 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type RechargeResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data struct { + OrderNumber string `json:"order_number"` // 系统定单号 + Mobile string `json:"mobile"` // 充值手机号 + ProductId string `json:"product_id"` // 产品ID + TotalPrice string `json:"total_price"` // 消费金额 + OutTradeNum string `json:"out_trade_num"` // 商户订单号 + Title string `json:"title"` // 充值产品说明 + } `json:"data"` +} + +type RechargeResult struct { + Result RechargeResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newRechargeResult(result RechargeResponse, body []byte, http gorequest.Response) *RechargeResult { + return &RechargeResult{Result: result, Body: body, Http: http} +} + +// Recharge 充值提交接口 +// https://www.showdoc.com.cn/dyr/9227003154511692 +func (c *Client) Recharge(ctx context.Context, notMustParams ...gorequest.Params) (*RechargeResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + // 请求 + request, err := c.request(ctx, apiUrl+"/index/recharge", params) + if err != nil { + return newRechargeResult(RechargeResponse{}, request.ResponseBody, request), err + } + // 定义 + var response RechargeResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newRechargeResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/remove.go b/service/wnfuwu/remove.go new file mode 100644 index 00000000..63771303 --- /dev/null +++ b/service/wnfuwu/remove.go @@ -0,0 +1,43 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type RemoveResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data struct{} `json:"data"` +} + +type RemoveResult struct { + Result RemoveResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newRemoveResult(result RemoveResponse, body []byte, http gorequest.Response) *RemoveResult { + return &RemoveResult{Result: result, Body: body, Http: http} +} + +// Remove 申请撤单【已正式上线】 +// https://www.showdoc.com.cn/dyr/9745453200292104 +func (c *Client) Remove(ctx context.Context, outTradeNums string, notMustParams ...gorequest.Params) (*RemoveResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + if outTradeNums != "" { + params.Set("out_trade_nums", outTradeNums) + } + // 请求 + request, err := c.request(ctx, apiUrl+"/index/remove", params) + if err != nil { + return newRemoveResult(RemoveResponse{}, request.ResponseBody, request), err + } + // 定义 + var response RemoveResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newRemoveResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/request.go b/service/wnfuwu/request.go new file mode 100644 index 00000000..f55c37c9 --- /dev/null +++ b/service/wnfuwu/request.go @@ -0,0 +1,42 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library" + "github.com/dtapps/go-library/utils/gorequest" +) + +// 请求接口 +func (c *Client) request(ctx context.Context, url string, params map[string]interface{}) (gorequest.Response, error) { + + // 签名 + params["sign"] = c.sign(params) + + // 创建请求 + client := c.requestClient + + // 设置请求地址 + client.SetUri(url) + + // 设置FORM格式 + client.SetContentTypeForm() + + // 设置参数 + client.SetParams(params) + + // 传入SDk版本 + client.AfferentSdkUserVersion(go_library.Version()) + + // 发起请求 + request, err := client.Post(ctx) + if err != nil { + return gorequest.Response{}, err + } + + // 日志 + if c.log.status { + go c.log.client.Middleware(ctx, request, go_library.Version()) + } + + return request, err +} diff --git a/service/wnfuwu/sign.go b/service/wnfuwu/sign.go new file mode 100644 index 00000000..979d45ff --- /dev/null +++ b/service/wnfuwu/sign.go @@ -0,0 +1,23 @@ +package wnfuwu + +import ( + "fmt" + "github.com/dtapps/go-library/utils/gomd5" + "github.com/dtapps/go-library/utils/gostring" + "sort" +) + +// 签名 +func (c *Client) sign(params map[string]interface{}) string { + var keys []string + for k := range params { + keys = append(keys, k) + } + sort.Strings(keys) + signStr := "" + for _, key := range keys { + signStr += fmt.Sprintf("%s=%s&", key, gostring.ToString(params[key])) + } + signStr += fmt.Sprintf("apikey=%s", c.GetApiKey()) + return gomd5.ToUpper(signStr) +} diff --git a/service/wnfuwu/typecate.go b/service/wnfuwu/typecate.go new file mode 100644 index 00000000..81c33330 --- /dev/null +++ b/service/wnfuwu/typecate.go @@ -0,0 +1,48 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type TypecateResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data []struct { + Id int64 `json:"id"` // 产品类型id + TypeName string `json:"type_name"` // 产品类型名称 + Cate []struct { + Id int64 `json:"id"` // 分类ID + Cate string `json:"type_name"` // 分类名称 + Type int64 `json:"type"` // 产品类型ID + } `json:"cate"` // 分类列表 + } `json:"data,omitempty"` +} + +type TypecateResult struct { + Result TypecateResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newTypecateResult(result TypecateResponse, body []byte, http gorequest.Response) *TypecateResult { + return &TypecateResult{Result: result, Body: body, Http: http} +} + +// Typecate 获取产品类型和产品分类 +// https://www.showdoc.com.cn/dyr/9227005390454727 +func (c *Client) Typecate(ctx context.Context, notMustParams ...gorequest.Params) (*TypecateResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + params.Set("userid", c.GetUserId()) + // 请求 + request, err := c.request(ctx, apiUrl+"/index/typecate", params) + if err != nil { + return newTypecateResult(TypecateResponse{}, request.ResponseBody, request), err + } + // 定义 + var response TypecateResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newTypecateResult(response, request.ResponseBody, request), err +} diff --git a/service/wnfuwu/user.go b/service/wnfuwu/user.go new file mode 100644 index 00000000..95903e38 --- /dev/null +++ b/service/wnfuwu/user.go @@ -0,0 +1,44 @@ +package wnfuwu + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" +) + +type UserResponse struct { + Errno int64 `json:"errno"` // 错误码,0代表成功,非0代表失败 + Errmsg string `json:"errmsg"` // 错误描述 + Data struct { + Id int64 `json:"id"` // userid + Username string `json:"username"` // 名称 + Balance string `json:"balance"` // 余额 + } `json:"data,omitempty"` +} + +type UserResult struct { + Result UserResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newUserResult(result UserResponse, body []byte, http gorequest.Response) *UserResult { + return &UserResult{Result: result, Body: body, Http: http} +} + +// User 查询用户信息 +// https://www.showdoc.com.cn/dyr/9227004018562421 +func (c *Client) User(ctx context.Context, notMustParams ...gorequest.Params) (*UserResult, error) { + // 参数 + params := gorequest.NewParams() + params.Set("userid", c.GetUserId()) + // 请求 + request, err := c.request(ctx, apiUrl+"/index/user", params) + if err != nil { + return newUserResult(UserResponse{}, request.ResponseBody, request), err + } + // 定义 + var response UserResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newUserResult(response, request.ResponseBody, request), err +}