user.js 8.5 KB

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