user.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.cookie('UID', user_id, { maxAge: 31536000000});
  70. res.send(userinfo);
  71. }else if (user.password.toString() !== newpassword.toString()) {
  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. res.cookie('UID', user.user_id, { maxAge: 31536000000});
  81. const userinfo = await UserInfoModel.findOne({user_id: user.user_id}, '-_id');
  82. res.send(userinfo)
  83. }
  84. }catch(err){
  85. console.log('登陆失败', err);
  86. res.send({
  87. status: 0,
  88. type: 'SAVE_USER_FAILED',
  89. message: '登陆失败',
  90. })
  91. }
  92. })
  93. }
  94. async getInfo(req, res, next){
  95. let user_id = req.session.user_id || req.cookies.UID;
  96. if (!user_id || !Number(user_id)) {
  97. console.log('sessions和cookie失效', req.session.user_id, req.cookies.UID)
  98. res.send({
  99. status: 0,
  100. type: 'GET_USER_INFO_FAIELD',
  101. message: '获取用户信息失败',
  102. })
  103. return
  104. }
  105. try{
  106. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  107. res.send(userinfo)
  108. }catch(err){
  109. console.log('获取用户信息失败', err);
  110. res.send({
  111. status: 0,
  112. type: 'GET_USER_INFO_FAIELD',
  113. message: '获取用户信息失败',
  114. })
  115. }
  116. }
  117. async getInfoById(req, res, next){
  118. const user_id = req.params.user_id;
  119. if (!user_id || !Number(user_id)) {
  120. console.log('通过ID获取用户信息失败')
  121. res.send({
  122. status: 0,
  123. type: 'GET_USER_INFO_FAIELD',
  124. message: '获取用户信息失败',
  125. })
  126. return
  127. }
  128. try{
  129. const userinfo = await UserInfoModel.findOne({user_id}, '-_id');
  130. res.send(userinfo)
  131. }catch(err){
  132. console.log('获取用户信息失败', err);
  133. res.send({
  134. status: 0,
  135. type: 'GET_USER_INFO_FAIELD',
  136. message: '获取用户信息失败',
  137. })
  138. }
  139. }
  140. async signout(req, res, next){
  141. delete req.session.user_id;
  142. res.clearCookie('UID');
  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').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 user_id = req.params.user_id;
  267. if (!user_id || !Number(user_id)) {
  268. console.log('更新头像,user_id错误', user_id)
  269. res.send({
  270. status: 0,
  271. type: 'ERROR_USERID',
  272. message: 'user_id参数错误',
  273. })
  274. return
  275. }else if(Number(sid) !== Number(user_id)){
  276. console.log('更新头像sid,user_id不一致', sid, user_id)
  277. res.send({
  278. status: 0,
  279. type: 'NEED_LOGIN_IN',
  280. message: '登录后才可修改头像',
  281. })
  282. return
  283. }
  284. try{
  285. const image_path = await this.qiniu(req);
  286. await UserInfoModel.findOneAndUpdate({user_id}, {$set: {avatar: image_path}});
  287. res.send({
  288. status: 1,
  289. image_path,
  290. })
  291. }catch(err){
  292. console.log('上传图片失败', err);
  293. res.send({
  294. status: 0,
  295. type: 'ERROR_UPLOAD_IMG',
  296. message: '上传图片失败'
  297. })
  298. }
  299. }
  300. async getUserCity(req, res, next){
  301. const cityArr = ['北京', '上海', '深圳', '杭州'];
  302. const filterArr = [];
  303. cityArr.forEach(item => {
  304. filterArr.push(UserInfoModel.find({city: item}).count())
  305. })
  306. filterArr.push(UserInfoModel.$where('!"北京上海深圳杭州".includes(this.city)').count())
  307. Promise.all(filterArr).then(result => {
  308. res.send({
  309. status: 1,
  310. user_city: {
  311. beijing: result[0],
  312. shanghai: result[1],
  313. shenzhen: result[2],
  314. hangzhou: result[3],
  315. qita: result[4],
  316. }
  317. })
  318. }).catch(err => {
  319. console.log('获取用户分布城市数据失败', err);
  320. res.send({
  321. status: 0,
  322. type: 'ERROR_GET_USER_CITY',
  323. message: '获取用户分布城市数据失败'
  324. })
  325. })
  326. }
  327. }
  328. export default new User()