| 7 | import dtime from 'time-formater' |
| 8 | |
| 9 | class Admin extends AddressComponent { |
| 10 | constructor(){ |
| 11 | super() |
| 12 | this.login = this.login.bind(this) |
| 13 | this.register = this.register.bind(this) |
| 14 | this.encryption = this.encryption.bind(this) |
| 15 | this.updateAvatar = this.updateAvatar.bind(this) |
| 16 | } |
| 17 | async login(req, res, next){ |
| 18 | const form = new formidable.IncomingForm(); |
| 19 | form.parse(req, async (err, fields, files) => { |
| 20 | if (err) { |
| 21 | res.send({ |
| 22 | status: 0, |
| 23 | type: 'FORM_DATA_ERROR', |
| 24 | message: '表单信息错误' |
| 25 | }) |
| 26 | return |
| 27 | } |
| 28 | const {user_name, password, status = 1} = fields; |
| 29 | try{ |
| 30 | if (!user_name) { |
| 31 | throw new Error('用户名参数错误') |
| 32 | }else if(!password){ |
| 33 | throw new Error('密码参数错误') |
| 34 | } |
| 35 | }catch(err){ |
| 36 | console.log(err.message, err); |
| 37 | res.send({ |
| 38 | status: 0, |
| 39 | type: 'GET_ERROR_PARAM', |
| 40 | message: err.message, |
| 41 | }) |
| 42 | return |
| 43 | } |
| 44 | const newpassword = this.encryption(password); |
| 45 | try{ |
| 46 | const admin = await AdminModel.findOne({user_name}) |
| 47 | if (!admin) { |
| 48 | const adminTip = status == 1 ? '管理员' : '超级管理员' |
| 49 | const admin_id = await this.getId('admin_id'); |
| 50 | const cityInfo = await this.guessPosition(req); |
| 51 | const newAdmin = { |
| 52 | user_name, |
| 53 | password: newpassword, |
| 54 | id: admin_id, |
| 55 | create_time: dtime().format('YYYY-MM-DD HH:mm'), |
| 56 | admin: adminTip, |
| 57 | status, |
| 58 | city: cityInfo.city |
| 59 | } |
| 60 | await AdminModel.create(newAdmin) |
| 61 | req.session.admin_id = admin_id; |
| 62 | res.send({ |
| 63 | status: 1, |
| 64 | success: '注册管理员成功', |
| 65 | }) |
| 66 | }else if(newpassword.toString() != admin.password.toString()){ |
nothing calls this directly
no outgoing calls
no test coverage detected