|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- package svc
-
- import (
- "applet/app/e"
- "applet/app/md"
- "applet/app/utils"
- "crypto/sha1"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/syyongx/php2go"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
-
- var (
- USER = "1157453392@qq.com" //必填,飞鹅云后台注册的账号名
- UKEY = "apXI36MYv6rkY8ft" //必填,飞鹅云后台注册账号后生成的UKEY
- //SN = "xxxxxxxxx" //必填,打印机编号,必须要在管理后台里手动添加打印机或者通过API添加之后,才能调用API
-
- URL = "http://api.feieyun.cn/Api/Open/" //不需要修改
- )
-
- // 添加打印机
- func AddPrinter(c *gin.Context, args md.PrinterReq) error {
- //提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
- if args.PrinterType == "cloud" { //云打印
- //AddCloudPrinter(c, args)
- } else {
- //拼接参数
- snList := fmt.Sprintf("%s#%s#%s", args.Sn, args.Key, args.Remark)
- //请求接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_printerAddlist") //固定
- postValues.Add("printerContent", snList) //打印机
- data := post(postValues)
- fmt.Println(data)
- var res md.AddPrinterResult
- err := json.Unmarshal([]byte(data), &res)
- if err != nil {
- return e.NewErr(400, "添加失败")
- }
- //处理错误信息
- if len(res.Data.No) > 0 {
- msg := res.Data.No[0]
- msgArr := strings.Split(msg, " ")
- msg = ""
- if len(msgArr) > 0 {
- msg = msgArr[1]
- }
- //被添加过的 默认成功
- if strings.Contains(msg, "已被添加过") {
- e.OutSuc(c, "success", nil)
- return nil
- }
- return e.NewErr(400, "添加失败,"+msg)
- }
- return nil
- }
- return nil
-
- }
-
- // 打印机状态
- func PrinterState(c *gin.Context) {
- var args md.PrinterReq
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS)
- return
- }
- if args.PrinterType == "cloud" { //云打印
- //GetPrinterStatus(c, args)
- } else {
- //调用接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_queryPrinterStatus") //固定
- postValues.Add("sn", args.Sn) //打印机编号
- data := post(postValues)
- fmt.Println(data)
- var res md.PrinterStateResult
- err := json.Unmarshal([]byte(data), &res)
- //处理返回信息
- r := make(map[string]string, 0)
- if err != nil {
- r["state"] = "0"
- r["msg"] = err.Error()
- }
- if res.Ret != 0 {
- r["state"] = "0"
- r["msg"] = res.Msg
- }
- data1 := utils.AnyToString(res.Data)
- r["msg"] = data1
- if strings.Contains(data, "离线") {
- r["state"] = "1"
- }
- if strings.Contains(data, "在线,工作状态正常") {
- r["state"] = "2"
- }
- if strings.Contains(data, "在线,工作状态不正常") {
- r["state"] = "3"
- }
- e.OutSuc(c, r, nil)
- return
- }
- }
-
- // 删除打印机
- func DelPrinter(c *gin.Context, args md.PrinterReq) error {
-
- if args.PrinterType == "cloud" { //云打印
- //DelCloudPrinter(c, args)
- } else {
- //拼接参数
- snList := args.Sn
- //请求接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_printerDelList") //固定
- postValues.Add("snlist", snList) //打印机
- data := post(postValues)
- fmt.Println(data)
- var res md.AddPrinterResult
- err := json.Unmarshal([]byte(data), &res)
- if err != nil {
- return e.NewErr(400, "删除失败")
- }
- //处理错误信息
- if len(res.Data.No) > 0 {
- msg := res.Data.No[0]
- return e.NewErr(400, "删除失败,"+msg)
- }
- return nil
- }
- return nil
- }
-
- // 修改打印机
- func EditPrinter(c *gin.Context, args md.PrinterReq) error {
- if args.PrinterType == "cloud" { //云打印
- //AddCloudPrinter(c, args)
- } else {
- //请求接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_printerEdit") //固定
- postValues.Add("sn", args.Sn) //打印机编号
- postValues.Add("name", args.Remark) //打印机备注名称
- if args.PhoneNum != "" {
- postValues.Add("phonenum", args.PhoneNum)
- } //打印机流量卡号码
- data := post(postValues)
- fmt.Println(data)
- var res md.PrinterStateResult
- err := json.Unmarshal([]byte(data), &res)
- if err != nil {
- return e.NewErr(400, "修改失败")
- }
- //处理错误信息
- if res.Ret != 0 {
- return e.NewErr(400, "修改失败,"+res.Msg)
- }
- return nil
- }
- return nil
- }
-
- // 打印机操作
- func PrinterDoing(c *gin.Context) {
- var args md.PrinterReq
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS)
- return
- }
- data, msg := CommDoing(c, args.Content, args.Sn, args.PrinterType, args.Oid)
- if msg != "" {
- e.OutErr(c, 400, e.NewErr(400, msg))
- return
- }
- e.OutSuc(c, map[string]string{"oid": utils.AnyToString(data)}, nil)
- return
-
- }
- func CommDoing(c *gin.Context, content, sn, printerType, oid string) (string, string) {
- if printerType == "cloud" { //云打印
- //args := md.PrinterReq{
- // Sn: sn,
- // Content: content,
- // Oid: oid,
- // PrinterType: printerType,
- //}
- //oids, err := CloudPrintDoing(c, args)
- //if err != nil || oids == "" {
- // return "", "打印失败"
- //}
- return "", ""
- } else {
- content1, _ := php2go.Base64Decode(content)
- if content1 != "" {
- content = content1
- }
- //请求接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_printMsg") //固定
- postValues.Add("sn", sn) //打印机编号
- postValues.Add("content", content) //打印内容
- postValues.Add("times", "1") //打印次数,默认为1。
- data := post(postValues)
- fmt.Println(data)
- var res md.PrinterStateResult
- err := json.Unmarshal([]byte(data), &res)
- if err != nil {
- return "", "打印失败"
- }
- //处理错误信息
- if res.Ret != 0 {
- return "", "打印失败," + res.Msg
- }
- return res.Data.(string), ""
- }
- }
-
- // 订单打印状态
- func PrinterOrderState(c *gin.Context) {
- var args md.PrinterReq
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS)
- return
- }
- if args.PrinterType == "cloud" { //云打印
- //GetOrdStatus(c, args)
- } else {
- //请求接口
- postValues := url.Values{}
- postValues.Add("apiname", "Open_queryOrderState") //固定
- postValues.Add("orderid", args.Oid) //订单ID
- data := post(postValues)
- fmt.Println(data)
- var res md.PrinterStateResult
- err := json.Unmarshal([]byte(data), &res)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "查询失败"))
- return
- }
- //处理错误信息
- if res.Ret != 0 {
- e.OutErr(c, 400, e.NewErr(400, "查询失败,"+res.Msg))
- return
- }
- reqState := utils.AnyToBool(res.Data)
- state := "0"
- if reqState {
- state = "1"
- }
- e.OutSuc(c, map[string]string{"state": state}, nil)
- return
- }
-
- }
-
- func post(postValues url.Values) string {
- itime := time.Now().Unix()
- stime := strconv.FormatInt(itime, 10)
- sig := SHA1(USER + UKEY + stime) //生成签名
- client := http.Client{}
- postValues.Add("user", USER) //账号名
- postValues.Add("stime", stime) //当前时间的秒数,请求时间
- postValues.Add("sig", sig) //签名
- res, _ := client.PostForm(URL, postValues)
- data, _ := ioutil.ReadAll(res.Body)
- fmt.Println(string(data)) //服务器返回的JSON字符串,建议要当做日志记录起来
- res.Body.Close()
- return string(data)
- }
-
- func SHA1(str string) string {
- s := sha1.Sum([]byte(str))
- strsha1 := hex.EncodeToString(s[:])
- return strsha1
- }
|