user.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. 'use strict';
  2. import AddressComponent from '../../prototype/addressComponent'
  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. import dtime from 'time-formater'
  8. class User extends AddressComponent {
  9. constructor(){
  10. super()
  11. this.login = this.login.bind(this);
  12. this.encryption = this.encryption.bind(this);
  13. this.chanegPassword = this.chanegPassword.bind(this);
  14. this.updateAvatar = this.updateAvatar.bind(this);
  15. }
  16. async login(req, res, next){
  17. const cap = req.cookies.cap;
  18. if (!cap) {
  19. res.send({
  20. status: 0,
  21. type: 'ERROR_CAPTCHA',
  22. message: '验证码失效',
  23. })
  24. return
  25. }
  26. const form = new formidable.IncomingForm();
  27. form.parse(req, async (err, fields, files) => {
  28. const {username, password, captcha_code} = fields;
  29. try{
  30. if (!username) {
  31. throw new Error('用户名参数错误');
  32. }else if(!password){
  33. throw new Error('密码参数错误');
  34. }else if(!captcha_code){
  35. throw new Error('验证码参数错误');
  36. }
  37. }catch(err){
  38. console.log('登陆参数错误', err);
  39. res.send({
  40. status: 0,
  41. type: 'ERROR_QUERY',
  42. message: err.message,
  43. })
  44. return
  45. }
  46. if (cap.toString() !== captcha_code.toString()) {
  47. res.send({
  48. status: 0,
  49. type: 'ERROR_CAPTCHA',
  50. message: '验证码不正确',
  51. })
  52. return
  53. }
  54. const newpassword = this.encryption(password);
  55. try{
  56. const user = await UserModel.findOne({username});
  57. //创建一个新的用户
  58. if (!user) {
  59. const user_id = await this.getId('user_id');
  60. const cityInfo = await this.guessPosition(req);
  61. const registe_time = dtime().format('YYYY-MM-DD HH:mm');
  62. const newUser = {username, password: newpassword, user_id};
  63. const newUserInfo = {username, user_id, id: user_id, city: cityInfo.city, registe_time, };
  64. UserModel.create(newUser);
  65. const createUser = new UserInfoModel(newUserInfo);
  66. const userinfo = await createUser.save();
  67. req.session.user_id = user_id;
  68. res.send(userinfo);
  69. }else if (user.password.toString() !== newpassword.toString()) {
  70. res.send({
  71. status: 0,
  72. type: 'ERROR_PASSWORD',
  73. message: '密码错误',
  74. })
  75. return
  76. }else{
  77. req.session.user_id = user.user_id;
  78. const userinfo = await UserInfoModel.findOne({user_id: user.user_id}, '-_id');
  79. res.send(userinfo)
  80. }
  81. }catch(err){
  82. console.log('登陆失败', err);
  83. res.send({
  84. status: 0,
  85. type: 'SAVE_USER_FAILED',
  86. message: '登陆失败',
  87. })
  88. }
  89. })
  90. }
  91. async getInfo(req, res, next){
  92. const user_id = req.session.user_id;
  93. if (!user_id || !Number(user_id)) {
  94. res.send({
  95. status: 0,
  96. type: 'GET_USER_INFO_FAIELD',
  97. message: '获取用户信息失败',
  98. })
  99. return
  100. }
  101. try{
  102. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  103. res.send(userinfo)
  104. }catch(err){
  105. console.log('获取用户信息失败', err);
  106. res.send({
  107. status: 0,
  108. type: 'GET_USER_INFO_FAIELD',
  109. message: '获取用户信息失败',
  110. })
  111. }
  112. }
  113. async getInfoById(req, res, next){
  114. const user_id = req.params.user_id;
  115. if (!user_id || !Number(user_id)) {
  116. res.send({
  117. status: 0,
  118. type: 'GET_USER_INFO_FAIELD',
  119. message: '获取用户信息失败',
  120. })
  121. return
  122. }
  123. try{
  124. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  125. res.send(userinfo)
  126. }catch(err){
  127. console.log('获取用户信息失败', err);
  128. res.send({
  129. status: 0,
  130. type: 'GET_USER_INFO_FAIELD',
  131. message: '获取用户信息失败',
  132. })
  133. }
  134. }
  135. async signout(req, res, next){
  136. delete req.session.user_id
  137. res.send({
  138. status: 1,
  139. message: '退出成功'
  140. })
  141. }
  142. async chanegPassword(req, res, next){
  143. const cap = req.cookies.cap;
  144. if (!cap) {
  145. res.send({
  146. status: 0,
  147. type: 'ERROR_CAPTCHA',
  148. message: '验证码失效',
  149. })
  150. return
  151. }
  152. const form = new formidable.IncomingForm();
  153. form.parse(req, async (err, fields, files) => {
  154. const {username, oldpassWord, newpassword, confirmpassword, captcha_code} = fields;
  155. try{
  156. if (!username) {
  157. throw new Error('用户名参数错误');
  158. }else if(!oldpassWord){
  159. throw new Error('必须添加旧密码');
  160. }else if(!newpassword){
  161. throw new Error('必须填写新密码');
  162. }else if(!confirmpassword){
  163. throw new Error('必须填写确认密码');
  164. }else if(newpassword !== confirmpassword){
  165. throw new Error('两次密码不一致');
  166. }else if(!captcha_code){
  167. throw new Error('请填写验证码');
  168. }
  169. }catch(err){
  170. console.log('修改密码参数错误', err);
  171. res.send({
  172. status: 0,
  173. type: 'ERROR_QUERY',
  174. message: err.message,
  175. })
  176. return
  177. }
  178. if (cap.toString() !== captcha_code.toString()) {
  179. res.send({
  180. status: 0,
  181. type: 'ERROR_CAPTCHA',
  182. message: '验证码不正确',
  183. })
  184. return
  185. }
  186. const md5password = this.encryption(oldpassWord);
  187. try{
  188. const user = await UserModel.findOne({username});
  189. if (!user) {
  190. res.send({
  191. status: 0,
  192. type: 'USER_NOT_FOUND',
  193. message: '未找到当前用户',
  194. })
  195. }else if(user.password.toString() !== md5password.toString()){
  196. res.send({
  197. status: 0,
  198. type: 'ERROR_PASSWORD',
  199. message: '密码不正确',
  200. })
  201. }else{
  202. user.password = this.encryption(newpassword);
  203. user.save();
  204. res.send({
  205. status: 1,
  206. success: '密码修改成功',
  207. })
  208. }
  209. }catch(err){
  210. console.log('修改密码失败', err);
  211. res.send({
  212. status: 0,
  213. type: 'ERROR_CHANGE_PASSWORD',
  214. message: '修改密码失败',
  215. })
  216. }
  217. })
  218. }
  219. encryption(password){
  220. const newpassword = this.Md5(this.Md5(password).substr(2, 7) + this.Md5(password));
  221. return newpassword
  222. }
  223. Md5(password){
  224. const md5 = crypto.createHash('md5');
  225. return md5.update(password).digest('base64');
  226. }
  227. async getUserList(req, res, next){
  228. const {limit = 20, offset = 0} = req.query;
  229. try{
  230. const users = await UserInfoModel.find({}, '-_id').limit(Number(limit)).skip(Number(offset));
  231. res.send(users);
  232. }catch(err){
  233. console.log('获取用户列表数据失败', err);
  234. res.send({
  235. status: 0,
  236. type: 'GET_DATA_ERROR',
  237. message: '获取用户列表数据失败'
  238. })
  239. }
  240. }
  241. async getUserCount(req, res, next){
  242. try{
  243. const count = await UserInfoModel.count();
  244. res.send({
  245. status: 1,
  246. count,
  247. })
  248. }catch(err){
  249. console.log('获取用户数量失败', err);
  250. res.send({
  251. status: 0,
  252. type: 'ERROR_TO_GET_USER_COUNT',
  253. message: '获取用户数量失败'
  254. })
  255. }
  256. }
  257. async updateAvatar(req, res, next){
  258. const sid = req.session.user_id;
  259. const user_id = req.params.user_id;
  260. if (!user_id || !Number(user_id)) {
  261. res.send({
  262. status: 0,
  263. type: 'ERROR_USERID',
  264. message: 'user_id参数错误',
  265. })
  266. return
  267. }else if(Number(sid) !== Number(user_id)){
  268. res.send({
  269. status: 0,
  270. type: 'NEED_LOGIN_IN',
  271. message: '登录后才可修改头像',
  272. })
  273. return
  274. }
  275. try{
  276. const image_path = await this.qiniu(req);
  277. await UserInfoModel.findOneAndUpdate({user_id}, {$set: {avatar: image_path}});
  278. res.send({
  279. status: 1,
  280. image_path,
  281. })
  282. }catch(err){
  283. console.log('上传图片失败', err);
  284. res.send({
  285. status: 0,
  286. type: 'ERROR_UPLOAD_IMG',
  287. message: '上传图片失败'
  288. })
  289. }
  290. }
  291. }
  292. export default new User()