user.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. import BaseComponent from '../../prototype/baseComponent'
  3. import formidable from 'formidable'
  4. import UserInfoModel from '../../models/v2/userInfo'
  5. import UserModel from '../../models/v2/user'
  6. import crypto from 'crypto'
  7. class User extends BaseComponent {
  8. constructor(){
  9. super()
  10. this.login = this.login.bind(this);
  11. this.encryption = this.encryption.bind(this);
  12. this.chanegPassword = this.chanegPassword.bind(this);
  13. }
  14. async login(req, res, next){
  15. const cap = req.cookies.cap;
  16. if (!cap) {
  17. res.send({
  18. status: 0,
  19. type: 'ERROR_CAPTCHA',
  20. message: '验证码失效',
  21. })
  22. return
  23. }
  24. const form = new formidable.IncomingForm();
  25. form.parse(req, async (err, fields, files) => {
  26. const {username, password, captcha_code} = fields;
  27. try{
  28. if (!username) {
  29. throw new Error('用户名参数错误');
  30. }else if(!password){
  31. throw new Error('密码参数错误');
  32. }else if(!captcha_code){
  33. throw new Error('验证码参数错误');
  34. }
  35. }catch(err){
  36. console.log('登陆参数错误', err);
  37. res.send({
  38. status: 0,
  39. type: 'ERROR_QUERY',
  40. message: err.message,
  41. })
  42. return
  43. }
  44. if (cap.toString() !== captcha_code.toString()) {
  45. res.send({
  46. status: 0,
  47. type: 'ERROR_CAPTCHA',
  48. message: '验证码不正确',
  49. })
  50. return
  51. }
  52. const newpassword = this.encryption(password);
  53. try{
  54. const user = await UserModel.findOne({username});
  55. if (!user) {
  56. const user_id = await this.getId('user_id');
  57. const newUser = {username, password: newpassword, user_id};
  58. const newUserInfo = {username, user_id, id: user_id};
  59. UserModel.create(newUser);
  60. UserInfoModel.create(newUserInfo);
  61. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  62. req.session.user_id = user_id;
  63. res.send(userinfo);
  64. }else if (user.password.toString() !== newpassword.toString()) {
  65. res.send({
  66. status: 0,
  67. type: 'ERROR_PASSWORD',
  68. message: '密码错误',
  69. })
  70. return
  71. }else{
  72. req.session.user_id = user.user_id;
  73. const userinfo = await UserInfoModel.findOne({user_id: user.user_id}, '-_id');
  74. res.send(userinfo)
  75. }
  76. }catch(err){
  77. console.log('登陆失败', err);
  78. res.send({
  79. status: 0,
  80. type: 'SAVE_USER_FAILED',
  81. message: '登陆失败',
  82. })
  83. }
  84. })
  85. }
  86. async getInfo(req, res, next){
  87. const user_id = req.session.user_id;
  88. if (!user_id) {
  89. res.send({
  90. status: 0,
  91. type: 'GET_USER_INFO_FAIELD',
  92. message: '获取用户信息失败',
  93. })
  94. return
  95. }
  96. try{
  97. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  98. res.send(userinfo)
  99. }catch(err){
  100. console.log('获取用户信息失败', err);
  101. res.send({
  102. status: 0,
  103. type: 'GET_USER_INFO_FAIELD',
  104. message: '获取用户信息失败',
  105. })
  106. }
  107. }
  108. async signout(req, res, next){
  109. req.session.user_id = null;
  110. res.send({
  111. status: 1,
  112. message: '退出成功'
  113. })
  114. }
  115. async chanegPassword(req, res, next){
  116. const cap = req.cookies.cap;
  117. if (!cap) {
  118. res.send({
  119. status: 0,
  120. type: 'ERROR_CAPTCHA',
  121. message: '验证码失效',
  122. })
  123. return
  124. }
  125. const form = new formidable.IncomingForm();
  126. form.parse(req, async (err, fields, files) => {
  127. const {username, oldpassWord, newpassword, confirmpassword, captcha_code} = fields;
  128. try{
  129. if (!username) {
  130. throw new Error('用户名参数错误');
  131. }else if(!oldpassWord){
  132. throw new Error('必须添加旧密码');
  133. }else if(!newpassword){
  134. throw new Error('必须填写新密码');
  135. }else if(!confirmpassword){
  136. throw new Error('必须填写确认密码');
  137. }else if(newpassword !== confirmpassword){
  138. throw new Error('两次密码不一致');
  139. }else if(!captcha_code){
  140. throw new Error('请填写验证码');
  141. }
  142. }catch(err){
  143. console.log('修改密码参数错误', err);
  144. res.send({
  145. status: 0,
  146. type: 'ERROR_QUERY',
  147. message: err.message,
  148. })
  149. return
  150. }
  151. if (cap.toString() !== captcha_code.toString()) {
  152. res.send({
  153. status: 0,
  154. type: 'ERROR_CAPTCHA',
  155. message: '验证码不正确',
  156. })
  157. return
  158. }
  159. const md5password = this.encryption(oldpassWord);
  160. try{
  161. const user = await UserModel.findOne({username});
  162. if (!user) {
  163. res.send({
  164. status: 0,
  165. type: 'USER_NOT_FOUND',
  166. message: '未找到当前用户',
  167. })
  168. }else if(user.password.toString() !== md5password.toString()){
  169. res.send({
  170. status: 0,
  171. type: 'ERROR_PASSWORD',
  172. message: '密码不正确',
  173. })
  174. }else{
  175. user.password = this.encryption(newpassword);
  176. user.save();
  177. res.send({
  178. status: 1,
  179. success: '密码修改成功',
  180. })
  181. }
  182. }catch(err){
  183. console.log('修改密码失败', err);
  184. res.send({
  185. status: 0,
  186. type: 'ERROR_CHANGE_PASSWORD',
  187. message: '修改密码失败',
  188. })
  189. }
  190. })
  191. }
  192. encryption(password){
  193. const newpassword = this.Md5(this.Md5(password).substr(2, 7) + this.Md5(password));
  194. return newpassword
  195. }
  196. Md5(password){
  197. const md5 = crypto.createHash('md5');
  198. return md5.update(password).digest('base64');
  199. }
  200. }
  201. export default new User()