Conditions | 4 |
Paths | 1 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict'; |
||
37 | login: co.wrap(function *(id, pwd) { |
||
38 | let ires, iires; |
||
39 | |||
40 | // 通过GET首页,来获取cookie |
||
41 | try { |
||
42 | ires = yield superagent |
||
43 | .get('http://csujwc.its.csu.edu.cn/jsxsd') |
||
44 | .endThunk(); |
||
45 | } catch (err) { |
||
46 | err.eqMessage = { |
||
47 | inner: `Failed to get the Cookie for login.\n${err.stack}`, |
||
48 | public: '获取Cookie失败' |
||
49 | }; |
||
50 | throw err; |
||
51 | } |
||
52 | |||
53 | const headers = { |
||
54 | Host: 'csujwc.its.csu.edu.cn', |
||
55 | Connection: 'keep-alive', |
||
56 | 'Cache-Control': 'max-age=0', |
||
57 | Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
||
58 | Origin: 'http://csujwc.its.csu.edu.cn', |
||
59 | 'Upgrade-Insecure-Requests': 1, |
||
60 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', |
||
61 | 'Content-Type': 'application/x-www-form-urlencoded', |
||
62 | Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/', |
||
63 | 'Accept-Encoding': 'gzip, deflate', |
||
64 | 'Accept-Language': 'zh-CN,zh;q=0.8', |
||
65 | Cookie: ires.headers['set-cookie'] |
||
66 | }; |
||
67 | |||
68 | try { |
||
69 | iires = yield superagent |
||
70 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk') |
||
71 | .set(headers) |
||
72 | .type('form') |
||
73 | .send({ encoded: `${encodeInp(id)}%%%${encodeInp(pwd)}` }) |
||
74 | .endThunk(); |
||
75 | } catch (err) { |
||
76 | err.eqMessage = { |
||
77 | inner: `Failed to login\n${err.stack}`, |
||
78 | public: '登录失败' |
||
79 | }; |
||
80 | throw err; |
||
81 | } |
||
82 | |||
83 | if (/POST/i.test(iires.req.method)) { |
||
84 | let err = new Error('The request method to the logged in page is POST instead of GET'); |
||
85 | err.eqMessage = { |
||
86 | inner: `Failed to login (possibily id or password provided were wrong)\n${err.stack}`, |
||
87 | public: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' |
||
88 | }; |
||
89 | throw err; |
||
90 | } |
||
91 | |||
92 | return headers; |
||
93 | }), |
||
94 | // 登出模块 |
||
110 |