gadget/docs/cn/environment.md
2023-12-05 11:33:44 +08:00

78 lines
1.6 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 环境
gadget服务器给处理器提供了一个执行环境处理器可以引用环境提供的模块变量函数等资源来完成处理器等处理任务
## 请求相关环境
### request
服务器接收到的服务请求对象,具体内容请参考[AioHttp](https://github.com/aiohttp/aiohttp)
### params_kw
服务器接收到浏览器发送过来的参数支持各种https方法过来的数据
### get_user()
获得用户id, get_user是一个协程
#### 例子
```
uid = await get_user()
```
#### 返回值
null 或者当前登录用户id
### remember_user()
#### 例子
```
db = DBPools()
async with db.sqlorContext(dbname) as sor:
pwd = password(params_kw.password)
ns = {
"password":pwd,
"name":params_kw.get('name')
}
sql = "select * from users where name=${name}$ and password=${pwd}"
x = await sor.sqlExe(sql, ns)
if len(x) < 1:
return Error(msg="user or password error")
userid = x[0].['id']
await remember_user(userid)
```
从前台接收用户名和密码密码加密后从数据库检索符合名字和密码与参数重相同的用户记录如果没有找到提示前提错误否则用户登录成功服务器记住登录用户的ID
#### 参数说明
##### userid
用户id
##### 返回值
### entire_url(url)
获得参数URL的绝对url
#### 例子
dspy
```
url = entire_url('insert.dspy')
```
template
```
{
"widgettype":"urlwidget",
"options":{
"url":"{{entire_url('insert.dspy')}}"
}
}
```
#### 参数说明
##### url
相对路径或绝对路径
可以上一下形式
* http://.....
* https://......
* /abc/lerltg.tmpl
* abc/gggg.html
##### 返回值
带http的路径