写代码时跟前端时没跟前端约定返回格式,就随意写了,等约定后,发现需要改的地方有近100处,发现手动改要手抽筋,就chatgpt问了个答案,然后自己魔改了下,快速用正则给搞定了。
约定格式为:
jsonify(code, message, data), http_code
1. 缺少data数据
原始代码如下:
jsonify(code=-1, message="queries not found"), 404
使用正则如下,$1是引用正则匹配的结果,然后拼接上data=‘’即可。

替换结果如下:
jsonify(code=-1, message="queries not found", data=''), 404
2. 缺少message
原始代码如下:
jsonify(code=-1, data=user.to_json()), 404
使用正则如下:

替换结果如下:
jsonify(code=-1, message="queries not found", data=''), 404
文章讲述了开发者在编写代码时未遵循前端约定的JSON格式,导致大量修改需求。作者使用正则表达式快速修复了返回结构,包括添加缺失的`message`和`data`字段。
2670

被折叠的 条评论
为什么被折叠?



