user.router.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. const Router = require('@koa/router');
  2. const router = new Router({ prefix: '/user' });
  3. const util = require('../utils/util');
  4. const userService = require('../service/user.service');
  5. const projectService = require('../service/projects.service');
  6. const projectUserService = require('../service/project.user.service');
  7. const pageRoleService = require('../service/pagesRole.service');
  8. const pageService = require('../service/pages.service');
  9. const menuService = require('../service/menu.service');
  10. const publishService = require('../service/publish.service');
  11. const roleService = require('../service/roles.service');
  12. const imgcloudService = require('../service/imgcloud.service');
  13. const md5 = require('md5.js');
  14. const nodemailer = require('nodemailer');
  15. const config = require('../config');
  16. const request = require('../utils/request');
  17. const { Keyv } = require('keyv');
  18. const keyv = new Keyv();
  19. /**
  20. * 编辑器端用户登录
  21. */
  22. router.post('/login', async (ctx) => {
  23. const { userName, userPwd, openId } = ctx.request.body;
  24. if (!userName || !userPwd) {
  25. util.fail(ctx, '用户名或密码不能为空');
  26. return;
  27. }
  28. const pwd = new md5().update(userPwd).digest('hex');
  29. const res = await userService.findUser(userName, pwd, openId || userName);
  30. if (!res) {
  31. util.fail(ctx, '用户名或密码错误');
  32. return;
  33. }
  34. if (!res.openId && openId) {
  35. const cacheUser = await keyv.get(openId);
  36. if (cacheUser) {
  37. await userService.bindOpenId({ ...cacheUser, id: res.id });
  38. }
  39. }
  40. const token = util.createToken({ userName, userId: res.id, nickName: res.nickName });
  41. userService.updateUserInfo(res.id);
  42. util.success(ctx, {
  43. userId: res.id,
  44. userName,
  45. token,
  46. });
  47. });
  48. /**
  49. * admin端登录
  50. */
  51. router.post('/admin/login', async (ctx) => {
  52. const { userName, userPwd, openId } = ctx.request.body;
  53. if (!userName || !userPwd) {
  54. util.fail(ctx, '用户名或密码不能为空');
  55. return;
  56. }
  57. const pwd = new md5().update(userPwd).digest('hex');
  58. const res = await userService.findSubUser(userName, pwd, openId || userName);
  59. if (!res) {
  60. util.fail(ctx, '用户名或密码错误');
  61. return;
  62. }
  63. if (!res.openId && openId) {
  64. const cacheUser = await keyv.get(openId);
  65. if (cacheUser) {
  66. await userService.bindOpenId({ ...cacheUser, id: res.id });
  67. }
  68. }
  69. console.log("user res信息")
  70. console.log(res)
  71. const token = util.createToken({ userName, userId: res.id, nickName: res.nickName });
  72. userService.updateUserInfo(res.id);
  73. util.success(ctx, {
  74. userId: res.id,
  75. userName,
  76. token,
  77. });
  78. console.log("新的token:"+token)
  79. });
  80. /**
  81. * 微信授权登录
  82. */
  83. router.post('/wechat', async (ctx) => {
  84. const { code } = ctx.request.body;
  85. if (!code) {
  86. util.fail(ctx, 'code不能为空');
  87. return;
  88. }
  89. const response = await request.get(
  90. `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${config.WECHAT_APP_ID}&secret=${config.WECHAT_APP_SECRET}&code=${code}&grant_type=authorization_code`,
  91. {},
  92. );
  93. const { access_token, openid, unionid, errcode } = JSON.parse(response.data);
  94. if (errcode) {
  95. util.success(ctx, '');
  96. return;
  97. }
  98. const wxUser = await userService.findUser(openid, openid, openid);
  99. if (wxUser) {
  100. userService.updateUserInfo(wxUser.id);
  101. const token = util.createToken({ userName: wxUser.userName, userId: wxUser.id, nickName: wxUser.nickName });
  102. util.success(ctx, {
  103. userId: wxUser.id,
  104. userName: wxUser.userName,
  105. token,
  106. });
  107. return;
  108. }
  109. const res1 = await request.get(`https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}`);
  110. const { nickname, headimgurl } = JSON.parse(res1.data);
  111. await keyv.set(
  112. openid,
  113. {
  114. openid,
  115. unionid,
  116. nickname,
  117. headimgurl,
  118. },
  119. 3 * 60 * 1000,
  120. );
  121. util.success(ctx, {
  122. openId: openid,
  123. });
  124. });
  125. /**
  126. * 获取用户信息
  127. */
  128. router.get('/info', async (ctx) => {
  129. const { userId } = util.decodeToken(ctx);
  130. const res = await userService.profile(userId);
  131. util.success(ctx, res);
  132. });
  133. /**
  134. * 获取个人信息
  135. */
  136. router.get('/profile', async (ctx) => {
  137. const { userId } = util.decodeToken(ctx);
  138. const res = await userService.profile(userId);
  139. util.success(ctx, res);
  140. });
  141. /**
  142. * 用户搜索
  143. */
  144. router.post('/search', async (ctx) => {
  145. const { keyword } = ctx.request.body;
  146. if (!keyword) {
  147. util.fail(ctx, '关键字不能为空');
  148. return;
  149. }
  150. const res = await userService.search(keyword);
  151. if (!res) {
  152. util.fail(ctx, '当前用户名不存在');
  153. return;
  154. }
  155. util.success(ctx, res);
  156. });
  157. /**
  158. * 用户信息更新
  159. */
  160. router.post('/update/profile', async (ctx) => {
  161. const { nickName, avatar } = ctx.request.body;
  162. if (!nickName && !avatar) {
  163. util.fail(ctx, '参数异常,请重新提交');
  164. return;
  165. }
  166. const { userId } = util.decodeToken(ctx);
  167. await userService.updateUserInfo(userId, nickName, avatar);
  168. util.success(ctx, '更新成功');
  169. });
  170. /**
  171. * 用户注册 - 发送验证码
  172. */
  173. router.post('/sendEmail', async (ctx) => {
  174. try {
  175. const { email } = ctx.request.body;
  176. if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
  177. util.fail(ctx, '邮箱不能为空或格式错误');
  178. return;
  179. }
  180. const val = await keyv.get(email);
  181. if (val) {
  182. util.fail(ctx, '验证码已发送,请查收');
  183. return;
  184. }
  185. let transporter = nodemailer.createTransport({
  186. host: config.EMAIL_HOST,
  187. port: config.EMAIL_PORT,
  188. auth: {
  189. user: config.EMAIL_USER, // 你的Gmail地址
  190. pass: config.EMAIL_PASSWORD, // 你的Gmail密码或应用专用密码
  191. },
  192. });
  193. const random = Math.random().toString().slice(2, 7).replace(/^(0)+/, '1');
  194. let mailOptions = {
  195. from: `"Marsview" <${config.EMAIL_USER}>`, // 发送者地址
  196. to: email, // 接收者列表
  197. subject: 'Marsview账号注册', // 主题行
  198. text: '验证码发送', // 纯文字正文
  199. html: `当前验证码为:<b>${random}</b>,3分钟内有效。<br/><br/>感谢您体验 Marsview 搭建平台,线上平台不保证数据的稳定性,建议有条件用户,切换到 Marsview 私有化部署服务,您在使用过程中遇到任何问题均联系我。<br/><br/>邮 箱:marsview@163.com<br/>微 信:17611021717`, // HTML正文
  200. };
  201. await transporter.sendMail(mailOptions);
  202. await keyv.set(email, random, 3 * 60 * 1000);
  203. util.success(ctx, '发送成功');
  204. } catch (error) {
  205. util.fail(ctx, error.message);
  206. }
  207. });
  208. /**
  209. * 用户注册
  210. */
  211. router.post('/regist', async (ctx) => {
  212. const { userName, code, userPwd } = ctx.request.body;
  213. if (!userName || !userPwd) {
  214. util.fail(ctx, '用户名或密码不能为空');
  215. return;
  216. }
  217. if (!code) {
  218. util.fail(ctx, '邮箱验证码不能为空');
  219. return;
  220. }
  221. const val = await keyv.get(userName);
  222. if (!val) {
  223. util.fail(ctx, '验证码已过期');
  224. return;
  225. }
  226. if (val != code) {
  227. util.fail(ctx, '验证码错误');
  228. return;
  229. }
  230. const user = await userService.search(userName);
  231. if (user) {
  232. util.fail(ctx, '当前用户已存在');
  233. return;
  234. }
  235. const nickName = userName.split('@')[0];
  236. const pwd = new md5().update(userPwd).digest('hex');
  237. const res = await userService.create(nickName, userName, pwd);
  238. if (res.affectedRows == 1) {
  239. // 生成用户token
  240. const token = util.createToken({ userName, userId: res.insertId });
  241. util.success(ctx, {
  242. userId: res.id,
  243. userName,
  244. token,
  245. });
  246. } else {
  247. util.fail(ctx, '注册失败,请重试');
  248. }
  249. });
  250. /**
  251. * 忘记密码 - 生成链接
  252. */
  253. router.post('/password/forget', async (ctx) => {
  254. const { userEmail } = ctx.request.body;
  255. if (!userEmail) {
  256. util.fail(ctx, '邮箱不能为空');
  257. return;
  258. }
  259. const user = await userService.search(userEmail);
  260. if (!user) {
  261. util.fail(ctx, '当前用户不存在');
  262. return;
  263. }
  264. // 生成验证码,保存在redis中,用来验证链接有效期
  265. const random = Math.random().toString().slice(2, 7);
  266. await keyv.set(userEmail, random, 5 * 60 * 1000);
  267. // 生成加密后token
  268. const token = util.createToken({ userEmail });
  269. // 发送邮件
  270. let transporter = nodemailer.createTransport({
  271. host: config.EMAIL_HOST,
  272. port: config.EMAIL_PORT,
  273. auth: {
  274. user: config.EMAIL_USER, // 你的Gmail地址
  275. pass: config.EMAIL_PASSWORD, // 你的Gmail密码或应用专用密码
  276. },
  277. });
  278. let mailOptions = {
  279. from: `"Marsview" <${config.EMAIL_USER}>`, // 发送者地址
  280. to: userEmail, // 接收者列表
  281. subject: 'Marsview密码找回', // 主题行
  282. text: '验证码发送', // 纯文字正文
  283. html: `Hello,${userEmail}! <br/> 我们收到了你重置密码的申请,请点击下方按链接行重置,<a href="https://www.marsview.com.cn/password-reset?resetToken=${token}">重置密码</a> <br/> 链接 3分钟内有效,请尽快操作,如不是你发起的请求,请忽略。`, // HTML正文
  284. };
  285. await transporter.sendMail(mailOptions);
  286. util.success(ctx, '发送成功');
  287. });
  288. /**
  289. * 忘记密码 - 获取账号
  290. */
  291. router.post('/password/getUserByToken', async (ctx) => {
  292. const { resetToken } = ctx.request.query;
  293. const { userEmail } = util.decodeResetToken(resetToken);
  294. const val = await keyv.get(userEmail);
  295. if (!val) {
  296. util.fail(ctx, '链接已失效,请重新操作');
  297. return;
  298. }
  299. util.success(ctx, userEmail);
  300. });
  301. /**
  302. * 忘记密码 - 重置密码
  303. */
  304. router.post('/password/reset', async (ctx) => {
  305. const { resetToken, userPwd } = ctx.request.body;
  306. if (!resetToken) {
  307. util.fail(ctx, '重置Token不能为空');
  308. return;
  309. }
  310. if (!userPwd) {
  311. util.fail(ctx, '重置密码不能为空');
  312. return;
  313. }
  314. const { userEmail } = util.decodeResetToken(resetToken);
  315. if (!userEmail) {
  316. util.fail(ctx, 'Token 识别错误,请重新操作');
  317. return;
  318. }
  319. const val = await keyv.get(userEmail);
  320. if (!val) {
  321. util.fail(ctx, '链接已失效,请重新操作');
  322. return;
  323. }
  324. const pwd = new md5().update(userPwd).digest('hex');
  325. await userService.resetPwd(userEmail, pwd);
  326. await keyv.delete(userEmail);
  327. util.success(ctx, '更新成功');
  328. });
  329. /**
  330. * 密码修改
  331. */
  332. router.post('/password/update', async (ctx) => {
  333. const { oldPwd, userPwd, confirmPwd } = ctx.request.body;
  334. if (!oldPwd || !userPwd || !confirmPwd) {
  335. util.fail(ctx, '密码不能为空');
  336. return;
  337. }
  338. if (userPwd !== confirmPwd) {
  339. util.fail(ctx, '两次密码不一致');
  340. return;
  341. }
  342. const { userName } = util.decodeToken(ctx);
  343. try {
  344. const res = await userService.verifyOldPwd(userName, oldPwd);
  345. if (res) {
  346. const pwd = new md5().update(userPwd).digest('hex');
  347. await userService.resetPwd(userName, pwd);
  348. util.success(ctx, '密码更改成功');
  349. } else {
  350. util.fail(ctx, '原密码输入错误');
  351. }
  352. } catch (error) {
  353. util.fail(ctx, error.message);
  354. }
  355. });
  356. /**
  357. * 用户注销
  358. */
  359. router.post('/logout', async (ctx) => {
  360. const { userId, userName } = util.decodeToken(ctx);
  361. if (userId == 50) {
  362. util.fail(ctx, '管理员账号不支持注销');
  363. return;
  364. }
  365. // 删除用户所有项目
  366. await projectService.deleteAllProject(userId, userName);
  367. // 删除用户所有关联
  368. await projectUserService.deleteAllProjectUser(userId);
  369. // 删除用户所有页面角色
  370. await pageRoleService.deleteAllPageRole(userId);
  371. // 删除用户所有页面
  372. await pageService.deleteAllPage(userId, userName);
  373. // 删除用户所有菜单
  374. await menuService.deleteAllMenu(userId);
  375. // 删除用户所有发布
  376. await publishService.deleteAllPublish(userId);
  377. // 删除用户所有角色
  378. await roleService.deleteAllRole(userId);
  379. // 删除用户
  380. await userService.deleteUser(userId, userName);
  381. // 删除用户图片
  382. await imgcloudService.deleteAllImg(userId);
  383. util.success(ctx, '注销成功');
  384. });
  385. /**
  386. * 查询子用户列表
  387. */
  388. router.get('/subUser/list', async (ctx) => {
  389. const { userId } = util.decodeToken(ctx);
  390. const { pageNum, pageSize, keyword } = ctx.request.query;
  391. const { total } = await userService.getSubUsersCount(userId, keyword);
  392. if (total == 0) {
  393. return util.success(ctx, {
  394. list: [],
  395. total: 0,
  396. pageSize: +pageSize || 12,
  397. pageNum: +pageNum || 1,
  398. });
  399. }
  400. const list = await userService.getSubUsersList(userId, pageNum || 1, pageSize || 12, keyword);
  401. util.success(ctx, {
  402. list,
  403. total,
  404. pageSize: +pageSize,
  405. pageNum: +pageNum,
  406. });
  407. });
  408. // 创建子用户
  409. router.post('/subUser/create', async (ctx) => {
  410. const { userName, userPwd } = ctx.request.body;
  411. if (!userName || !userPwd) {
  412. util.fail(ctx, '用户名或密码不能为空');
  413. return;
  414. }
  415. const { userId } = util.decodeToken(ctx);
  416. const user = await userService.search(userName);
  417. if (user) {
  418. util.fail(ctx, '当前用户已存在');
  419. return;
  420. }
  421. const nickName = userName.split('@')[0];
  422. const pwd = new md5().update(userPwd).digest('hex');
  423. const res = await userService.create(nickName, userName, pwd, userId);
  424. if (res.affectedRows == 1) {
  425. util.success(ctx, '注册成功');
  426. } else {
  427. util.fail(ctx, '注册失败,请重试');
  428. }
  429. });
  430. // 删除子用户
  431. router.post('/subUser/delete', async (ctx) => {
  432. const { id } = ctx.request.body;
  433. if (!id) {
  434. util.fail(ctx, '用户ID不能为空');
  435. return;
  436. }
  437. const { userId } = util.decodeToken(ctx);
  438. const res = await userService.deleteSubUser(id, userId);
  439. if (res.affectedRows == 1) {
  440. util.success(ctx, '删除成功');
  441. } else {
  442. util.fail(ctx, '删除失败,请重试');
  443. }
  444. });
  445. /**
  446. * 平台管理员创建用户账号
  447. */
  448. router.post('/create/account', async (ctx) => {
  449. const { userName, userPwd } = ctx.request.body;
  450. if (!userName || !userPwd) {
  451. util.fail(ctx, '用户名或密码不能为空');
  452. return;
  453. }
  454. const user = await userService.search(userName);
  455. if (user) {
  456. util.fail(ctx, '当前用户已存在');
  457. return;
  458. }
  459. const nickName = userName.split('@')[0];
  460. const pwd = new md5().update(userPwd).digest('hex');
  461. const res = await userService.create(nickName, userName, pwd);
  462. if (res.affectedRows == 1) {
  463. const token = util.createToken({ userName, userId: res.insertId });
  464. util.success(ctx, {
  465. userId: res.id,
  466. userName,
  467. token,
  468. });
  469. } else {
  470. util.fail(ctx, '创建失败,请稍后重试');
  471. }
  472. });
  473. //addbywanghao sso 20250512
  474. router.post('/ssoLogin', async (ctx) => {
  475. const { userIdno } = ctx.request.body;
  476. if (!userIdno ) {
  477. util.fail(ctx, 'userIdno不能为空');
  478. return;
  479. }
  480. const res = await userService.findUserSso(userIdno);
  481. if (!res) {
  482. util.fail(ctx, 'userIdno 不存在,暂无权限');
  483. return;
  484. }
  485. console.log("user res信息")
  486. console.log(res)
  487. const token = util.createToken({ userName: res.userName, userId: res.id, nickName: res.nickName });
  488. userService.updateUserInfo(res.id);
  489. util.success(ctx, {
  490. userId: res.id,
  491. userName: res.userName,
  492. token,
  493. });
  494. console.log("sso新的token:"+token)
  495. });
  496. module.exports = router;