go语言写的人脸本地化程序
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.
 
 
 
 
 

920 line
24 KiB

  1. package business
  2. /**
  3. * @Author: yk
  4. * @Date: 2024/01/30 15:04
  5. * @Desc: 外部接口实现,关于人员的接口
  6. */
  7. import (
  8. "bytes"
  9. "digimetastaffsync/common"
  10. "digimetastaffsync/mydatabase"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "mime/multipart"
  16. "net/http"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. var timeStep int
  23. func init() {
  24. common.LoadConfig()
  25. //初始化时间步长时间步长
  26. timeStep = common.ConfigData.Timer
  27. }
  28. type FeatureResp struct {
  29. Status int `json:"status"`
  30. Message string `json:"message"`
  31. Result []interface{} `json:"result"`
  32. }
  33. type Staff struct {
  34. ID string `json:"id"`
  35. Name string `json:"name"`
  36. NickName string `json:"nickName"`
  37. Phone string `json:"phone"`
  38. Type int `json:"type"`
  39. Url string `json:"Url"`
  40. Image string `json:"image"`
  41. }
  42. type Req struct {
  43. Code string `json:"code"`
  44. StaffName string `json:"staffName"`
  45. StaffType int `json:"staffType"`
  46. Gender int `json:"gender"`
  47. DevId string `json:"devId"`
  48. FaceFeature string `json:"faceFeature"`
  49. HireDate string `json:"hireDate"`
  50. Birthday string `json:"birthDate"`
  51. Phone string `json:"phone"`
  52. StaffBase64Img string `json:"staffBase64Img"`
  53. DelFlag int `json:"status"`
  54. Modified string `json:"modified"`
  55. }
  56. func RegStaff(param Req) (*common.Response, error) {
  57. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.PushDataUrl
  58. common.Info(common.GenLogLine()+"reg staff url:%s\n", url)
  59. method := "POST"
  60. param.DevId = common.ConfigData.DevId
  61. if len(param.StaffBase64Img) < 20000 {
  62. param.StaffBase64Img = ""
  63. }
  64. fmt.Printf("code:%s, staffName:%s, staffType:%d, gender:%d, devId:%s, faceFeature:%s, hireDate:%s, birthday:%s, phone:%s, DelFlag:%d, modified:%s\n",
  65. param.Code, param.StaffName, param.StaffType, param.Gender, param.DevId, param.FaceFeature, param.HireDate, param.Birthday, param.Phone, param.DelFlag, param.Modified)
  66. common.Info(common.GenLogLine()+"exec reg code:%s, staffName:%s, staffType:%d, gender:%d, devId:%s, faceFeature:%s, hireDate:%s, birthday:%s, phone:%s, DelFlag:%d, modified:%s\n",
  67. param.Code, param.StaffName, param.StaffType, param.Gender, param.DevId, param.FaceFeature, param.HireDate, param.Birthday, param.Phone, param.DelFlag, param.Modified)
  68. fmt.Println()
  69. jsonData, err := json.Marshal(param)
  70. if err != nil {
  71. fmt.Println("Error marshaling JSON:", err)
  72. }
  73. req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
  74. if err != nil {
  75. fmt.Errorf(common.GenLogLine()+"%s", err)
  76. common.Error(common.GenLogLine()+"%s", err)
  77. return nil, err
  78. }
  79. req.Header.Add("Content-Type", "application/json")
  80. client := &http.Client{}
  81. res, err := client.Do(req)
  82. if err != nil {
  83. fmt.Errorf(common.GenLogLine()+"%s", err)
  84. common.Error(common.GenLogLine()+"%s", err)
  85. return nil, err
  86. }
  87. defer res.Body.Close()
  88. body, err := io.ReadAll(res.Body)
  89. if err != nil {
  90. fmt.Errorf(common.GenLogLine()+"%s", err)
  91. common.Error(common.GenLogLine()+"%s", err)
  92. return nil, err
  93. }
  94. var response *common.Response
  95. err = json.Unmarshal([]byte(string(body)), &response)
  96. if err != nil {
  97. fmt.Println("Error:", err)
  98. return nil, err
  99. }
  100. if response.Code != 200 || response.Code != 0 {
  101. return response, errors.New(response.Msg)
  102. }
  103. common.Error(common.GenLogLine()+"reg result %s", string(body))
  104. return response, nil
  105. }
  106. func RegOnlineStaff(param Req) error {
  107. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.PushDataUrl
  108. fmt.Printf("url:%s\n", url)
  109. method := "POST"
  110. param.DevId = common.ConfigData.DevId
  111. fmt.Printf("====param:%v\n", param)
  112. jsonData, err := json.Marshal(param)
  113. if err != nil {
  114. fmt.Println("Error marshaling JSON:", err)
  115. }
  116. req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
  117. if err != nil {
  118. fmt.Errorf(common.GenLogLine()+"%s", err)
  119. common.Error(common.GenLogLine()+"%s", err)
  120. return err
  121. }
  122. req.Header.Add("Content-Type", "application/json")
  123. client := &http.Client{}
  124. res, err := client.Do(req)
  125. if err != nil {
  126. fmt.Errorf(common.GenLogLine()+"%s", err)
  127. common.Error(common.GenLogLine()+"%s", err)
  128. return err
  129. }
  130. defer res.Body.Close()
  131. body, err := io.ReadAll(res.Body)
  132. if err != nil {
  133. fmt.Errorf(common.GenLogLine()+"%s", err)
  134. common.Error(common.GenLogLine()+"%s", err)
  135. return err
  136. }
  137. fmt.Println(string(body))
  138. return nil
  139. }
  140. func HandleStaff(emp Req) (*common.Response, error) {
  141. if emp.StaffBase64Img != "" {
  142. //feature = FetchStaffFaceFeature(emp.StaffBase64Img)
  143. //emp.FaceFeature = LocalFetchStaffFaceFeature(emp.StaffBase64Img)
  144. }
  145. if common.ConfigData.Mode != "online" {
  146. emp.StaffBase64Img = ""
  147. }
  148. //======东方微银逻辑======start====
  149. if strings.Contains(common.ConfigData.EnterpriseName, "东方微银") {
  150. var sType int
  151. if emp.StaffType > 70 {
  152. sType = 4
  153. } else {
  154. sType = 5
  155. }
  156. emp.StaffType = sType
  157. var sDels = []int{5, 6, 7, 8}
  158. if common.AryContainInt(sDels, emp.DelFlag) {
  159. emp.DelFlag = 1
  160. } else {
  161. emp.DelFlag = 0
  162. }
  163. }
  164. //======东方微银逻辑======end====
  165. resp, err := RegStaff(emp)
  166. if err != nil {
  167. return resp, err
  168. }
  169. return resp, nil
  170. }
  171. // Fetch2 获取员工信息,模拟获取saas的人员数据
  172. func Fetch2() []Staff {
  173. _token, err := common.GetToken()
  174. if err != nil {
  175. fmt.Errorf(common.GenLogLine()+"%s", err)
  176. common.Error(common.GenLogLine()+"%s", err)
  177. return nil
  178. }
  179. url := common.ConfigData.FetchDataUrl
  180. method := "GET"
  181. payload := strings.NewReader(``)
  182. client := &http.Client{}
  183. req, err := http.NewRequest(method, url, payload)
  184. if err != nil {
  185. fmt.Errorf(common.GenLogLine()+"%s", err)
  186. common.Error(common.GenLogLine()+"%s", err)
  187. return nil
  188. }
  189. token := fmt.Sprintf(`Bearer %s`, _token)
  190. req.Header.Add("Authorization", token)
  191. res, err := client.Do(req)
  192. if err != nil {
  193. fmt.Errorf(common.GenLogLine()+"%s", err)
  194. common.Error(common.GenLogLine()+"%s", err)
  195. return nil
  196. }
  197. defer res.Body.Close()
  198. body, err := io.ReadAll(res.Body)
  199. if err != nil {
  200. fmt.Errorf(common.GenLogLine()+"%s", err)
  201. common.Error(common.GenLogLine()+"%s", err)
  202. return nil
  203. }
  204. var response *common.Response
  205. err = json.Unmarshal([]byte(string(body)), &response)
  206. if err != nil {
  207. fmt.Println("Error:", err)
  208. }
  209. var result []Staff
  210. switch v := response.Data["items"].(type) {
  211. case []interface{}:
  212. for _, item := range v {
  213. var s Staff
  214. //fmt.Println(item)
  215. if item.(map[string]interface{})["id"] != nil {
  216. s.ID = (item.(map[string]interface{})["id"]).(string)
  217. }
  218. if item.(map[string]interface{})["name"] != nil {
  219. s.Name = (item.(map[string]interface{})["name"]).(string)
  220. }
  221. if item.(map[string]interface{})["nickName"] != nil {
  222. s.NickName = (item.(map[string]interface{})["nickName"]).(string)
  223. }
  224. if item.(map[string]interface{})["phone"] != nil {
  225. s.Phone = (item.(map[string]interface{})["phone"]).(string)
  226. }
  227. if item.(map[string]interface{})["type"] != nil {
  228. s.Type = (item.(map[string]interface{})["type"]).(int)
  229. }
  230. if item.(map[string]interface{})["resource"] != nil {
  231. if item.(map[string]interface{})["resource"].(map[string]interface{})["url"] != nil {
  232. s.Url = (item.(map[string]interface{})["resource"].(map[string]interface{})["url"]).(string)
  233. }
  234. }
  235. result = append(result, s)
  236. }
  237. default:
  238. fmt.Println("无法解析的类型")
  239. }
  240. return result
  241. }
  242. // AbleSendCode 发送验证码
  243. func AbleSendCode(phone string) string {
  244. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.CheckSendCodeUrl + "/" + phone
  245. method := "GET"
  246. payload := strings.NewReader("")
  247. client := &http.Client{}
  248. req, err := http.NewRequest(method, url, payload)
  249. if err != nil {
  250. fmt.Errorf(common.GenLogLine()+"%s", err)
  251. common.Error(common.GenLogLine()+"%s", err)
  252. return err.Error()
  253. }
  254. res, err := client.Do(req)
  255. if err != nil {
  256. fmt.Errorf(common.GenLogLine()+"%s", err)
  257. common.Error(common.GenLogLine()+"%s", err)
  258. return err.Error()
  259. }
  260. defer res.Body.Close()
  261. body, err := io.ReadAll(res.Body)
  262. if err != nil {
  263. fmt.Errorf(common.GenLogLine()+"%s", err)
  264. common.Error(common.GenLogLine()+"%s", err)
  265. return err.Error()
  266. }
  267. fmt.Println("response Body:", string(body))
  268. return string(body)
  269. }
  270. func SendCode(phone string) string {
  271. ableSend := AbleSendCode(phone)
  272. fmt.Println("ableSend:", ableSend)
  273. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.SendCodeUrl
  274. method := "POST"
  275. payload := strings.NewReader(`{
  276. "phone":"` + phone + `"}`)
  277. client := &http.Client{}
  278. req, err := http.NewRequest(method, url, payload)
  279. if err != nil {
  280. fmt.Errorf(common.GenLogLine()+"%s", err)
  281. common.Error(common.GenLogLine()+"%s", err)
  282. return err.Error()
  283. }
  284. req.Header.Add("Content-Type", "application/json")
  285. res, err := client.Do(req)
  286. if err != nil {
  287. fmt.Errorf(common.GenLogLine()+"%s", err)
  288. common.Error(common.GenLogLine()+"%s", err)
  289. return err.Error()
  290. }
  291. defer res.Body.Close()
  292. body, err := io.ReadAll(res.Body)
  293. if err != nil {
  294. fmt.Errorf(common.GenLogLine()+"%s", err)
  295. common.Error(common.GenLogLine()+"%s", err)
  296. return err.Error()
  297. }
  298. fmt.Println("response Body:", string(body))
  299. var result map[string]interface{}
  300. err = json.Unmarshal([]byte(string(body)), &result)
  301. if err != nil {
  302. fmt.Println("解析JSON出错:", err)
  303. return err.Error()
  304. }
  305. return string(body)
  306. }
  307. func ValidCode(phone string, code string) (string, error) {
  308. ableSend := AbleSendCode(phone)
  309. fmt.Println("ableSend:", ableSend)
  310. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.ValidCodeUrl + phone + "/" + code
  311. method := "GET"
  312. payload := strings.NewReader(``)
  313. client := &http.Client{}
  314. req, err := http.NewRequest(method, url, payload)
  315. res, err := client.Do(req)
  316. if err != nil {
  317. fmt.Errorf(common.GenLogLine()+"%s", err)
  318. common.Error(common.GenLogLine()+"%s", err)
  319. return "", err
  320. }
  321. defer res.Body.Close()
  322. body, err := io.ReadAll(res.Body)
  323. if err != nil {
  324. fmt.Errorf(common.GenLogLine()+"%s", err)
  325. common.Error(common.GenLogLine()+"%s", err)
  326. return "", err
  327. }
  328. fmt.Println("response Body:", string(body))
  329. common.Error(common.GenLogLine()+"reg result %s", string(body))
  330. return string(body), nil
  331. }
  332. func StaffInfo(staffId string) (map[string]interface{}, error) {
  333. fmt.Println("staffId:", staffId)
  334. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.StaffInfoUrl
  335. method := "GET"
  336. token, err := common.GetToken()
  337. if err != nil {
  338. return nil, err
  339. }
  340. payload := &bytes.Buffer{}
  341. writer := multipart.NewWriter(payload)
  342. _ = writer.WriteField("id", staffId)
  343. err = writer.Close()
  344. if err != nil {
  345. fmt.Println(err)
  346. return nil, err
  347. }
  348. client := &http.Client{}
  349. req, err := http.NewRequest(method, url, payload)
  350. if err != nil {
  351. fmt.Println(err)
  352. return nil, err
  353. }
  354. req.Header.Add("Authorization", "Bearer "+token)
  355. req.Header.Set("Content-Type", writer.FormDataContentType())
  356. res, err := client.Do(req)
  357. if err != nil {
  358. fmt.Println(err)
  359. return nil, err
  360. }
  361. defer res.Body.Close()
  362. body, err := io.ReadAll(res.Body)
  363. if err != nil {
  364. fmt.Println(err)
  365. return nil, err
  366. }
  367. fmt.Println(string(body))
  368. var result map[string]interface{}
  369. err = json.Unmarshal([]byte(string(body)), &result)
  370. if err != nil {
  371. fmt.Println("解析JSON出错:", err)
  372. return nil, err
  373. }
  374. items, ok := result["data"].(map[string]interface{})["items"].([]interface{})
  375. if !ok {
  376. fmt.Println("无法获取items字段")
  377. return nil, err
  378. }
  379. if len(items) == 0 {
  380. return nil, errors.New("未找到员工信息")
  381. }
  382. firstItem := items[0].(map[string]interface{})
  383. common.Error(common.GenLogLine()+"info result %v", firstItem)
  384. return firstItem, nil
  385. }
  386. func MgrLogin(userName string, password string) string {
  387. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.MgrLoginUrl
  388. fmt.Println("url:", url)
  389. enterpriseName := common.ConfigData.EnterpriseName
  390. method := "POST"
  391. payload := strings.NewReader(`{
  392. "enterpriseName":"` + enterpriseName + `",
  393. "userName":"` + userName + `",
  394. "password":"` + password + `"}`)
  395. client := &http.Client{}
  396. req, err := http.NewRequest(method, url, payload)
  397. if err != nil {
  398. fmt.Errorf(common.GenLogLine()+"%s", err)
  399. common.Error(common.GenLogLine()+"%s", err)
  400. return err.Error()
  401. }
  402. req.Header.Add("Content-Type", "application/json")
  403. res, err := client.Do(req)
  404. if err != nil {
  405. fmt.Errorf(common.GenLogLine()+"%s", err)
  406. common.Error(common.GenLogLine()+"%s", err)
  407. return err.Error()
  408. }
  409. defer res.Body.Close()
  410. body, err := io.ReadAll(res.Body)
  411. if err != nil {
  412. fmt.Errorf(common.GenLogLine()+"%s", err)
  413. common.Error(common.GenLogLine()+"%s", err)
  414. return err.Error()
  415. }
  416. fmt.Println("response Body:", string(body))
  417. return string(body)
  418. }
  419. func PhoneLogin(phone string) (map[string]interface{}, error) {
  420. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.PhoneLoginUrl
  421. method := "POST"
  422. payload := strings.NewReader(`{
  423. "phone":"` + phone + `"}`)
  424. client := &http.Client{}
  425. req, _ := http.NewRequest(method, url, payload)
  426. req.Header.Add("Content-Type", "application/json")
  427. res, err := client.Do(req)
  428. if err != nil {
  429. fmt.Errorf(common.GenLogLine()+"%s", err)
  430. common.Error(common.GenLogLine()+"%s", err)
  431. return nil, err
  432. }
  433. defer res.Body.Close()
  434. body, err := io.ReadAll(res.Body)
  435. if err != nil {
  436. fmt.Errorf(common.GenLogLine()+"%s", err)
  437. common.Error(common.GenLogLine()+"%s", err)
  438. return nil, err
  439. }
  440. fmt.Println("response Body:", string(body))
  441. var result map[string]interface{}
  442. err = json.Unmarshal([]byte(string(body)), &result)
  443. if err != nil {
  444. fmt.Println("解析JSON出错:", err)
  445. return nil, err
  446. }
  447. return result, nil
  448. }
  449. func Fetch() []Req {
  450. url := common.ConfigData.FetchData2Url
  451. method := "POST"
  452. payload := strings.NewReader("appId=dfwycustomer2u8&ygbh")
  453. client := &http.Client{}
  454. req, err := http.NewRequest(method, url, payload)
  455. if err != nil {
  456. fmt.Errorf(common.GenLogLine()+"%s", err)
  457. common.Error(common.GenLogLine()+"%s", err)
  458. return nil
  459. }
  460. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  461. res, err := client.Do(req)
  462. if err != nil {
  463. fmt.Errorf(common.GenLogLine()+"%s", err)
  464. common.Error(common.GenLogLine()+"%s", err)
  465. return nil
  466. }
  467. defer res.Body.Close()
  468. body, err := io.ReadAll(res.Body)
  469. if err != nil {
  470. fmt.Errorf(common.GenLogLine()+"%s", err)
  471. common.Error(common.GenLogLine()+"%s", err)
  472. return nil
  473. }
  474. var response *common.Resp
  475. err = json.Unmarshal([]byte(string(body)), &response)
  476. if err != nil {
  477. fmt.Println("Error:", err)
  478. }
  479. var result []Req
  480. fmt.Printf("====%#v====datassss\n", response)
  481. for _, item := range response.Data {
  482. var s Req
  483. //fmt.Println(item)
  484. if item.(map[string]interface{})["workcode"] != nil {
  485. s.Code = (item.(map[string]interface{})["workcode"]).(string)
  486. }
  487. if item.(map[string]interface{})["name"] != nil {
  488. s.StaffName = (item.(map[string]interface{})["name"]).(string)
  489. }
  490. if item.(map[string]interface{})["mobile"] != nil {
  491. s.Phone = (item.(map[string]interface{})["mobile"]).(string)
  492. }
  493. if item.(map[string]interface{})["level"] != nil {
  494. s.StaffType, _ = strconv.Atoi((item.(map[string]interface{})["level"]).(string))
  495. }
  496. if item.(map[string]interface{})["birthday"] != nil {
  497. s.Birthday = (item.(map[string]interface{})["birthday"]).(string)
  498. }
  499. if item.(map[string]interface{})["companystartdate"] != nil {
  500. s.HireDate = (item.(map[string]interface{})["companystartdate"]).(string)
  501. }
  502. if item.(map[string]interface{})["status"] != nil {
  503. s.DelFlag, _ = strconv.Atoi((item.(map[string]interface{})["status"]).(string))
  504. }
  505. if item.(map[string]interface{})["image"] != nil {
  506. s.StaffBase64Img = (item.(map[string]interface{})["image"]).(string)
  507. }
  508. if item.(map[string]interface{})["modified"] != nil {
  509. s.Modified = (item.(map[string]interface{})["modified"]).(string)
  510. }
  511. result = append(result, s)
  512. }
  513. if len(result) > 0 {
  514. sort.Slice(result, func(i, j int) bool {
  515. a, err := time.Parse("2006-01-02 15:04:05", result[i].Modified)
  516. if err != nil {
  517. common.Error(common.GenLogLine() + err.Error())
  518. }
  519. b, err2 := time.Parse("2006-01-02 15:04:05", result[j].Modified)
  520. if err2 != nil {
  521. common.Error(common.GenLogLine() + err2.Error())
  522. }
  523. return b.Before(a)
  524. })
  525. }
  526. return result
  527. }
  528. func FetchStaffImage(ygbh string) string {
  529. url := common.ConfigData.FetchData2Url
  530. method := "POST"
  531. payload := strings.NewReader("appId=dfwycustomer2u8&ygbh=" + fmt.Sprintf("%s", ygbh))
  532. client := &http.Client{}
  533. req, err := http.NewRequest(method, url, payload)
  534. if err != nil {
  535. fmt.Errorf(common.GenLogLine()+"%s", err)
  536. common.Error(common.GenLogLine()+"%s", err)
  537. return ""
  538. }
  539. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  540. res, _ := client.Do(req)
  541. defer res.Body.Close()
  542. body, err := io.ReadAll(res.Body)
  543. if err != nil {
  544. fmt.Errorf(common.GenLogLine()+"%s", err)
  545. common.Error(common.GenLogLine()+"%s", err)
  546. return ""
  547. }
  548. var response *common.Resp
  549. err = json.Unmarshal([]byte(string(body)), &response)
  550. if err != nil {
  551. fmt.Println("Error:", err)
  552. }
  553. var result = ""
  554. for _, item := range response.Data {
  555. var s string
  556. //fmt.Println(item)
  557. if item.(map[string]interface{})["image"] != nil {
  558. s = (item.(map[string]interface{})["image"]).(string)
  559. }
  560. result = s
  561. }
  562. return strings.Replace(result, "\n", "", -1)
  563. }
  564. func FetchStaffFaceFeature(base64 string) (string, error) {
  565. url := "http://" + common.ConfigData.ServerIp + ":8080/" + common.ConfigData.FaceDetectUrl
  566. method := "POST"
  567. err := common.SaveBase64ImageToFile(base64, "images/"+fmt.Sprintf("%d", time.Now().Unix())+"test.jpg")
  568. fmt.Printf("%v\n", err)
  569. payload := strings.NewReader(`{
  570. "detect":true,
  571. "base64Data":"` + base64 + `"}`)
  572. client := &http.Client{}
  573. req, err := http.NewRequest(method, url, payload)
  574. if err != nil {
  575. fmt.Errorf(common.GenLogLine()+"%s", err)
  576. common.Error(common.GenLogLine()+"%s", err)
  577. return "", err
  578. }
  579. req.Header.Add("Authorization", "Bearer ")
  580. req.Header.Add("Content-Type", "application/json")
  581. res, err := client.Do(req)
  582. if err != nil {
  583. fmt.Errorf(common.GenLogLine()+"%s", err)
  584. common.Error(common.GenLogLine()+"%s", err)
  585. return "", err
  586. }
  587. defer res.Body.Close()
  588. body, err := io.ReadAll(res.Body)
  589. if err != nil {
  590. fmt.Errorf(common.GenLogLine()+"%s", err)
  591. common.Error(common.GenLogLine()+"%s", err)
  592. return "", err
  593. }
  594. fmt.Println(string(body))
  595. var response *FeatureResp
  596. err = json.Unmarshal([]byte(string(body)), &response)
  597. if err != nil {
  598. fmt.Println("Error:", err)
  599. return "", err
  600. }
  601. var result string
  602. result, err = extractFeature(response.Result[0])
  603. if err != nil {
  604. return "", err
  605. }
  606. fmt.Printf(" success data : %+v\n", result)
  607. return result, nil
  608. }
  609. func extractFeature(data interface{}) (string, error) {
  610. // 将 interface{} 类型转换为 map[string]interface{}
  611. resultMap, ok := data.(map[string]interface{})
  612. if !ok {
  613. return "", fmt.Errorf("Invalid data format")
  614. }
  615. // 取出 "faces" 字段
  616. faces, ok := resultMap["faces"].([]interface{})
  617. if !ok || len(faces) == 0 {
  618. return "", fmt.Errorf("No faces found")
  619. }
  620. // 取出 "feature" 字段
  621. feature, ok := faces[0].(map[string]interface{})["feature"].([]interface{})
  622. if !ok || len(feature) == 0 {
  623. return "", fmt.Errorf("No feature found")
  624. }
  625. // 转换 feature 字段为 []float64 类型
  626. var featureValues []float64
  627. for _, value := range feature {
  628. if floatValue, ok := value.(float64); ok {
  629. featureValues = append(featureValues, floatValue)
  630. } else {
  631. return "", fmt.Errorf("Invalid feature value type")
  632. }
  633. }
  634. str := common.FloatSliceToString(featureValues)
  635. return str, nil
  636. }
  637. func LocalFetchStaffFaceFeature(base64 string) (string, string, error) {
  638. url := "http://" + common.ConfigData.DevIp + common.ConfigData.LocalDetectUrl
  639. method := "POST"
  640. imgName := "static/faces/" + fmt.Sprintf("%d", time.Now().Unix()) + "test.jpg"
  641. err := common.SaveBase64ImageToFile(base64, imgName)
  642. payload := strings.NewReader(`{
  643. "imgbase64":"` + base64 + `"}`)
  644. client := &http.Client{}
  645. req, _ := http.NewRequest(method, url, payload)
  646. //req.Header.Add("Authorization", "Bearer ")
  647. req.Header.Add("Content-Type", "application/json")
  648. res, err := client.Do(req)
  649. if err != nil {
  650. fmt.Errorf(common.GenLogLine()+"%s", err)
  651. common.Error(common.GenLogLine()+"%s", err)
  652. return "", imgName, err
  653. }
  654. defer res.Body.Close()
  655. body, err := io.ReadAll(res.Body)
  656. if err != nil {
  657. fmt.Errorf(common.GenLogLine()+"%s", err)
  658. common.Error(common.GenLogLine()+"%s", err)
  659. return "", imgName, err
  660. }
  661. fmt.Println(string(body))
  662. var response map[string]interface{}
  663. err = json.Unmarshal([]byte(string(body)), &response)
  664. if err != nil {
  665. fmt.Println("Error:", err)
  666. return "", imgName, err
  667. }
  668. faces, ok := response["data"].(map[string]interface{})
  669. if !ok {
  670. fmt.Errorf("No faces found")
  671. return "", imgName, errors.New("No faces found")
  672. }
  673. feature, ok1 := faces["faceData"].(map[string]interface{})["theFeature"].([]interface{})
  674. if !ok1 || len(feature) == 0 {
  675. return "", imgName, err
  676. }
  677. // 转换 feature 字段为 []float64 类型
  678. var featureValues []float64
  679. for _, value := range feature {
  680. if floatValue, ok := value.(float64); ok {
  681. featureValues = append(featureValues, floatValue)
  682. } else {
  683. return "", imgName, err
  684. }
  685. }
  686. str := common.FloatSliceToString(featureValues)
  687. return str, imgName, nil
  688. }
  689. func GetMaxId(dataList []Staff) string {
  690. maxID := dataList[0].ID
  691. common.Info(common.GenLogLine()+"maxID:", maxID)
  692. for _, data := range dataList {
  693. if data.ID > maxID {
  694. maxID = data.ID
  695. common.Info(common.GenLogLine()+"maxID:%s", maxID)
  696. }
  697. }
  698. return maxID
  699. }
  700. func GetNewestModified(dataList []Req) string {
  701. if len(dataList) == 0 {
  702. return ""
  703. }
  704. maxID := dataList[0].Modified
  705. return maxID
  706. }
  707. func TimerHandle() {
  708. fmt.Printf("TimerHandle start %d\n", timeStep)
  709. ticker := time.NewTicker(time.Second * time.Duration(timeStep))
  710. defer ticker.Stop()
  711. for range ticker.C { //十秒执行一次
  712. common.Info(common.GenLogLine() + "=====执行timerHandle开始=====")
  713. now := time.Now()
  714. // 将时间设置为23点59分59秒
  715. t := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 55, 0, now.Location())
  716. duration := t.Sub(now)
  717. isWithin10Seconds := duration > 0*time.Second && duration <= time.Duration(timeStep)*time.Second
  718. common.Info(common.GenLogLine() + "--1,执行日志分割判断 是否开始切割:--" + strconv.FormatBool(isWithin10Seconds))
  719. if isWithin10Seconds {
  720. common.Info(common.GenLogLine() + "--1.1执行RenameLogFile--")
  721. common.RenameLogFile()
  722. }
  723. common.Info(common.GenLogLine() + "--1,执行日志分割判断 END--")
  724. if common.ConfigData.Sync == 1 {
  725. common.Info(common.GenLogLine() + "--2,数据同步 START--")
  726. //获取接口数据
  727. staffs := Fetch()
  728. cachedTime := common.GetCache("maxTime")
  729. if len(staffs) == 0 {
  730. common.Info(common.GenLogLine() + "数据为空")
  731. } else {
  732. //获取最新更新时间
  733. maxTime := GetNewestModified(staffs)
  734. var t1 time.Time
  735. var t2 time.Time
  736. var t3 time.Time
  737. if cachedTime == "" {
  738. cachedTime = common.ReadFile("di.txt")
  739. common.SetCacheWithExpire("maxTime", cachedTime, 0)
  740. }
  741. t1, _ = time.Parse("2006-01-02 15:04:05", cachedTime)
  742. t2, _ = time.Parse("2006-01-02 15:04:05", maxTime)
  743. if !t2.After(t1) {
  744. common.Info(common.GenLogLine() + "--2.1, 数据已被同步")
  745. } else {
  746. common.SetCacheWithExpire("maxTime", maxTime, 0)
  747. for _, staff := range staffs {
  748. time.Sleep(1 * time.Second) // 暂停1秒
  749. t3, _ = time.Parse("2006-01-02 15:04:05", staff.Modified)
  750. if t3.After(t1) {
  751. staff.StaffBase64Img = FetchStaffImage(staff.Code)
  752. common.Info(common.GenLogLine()+"----%v", "" == staff.StaffBase64Img)
  753. _, str := HandleStaff(staff)
  754. common.Info(common.GenLogLine()+"--2.2注册人员 姓名:%s 编号:%s 更新时间:%s--\n", staff.StaffName, staff.Code, staff.Modified)
  755. //str := RegOnlineStaff(staff)
  756. ss := ""
  757. if str == nil {
  758. ss = "保存成功"
  759. } else {
  760. ss = str.Error()
  761. }
  762. fmt.Printf("注册人员:%v\n", ss)
  763. common.Info(common.GenLogLine()+"--2.3注册人员:%v--\n", ss)
  764. } else {
  765. break
  766. }
  767. }
  768. common.WriteFile("di.txt", maxTime)
  769. common.Info(common.GenLogLine()+"--2.4,数据同步,缓存数据maxID-- :%s", common.GetCache("maxTime"))
  770. }
  771. }
  772. common.Info(common.GenLogLine() + "--2,数据同步 END--")
  773. }
  774. common.Info(common.GenLogLine() + "=====执行timerHandle结束=====")
  775. }
  776. }
  777. func EmpToReq(emp mydatabase.Emp) Req {
  778. var req Req
  779. req.StaffName = emp.Name
  780. req.StaffType = 5 //普通员工
  781. req.Phone = emp.Phone
  782. str, _ := common.FileToBase64(emp.Avatar)
  783. req.StaffBase64Img = str
  784. req.FaceFeature = emp.Features
  785. return req
  786. }