From 593bf8e46a6dc6accc0c9d5c034d6d2d2a6d7ab9 Mon Sep 17 00:00:00 2001 From: jiangyong Date: Thu, 18 Jun 2026 23:48:47 +0800 Subject: [PATCH] ext --- erp/approval_profit.go | 53 ++++++++++++++++++++++++++++++++ erp/erp.go | 19 ++++++++++++ erp/staff_target.go | 42 +++++++++++++++++++++++++ erp/staff_user.go | 69 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 erp/approval_profit.go create mode 100644 erp/erp.go create mode 100644 erp/staff_target.go create mode 100644 erp/staff_user.go diff --git a/erp/approval_profit.go b/erp/approval_profit.go new file mode 100644 index 0000000..ec38428 --- /dev/null +++ b/erp/approval_profit.go @@ -0,0 +1,53 @@ +package erp + +import ( + "errors" + + "git.u8t.cn/open/goutil" + "github.com/tidwall/gjson" +) + +const ( + ApprovalProfitAmountTypeCost = "消耗" + ApprovalProfitAmountTypeProfit = "利润" + ApprovalProfitAmountTypeAward = "奖励" +) + +type ApprovalProfitCreate struct { + SpNo string `json:"sp_no"` //申请单号,相同的单号数据将会被替换 + Month string `json:"month"` //业绩对应月份 + Username string `json:"username"` //业绩对应责任人 + AmountType string `json:"amount_type"` //业绩类型 + Amount string `json:"amount"` //业绩金额 单位元 + Description string `json:"description"` //业绩描述 + AppId string `json:"app_id"` //业绩对应的业务ID +} + +type ApprovalProfit struct { + Erp +} + +func NewApprovalProfit(baseUrl, token string) *ApprovalProfit { + return &ApprovalProfit{ + Erp: Erp{ + baseUrl: baseUrl, + token: token, + }, + } +} + +func (e *ApprovalProfit) Create(req *ApprovalProfitCreate) error { + var reqBody string + reqBody = goutil.EncodeJSON(req) + reqUrl := e.GetBaseUrl() + "/ext/staff/profit" + + body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(reqBody)) + if err != nil { + return err + } + g := gjson.ParseBytes(body) + if g.Get("code").Int() != 0 { + return errors.New(string(body)) + } + return nil +} diff --git a/erp/erp.go b/erp/erp.go new file mode 100644 index 0000000..ad9767b --- /dev/null +++ b/erp/erp.go @@ -0,0 +1,19 @@ +package erp + +type Erp struct { + token string + baseUrl string +} + +func (e *Erp) GetHeader() map[string]string { + return map[string]string{ + "x-token": e.token, + } +} + +func (e *Erp) GetBaseUrl() string { + if e.baseUrl != "" { + return e.baseUrl + } + return "http://eapi.batiao8.com" +} diff --git a/erp/staff_target.go b/erp/staff_target.go new file mode 100644 index 0000000..475cc30 --- /dev/null +++ b/erp/staff_target.go @@ -0,0 +1,42 @@ +package erp + +import ( + "errors" + + "git.u8t.cn/open/goutil" + "github.com/tidwall/gjson" +) + +type StaffTarget struct { + Erp +} + +type StaffTargetCreate struct { + Username string `json:"username"` + Month string `json:"month"` + Day string `json:"day"` + Score string `json:"score"` + Comment string `json:"comment"` +} + +func NewStaffTarget(baseUrl, token string) *StaffTarget { + return &StaffTarget{ + Erp: Erp{ + baseUrl: baseUrl, + token: token, + }, + } +} + +func (e *StaffTarget) Create(req *StaffTargetCreate) error { + reqUrl := e.GetBaseUrl() + "/ext/staff/target" + body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(goutil.EncodeJSON(req))) + if err != nil { + return err + } + g := gjson.ParseBytes(body) + if g.Get("code").Int() != 0 { + return errors.New(string(body)) + } + return nil +} diff --git a/erp/staff_user.go b/erp/staff_user.go new file mode 100644 index 0000000..751cdbc --- /dev/null +++ b/erp/staff_user.go @@ -0,0 +1,69 @@ +package erp + +import ( + "errors" + "fmt" + + "git.u8t.cn/open/goutil" + "github.com/tidwall/gjson" +) + +type StaffCode struct { + Timestamp string `json:"timestamp"` +} +type StaffInfo struct { + Username string `json:"username"` + Realname string `json:"realname"` + Phone string `json:"phone"` +} +type StaffUser struct { + Erp +} + +func NewStaffUser(baseUrl, token string) *StaffUser { + return &StaffUser{ + Erp: Erp{ + baseUrl: baseUrl, + token: token, + }, + } +} + +func (e *StaffUser) SendCode(username, scene string) (*StaffCode, error) { + var reqBody string + reqBody = fmt.Sprintf(`{"username":"%s","scene":"%s"}`, username, scene) + reqUrl := e.GetBaseUrl() + "/ext/staff/user/code" + + body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(reqBody)) + if err != nil { + return nil, err + } + g := gjson.ParseBytes(body) + if g.Get("code").Int() != 0 { + return nil, errors.New(string(body)) + } + r := new(StaffCode) + r.Timestamp = g.Get("data.timestamp").String() + + return r, nil +} + +func (e *StaffUser) Login(username, timestamp, code string) (*StaffInfo, error) { + reqBody := fmt.Sprintf(`{"username":"%s","timestamp":"%s","code":"%s"}`, username, timestamp, code) + reqUrl := e.GetBaseUrl() + "/ext/staff/login" + body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(reqBody)) + if err != nil { + return nil, err + } + g := gjson.ParseBytes(body) + if g.Get("code").Int() != 0 { + return nil, errors.New(string(body)) + } + + r := new(StaffInfo) + r.Realname = g.Get("data.realname").String() + r.Username = g.Get("data.username").String() + r.Phone = g.Get("data.phone").String() + + return r, nil +}