Skip to content

Commit d1f5b65

Browse files
committed
添加条件比较功能
1 parent eafb2f9 commit d1f5b65

File tree

5 files changed

+74
-372
lines changed

5 files changed

+74
-372
lines changed

README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,24 @@ https://toolapi.us-south.cf.appdomain.cloud/regex?data=%7b"code"%3a0%2c"msg"%3a"
8585
## 3 字符串处理(string)
8686
目前实现字符串替换功能
8787
### 3.1 字符串替换(replace)
88+
#### 3.1.1 参数
8889
| 参数 | 默认值 | 是否必要参数 | 解释 |
8990
| :----: | :---: | :------------- | :---- |
9091
| s | "" || 要替换的数据 |
9192
| f | "" || 必须是replace |
9293
| p | "" || 要替换的关键词,支持正则 |
9394
| t | "" || 替换后的内容 |
9495
| r | "" || 返回的内容选择是json和纯文本, 默认是返回json,json会先显示转义字符,纯文本不会 |
95-
### 3.2 字符串替换(replace)
96-
| 参数 | 解释 |
97-
| :----: | :---- |
96+
#### 3.1.2 返回
97+
<font color='red'> 如果r = json,返回值如下:</font><br>
98+
| 参数 | 解释 |
99+
| :------ | :---- |
98100
| 原始字符串 | 输入的内容 |
99101
| 处理后字符串 | 替换后的字符串 |
100102
| 状态 | 运行的状态, 非OK都是异常 |
101-
text
102-
直接返回替换后的字符串
103-
### 3.1.3 用例
103+
<font color='red'> 如果r = text,直接返回文本</font><br>
104+
105+
#### 3.1.3 用例
104106
```
105107
返回json
106108
https://toolapi.us-south.cf.appdomain.cloud/string?p=%E4%BA%BA&t=%E5%AD%97%E7%AC%A6%E4%B8%B2&f=replace&r=json&s={%22text%22:%22%E6%88%91%E6%98%AF%E4%BA%BA%22}
@@ -126,4 +128,37 @@ t = 字符串
126128
r = text
127129
返回
128130
{"text":"我是字符串"}
131+
```
132+
## 4 条件(condition)
133+
### 4.1 判断(judge)
134+
判断并返回布尔值
135+
#### 4.1.1 参数
136+
| 参数 | 默认值 | 是否必要参数 | 解释 |
137+
| :----: | :---: | :------------- | :---- |
138+
| s | "" || 输入的公式 |
139+
| f | "" || 必须是judge |
140+
#### 4.1.2 返回
141+
142+
| 参数 | 解释 |
143+
| :---- | :---- |
144+
| 公式 | 输入的内容 |
145+
| 结果 | 判断结果 |
146+
| 状态 | 运行的状态, 非OK都是异常 |
147+
#### 4.1.3 用例
148+
```
149+
https://toolapi.us-south.cf.appdomain.cloud/condition?f=judge&s=1%20%3E%202
150+
返回:
151+
{
152+
"公式": "1 > 2",
153+
"结果": false,
154+
"状态": "OK"
155+
}
156+
https://toolapi.us-south.cf.appdomain.cloud/condition?f=judge&s=%272%27%3E%271%27
157+
返回:
158+
{
159+
"公式": "'2'>'1'",
160+
"结果": true,
161+
"状态": "OK"
162+
}
163+
129164
```

main.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#app.config['SECRET_KEY'] = 'hard to guess string'
1616

1717
@app.route('/', methods=['GET', 'POST'])
18-
def form():
19-
return render_template('index.html')
18+
def home():
19+
return render_template('./index.html')
2020

2121
@app.route('/timestamp', methods=['GET', 'POST'])
2222
def timestamp():
@@ -145,8 +145,23 @@ def strf():
145145
Rtv = json.dumps(Rtv, ensure_ascii=False, indent=4)
146146
return Response(Rtv, mimetype='application/json')
147147

148+
@app.route('/condition', methods=['GET', 'POST'])
149+
def judge():
150+
Rtv = {}
148151

149-
152+
f = request.args.get("f", default="")
153+
try:
154+
if (f != ""):
155+
if (f.find("judge") > -1):
156+
Rtv["公式"] = request.args.get("s", default="")
157+
Rtv["结果"] = eval(Rtv["公式"])
158+
Rtv["状态"] = "OK"
159+
else:
160+
Rtv["状态"] = "请选择功能"
161+
except Exception as e:
162+
Rtv["状态"] = str(e)
163+
164+
return Response(json.dumps(Rtv, ensure_ascii=False, indent=4), mimetype='application/json')
150165

151166
port = int(os.getenv('PORT', 80))
152167
if __name__ == "__main__":

templates/index.html

Lines changed: 8 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -15,239 +15,19 @@
1515
</style>
1616
</head>
1717
<body>
18-
<h1 id="welcome">Welcome</h1>
18+
<h1 id="toolapi">toolapi</h1>
1919
<h1 id="section">这是什么?</h1>
2020
<p>一个简单的工具API
2121
项目仓库<a href="https://github.com/AragonSnow/toolapi">https://github.com/AragonSnow/toolapi</a><br>
22+
博客:<a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/">简单工具api</a><br>
2223
如果有更多api需求,可以留言给我添加</p>
2324
<h1 id="section-1">功能</h1>
24-
<h2 id="timestamp">1 时间戳转换(/timestamp)</h2>
25-
<p>目前仅支持GMT+8 北京时间</p>
26-
<h3 id="section-2">1.1 参数</h3>
27-
<table>
28-
<thead>
29-
<tr>
30-
<th style="text-align: center;">参数</th>
31-
<th style="text-align: center;">默认值</th>
32-
<th style="text-align: left;">是否必要参数</th>
33-
<th style="text-align: left;">解释</th>
34-
</tr>
35-
</thead>
36-
<tbody>
37-
<tr>
38-
<td style="text-align: center;">t</td>
39-
<td style="text-align: center;">now</td>
40-
<td style="text-align: left;"></td>
41-
<td style="text-align: left;">要转换成时间戳的北京时间</td>
42-
</tr>
43-
<tr>
44-
<td style="text-align: center;">ts</td>
45-
<td style="text-align: center;">null</td>
46-
<td style="text-align: left;"></td>
47-
<td style="text-align: left;">要转换成北京时间的时间戳</td>
48-
</tr>
49-
<tr>
50-
<td style="text-align: center;">form</td>
51-
<td style="text-align: center;">%Y-%m-%d %H:%M:%S</td>
52-
<td style="text-align: left;"></td>
53-
<td style="text-align: left;">输出的时间格式</td>
54-
</tr>
55-
</tbody>
56-
</table>
57-
<h3 id="section-3">1.2 返回值</h3>
58-
<table>
59-
<thead>
60-
<tr>
61-
<th style="text-align: center;">参数</th>
62-
<th style="text-align: left;">解释</th>
63-
</tr>
64-
</thead>
65-
<tbody>
66-
<tr>
67-
<td style="text-align: center;">时间戳</td>
68-
<td style="text-align: left;">转换的时间戳</td>
69-
</tr>
70-
<tr>
71-
<td style="text-align: center;">时间</td>
72-
<td style="text-align: left;">格式化输出的北京时间</td>
73-
</tr>
74-
<tr>
75-
<td style="text-align: center;">状态</td>
76-
<td style="text-align: left;">运行的状态, 非OK都是异常</td>
77-
</tr>
78-
</tbody>
79-
</table>
80-
<h3 id="section-4">1.3 用例</h3>
81-
<pre><code>https://toolapi.us-south.cf.appdomain.cloud/timestamp
82-
返回现在北京时间
83-
{
84-
&quot;时间戳&quot;: 1586917839,
85-
&quot;时间&quot;: &quot;2020-04-15 10:30:39&quot;,
86-
&quot;状态&quot;: &quot;OK&quot;
87-
}
88-
</code></pre>
89-
<pre><code>https://toolapi.us-south.cf.appdomain.cloud/timestamp?ts=1586921249
90-
如果参数带时间戳,会忽略其他参数转换成北京时间
91-
{
92-
&quot;时间戳&quot;: &quot;1586921249&quot;,
93-
&quot;时间&quot;: &quot;2020-04-15 11:27:29&quot;,
94-
&quot;状态&quot;: &quot;OK&quot;
95-
}
96-
</code></pre>
97-
<pre><code>https://toolapi.us-south.cf.appdomain.cloud/timestamp?t=2020-04-15+11%3a00%3a09
98-
北京时间返回时间戳
99-
{
100-
&quot;时间戳&quot;: 1586919609,
101-
&quot;时间&quot;: &quot;2020-04-15 11:00:09&quot;,
102-
&quot;状态&quot;: &quot;OK&quot;
103-
}
104-
</code></pre>
105-
<h2 id="regex">2. 正则表达是(/regex)</h2>
106-
<p>正则匹配参数并返回匹配结果</p>
107-
<h3 id="section-5">2.1 参数</h3>
108-
<table>
109-
<thead>
110-
<tr>
111-
<th style="text-align: center;">参数</th>
112-
<th style="text-align: center;">默认值</th>
113-
<th style="text-align: left;">是否必要参数</th>
114-
<th style="text-align: left;">解释</th>
115-
</tr>
116-
</thead>
117-
<tbody>
118-
<tr>
119-
<td style="text-align: center;">data</td>
120-
<td style="text-align: center;">&quot;&quot;</td>
121-
<td style="text-align: left;"></td>
122-
<td style="text-align: left;">要匹配的数据</td>
123-
</tr>
124-
<tr>
125-
<td style="text-align: center;">p</td>
126-
<td style="text-align: center;">&quot;&quot;</td>
127-
<td style="text-align: left;"></td>
128-
<td style="text-align: left;">正则表达式</td>
129-
</tr>
130-
</tbody>
131-
</table>
132-
<h3 id="section-6">2.2 返回值</h3>
133-
<table>
134-
<thead>
135-
<tr>
136-
<th style="text-align: center;">参数</th>
137-
<th style="text-align: left;">解释</th>
138-
</tr>
139-
</thead>
140-
<tbody>
141-
<tr>
142-
<td style="text-align: center;">数据</td>
143-
<td style="text-align: left;">匹配的结果</td>
144-
</tr>
145-
<tr>
146-
<td style="text-align: center;">状态</td>
147-
<td style="text-align: left;">运行的状态, 非OK都是异常</td>
148-
</tr>
149-
</tbody>
150-
</table>
151-
<h3 id="section-7">2.3 用例</h3>
152-
<pre><code>https://toolapi.us-south.cf.appdomain.cloud/regex?data=%7b&quot;code&quot;%3a0%2c&quot;msg&quot;%3a&quot;成功&quot;%2c&quot;data&quot;%3a%7b&quot;users&quot;%3a%5b%7b&quot;name&quot;%3a&quot;张三&quot;%2c&quot;gender&quot;%3a&quot;male&quot;%2c&quot;age&quot;%3a12%7d%2c%7b&quot;name&quot;%3a&quot;李四&quot;%2c&quot;gender&quot;%3a&quot;female&quot;%2c&quot;age&quot;%3a15%7d%2c%7b&quot;name&quot;%3a&quot;王五&quot;%2c&quot;gender&quot;%3a&quot;male&quot;%2c&quot;age&quot;%3a22%7d%2c%7b&quot;name&quot;%3a&quot;赵六&quot;%2c&quot;gender&quot;%3a&quot;male&quot;%2c&quot;age&quot;%3a24%7d%5d%2c&quot;goods&quot;%3a%5b%7b&quot;name&quot;%3a&quot;apple&quot;%2c&quot;price&quot;%3a15%2c&quot;num&quot;%3a200%7d%2c%7b&quot;name&quot;%3a&quot;pear&quot;%2c&quot;price&quot;%3a18%2c&quot;num&quot;%3a100%7d%2c%7b&quot;name&quot;%3a&quot;banana&quot;%2c&quot;price&quot;%3a16%2c&quot;num&quot;%3a210%7d%5d%7d%7d&amp;p=name&quot;%3a&quot;(.%2b%3f)&quot;
153-
154-
测试数据:
155-
{&quot;code&quot;:0,&quot;msg&quot;:&quot;成功&quot;,&quot;data&quot;:{&quot;users&quot;:[{&quot;name&quot;:&quot;张三&quot;,&quot;gender&quot;:&quot;male&quot;,&quot;age&quot;:12},{&quot;name&quot;:&quot;李四&quot;,&quot;gender&quot;:&quot;female&quot;,&quot;age&quot;:15},{&quot;name&quot;:&quot;王五&quot;,&quot;gender&quot;:&quot;male&quot;,&quot;age&quot;:22},{&quot;name&quot;:&quot;赵六&quot;,&quot;gender&quot;:&quot;male&quot;,&quot;age&quot;:24}],&quot;goods&quot;:[{&quot;name&quot;:&quot;apple&quot;,&quot;price&quot;:15,&quot;num&quot;:200},{&quot;name&quot;:&quot;pear&quot;,&quot;price&quot;:18,&quot;num&quot;:100},{&quot;name&quot;:&quot;banana&quot;,&quot;price&quot;:16,&quot;num&quot;:210}]}}
156-
157-
正则表达式: name&quot;:&quot;(.+?)&quot;
158-
159-
返回结果:
160-
{
161-
&quot;数据&quot;: {
162-
&quot;1&quot;: &quot;张三&quot;,
163-
&quot;2&quot;: &quot;李四&quot;,
164-
&quot;3&quot;: &quot;王五&quot;,
165-
&quot;4&quot;: &quot;赵六&quot;,
166-
&quot;5&quot;: &quot;apple&quot;,
167-
&quot;6&quot;: &quot;pear&quot;,
168-
&quot;7&quot;: &quot;banana&quot;
169-
},
170-
&quot;状态&quot;: &quot;OK&quot;
171-
}
172-
</code></pre>
173-
<h2 id="string">3 字符串处理(string)</h2>
174-
<p>目前实现字符串替换功能</p>
175-
<h3 id="replace">3.1 字符串替换(replace)</h3>
176-
<table>
177-
<thead>
178-
<tr>
179-
<th style="text-align: center;">参数</th>
180-
<th style="text-align: center;">默认值</th>
181-
<th style="text-align: left;">是否必要参数</th>
182-
<th style="text-align: left;">解释</th>
183-
</tr>
184-
</thead>
185-
<tbody>
186-
<tr>
187-
<td style="text-align: center;">s</td>
188-
<td style="text-align: center;">&quot;&quot;</td>
189-
<td style="text-align: left;"></td>
190-
<td style="text-align: left;">要替换的数据</td>
191-
</tr>
192-
<tr>
193-
<td style="text-align: center;">f</td>
194-
<td style="text-align: center;">&quot;&quot;</td>
195-
<td style="text-align: left;"></td>
196-
<td style="text-align: left;">必须是replace</td>
197-
</tr>
198-
<tr>
199-
<td style="text-align: center;">p</td>
200-
<td style="text-align: center;">&quot;&quot;</td>
201-
<td style="text-align: left;"></td>
202-
<td style="text-align: left;">要替换的关键词,支持正则</td>
203-
</tr>
204-
<tr>
205-
<td style="text-align: center;">t</td>
206-
<td style="text-align: center;">&quot;&quot;</td>
207-
<td style="text-align: left;"></td>
208-
<td style="text-align: left;">替换后的内容</td>
209-
</tr>
210-
<tr>
211-
<td style="text-align: center;">r</td>
212-
<td style="text-align: center;">&quot;&quot;</td>
213-
<td style="text-align: left;"></td>
214-
<td style="text-align: left;">返回的内容选择是json和纯文本, 默认是返回json,json会先显示转义字符,纯文本不会</td>
215-
</tr>
216-
</tbody>
217-
</table>
218-
<h3 id="replace-1">3.2 字符串替换(replace)</h3>
219-
<p>| 参数 | 解释 |
220-
| :----: | :---- |
221-
| 原始字符串 | 输入的内容 |
222-
| 处理后字符串 | 替换后的字符串 |
223-
| 状态 | 运行的状态, 非OK都是异常 |
224-
text
225-
直接返回替换后的字符串</p>
226-
<h3 id="section-8">3.1.3 用例</h3>
227-
<pre><code>返回json
228-
https://toolapi.us-south.cf.appdomain.cloud/string?p=%E4%BA%BA&amp;t=%E5%AD%97%E7%AC%A6%E4%B8%B2&amp;f=replace&amp;r=json&amp;s={&quot;text&quot;:&quot;我是人&quot;}
229-
s = {&quot;text&quot;:&quot;我是人&quot;}
230-
f = replace
231-
p = 人
232-
t = 字符串
233-
r = json (可不用,默认)
234-
返回
235-
{
236-
&quot;原始字符串&quot;: &quot;{\&quot;text\&quot;:\&quot;我是人\&quot;}&quot;,
237-
&quot;处理后字符串&quot;: &quot;{\&quot;text\&quot;:\&quot;我是字符串\&quot;}&quot;,
238-
&quot;状态&quot;: &quot;OK&quot;
239-
}
240-
</code></pre>
241-
<pre><code>返回文本
242-
https://toolapi.us-south.cf.appdomain.cloud/string?p=%E4%BA%BA&amp;t=%E5%AD%97%E7%AC%A6%E4%B8%B2&amp;f=replace&amp;r=text&amp;s={&quot;text&quot;:&quot;我是人&quot;}
243-
s = {&quot;text&quot;:&quot;我是人&quot;}
244-
f = replace
245-
p = 人
246-
t = 字符串
247-
r = text
248-
返回
249-
{&quot;text&quot;:&quot;我是字符串&quot;}
250-
</code></pre>
25+
<h2 id="timestamp"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#1-%E6%97%B6%E9%97%B4%E6%88%B3%E8%BD%AC%E6%8D%A2%EF%BC%88-timestamp%EF%BC%89">1 时间戳转换(/timestamp)</a></h2>
26+
<h2 id="regex"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#2-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E6%98%AF%EF%BC%88-regex%EF%BC%89">2. 正则表达是(/regex)</a></h2>
27+
<h2 id="string"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#3-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%84%E7%90%86-string">3 字符串处理(string)</a></h2>
28+
<h3 id="replace"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#3-1-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%9B%BF%E6%8D%A2-replace">3.1 字符串替换(replace)</a></h3>
29+
<h2 id="condition"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#4-%E6%9D%A1%E4%BB%B6-condition">4 条件(condition)</a></h2>
30+
<h3 id="judge"><a href="https://hexo.aragon.wang/2020/04/16/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%85%B7api/#4-1-%E5%88%A4%E6%96%AD-judge">4.1 判断(judge)</a></h3>
25131

25232
</body>
25333
</html>

0 commit comments

Comments
 (0)