Skip to content

Commit ba881df

Browse files
author
gusibi
authored
Merge pull request #41 from gusibi/wxapp_qcloud
Wxapp qcloud
2 parents 18d0146 + 05a0a4f commit ba881df

22 files changed

Lines changed: 891 additions & 430 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ venv
1717
weixin/.ropeproject/
1818

1919
# test
20-
test_example.py
20+
# test_example.py
2121
test_settings.py

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ python:
44
- '3.5'
55
install:
66
- pip install -r requirements.txt
7-
script: python test_example.py
7+
script: python tests/test_example.py
88
deploy:
99
provider: pypi
1010
user: goodspeed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
python-weixin
22
-----
33
![Build Status](https://travis-ci.org/gusibi/python-weixin.svg?branch=master)
4-
![](https://img.shields.io/badge/version-0.4.3--dev-FF00CC.svg)
4+
![](https://img.shields.io/badge/version-0.5.0--dev-FF00CC.svg)
55
![](https://img.shields.io/github/forks/gusibi/python-weixin.svg)
66
![](https://img.shields.io/github/stars/gusibi/python-weixin.svg)
77
![](https://img.shields.io/github/issues/gusibi/python-weixin.svg)
@@ -40,6 +40,7 @@ Requires
4040
* [微信公众平台](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%B9%B3%E5%8F%B0)
4141
* [微信授权](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E6%8E%88%E6%9D%83)
4242
* [微信支付](https://github.com/gusibi/python-weixin/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98)
43+
* [微信小程序云开发](https://github.com/gusibi/python-weixin/wiki/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BA%91%E5%BC%80%E5%8F%91)
4344

4445
### 微信小程序使用示例:
4546

File renamed without changes.

example/example_db

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_id,age
2+
123,45
3+
456,21
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from weixin.client import WeixinAPI
66
from weixin.oauth2 import OAuth2AuthExchangeError
77

8-
if len(sys.argv) > 1 and sys.argv[1] == 'local':
8+
if len(sys.argv) > 1 and sys.argv[1] == "local":
99
try:
1010
from test_settings import *
1111

@@ -22,31 +22,32 @@
2222
# Fix Python 2.x.
2323
try:
2424
import __builtin__
25-
input = getattr(__builtin__, 'raw_input')
25+
26+
input = getattr(__builtin__, "raw_input")
2627
except (ImportError, AttributeError):
2728
pass
2829

2930

3031
appid = input("App ID: ").strip()
3132
app_secret = input("App Secret: ").strip()
3233
redirect_uri = input("Redirect URI: ").strip()
33-
raw_scope = input("Requested scope (separated by spaces, blank for just basic read): ").strip()
34-
scope = raw_scope.split(' ')
34+
raw_scope = input(
35+
"Requested scope (separated by spaces, blank for just basic read): "
36+
).strip()
37+
scope = raw_scope.split(" ")
3538
# For basic, API seems to need to be set explicitly
3639
if not scope or scope == [""]:
3740
scope = ["snsapi_login"]
3841

3942

40-
api = WeixinAPI(appid=appid,
41-
app_secret=app_secret,
42-
redirect_uri=redirect_uri)
43+
api = WeixinAPI(appid=appid, app_secret=app_secret, redirect_uri=redirect_uri)
4344
redirect_uri = api.get_authorize_login_url(scope=scope)
4445

45-
print ("Visit this page and authorize access in your browser: "+ redirect_uri)
46+
print("Visit this page and authorize access in your browser: " + redirect_uri)
4647

47-
code = (str(input("Paste in code in query string after redirect: ").strip()))
48+
code = str(input("Paste in code in query string after redirect: ").strip())
4849

4950
access_token = api.exchange_code_for_access_token(code)
5051

51-
print ("access token: " )
52-
print (access_token)
52+
print("access token: ")
53+
print(access_token)

example/wxapp_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#! -*- coding: utf-8 -*-
2+
from os import environ
3+
4+
from weixin import WXAPPAPI
5+
6+
appid = environ.get("WXAPP_APPID", "appid")
7+
secret = environ.get("WXAPP_SECRET", "secret")
8+
9+
api = WXAPPAPI(appid=appid, app_secret=secret, grant_type="client_credential")
10+
token = api.client_credential_for_access_token().get("access_token")
11+
print(token)

example/wxapp_cloud_api.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#! -*- coding: utf-8 -*-
2+
from os import environ, path
3+
4+
from weixin import WxAppCloudAPI
5+
6+
appid = environ.get("WXAPP_APPID", "appid")
7+
secret = environ.get("WXAPP_SECRET", "secret")
8+
env = "test-id"
9+
10+
example_db = path.abspath(path.join(path.dirname(__file__), "./example_db"))
11+
12+
13+
app_cloud = WxAppCloudAPI(
14+
appid=appid, app_secret=secret, grant_type="client_credential"
15+
)
16+
token = app_cloud.client_credential_for_access_token().get("access_token")
17+
print(token)
18+
19+
cloud_api = WxAppCloudAPI(access_token=token)
20+
21+
# 获取库的集合信息
22+
db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10})
23+
print(db_info)
24+
# 新建集合
25+
resp = cloud_api.db_collection_add(json_body={"env": env, "collection_name": "example"})
26+
print("new collection: ", resp)
27+
28+
# 获取库的集合信息
29+
db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10})
30+
print("db info: ", db_info)
31+
32+
# 导入数据
33+
resp = cloud_api.db_migrate_import(
34+
json_body={
35+
"env": env,
36+
"collection_name": "example",
37+
"file_path": example_db,
38+
"file_type": 2,
39+
"stop_on_error": False,
40+
"conflict_mode": 2,
41+
}
42+
)
43+
print("db migrate import: ", resp)
44+
45+
# 数据库统计记录数量
46+
resp = cloud_api.db_count(
47+
json_body={"env": env, "query": 'db.collection("example").count()'}
48+
)
49+
print("count", resp)
50+
51+
# 插入数据
52+
resp = cloud_api.db_add(
53+
json_body={"env": env, "query": 'db.collection("example").add({data: [{age: 12}]})'}
54+
)
55+
print("add: ", resp)
56+
57+
id_list = resp.get("id_list")
58+
item_id = id_list[0]
59+
60+
# 数据库统计记录数量
61+
resp = cloud_api.db_count(
62+
json_body={"env": env, "query": 'db.collection("example").count()'}
63+
)
64+
print("count", resp)
65+
# 查询记录
66+
67+
resp = cloud_api.db_query(
68+
json_body={
69+
"env": env,
70+
"query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()'
71+
% item_id,
72+
}
73+
)
74+
print("query: ", resp)
75+
76+
# 更新数据
77+
78+
resp = cloud_api.db_update(
79+
json_body={
80+
"env": env,
81+
"query": 'db.collection("example").where({_id: "%s"}).update({data: {age: _.inc(1)}})'
82+
% item_id,
83+
}
84+
)
85+
print("update: ", resp)
86+
87+
# 查询记录
88+
resp = cloud_api.db_query(
89+
json_body={
90+
"env": env,
91+
"query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()'
92+
% item_id,
93+
}
94+
)
95+
print("query: ", resp)
96+
# 删除数据
97+
resp = cloud_api.db_delete(
98+
json_body={
99+
"env": env,
100+
"query": 'db.collection("example").where({_id: "%s"}).remove()' % item_id,
101+
}
102+
)
103+
print("remove: ", resp)
104+
105+
# 查询记录
106+
resp = cloud_api.db_query(
107+
json_body={
108+
"env": env,
109+
"query": 'db.collection("example").where({_id: "%s"}).limit(10).skip(0).get()'
110+
% item_id,
111+
}
112+
)
113+
print("query: ", resp)
114+
# 删除集合
115+
resp = cloud_api.db_collection_delete(
116+
json_body={"env": env, "collection_name": "example"}
117+
)
118+
119+
# 获取库的集合信息
120+
121+
db_info = cloud_api.db_collection_info(json_body={"env": env, "limit": 10})
122+
print("db info: ", db_info)

example/wxapp_cloud_file.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! -*- coding: utf-8 -*-
2+
from os import environ, path
3+
from pprint import pprint
4+
5+
from weixin import WxAppCloudAPI
6+
7+
appid = environ.get("WXAPP_APPID", "appid")
8+
secret = environ.get("WXAPP_SECRET", "secret")
9+
env = environ.get("WXAPP_ENV", "test-id")
10+
11+
app_cloud = WxAppCloudAPI(
12+
appid=appid, app_secret=secret, grant_type="client_credential"
13+
)
14+
token = app_cloud.client_credential_for_access_token().get("access_token")
15+
16+
cloud_api = WxAppCloudAPI(access_token=token)
17+
18+
path = "test/author.jpg"
19+
filepath = "/Users/gs/Desktop/author.jpg"
20+
path = "test/id2uid1.lua"
21+
filepath = "/Users/gs/Desktop/id2uid.lua"
22+
path = "test/aws-serverless-games.pdf"
23+
filepath = "/Users/gs/Desktop/aws-serverless-games.pdf"
24+
resp = cloud_api.upload_file(json_body={"env": env, "path": path, "filepath": filepath})
25+
print(resp)

test_example.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)