Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //rpc file
- message LoginRequest {
- string userName = 1;
- string password = 2;
- }
- message LoginResponse {
- string userName = 1;
- string password = 2;
- }
- rpc login(LoginRequest) returns (LoginResponse) {
- option (error) = "Error";
- }
- //rpc request file
- import {
- LoginReq,
- LoginResp,
- } from './LoginServiceObjs';
- import { generateUrl, errorHandling } from './helper';
- import promisify from "../utils/util"
- const wxPromisify = promisify(wx)
- const wxRequest = wxPromisify('request')
- export function SetBaseUrl(url: string) {
- baseUrl = url;
- }
- var authToken = "token";
- export function SetAuthToken(token: string) {
- authToken = token;
- }
- export function Login(params:LoginReq): Promise<LoginResp|never> {
- let url: string = generateUrl(baseUrl, "LoginService", "Login");
- return wxRequest({ url: url,
- data: params, method:'POST', header:{'Authorization': 'token ' + authToken}}).then(res => {
- if (typeof res.data === 'object') {
- try {
- return Promise.resolve(res.data as LoginResp)
- } catch (e) {
- return Promise.reject(res.data);
- }
- }
- return Promise.reject(res.data);
- }).catch(err => {
- // handle error response
- return errorHandling(err)
- });
- }
- }
- //promisify file
- /**
- * fn {function} API service
- */
- const promisify = (wx) => {
- return (method) => {
- return (option) => {
- return new Promise ((resolve,reject) => {
- wx[method]({
- ...option,
- success:(res) => { resolve(res) },
- fail: (err) => {reject(err)}
- })
- })
- }
- }
- }
- export default promisify
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement