user.js 8.4 KB

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