用python写一个有趣的小程序,python编写的实用小程序

  用python写一个有趣的小程序,python编写的实用小程序

  要求:微信小程序云必须开发数据库存储,并导出数据库中的答案以生成单词

  难点1 :

  使用大蟒的docxtpl包导出模板

  难点2 :

  大蟒获取云开发的数据库数据

  难点3 :

  数据库包含文件ID,只能在微信开发工具中使用,不能从外部访问,而是通过接口转换

  实现方法

  1 .在1.python环境中

  点安装文档第三方物流

  fromdocxtplimportdocxtemplatedoc=docx template( my _ word _ template。docx )context={ company _ name 3365292;

  fromdocxtplimportinlineimagefromdocx。sharedimportmmyimage=inline image(doc,image _ descriptor= test _ files/pytttor

  的所有操作前提都是在获取访问令牌下进行的,首先在下面的代码中获取访问令牌并执行以下操作:

  ef access _ token(: access _ token appid= * * * * * * /app secret= * * * * * * gant _ type=client _ credential appid= appid secret= appsecretresponse=requests。获取(微信u u URL)结果=其中,访问令牌具有2小时的时效性,访问令牌不需要始终检索,可以在数据库中存储访问令牌

  类accesstoken(db。型号):id=db。列)db。integer,primary _ key=True)access _ token=db。列索引=True(expire=db。列)db。整数)save _ time=db。列)db。float,efault=time。time(def get _ access _ token)):access _ token=访问令牌。查询。order _ by-access令牌降序expiretime.time(-3003360#直接从数据库中提取令牌=访问令牌。access _ token else:app _ id= 12323213 app _ secret= 23123123 URL= 3https://secret={ 1 } .格式(app_id,app _ secret(响应=请求。获取)网址)结果=response.json)令牌=ren

  me())db。会话。添加(a _ t)db。会话。提交()返回令牌创建集合

  def databaseCollectionAdd(access _ token): 创建数据库 URL=https://api.weixin.qq.com/tcb/databasecollectionadd?access _ token= access _ token data={ env : * * * * ,//用户的数据库环境ID "收藏名称":"* * * * * * * * */"数据库集合的名称} response=requests.post(url,data=JSON。dumps(data))结果=响应。JSON()print(result)//将返回值打印查询记录

  定义数据库查询(访问令牌,集合名称): 检索数据库收藏名称集合的名称。极限()括号内的数值限定返回的记录数 URL=https://api.weixin.qq.com/tcb/databasequery?access _ token= access _ token data={ env : * * * * * * * * * *,//用户的数据库环境ID“查询”:“数据库。集合(“集合名称”).限制(100)。get() } response=请求。post(URL,data=JSON。dumps(data))结果=响应。JSON()print(result)//将返回值打印插入记录

  定义数据库添加(访问令牌,集合名称): 新建记录并对内容进行定义收藏名称集合的名称 URL=https://api.weixin.qq.com/tcb/databaseadd?access _ token= access _ token data={ env : , query : db。集合(“集合名称”).add({ datas }) } datas=[{ 字段名称:内容 这里主要是对创建记录的直接定义 }] response=requests.post(url,data=JSON。dumps(data))结果=响应。JSON()print(result)//将返回值打印删除记录

  定义数据库删除(访问令牌,集合名称): 集合名称集合的名称 URL=https://api.weixin.qq.com/tcb/databasedelete?access _ token= access _ token data={ env : * * * * * * * * * *,//用户的数据库环境ID"query":"db。集合(集合名称 ).where({done:false}).remove() } response=请求。post(URL,data=JSON。dumps(data))结果=响应。JSON()print(result)//将返回值打印更新记录

  定义数据库更新(访问令牌,集合名称): 。哪里()该集合所在记录的检索 URL=https://api.weixin.qq.com/tcb/databaseupdate?access _ token= access _ token data={ env : , query : db。集合(“集合名称”).其中({年龄:14})。更新({数据:{年龄:_。inc(1)}}) } 这个例子是将集合里年龄=14岁所有记录自增1 response=requests.post(url,data=JSON。dumps(data))结果=响应。JSON()print(result)//将返回值打印也可间接操作数据库,直接调用云函数

  def xxxx(自身)://在函数括号内需要添加参数ACCESS_TOKEN=xxx //获取的access_token ENV=xxx //用户的数据库环境ID FUNCTION_NAME=xxx //所要执行的云函数名网址=https://api.weixin.qq.com/tcb/invokecloudfunction?访问令牌=访问令牌环境=环境名称=功能名称数据={ //所要传输的参数}响应=请求。post(URL=URL,data=JSON。dumps(data))结果=响应。JSON()3 .关于数据库中存储的是文件的文件ID,我们需要得到对应的外部可以访问的连接:

  def get _ download _ URL(access _ token,fileid):URL=https://api.weixin.qq.com/tcb/batchdownloadfile?access _ token= access _ token data={ env : ENVID , file_list: [{ fileid: fileid, max _ age :7200 }]}响应=请求。post(URL,data=JSON。dumps(data))结果=响应。JSON()print( URL ,结果[ file _ list ][0][ download _ URL ])返回结果[文件列表][0][下载URL ]在获取外部可以访问的连接之后,我们有两种方式转化为docxtpl中所需要的,方式一:直接将外部访问的链接存储到本地:

  urllib。请求。URL retrieve(download _ URL, 00000001.jpg )insert _ image=inline image(doc, 00000001。jpg ,width=Mm(80))答案[index][ imageurl ]=insert _ image方法二:直接获取文件流

  响应=请求。get(URL)image=image。open(BytesIO(响应。content))imgByteArr=BytesIO()#初始化一个空字节流image.save(imgByteArr,format(PNG)) #把我们得图片以巴布亚新几内亚保存到空字节流insert_image=InlineImage(doc,imgByteArr,width=Mm(80))答案[索引][ imageurl ]=插入图像好了以上难点基本就这些,总结一下这几天的知识积累,最后再给大家看一下单词中的模板:

  小程序中的云数据库模糊查询:

  //数据库正则对象数据库集合( todos ).其中({描述:db .RegExp({ regexp: miniprogram ,选项: I ,})})小程序的线查询文档:https://开发者。微信。QQ。com/mini program/dev/wx cloud/reference-http-API/storage/batchdownloadfile。超文本标记语言

  参考文章:https://博客。csdn。net/QQ _ 44001007/文章/详情/103944492

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: