361 lines
9.7 KiB
Go
361 lines
9.7 KiB
Go
package main
|
||
|
||
import (
|
||
"../../hotime"
|
||
"../../hotime/common"
|
||
"../dri/ddsms"
|
||
"../dri/tencent"
|
||
"./admin"
|
||
"./app"
|
||
"encoding/base64"
|
||
"fmt"
|
||
"io"
|
||
"io/ioutil"
|
||
"net"
|
||
"os"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
func main() {
|
||
date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00")
|
||
fmt.Println(date, date.Unix())
|
||
//fmt.Println("0123456"[1:7])
|
||
appIns := hotime.Init("config/config.json")
|
||
go runTcpServer(&appIns) //运行tcp监测,产线监测使用
|
||
//RESTfull接口适配
|
||
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
||
|
||
if len(context.RouterString) > 1 && context.RouterString[0] == "admin" {
|
||
if context.RouterString[1] == "hotime" && context.RouterString[2] == "login" {
|
||
return true
|
||
}
|
||
if context.RouterString[1] == "hotime" && context.RouterString[2] == "logout" {
|
||
return true
|
||
}
|
||
|
||
if context.Session("admin_id").Data == nil {
|
||
context.Display(2, "你还没有登录")
|
||
return false
|
||
}
|
||
}
|
||
|
||
//文件上传接口
|
||
if len(context.RouterString) == 1 && context.RouterString[0] == "ocr" && context.Req.Method == "POST" {
|
||
|
||
//读取网络文件
|
||
ocr := context.Req.FormValue("ocr")
|
||
if len(ocr) < 100 {
|
||
|
||
context.Display(3, "没有上传文件")
|
||
return false
|
||
|
||
}
|
||
filePath := context.Config.GetString("filePath")
|
||
if filePath == "" {
|
||
filePath = "file/2006/01/02/"
|
||
}
|
||
path := time.Now().Format(filePath)
|
||
os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir)
|
||
|
||
fi, _ := base64.StdEncoding.DecodeString(ocr)
|
||
filePath = path + common.Md5(common.ObjToStr(common.RandX(100000, 9999999))) + ".jpg"
|
||
go func() {
|
||
|
||
ioutil.WriteFile(context.Config.GetString("tpt")+"/"+filePath, fi, 0666)
|
||
}()
|
||
|
||
re := tencent.OCR(ocr)
|
||
|
||
re1 := tencent.Qrcode(ocr)
|
||
data := common.Map{"url": filePath}
|
||
//fmt.Println(re1)
|
||
data["text"] = common.ObjToMap(re)
|
||
|
||
if data["text"] != nil {
|
||
lis := data.GetMap("text").GetMap("Response").GetSlice("TextDetections")
|
||
textList := common.Slice{}
|
||
for k, _ := range lis {
|
||
li := lis.GetMap(k).GetString("DetectedText")
|
||
if li != "" {
|
||
textList = append(textList, li)
|
||
}
|
||
}
|
||
data["text"] = textList
|
||
}
|
||
|
||
data["qrcode"] = common.ObjToMap(re1)
|
||
if data["qrcode"] != nil {
|
||
lis := data.GetMap("qrcode").GetMap("Response").GetSlice("CodeResults")
|
||
textList := common.Slice{}
|
||
for k, _ := range lis {
|
||
li := lis.GetMap(k).GetString("Url")
|
||
if li != "" {
|
||
textList = append(textList, li)
|
||
}
|
||
}
|
||
data["qrcode"] = textList
|
||
}
|
||
|
||
fmt.Println(data.ToJsonString())
|
||
context.Display(0, data)
|
||
return false
|
||
}
|
||
//文件上传接口
|
||
if len(context.RouterString) == 1 && context.RouterString[0] == "qrcode" && context.Req.Method == "POST" {
|
||
|
||
//读取网络文件
|
||
ocr := context.Req.FormValue("ocr")
|
||
if len(ocr) < 100 {
|
||
|
||
context.Display(3, "没有上传文件")
|
||
return false
|
||
|
||
}
|
||
|
||
go func() {
|
||
filePath := context.Config.GetString("filePath")
|
||
if filePath == "" {
|
||
filePath = "file/2006/01/02/"
|
||
}
|
||
path := time.Now().Format(filePath)
|
||
e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir)
|
||
if e != nil {
|
||
return
|
||
}
|
||
|
||
fi, _ := base64.StdEncoding.DecodeString(ocr)
|
||
filePath = path + common.Md5(common.ObjToStr(common.RandX(100000, 9999999))) + ".jpg"
|
||
ioutil.WriteFile(context.Config.GetString("tpt")+"/"+filePath, fi, 0666)
|
||
}()
|
||
|
||
re1 := tencent.Qrcode(ocr)
|
||
data := common.Map{}
|
||
data["qrcode"] = common.ObjToMap(re1)
|
||
if data["qrcode"] != nil {
|
||
lis := data.GetMap("qrcode").GetMap("Response").GetSlice("CodeResults")
|
||
textList := common.Slice{}
|
||
for k, _ := range lis {
|
||
li := lis.GetMap(k).GetString("Url")
|
||
if li != "" {
|
||
textList = append(textList, li)
|
||
}
|
||
}
|
||
data["qrcode"] = textList
|
||
}
|
||
|
||
fmt.Println(data.ToJsonString())
|
||
context.Display(0, data)
|
||
return false
|
||
}
|
||
|
||
//文件上传接口
|
||
if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" {
|
||
|
||
//读取网络文件
|
||
fi, fheader, err := context.Req.FormFile("file")
|
||
if err != nil {
|
||
context.Display(3, err)
|
||
return false
|
||
|
||
}
|
||
filePath := context.Config.GetString("filePath")
|
||
if filePath == "" {
|
||
filePath = "file/2006/01/02/"
|
||
}
|
||
|
||
path := time.Now().Format(filePath)
|
||
e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir)
|
||
if e != nil {
|
||
context.Display(3, e)
|
||
return false
|
||
}
|
||
filePath = path + common.Md5(common.ObjToStr(common.RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):]
|
||
newFile, e := os.Create(context.Config.GetString("tpt") + "/" + filePath)
|
||
|
||
if e != nil {
|
||
context.Display(3, e)
|
||
return false
|
||
}
|
||
|
||
_, e = io.Copy(newFile, fi)
|
||
|
||
if e != nil {
|
||
context.Display(3, e)
|
||
return false
|
||
}
|
||
|
||
context.Display(0, filePath)
|
||
return false
|
||
}
|
||
|
||
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
||
!(context.Router[context.RouterString[0]] != nil &&
|
||
context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {
|
||
return true
|
||
}
|
||
//排除无效操作
|
||
if len(context.RouterString) == 2 &&
|
||
context.Req.Method != "GET" &&
|
||
context.Req.Method != "POST" {
|
||
return true
|
||
}
|
||
//列表检索
|
||
if len(context.RouterString) == 2 &&
|
||
context.Req.Method == "GET" {
|
||
if context.Router[context.RouterString[0]][context.RouterString[1]]["search"] == nil {
|
||
return true
|
||
}
|
||
context.Router[context.RouterString[0]][context.RouterString[1]]["search"](context)
|
||
}
|
||
//新建
|
||
if len(context.RouterString) == 2 &&
|
||
context.Req.Method == "POST" {
|
||
if context.Router[context.RouterString[0]][context.RouterString[1]]["add"] == nil {
|
||
return true
|
||
}
|
||
context.Router[context.RouterString[0]][context.RouterString[1]]["add"](context)
|
||
}
|
||
if len(context.RouterString) == 3 &&
|
||
context.Req.Method == "POST" {
|
||
return true
|
||
}
|
||
//查询单条
|
||
if len(context.RouterString) == 3 &&
|
||
context.Req.Method == "GET" {
|
||
|
||
if context.Router[context.RouterString[0]][context.RouterString[1]]["info"] == nil {
|
||
return true
|
||
}
|
||
|
||
context.Router[context.RouterString[0]][context.RouterString[1]]["info"](context)
|
||
}
|
||
//更新
|
||
if len(context.RouterString) == 3 &&
|
||
context.Req.Method == "PUT" {
|
||
|
||
if context.Router[context.RouterString[0]][context.RouterString[1]]["update"] == nil {
|
||
return true
|
||
}
|
||
|
||
context.Router[context.RouterString[0]][context.RouterString[1]]["update"](context)
|
||
}
|
||
//移除
|
||
if len(context.RouterString) == 3 &&
|
||
context.Req.Method == "DELETE" {
|
||
|
||
if context.Router[context.RouterString[0]][context.RouterString[1]]["remove"] == nil {
|
||
return true
|
||
}
|
||
|
||
context.Router[context.RouterString[0]][context.RouterString[1]]["remove"](context)
|
||
}
|
||
context.View()
|
||
return false
|
||
})
|
||
|
||
//makeCode := code.MakeCode{}
|
||
//fmt.Println(common.ObjToStr(makeCode.Db2JSON("admin","test",appIns.Db)))
|
||
if ddsms.DefaultDDY.ApiKey == "" {
|
||
ddsms.DefaultDDY.Init(appIns.Config.GetString("smsKey"))
|
||
}
|
||
|
||
appIns.Run(hotime.Router{
|
||
"admin": admin.Project,
|
||
"app": app.Project,
|
||
})
|
||
}
|
||
func Process(conn net.Conn, appIns *hotime.Application) {
|
||
// 循环接收客户端发送的数据
|
||
client := conn.RemoteAddr().String() // 客户端IP:port
|
||
client = client[:strings.Index(client, ":")]
|
||
defer conn.Close() // 关闭conn
|
||
for {
|
||
// 创建一个新的切片
|
||
buf := make([]byte, 1024)
|
||
// fmt.Printf("服务器在等待客户端%s发送信息\n", conn.RemoteAddr().String())
|
||
n, err := conn.Read(buf) // 从conn中读取
|
||
// 等待客户端通过conn发送信息,
|
||
// 如果客户端没有发送(write),就会阻塞在这里
|
||
if err != nil {
|
||
// 一般为这个err
|
||
fmt.Printf("客户端%s已退出..\n", client)
|
||
return
|
||
}
|
||
// 显示客户端发送的内容到服务器的终端
|
||
sn := string(buf[:n])
|
||
sn = strings.Replace(sn, "\r", "", -1)
|
||
fmt.Println(client, sn) // 读到了n个数据
|
||
|
||
if len(sn) < 3 {
|
||
//that.Display(3, "参数不足,请补充参数")
|
||
fmt.Println(client, sn, "参数过短") // 读到了n个数据
|
||
continue
|
||
}
|
||
|
||
vs := appIns.Db.Select("produce", "sn,id,product_id",
|
||
common.Map{"AND": common.Map{"produce.sn[~]": sn[:len(sn)/2+1], "state[!]": 0}})
|
||
produce := common.Map{"ld": 10000}
|
||
for _, v := range vs {
|
||
ld := common.StrLd(v.GetString("sn"), sn, true)
|
||
if ld < produce.GetCeilInt("ld") {
|
||
v["ld"] = ld
|
||
produce = v
|
||
}
|
||
}
|
||
|
||
oldSn := appIns.Db.Get("produce_product", "id", common.Map{"AND": common.Map{"sn": sn, "produce_id": produce.GetCeilInt("id")}})
|
||
if oldSn != nil {
|
||
fmt.Println(client, sn, "已经添加请勿重复添加") // 读到了n个数据
|
||
continue
|
||
|
||
}
|
||
|
||
data := common.Map{
|
||
"sn": sn,
|
||
"product_id": produce.GetCeilInt("product_id"),
|
||
"produce_id": produce.GetCeilInt("id"),
|
||
"create_time": time.Now().Unix(),
|
||
"modify_time": time.Now().Unix(),
|
||
}
|
||
|
||
productLine := appIns.Db.Get("product_line", "id", common.Map{"ipaddr": client})
|
||
|
||
if productLine != nil {
|
||
data["product_line_id"] = productLine.GetCeilInt("id")
|
||
}
|
||
|
||
id := appIns.Db.Insert("produce_product", data)
|
||
if id == 0 {
|
||
//that.Display(4, "添加新成品失败,请重新添加")
|
||
fmt.Println(client, sn, "添加新成品失败,请重新添加") // 读到了n个数据
|
||
continue
|
||
}
|
||
}
|
||
}
|
||
|
||
func runTcpServer(appIns *hotime.Application) {
|
||
|
||
listen, err := net.Listen("tcp", "0.0.0.0:10000")
|
||
if err != nil {
|
||
fmt.Println("listen err =", err)
|
||
return
|
||
}
|
||
defer listen.Close() // 延时关闭listen
|
||
fmt.Println("listening success:", listen.Addr())
|
||
|
||
// 循环等待客户端来连接
|
||
fmt.Println("等待客户端来连接..")
|
||
for {
|
||
conn, err := listen.Accept()
|
||
if err != nil {
|
||
fmt.Println("Accept() err =", err)
|
||
} else {
|
||
fmt.Printf("客户端%s已连接..\n", conn.RemoteAddr().String())
|
||
}
|
||
// 准备一个协程,为客户端服务
|
||
go Process(conn, appIns)
|
||
}
|
||
|
||
}
|