Skip to content

Commit

Permalink
feat: 支持编辑用户配置
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 3, 2024
1 parent e960f28 commit b7781d1
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 4 deletions.
3 changes: 3 additions & 0 deletions webview/src/apps/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { KeywordCreateComponent } from './keyword/create';

import { ProfileListComponent } from './profile/list';
import { ProfileCreateComponent } from './profile/create';
import { ProfileUpdateComponent } from './profile/update';

import { SettingListComponent } from './setting/list';
import { SettingUpdateComponent } from './setting/update';
Expand Down Expand Up @@ -46,6 +47,7 @@ export const AppComponents = [

ProfileListComponent,
ProfileCreateComponent,
ProfileUpdateComponent,

SettingListComponent,
SettingUpdateComponent,
Expand Down Expand Up @@ -77,6 +79,7 @@ export const AppRoutes: Routes = [

{ path: 'profile/list', component: ProfileListComponent, canActivate: [LoginGuard] },
{ path: 'profile/create', component: ProfileCreateComponent, canActivate: [LoginGuard] },
{ path: 'profile/update/:rd', component: ProfileUpdateComponent, canActivate: [LoginGuard] },

{ path: 'setting/list', component: SettingListComponent, canActivate: [LoginGuard] },
{ path: 'setting/update/:name', component: SettingUpdateComponent, canActivate: [LoginGuard] },
Expand Down
4 changes: 2 additions & 2 deletions webview/src/apps/profile/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<form class="row gy-3" (ngSubmit)="createProfile()" #myform="ngForm">
<div class="col-12">
<label class="form-label">场景 *</label>
<select name="roomid" class="form-select" [(ngModel)]="formdata.roomid" (change)="changeRoomid()" required>
<select name="roomid" class="form-select" [(ngModel)]="formdata.roomid" (change)="changeConacts()" required>
<option value="-">私聊</option>
@for (item of wcfChatrooms; track item.wxid) {
<option [value]="item.wxid">{{item.name}}</option>
Expand All @@ -24,7 +24,7 @@
</div>
<div class="col-12">
<label class="form-label">用户 *</label>
<select name="wxid" class="form-select" [(ngModel)]="formdata.wxid" required [disabled]="conacts.length == 0" required>
<select name="wxid" class="form-select" [(ngModel)]="formdata.wxid" [disabled]="conacts.length == 0" required>
@for (item of conacts; track item.wxid) {
<option [value]="item.wxid">{{item.wxid}}({{item.name}})</option>
}
Expand Down
3 changes: 1 addition & 2 deletions webview/src/apps/profile/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export class ProfileCreateComponent {
});
}

public changeRoomid() {
this.formdata.wxid = '';
public changeConacts() {
const id = this.formdata.roomid || '-';
this.conacts = id == '-' ? this.wcfFriends : this.wcfRoomMembers[id] || [];
}
Expand Down
1 change: 1 addition & 0 deletions webview/src/apps/profile/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</td>
<td class="d-none d-xxl-table-cell">{{item.remark}}</td>
<td class="text-nowrap">
<button class="btn btn-sm btn-primary me-2" routerLink="/profile/update/{{item.rd}}">编辑</button>
<button class="btn btn-sm btn-danger" (click)="deleteProfile(item)">删除</button>
</td>
</tr>
Expand Down
72 changes: 72 additions & 0 deletions webview/src/apps/profile/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<layout-header></layout-header>

<nav class="container-lg mb-3">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a routerLink="/welcome">首页</a></li>
<li class="breadcrumb-item"><a routerLink="/profile/list">用户配置</a></li>
<li class="breadcrumb-item active">更新</li>
</ol>
</nav>

<div class="container-lg mb-3">
<form class="row gy-3" (ngSubmit)="updateProfile()" #myform="ngForm">
<div class="col-12">
<label class="form-label">场景 *</label>
<select name="roomid" class="form-select" [(ngModel)]="formdata.roomid" (change)="changeConacts()" required>
<option value="-">私聊</option>
@for (item of wcfChatrooms; track item.wxid) {
<option [value]="item.wxid">{{item.name}}</option>
}
</select>
<div class="form-text">
用户配置所属场景,暂用作生效范围
</div>
</div>
<div class="col-12">
<label class="form-label">用户 *</label>
<select name="wxid" class="form-select" [(ngModel)]="formdata.wxid" [disabled]="conacts.length == 0" required>
@for (item of conacts; track item.wxid) {
<option [value]="item.wxid">{{item.wxid}}({{item.name}})</option>
}
</select>
<div class="form-text">
关联的用户,非好友无法添加私聊配置
</div>
</div>
<div class="col-12">
<label class="form-label">级别</label>
<select name="level" class="form-select" [(ngModel)]="formdata.level">
@for (item of userLevels | keyvalue; track item.key) {
<option [value]="item.key">{{item.value.name}}</option>
}
</select>
<div class="form-text">
机器人使用权限
</div>
</div>
<div class="col-12">
<label class="form-label">AI 唤醒词</label>
<input type="text" name="ai_argot" class="form-control" [(ngModel)]="formdata.ai_argot" />
<div class="form-text">
自定义唤醒词
</div>
</div>
<div class="col-12">
<label class="form-label">AI 模型</label>
<input type="text" name="ai_model" class="form-control" [(ngModel)]="formdata.ai_model" />
<div class="form-text">
当前使用的模型
</div>
</div>
<div class="col-12">
<label class="form-label">备注</label>
<input type="text" name="remark" class="form-control" [(ngModel)]="formdata.remark" />
<div class="form-text">
备注信息
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary" [disabled]="myform.invalid">确认</button>
</div>
</form>
</div>
82 changes: 82 additions & 0 deletions webview/src/apps/profile/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { UserLevels } from 'src/openapi/const';
import { RobotApi, ProfileUpdateParam } from '../../openapi/wrobot';
import { WrestApi, WcfrestContactPayload } from '../../openapi/wcfrest';


@Component({
selector: 'page-profile-update',
templateUrl: 'update.html'
})
export class ProfileUpdateComponent implements OnInit {

public userLevels = UserLevels;

public wcfAvatars: Record<string, string> = {};
public wcfFriends: Array<WcfrestContactPayload> = [];
public wcfContacts: Record<string, WcfrestContactPayload> = {};
public wcfChatrooms: Array<WcfrestContactPayload> = [];
public wcfRoomMembers: Record<string, Array<WcfrestContactPayload>> = {};

public conacts: Array<WcfrestContactPayload> = [];
public formdata = {} as ProfileUpdateParam;

constructor(
private router: Router,
private route: ActivatedRoute
) {
this.getWcfFriends();
this.getWcfChatrooms();
}

public ngOnInit() {
const rd = this.route.snapshot.paramMap.get('rd');
rd && this.getProfile(+rd);
}

public getProfile(rd: number) {
RobotApi.profileDetail({ rd }).then((data) => {
data && Object.assign(this.formdata, data);
});
}

public updateProfile() {
if (this.formdata.level) {
this.formdata.level = +this.formdata.level;
}
RobotApi.profileUpdate(this.formdata).then(() => {
this.router.navigate(['profile/list']);
});
}

public changeConacts() {
const id = this.formdata.roomid || '-';
this.conacts = id == '-' ? this.wcfFriends : this.wcfRoomMembers[id] || [];
}

public getWcfFriends() {
WrestApi.friends().then((data) => {
this.wcfFriends = data || [];
});
}

public getWcfChatrooms() {
WrestApi.chatrooms().then((data) => {
this.wcfChatrooms = data || [];
this.getWcfRoomMembers(this.wcfChatrooms.map((item) => item.wxid));
});
}

public getWcfRoomMembers(ids: string[]) {
[...new Set(ids)].forEach((id) => {
WrestApi.chatroomMembers({ roomid: id }).then((data) => {
this.wcfRoomMembers[id] = data || [];
// 尝试更新当前人员列表
this.changeConacts();
});
});
}

}

0 comments on commit b7781d1

Please sign in to comment.