Reconstruct daily+jw_notification
Engilsh Version
Low-Level Errors
- When using Gorm statements, the model was not specified (missing
.Modelor.TableNamemethods). - Forgot to change the
template_idwhen sending data to the WeChat interface. - Forgot to concatenate
miniprogram. - Changed
AEEESS_TOKENrandomly when querying data frommp_conf, here it should be set to9. - Did not pay attention to the required data when requesting data from the interface.
- After defining the global variable
cstlocGlobal, used:=when using theLoadLocationmethod, which assigned a local variable, causing a "miss location" issue later. - Added an extra space after
"branch_id"when sending a request, leading to a missing parameter error. - Misspelled the
"color"property as"color'"when assemblingnoti, causing the data to not display correctly. - Wrote
req.Paramas amap[string]interface{}type. - Used incorrect data types with the
fmt.Sprintfmethod, leading to garbled display. - Forgot to add
"/person","/time", etc., when concatenating data.
Misunderstandings
- Used the
Findmethod when matchingbranchIDandbranchTitle, thinking there was a many-to-many relationship between them, so retrieving any one corresponding relationship was enough. This led to only retrieving the information for one organization. The error was due to a lack of understanding of theFirstandFindmethods and the model. - The operation for checking whether the data queried from the database was empty was cumbersome; simply checking
RowsAffectedwould suffice. - Used multiple nested
forloops; using themapdata type appropriately could reduce time costs: match by key and retrieve the value if successful.
New Techniques Used
- Created an MD5 object; MD5 is a type of encryption, and different interfaces require different encrypted source data.
- Get the time for yesterday:
yesterday := time.Now().AddDate(0, 0, -1) // Do not write AddDate as Add - Get a timestamp:
time := strconv.FormatInt(time.Now().Unix(), 10) - When getting time, use the
LoadLocationmethod first:cstlocGlobal, err = time.LoadLocation("Asia/Shanghai") // If a global variable is defined, do not use := - Convert an
intto afloatdirectly usingfloat(int data). - When converting
floattostring, use theFormatFloatmethod:var a float str := strconv.FormatFloat(a, 'f', 2, 64) // 'f' indicates that `a` is in non-exponential form, 2 indicates two decimal places, and 64 indicates that `a` is of type float64 - When making a request with
auth, encrypt theauthfirst:Then add it to the request header:func basicAuth(username, password string) string { auth := username + ":" + password return base64.StdEncoding.EncodeToString([]byte(auth)) }header := req.Header{ "Authorization": "Basic " + auth, }
低级错误
-
-
给微信接口发送数据时忘记换template_id
-
忘记拼接miniprogram
-
从mp_conf中查询数据时乱改AEEESS_TOKEN,这里指定为9
-
向接口请求数据时没有注意要求的数据
-
定义全局变量cstlocGlobal后,在使用LoadLocation方法时用了:=,这样赋值的是局部变量,后续使用时会导致miss location问题
-
发送请求时“branch_id”最后多了一个空格导致显示缺少参数
-
拼装noti时“color”属性打成了“color'”导致数据无法显示
-
将req.Param写成map[string]interface{}类型
-
使用fmt.Sprintf方法时数据类型对应错误,导致显示乱码
-
拼接数据时少“/人”,"/次"等字
理解错误
-
匹配branchID和branchTitle时使用了Find方法,当时的想法是branchID和branchTitle存在多对多关系,只要查出其中一条对应关系就可以了,导致只能查出机构一的信息。犯错原因是对First和Find方法以及模型理解不够深刻。
-
判断从数据库中查询的数据是否为空时操作繁琐,只要判断RowsAffected就可以了
-
套了多重for循环,适当使用map数据类型能降低时间开销:根据key去匹配,如果成功就取出value
新用到的技术
-
创建md5对象,md5是一种加密方式,不同的接口需要的加密源数据是不同的
-
获取昨天时间
yesterday := time.Now.AddDate(0,0,-1) //不要将AddDate写成Add -
获取时间戳
time := strconv.FormatInt(time.Now().Unix(), 10) -
获取时间要先使用LoadLocation方法
cstlocGlobal, err = time.LoadLocation("Asia/Shanghai") //若定义了全局变量,不能用:= -
int强转float,直接float(int数据)
-
float转string,要用到FormatFloat方法
var a float
str := strconv.FormatFloat(a, 'f', 2, 64) // 'f'表示a是无指数形式,2表示保留2两位小数,64表示a是float64类型 -
请求带auth,要先将auth加密转换
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}然后添加到请求头中
header := req.Header{
"Authorization":"Basic " + auth,
}

浙公网安备 33010602011771号