附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 4 недеља
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "crypto/sha1"
  7. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/syyongx/php2go"
  12. "io/ioutil"
  13. "net/http"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. var (
  20. USER = "1157453392@qq.com" //必填,飞鹅云后台注册的账号名
  21. UKEY = "apXI36MYv6rkY8ft" //必填,飞鹅云后台注册账号后生成的UKEY
  22. //SN = "xxxxxxxxx" //必填,打印机编号,必须要在管理后台里手动添加打印机或者通过API添加之后,才能调用API
  23. URL = "http://api.feieyun.cn/Api/Open/" //不需要修改
  24. )
  25. // 添加打印机
  26. func AddPrinter(c *gin.Context, args md.PrinterReq) error {
  27. //提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
  28. if args.PrinterType == "cloud" { //云打印
  29. //AddCloudPrinter(c, args)
  30. } else {
  31. //拼接参数
  32. snList := fmt.Sprintf("%s#%s#%s", args.Sn, args.Key, args.Remark)
  33. //请求接口
  34. postValues := url.Values{}
  35. postValues.Add("apiname", "Open_printerAddlist") //固定
  36. postValues.Add("printerContent", snList) //打印机
  37. data := post(postValues)
  38. fmt.Println(data)
  39. var res md.AddPrinterResult
  40. err := json.Unmarshal([]byte(data), &res)
  41. if err != nil {
  42. return e.NewErr(400, "添加失败")
  43. }
  44. //处理错误信息
  45. if len(res.Data.No) > 0 {
  46. msg := res.Data.No[0]
  47. msgArr := strings.Split(msg, " ")
  48. msg = ""
  49. if len(msgArr) > 0 {
  50. msg = msgArr[1]
  51. }
  52. //被添加过的 默认成功
  53. if strings.Contains(msg, "已被添加过") {
  54. e.OutSuc(c, "success", nil)
  55. return nil
  56. }
  57. return e.NewErr(400, "添加失败,"+msg)
  58. }
  59. return nil
  60. }
  61. return nil
  62. }
  63. // 打印机状态
  64. func PrinterState(c *gin.Context) {
  65. var args md.PrinterReq
  66. if err := c.ShouldBindJSON(&args); err != nil {
  67. e.OutErr(c, e.ERR_INVALID_ARGS)
  68. return
  69. }
  70. if args.PrinterType == "cloud" { //云打印
  71. //GetPrinterStatus(c, args)
  72. } else {
  73. //调用接口
  74. postValues := url.Values{}
  75. postValues.Add("apiname", "Open_queryPrinterStatus") //固定
  76. postValues.Add("sn", args.Sn) //打印机编号
  77. data := post(postValues)
  78. fmt.Println(data)
  79. var res md.PrinterStateResult
  80. err := json.Unmarshal([]byte(data), &res)
  81. //处理返回信息
  82. r := make(map[string]string, 0)
  83. if err != nil {
  84. r["state"] = "0"
  85. r["msg"] = err.Error()
  86. }
  87. if res.Ret != 0 {
  88. r["state"] = "0"
  89. r["msg"] = res.Msg
  90. }
  91. data1 := utils.AnyToString(res.Data)
  92. r["msg"] = data1
  93. if strings.Contains(data, "离线") {
  94. r["state"] = "1"
  95. }
  96. if strings.Contains(data, "在线,工作状态正常") {
  97. r["state"] = "2"
  98. }
  99. if strings.Contains(data, "在线,工作状态不正常") {
  100. r["state"] = "3"
  101. }
  102. e.OutSuc(c, r, nil)
  103. return
  104. }
  105. }
  106. // 删除打印机
  107. func DelPrinter(c *gin.Context, args md.PrinterReq) error {
  108. if args.PrinterType == "cloud" { //云打印
  109. //DelCloudPrinter(c, args)
  110. } else {
  111. //拼接参数
  112. snList := args.Sn
  113. //请求接口
  114. postValues := url.Values{}
  115. postValues.Add("apiname", "Open_printerDelList") //固定
  116. postValues.Add("snlist", snList) //打印机
  117. data := post(postValues)
  118. fmt.Println(data)
  119. var res md.AddPrinterResult
  120. err := json.Unmarshal([]byte(data), &res)
  121. if err != nil {
  122. return e.NewErr(400, "删除失败")
  123. }
  124. //处理错误信息
  125. if len(res.Data.No) > 0 {
  126. msg := res.Data.No[0]
  127. return e.NewErr(400, "删除失败,"+msg)
  128. }
  129. return nil
  130. }
  131. return nil
  132. }
  133. // 修改打印机
  134. func EditPrinter(c *gin.Context, args md.PrinterReq) error {
  135. if args.PrinterType == "cloud" { //云打印
  136. //AddCloudPrinter(c, args)
  137. } else {
  138. //请求接口
  139. postValues := url.Values{}
  140. postValues.Add("apiname", "Open_printerEdit") //固定
  141. postValues.Add("sn", args.Sn) //打印机编号
  142. postValues.Add("name", args.Remark) //打印机备注名称
  143. if args.PhoneNum != "" {
  144. postValues.Add("phonenum", args.PhoneNum)
  145. } //打印机流量卡号码
  146. data := post(postValues)
  147. fmt.Println(data)
  148. var res md.PrinterStateResult
  149. err := json.Unmarshal([]byte(data), &res)
  150. if err != nil {
  151. return e.NewErr(400, "修改失败")
  152. }
  153. //处理错误信息
  154. if res.Ret != 0 {
  155. return e.NewErr(400, "修改失败,"+res.Msg)
  156. }
  157. return nil
  158. }
  159. return nil
  160. }
  161. // 打印机操作
  162. func PrinterDoing(c *gin.Context) {
  163. var args md.PrinterReq
  164. if err := c.ShouldBindJSON(&args); err != nil {
  165. e.OutErr(c, e.ERR_INVALID_ARGS)
  166. return
  167. }
  168. data, msg := CommDoing(c, args.Content, args.Sn, args.PrinterType, args.Oid)
  169. if msg != "" {
  170. e.OutErr(c, 400, e.NewErr(400, msg))
  171. return
  172. }
  173. e.OutSuc(c, map[string]string{"oid": utils.AnyToString(data)}, nil)
  174. return
  175. }
  176. func CommDoing(c *gin.Context, content, sn, printerType, oid string) (string, string) {
  177. if printerType == "cloud" { //云打印
  178. //args := md.PrinterReq{
  179. // Sn: sn,
  180. // Content: content,
  181. // Oid: oid,
  182. // PrinterType: printerType,
  183. //}
  184. //oids, err := CloudPrintDoing(c, args)
  185. //if err != nil || oids == "" {
  186. // return "", "打印失败"
  187. //}
  188. return "", ""
  189. } else {
  190. content1, _ := php2go.Base64Decode(content)
  191. if content1 != "" {
  192. content = content1
  193. }
  194. //请求接口
  195. postValues := url.Values{}
  196. postValues.Add("apiname", "Open_printMsg") //固定
  197. postValues.Add("sn", sn) //打印机编号
  198. postValues.Add("content", content) //打印内容
  199. postValues.Add("times", "1") //打印次数,默认为1。
  200. data := post(postValues)
  201. fmt.Println(data)
  202. var res md.PrinterStateResult
  203. err := json.Unmarshal([]byte(data), &res)
  204. if err != nil {
  205. return "", "打印失败"
  206. }
  207. //处理错误信息
  208. if res.Ret != 0 {
  209. return "", "打印失败," + res.Msg
  210. }
  211. return res.Data.(string), ""
  212. }
  213. }
  214. // 订单打印状态
  215. func PrinterOrderState(c *gin.Context) {
  216. var args md.PrinterReq
  217. if err := c.ShouldBindJSON(&args); err != nil {
  218. e.OutErr(c, e.ERR_INVALID_ARGS)
  219. return
  220. }
  221. if args.PrinterType == "cloud" { //云打印
  222. //GetOrdStatus(c, args)
  223. } else {
  224. //请求接口
  225. postValues := url.Values{}
  226. postValues.Add("apiname", "Open_queryOrderState") //固定
  227. postValues.Add("orderid", args.Oid) //订单ID
  228. data := post(postValues)
  229. fmt.Println(data)
  230. var res md.PrinterStateResult
  231. err := json.Unmarshal([]byte(data), &res)
  232. if err != nil {
  233. e.OutErr(c, 400, e.NewErr(400, "查询失败"))
  234. return
  235. }
  236. //处理错误信息
  237. if res.Ret != 0 {
  238. e.OutErr(c, 400, e.NewErr(400, "查询失败,"+res.Msg))
  239. return
  240. }
  241. reqState := utils.AnyToBool(res.Data)
  242. state := "0"
  243. if reqState {
  244. state = "1"
  245. }
  246. e.OutSuc(c, map[string]string{"state": state}, nil)
  247. return
  248. }
  249. }
  250. func post(postValues url.Values) string {
  251. itime := time.Now().Unix()
  252. stime := strconv.FormatInt(itime, 10)
  253. sig := SHA1(USER + UKEY + stime) //生成签名
  254. client := http.Client{}
  255. postValues.Add("user", USER) //账号名
  256. postValues.Add("stime", stime) //当前时间的秒数,请求时间
  257. postValues.Add("sig", sig) //签名
  258. res, _ := client.PostForm(URL, postValues)
  259. data, _ := ioutil.ReadAll(res.Body)
  260. fmt.Println(string(data)) //服务器返回的JSON字符串,建议要当做日志记录起来
  261. res.Body.Close()
  262. return string(data)
  263. }
  264. func SHA1(str string) string {
  265. s := sha1.Sum([]byte(str))
  266. strsha1 := hex.EncodeToString(s[:])
  267. return strsha1
  268. }