admin.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. import AdminModel from '../../models/admin/admin'
  3. import BaseComponent from '../../prototype/baseComponent'
  4. import crypto from 'crypto'
  5. import formidable from 'formidable'
  6. import dtime from 'time-formater'
  7. class Admin extends BaseComponent {
  8. constructor(){
  9. super()
  10. this.login = this.login.bind(this)
  11. this.register = this.register.bind(this)
  12. this.encryption = this.encryption.bind(this)
  13. this.updateAvatar = this.updateAvatar.bind(this)
  14. }
  15. async login(req, res, next){
  16. const form = new formidable.IncomingForm();
  17. form.parse(req, async (err, fields, files) => {
  18. if (err) {
  19. res.send({
  20. status: 0,
  21. type: 'FORM_DATA_ERROR',
  22. message: '表单信息错误'
  23. })
  24. return
  25. }
  26. const {user_name, password, status = 1} = fields;
  27. try{
  28. if (!user_name) {
  29. throw new Error('用户名错误')
  30. }else if(!password){
  31. throw new Error('密码错误')
  32. }
  33. }catch(err){
  34. console.log(err.message, err);
  35. res.send({
  36. status: 0,
  37. type: 'GET_ERROR_PARAM',
  38. message: err.message,
  39. })
  40. return
  41. }
  42. const newpassword = this.encryption(password);
  43. try{
  44. const admin = await AdminModel.findOne({user_name})
  45. if (!admin) {
  46. const adminTip = status == 1 ? '普通管理员' : '超级管理员'
  47. const admin_id = await this.getId('admin_id');
  48. const newAdmin = {
  49. user_name,
  50. password: newpassword,
  51. id: admin_id,
  52. create_time: dtime().format('YYYY-MM-DD'),
  53. admin: adminTip,
  54. status,
  55. }
  56. await AdminModel.create(newAdmin)
  57. req.session.admin_id = admin_id;
  58. res.send({
  59. status: 1,
  60. success: '注册管理员成功',
  61. })
  62. }else if(newpassword.toString() != admin.password.toString()){
  63. console.log('密码错误');
  64. res.send({
  65. status: 0,
  66. type: 'ERROR_PASSWORD',
  67. message: '密码输入错误',
  68. })
  69. }else{
  70. req.session.admin_id = admin.id;
  71. res.send({
  72. status: 1,
  73. success: '登录成功'
  74. })
  75. }
  76. }catch(err){
  77. console.log('登录管理员失败', err);
  78. res.send({
  79. status: 0,
  80. type: 'LOGIN_ADMIN_FAILED',
  81. message: '登录管理员失败',
  82. })
  83. }
  84. })
  85. }
  86. async register(req, res, next){
  87. const form = new formidable.IncomingForm();
  88. form.parse(req, async (err, fields, files) => {
  89. if (err) {
  90. res.send({
  91. status: 0,
  92. type: 'FORM_DATA_ERROR',
  93. message: '表单信息错误'
  94. })
  95. return
  96. }
  97. const {user_name, password, status = 1} = fields;
  98. try{
  99. if (!user_name) {
  100. throw new Error('用户名错误')
  101. }else if(!password){
  102. throw new Error('密码错误')
  103. }
  104. }catch(err){
  105. console.log(err.message, err);
  106. res.send({
  107. status: 0,
  108. type: 'GET_ERROR_PARAM',
  109. message: err.message,
  110. })
  111. return
  112. }
  113. try{
  114. const admin = await AdminModel.findOne({user_name})
  115. if (admin) {
  116. console.log('该用户已经存在');
  117. res.send({
  118. status: 0,
  119. type: 'USER_HAS_EXIST',
  120. message: '该用户已经存在',
  121. })
  122. }else{
  123. const adminTip = status == 1 ? '普通管理员' : '超级管理员'
  124. const admin_id = await this.getId('admin_id');
  125. const newpassword = this.encryption(password);
  126. const newAdmin = {
  127. user_name,
  128. password: newpassword,
  129. id: admin_id,
  130. create_time: dtime().format('YYYY-MM-DD'),
  131. admin: adminTip,
  132. status,
  133. }
  134. await AdminModel.create(newAdmin)
  135. req.session.admin_id = admin_id;
  136. res.send({
  137. status: 1,
  138. message: '注册管理员成功',
  139. })
  140. }
  141. }catch(err){
  142. console.log('注册管理员失败', err);
  143. res.send({
  144. status: 0,
  145. type: 'REGISTER_ADMIN_FAILED',
  146. message: '注册管理员失败',
  147. })
  148. }
  149. })
  150. }
  151. encryption(password){
  152. const newpassword = this.Md5(this.Md5(password).substr(2, 7) + this.Md5(password));
  153. return newpassword
  154. }
  155. Md5(password){
  156. const md5 = crypto.createHash('md5');
  157. return md5.update(password).digest('base64');
  158. }
  159. async singout(req, res, next){
  160. try{
  161. delete req.session.admin_id;
  162. res.send({
  163. status: 1,
  164. success: '退出成功'
  165. })
  166. }catch(err){
  167. console.log('退出失败', err)
  168. res.send({
  169. status: 0,
  170. message: '退出失败'
  171. })
  172. }
  173. }
  174. async getAllAdmin(req, res, next){
  175. const {limit = 20, offset = 0} = req.query;
  176. try{
  177. const allAdmin = await AdminModel.find({}, '-_id -password').skip(Number(offset)).limit(Number(limit))
  178. res.send({
  179. status: 1,
  180. data: allAdmin,
  181. })
  182. }catch(err){
  183. console.log('获取超级管理列表失败', err);
  184. res.send({
  185. status: 0,
  186. type: 'ERROR_GET_ADMIN_LIST',
  187. message: '获取超级管理列表失败'
  188. })
  189. }
  190. }
  191. async getAdminCount(req, res, next){
  192. try{
  193. const count = await AdminModel.count()
  194. res.send({
  195. status: 1,
  196. count,
  197. })
  198. }catch(err){
  199. console.log('获取管理员数量失败', err);
  200. res.send({
  201. status: 0,
  202. type: 'ERROR_GET_ADMIN_COUNT',
  203. message: '获取管理员数量失败'
  204. })
  205. }
  206. }
  207. async getAdminInfo(req, res, next){
  208. const admin_id = req.session.admin_id;
  209. if (!admin_id || !Number(admin_id)) {
  210. console.log('session失效');
  211. res.send({
  212. status: 0,
  213. type: 'ERROR_SESSION',
  214. message: '获取管理员信息失败'
  215. })
  216. return
  217. }
  218. try{
  219. const info = await AdminModel.findOne({id: admin_id}, '-_id -__v -password');
  220. if (!info) {
  221. throw new Error('未找到当前管理员')
  222. }else{
  223. res.send({
  224. status: 1,
  225. data: info
  226. })
  227. }
  228. }catch(err){
  229. console.log('获取管理员信息失败');
  230. res.send({
  231. status: 0,
  232. type: 'GET_ADMIN_INFO_FAILED',
  233. message: '获取管理员信息失败'
  234. })
  235. }
  236. }
  237. async updateAvatar(req, res, next){
  238. const admin_id = req.params.admin_id;
  239. if (!admin_id || !Number(admin_id)) {
  240. console.log('admin_id参数错误', admin_id)
  241. res.send({
  242. status: 0,
  243. type: 'ERROR_ADMINID',
  244. message: 'admin_id参数错误',
  245. })
  246. return
  247. }
  248. try{
  249. const image_path = await this.qiniu(req);
  250. await AdminModel.findOneAndUpdate({id: admin_id}, {$set: {avatar: image_path}});
  251. res.send({
  252. status: 1,
  253. image_path,
  254. })
  255. }catch(err){
  256. console.log('上传图片失败', err);
  257. res.send({
  258. status: 0,
  259. type: 'ERROR_UPLOAD_IMG',
  260. message: '上传图片失败'
  261. })
  262. }
  263. }
  264. }
  265. export default new Admin()