Reconstruct daily+jw_notification

Engilsh Version

Low-Level Errors

  • When using Gorm statements, the model was not specified (missing .Model or .TableName methods).
  • Forgot to change the template_id when sending data to the WeChat interface.
  • Forgot to concatenate miniprogram.
  • Changed AEEESS_TOKEN randomly when querying data from mp_conf, here it should be set to 9.
  • Did not pay attention to the required data when requesting data from the interface.
  • After defining the global variable cstlocGlobal, used := when using the LoadLocation method, 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 assembling noti, causing the data to not display correctly.
  • Wrote req.Param as a map[string]interface{} type.
  • Used incorrect data types with the fmt.Sprintf method, leading to garbled display.
  • Forgot to add "/person", "/time", etc., when concatenating data.

Misunderstandings

  • Used the Find method when matching branchID and branchTitle, 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 the First and Find methods and the model.
  • The operation for checking whether the data queried from the database was empty was cumbersome; simply checking RowsAffected would suffice.
  • Used multiple nested for loops; using the map data 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 LoadLocation method first:
    cstlocGlobal, err = time.LoadLocation("Asia/Shanghai") // If a global variable is defined, do not use :=
  • Convert an int to a float directly using float(int data).
  • When converting float to string, use the FormatFloat method:
    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 the auth first:
    func basicAuth(username, password string) string { auth := username + ":" + password return base64.StdEncoding.EncodeToString([]byte(auth)) }
    Then add it to the request header:
    header := req.Header{ "Authorization": "Basic " + auth, }

 


 

低级错误

  1. 在使用gorm语句时没有指定模型(缺少.model或者tablename方法)

  2. 给微信接口发送数据时忘记换template_id

  3. 忘记拼接miniprogram

  4. 从mp_conf中查询数据时乱改AEEESS_TOKEN,这里指定为9

  5. 向接口请求数据时没有注意要求的数据

  6. 定义全局变量cstlocGlobal后,在使用LoadLocation方法时用了:=,这样赋值的是局部变量,后续使用时会导致miss location问题

  7. 发送请求时“branch_id”最后多了一个空格导致显示缺少参数

  8. 拼装noti时“color”属性打成了“color'”导致数据无法显示

  9. 将req.Param写成map[string]interface{}类型

  10. 使用fmt.Sprintf方法时数据类型对应错误,导致显示乱码

  11. 拼接数据时少“/人”,"/次"等字

 

理解错误

  1. 匹配branchID和branchTitle时使用了Find方法,当时的想法是branchID和branchTitle存在多对多关系,只要查出其中一条对应关系就可以了,导致只能查出机构一的信息。犯错原因是对First和Find方法以及模型理解不够深刻。

  2. 判断从数据库中查询的数据是否为空时操作繁琐,只要判断RowsAffected就可以了

  3. 套了多重for循环,适当使用map数据类型能降低时间开销:根据key去匹配,如果成功就取出value

 

新用到的技术

  1. 创建md5对象,md5是一种加密方式,不同的接口需要的加密源数据是不同的

  2. 获取昨天时间

    yesterday := time.Now.AddDate(0,0,-1)   //不要将AddDate写成Add
  3. 获取时间戳

    time := strconv.FormatInt(time.Now().Unix(), 10)
  4. 获取时间要先使用LoadLocation方法

    cstlocGlobal, err = time.LoadLocation("Asia/Shanghai")  //若定义了全局变量,不能用:=
  5. int强转float,直接float(int数据)

  6. float转string,要用到FormatFloat方法

    var a float
    str := strconv.FormatFloat(a, 'f', 2, 64) // 'f'表示a是无指数形式,2表示保留2两位小数,64表示a是float64类型
  7. 请求带auth,要先将auth加密转换

    func basicAuth(username, password string) string {
    auth := username + ":" + password
    return base64.StdEncoding.EncodeToString([]byte(auth))
    }

    然后添加到请求头中

    header = req.Header{
       "Authorization":"Basic " + auth,
    }
posted @ 2021-12-15 11:36  临卓  阅读(61)  评论(0)    收藏  举报