admin.js 6.4 KB

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