user.js 8.4 KB

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