aicode/kdb/module_logic.md
2025-12-11 20:24:41 +08:00

72 lines
2.9 KiB
Markdown
Raw 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.

## 要求
* 模块中的主要逻辑用py源码实现放在mymodule目录下
* 客户端业务功能放在wwwroot目录下以.ui和.dspy后缀结束用.ui文件遵守jinja2规范, 而.dspy文件是一个受控的脚本
* .dspy中不允许import模块
* 假设模块名为”mymodule“
## mymodule目录
“mymodule”为模块名称 模块目录下存放以py结束的代码文件其中init.py文件为必须
在此目录下的代码中可以通过ahserver.serverenv模块的ServerEnv来引用其他模块的放进来的变量
### init.py的主要内容
```
from ahserver.serverenv import ServerEnv
from appPublic.worker import awaitify
from .x import a, b, c # 从同目录下的源码x.py中import ab, c是协程
from .y import x, y # 从同目录下源码y.py中import x, y是普通函数
def load_mymodule(): # mymodule需替换为实际的模块名字
env = ServerEnv()
env.a = a # 脚本中用"a" 调用"a"
env.b = b # 脚本中用"b"调用"b"
env.cc = c # 脚本中用"cc"调用"c"
env.y = awaitify(y) # 将函数包装为协程
env.x = awaitify(x) # 将函数包装为协程
```
注释这里的load_mymodule中的mymodule是模块名字而不是“mymodule”本身
模块的其他代码都需要放在这个目录中而模块中所有需要在uidspy脚本中用到的变量均需要通过init.py函数的load_mymodule()函数传递
## ui, dspy可以直接使用的变量
### request
aiohttp.Request实例每个客户端请求有一个独立的Request实例
request._run_ns可以获得所有在.ui和.dspy源码中可以使用的变量通过ahserver.ServerEnv 传递
### params_kw
DictObjectdict子类, 支持a.b方式获取属性实例接收到客户端传来的数据如果有文件>文件都保存在服务器指定的位置params_kw中属性名保存的是其相对路径可用FileStorage().realPath(params_kw.myfile)来获得文件在服务器中的实际路径。
### get_user()
来自rbac模块协程函数获得当前登录用户如果用户没有登录返回None
### get_userorgid()
来自rbac模块协程函数获得当前登录用户的机构id如果用户没有登录返回None
### 各个模块通过load_xxxx()放到ServerEnv()中的变量
XXXX是模块名称
## pyproject.toml和README.md
编写pyproject.toml文件和README.md文件
## 模块中使用的编码
模块中如果用到编码编码需保存在appbase模块的appcodes表和appcodes_kv两个表中
appcodes(编码表)有如下字段:
[
"id" # str 32 主键,可设置为数据表字段名称
"name" # 编码名称
"hierarchy_flg", # str 1, '0':单级编码“1”:多级编码
]
appcodes_kv编码键值表字段如下
[
"id", # str, 32,主键,唯一值
"parentid" # str, 32, 一级编码为appcodes表的id, 否则为上级编码键值记录的id
“k" # str 32, 编码值
“v” “ str 255,编码显示文本
]