MCPcopy
hub / github.com/YMFE/yapi / update

Method update

server/controllers/user.js:489–544  ·  view source on GitHub ↗

* 更新用户个人信息 * @interface /user/update * @method POST * @param uid 用户uid * @param [role] 用户角色,只有管理员有权限修改 * @param [username] String * @param [email] String * @category user * @foldnumber 10 * @returns {Object} * @example

(ctx)

Source from the content-addressed store, hash-verified

487 * @example
488 */
489 async update(ctx) {
490 //更新用户信息
491 try {
492 let params = ctx.request.body;
493
494 params = yapi.commons.handleParams(params, {
495 username: 'string',
496 email: 'string'
497 });
498
499 if (this.getRole() !== 'admin' && params.uid != this.getUid()) {
500 return (ctx.body = yapi.commons.resReturn(null, 401, '没有权限'));
501 }
502
503 let userInst = yapi.getInst(userModel);
504 let id = params.uid;
505
506 if (!id) {
507 return (ctx.body = yapi.commons.resReturn(null, 400, 'uid不能为空'));
508 }
509
510 let userData = await userInst.findById(id);
511 if (!userData) {
512 return (ctx.body = yapi.commons.resReturn(null, 400, 'uid不存在'));
513 }
514
515 let data = {
516 up_time: yapi.commons.time()
517 };
518
519 params.username && (data.username = params.username);
520 params.email && (data.email = params.email);
521
522 if (data.email) {
523 var checkRepeat = await userInst.checkRepeat(data.email); //然后检查是否已经存在该用户
524 if (checkRepeat > 0) {
525 return (ctx.body = yapi.commons.resReturn(null, 401, '该email已经注册'));
526 }
527 }
528
529 let member = {
530 uid: id,
531 username: data.username || userData.username,
532 email: data.email || userData.email
533 };
534 let groupInst = yapi.getInst(groupModel);
535 await groupInst.updateMember(member);
536 let projectInst = yapi.getInst(projectModel);
537 await projectInst.updateMember(member);
538
539 let result = await userInst.update(id, data);
540 ctx.body = yapi.commons.resReturn(result);
541 } catch (e) {
542 ctx.body = yapi.commons.resReturn(null, 402, e.message);
543 }
544 }
545
546 /**

Callers 4

tokenMethod · 0.45
updateTokenMethod · 0.45
upStudyMethod · 0.45
changePasswordMethod · 0.45

Calls 6

handleParamsMethod · 0.80
getRoleMethod · 0.80
getUidMethod · 0.45
findByIdMethod · 0.45
checkRepeatMethod · 0.45
updateMemberMethod · 0.45

Tested by

no test coverage detected