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