perf: replace ioutil with io according to the official recommendation

main
zggsong 2 years ago
parent 613f695749
commit c960c0f90f

@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"luxshare-daily-report/model"
"luxshare-daily-report/util"
"net/http"
@ -14,13 +14,12 @@ import (
"strings"
)
//
// Login
// @Description: 登陆网站
// @param userName
// @param passwd
// @return string
//
// @Description: 登陆网站
// @param userName
// @param passwd
// @return string
func Login(userName, passwd string) (string, string, error) {
loginUrl := "https://m.luxshare-ict.com/api/Account/Login"
contentType := "application/x-www-form-urlencoded"
@ -39,7 +38,7 @@ func Login(userName, passwd string) (string, string, error) {
if err != nil {
return "", "", errors.New(fmt.Sprintf("(Login) Request Error: %v", err))
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
//fmt.Println(string(body))
var loginModel model.LoginResp
err = json.Unmarshal(body, &loginModel)
@ -88,13 +87,12 @@ func convert(info model.UserInfo) string {
return escape
}
//
// Upload2Azure
// @Description: 上传图片到Azure服务器
// @param auth
// @param images
// @return []string
//
// @Description: 上传图片到Azure服务器
// @param auth
// @param images
// @return []string
func Upload2Azure(auth, user string, images map[string]string) ([]string, error) {
var client = &http.Client{}
uploadUrl := "https://p.luxshare-ict.com/api/Azure/TencentFileToAzure"
@ -121,7 +119,7 @@ func Upload2Azure(auth, user string, images map[string]string) ([]string, error)
//resp, err := http.Post(uploadUrl, contentType, strings.NewReader(postData.Encode()))
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
var uploadModel model.Upload2AzureResp
err = json.Unmarshal(body, &uploadModel)
if err != nil {
@ -130,14 +128,13 @@ func Upload2Azure(auth, user string, images map[string]string) ([]string, error)
return uploadModel.Data.ImagePaths, nil
}
//
// GetLVIQuestInitModel
// @Description: 获取以往个人信息
// @param auth
// @param user
// @return model.LVIQuestInitModelData
// @return error
//
// @Description: 获取以往个人信息
// @param auth
// @param user
// @return model.LVIQuestInitModelData
// @return error
func GetLVIQuestInitModel(auth, user string) (model.EpidemicQuestLVI, error) {
var nilModel model.EpidemicQuestLVI
var client = &http.Client{}
@ -156,7 +153,7 @@ func GetLVIQuestInitModel(auth, user string) (model.EpidemicQuestLVI, error) {
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
var livModel model.LVIQuestInitModel
err = json.Unmarshal(body, &livModel)
if err != nil {
@ -165,13 +162,12 @@ func GetLVIQuestInitModel(auth, user string) (model.EpidemicQuestLVI, error) {
return livModel.Data.EpidemicQuestLVI, nil
}
//
// EpidemicRegistration
// @Description: 申报
// @param auth
// @param images
// @return error
//
// @Description: 申报
// @param auth
// @param images
// @return error
func EpidemicRegistration(auth, user string, images []string, data model.EpidemicQuestLVI) error {
var client = &http.Client{}
uploadUrl := "https://m.luxshare-ict.com/api/EpidemicSys/EpidemicRegistration/LVIQuestSave2"
@ -243,7 +239,7 @@ func EpidemicRegistration(auth, user string, images []string, data model.Epidemi
return errors.New(fmt.Sprintf("(EpidemicRegistration) Request Error: %v", err))
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
var epidemicRegistrationModel model.UniverseResp
err = json.Unmarshal(body, &epidemicRegistrationModel)
if err != nil {
@ -256,12 +252,11 @@ func EpidemicRegistration(auth, user string, images []string, data model.Epidemi
return nil
}
//
// RefreshDoor
// @Description: 刷新门禁
// @param auth
// @return error
//
// @Description: 刷新门禁
// @param auth
// @return error
func RefreshDoor(auth, user string) error {
var client = &http.Client{}
refreshUrl := "https://m.luxshare-ict.com/api/EpidemicSys/EpidemicRegistration/RefreshDoor"
@ -280,7 +275,7 @@ func RefreshDoor(auth, user string) error {
return errors.New(fmt.Sprintf("(RefreshDoor) Request Error: %v", err))
}
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
var refreshDoorResp model.UniverseResp
err = json.Unmarshal(body, &refreshDoorResp)
if err != nil {

@ -12,7 +12,6 @@ import (
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"luxshare-daily-report/global"
"luxshare-daily-report/serve/core"
"luxshare-daily-report/util"
@ -21,12 +20,11 @@ import (
"strconv"
)
//
// HandlerRoot
// @Description: 根GET访问处理
// @param w
// @param r
//
// @Description: 根GET访问处理
// @param w
// @param r
func HandlerRoot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`Receive File Api
@ -35,12 +33,11 @@ Just personal api for company daily reports...
https://github.com/zggsong`))
}
//
// HandlerSingleFile
// @Description: 单文件上传处理
// @param w
// @param r
//
// @Description: 单文件上传处理
// @param w
// @param r
func HandlerSingleFile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Write([]byte(`Receive File Api
@ -86,24 +83,31 @@ https://github.com/zggsong`))
w.Write([]byte("服务端接收成功...")) //这个写入到w的是输出到客户端的
}
//
// DeclarationService
// @Description: 每日申报服务
// @param files
//
// @Description: 每日申报服务
// @param files
func DeclarationService(files map[string]string) {
//登陆获取auth
auth, __user__, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord)
if auth == "" || err != nil {
log.Errorf("Ticket: %v, err:%v", auth, err.Error())
util.SendMessageError(err)
if auth == "" || __user__ == "" || err != nil {
log.Errorf("Login fail, ticket: %v, __user__: %v, err:%v", auth, __user__, err.Error())
util.SendMessageError(errors.New(fmt.Sprintf("Get token or userinfo fail, %v", err)))
return
}
//上传图片
var m = make(map[string]string, 2)
srcXcm, _ := ioutil.ReadFile(files["xcm"])
srcJkm, _ := ioutil.ReadFile(files["jkm"])
srcXcm, err := os.ReadFile(files["xcm"])
if err != nil {
util.SendMessageError(errors.New(fmt.Sprintf("Read xcm file err, %v", err)))
return
}
srcJkm, err := os.ReadFile(files["jkm"])
if err != nil {
util.SendMessageError(errors.New(fmt.Sprintf("Read jkm file err, %v", err)))
return
}
resXcm := base64.StdEncoding.EncodeToString(srcXcm)
resJkm := base64.StdEncoding.EncodeToString(srcJkm)
m["xcm"] = resXcm
@ -112,20 +116,21 @@ func DeclarationService(files map[string]string) {
//log.Printf("[DEBUG] get images links: %s", imagesLinks)
if err != nil {
log.Errorf(err.Error())
util.SendMessageError(err)
util.SendMessageError(errors.New(fmt.Sprintf("Upload images err, %v", err)))
return
}
if imagesLinks == nil {
err = errors.New("get no images links")
err = errors.New("get a link to the images")
log.Errorf(err.Error())
util.SendMessageError(err)
return
}
//TODO: if model is nil
epidemicQuestLVIData, err := core.GetLVIQuestInitModel(auth, __user__)
if err != nil {
log.Errorf("获取个人初始化信息失败: %v", err.Error())
log.Errorf("Failed to obtain personal initialization information: %v", err.Error())
util.SendMessageError(errors.New(fmt.Sprintf("Get user info fail, %v", err)))
return
}
//申报
@ -161,11 +166,10 @@ func DeclarationService(files map[string]string) {
util.SendSuccess("【成功】刷新门禁")
}
//
// CompressImageResource
// @Description: 压缩JPEG文件
// @param imagePath
//
// @Description: 压缩JPEG文件
// @param imagePath
func CompressImageResource(imagePath string) {
file, err := os.Open(imagePath)
if err != nil {
@ -200,12 +204,11 @@ func CompressImageResource(imagePath string) {
}
}
//
// HandlerMultiFiles
// @Description: 多文件上传处理
// @param w
// @param r
//
// @Description: 多文件上传处理
// @param w
// @param r
func HandlerMultiFiles(w http.ResponseWriter, r *http.Request) {
//设置内存大小
r.ParseMultipartForm(32 << 20)

Loading…
Cancel
Save