|
5 | 5 | from __future__ import print_function |
6 | 6 | from __future__ import unicode_literals |
7 | 7 |
|
8 | | -import six |
9 | 8 |
|
10 | | -import leancloud |
11 | | -from leancloud import utils |
12 | | -from leancloud.engine import leanengine |
| 9 | +from .cloud import run |
| 10 | +from .cloud import rpc |
| 11 | +from .cloud import request_sms_code |
| 12 | +from .cloud import verify_sms_code |
| 13 | +from .cloud import Captcha |
| 14 | +from .cloud import request_captcha |
| 15 | +from .cloud import verify_captcha |
| 16 | +from .cloud import get_server_time |
13 | 17 |
|
14 | 18 |
|
15 | 19 | __author__ = 'asaka <lan@leancloud.rocks>' |
16 | | - |
17 | | - |
18 | | -def run(_cloud_func_name, **params): |
19 | | - """ |
20 | | - 调用 LeanEngine 上的远程代码 |
21 | | - :param name: 需要调用的远程 Cloud Code 的名称 |
22 | | - :type name: string_types |
23 | | - :param params: 调用参数 |
24 | | - :return: 调用结果 |
25 | | - """ |
26 | | - response = leancloud.client.post('/functions/{0}'.format(_cloud_func_name), params=params) |
27 | | - content = response.json() |
28 | | - return utils.decode(None, content)['result'] |
29 | | - |
30 | | - |
31 | | -def _run_in_local(_cloud_func_name, **params): |
32 | | - if not leanengine.root_engine: |
33 | | - return |
34 | | - result = leanengine.dispatch_cloud_func(leanengine.root_engine.app.cloud_codes, {}, _cloud_func_name, False, params) |
35 | | - return utils.decode(None, result) |
36 | | - |
37 | | - |
38 | | -run.remote = run |
39 | | -run.local = _run_in_local |
40 | | - |
41 | | - |
42 | | -def rpc(_cloud_rpc_name, **params): |
43 | | - """ |
44 | | - 调用 LeanEngine 上的远程代码 |
45 | | - 与cloudfunc.run 类似,但是允许传入 leancloud.Object 作为参数,也允许传入 leancloud.Object 作为结果 |
46 | | - :param name: 需要调用的远程 Cloud Code 的名称 |
47 | | - :type name: basestring |
48 | | - :param params: 调用参数 |
49 | | - :return: 调用结果 |
50 | | - """ |
51 | | - encoded_params = {} |
52 | | - for key, value in params.items(): |
53 | | - if isinstance(params, leancloud.Object): |
54 | | - encoded_params[key] = utils.encode(value._dump()) |
55 | | - else: |
56 | | - encoded_params[key] = utils.encode(value) |
57 | | - response = leancloud.client.post('/call/{}'.format(_cloud_rpc_name), params=encoded_params) |
58 | | - content = response.json() |
59 | | - return utils.decode(None, content['result']) |
60 | | - |
61 | | - |
62 | | -def _rpc_in_local(_cloud_rpc_name, **params): |
63 | | - if not leanengine.root_engine: |
64 | | - return |
65 | | - result = leanengine.dispatch_cloud_func(leanengine.root_engine.app.cloud_codes, {}, _cloud_rpc_name, True, params) |
66 | | - return utils.decode(None, result) |
67 | | - |
68 | | - |
69 | | -rpc.remote = rpc |
70 | | -rpc.local = _rpc_in_local |
71 | | - |
72 | | - |
73 | | -def request_sms_code(phone_number, idd='+86', sms_type='sms', |
74 | | - validate_token=None, template=None, sign=None, |
75 | | - params=None): |
76 | | - """ |
77 | | - 请求发送手机验证码 |
78 | | - :param phone_number: 需要验证的手机号码 |
79 | | - :param idd: 号码的所在地国家代码,默认为中国(+86) |
80 | | - :param sms_type: 验证码发送方式,'voice' 为语音,'sms' 为短信 |
81 | | - :param template: 模版名称 |
82 | | - :param sign: 短信签名名称 |
83 | | - :return: None |
84 | | - """ |
85 | | - if not isinstance(phone_number, six.string_types): |
86 | | - raise TypeError('phone_number must be a string') |
87 | | - |
88 | | - data = { |
89 | | - 'mobilePhoneNumber': phone_number, |
90 | | - 'smsType': sms_type, |
91 | | - 'IDD': idd, |
92 | | - } |
93 | | - |
94 | | - if template is not None: |
95 | | - params['template'] = template |
96 | | - |
97 | | - if sign is not None: |
98 | | - params['sign'] = sign |
99 | | - |
100 | | - if validate_token is not None: |
101 | | - params['validate_token'] = validate_token |
102 | | - |
103 | | - if params is not None: |
104 | | - data.update(params) |
105 | | - |
106 | | - leancloud.client.post('/requestSmsCode', params=data) |
107 | | - |
108 | | - |
109 | | -def verify_sms_code(phone_number, code): |
110 | | - """ |
111 | | - 获取到手机验证码之后,验证验证码是否正确。如果验证失败,抛出异常。 |
112 | | - :param phone_number: 需要验证的手机号码 |
113 | | - :param code: 接受到的验证码 |
114 | | - :return: None |
115 | | - """ |
116 | | - params = { |
117 | | - 'mobilePhoneNumber': phone_number, |
118 | | - } |
119 | | - leancloud.client.post('/verifySmsCode/{0}'.format(code), params=params) |
120 | | - return True |
121 | | - |
122 | | - |
123 | | -class Captcha(object): |
124 | | - """ |
125 | | - 表示图形验证码 |
126 | | - """ |
127 | | - def __init__(self, token, url): |
128 | | - self.token = token |
129 | | - self.url = url |
130 | | - |
131 | | - def verify(self, code): |
132 | | - """ |
133 | | - 验证用户输入与图形验证码是否匹配 |
134 | | - :params code: 用户填写的验证码 |
135 | | - """ |
136 | | - return verify_captcha(code, self.token) |
137 | | - |
138 | | - |
139 | | -def request_captcha(size=None, width=None, height=None, ttl=None): |
140 | | - """ |
141 | | - 请求生成新的图形验证码 |
142 | | - :return: Captcha |
143 | | - """ |
144 | | - params = { |
145 | | - 'size': size, |
146 | | - 'width': width, |
147 | | - 'height': height, |
148 | | - 'ttl': ttl, |
149 | | - } |
150 | | - params = {k: v for k, v in params.items() if v is not None} |
151 | | - |
152 | | - response = leancloud.client.get('/requestCaptcha', params) |
153 | | - content = response.json() |
154 | | - return Captcha(content['captcha_token'], content['captcha_url']) |
155 | | - |
156 | | - |
157 | | -def verify_captcha(code, token): |
158 | | - """ |
159 | | - 验证用户输入与图形验证码是否匹配 |
160 | | - :params code: 用户填写的验证码 |
161 | | - :params token: 图形验证码对应的 token |
162 | | - :return: validate token |
163 | | - """ |
164 | | - params = { |
165 | | - 'captcha_token': token, |
166 | | - 'captcha_code': code, |
167 | | - } |
168 | | - response = leancloud.client.post('/verifyCaptcha', params) |
169 | | - return response.json()['validate_token'] |
170 | | - |
171 | | - |
172 | | -def get_server_time(): |
173 | | - return leancloud.client.get_server_time() |
0 commit comments