store-credit-grants.component.ts 2.8 KB

123456789101112131415
  1. import {Component,inject,signal} from '@angular/core';
  2. import {FormsModule} from '@angular/forms';
  3. import {OperationsService} from '../operations.service';
  4. interface Grant {objectId:string;name:string;username:string;role:string;enabled:boolean;}
  5. @Component({selector:'app-store-credit-grants',standalone:true,imports:[FormsModule],styleUrls:['./store-credit-account.component.scss'],template:`
  6. <button type="button" (click)="show()">充值权限</button>
  7. @if(open()) {<div class="backdrop" (click)="close()"></div><section class="credit-dialog" role="dialog" aria-modal="true" aria-label="门店充值权限"><header><h2>门店充值权限</h2><button type="button" (click)="close()" [disabled]="busy()">关闭</button></header><p>仅授权本帐套运营管理员办理门店充值及原单纠错,不授予超级管理员权限。</p>@if(error()){<p class="error" role="alert">{{error()}}</p>}@if(message()){<p class="success" role="status">{{message()}}</p>}
  8. <label>授权或撤销原因<input [(ngModel)]="reason" minlength="2" maxlength="500" [disabled]="busy()"/></label><div class="scroll"><table><thead><tr><th>账号</th><th>状态</th><th>操作</th></tr></thead><tbody>@for(row of items();track row.objectId){<tr><td>{{row.name}}<small>{{row.username}}</small></td><td>{{row.enabled?'已授权':'未授权'}}@if(row.role!=='ops-manager'){<small>账号或角色已失效</small>}</td><td><button type="button" (click)="save(row)" [disabled]="busy()||(!row.enabled&&row.role!=='ops-manager')">{{row.enabled?'撤销授权':'授权充值'}}</button></td></tr>}@empty{<tr><td colspan="3">{{busy()?'正在读取…':'暂无可授权的运营管理员'}}</td></tr>}</tbody></table></div></section>}`})
  9. export class StoreCreditGrantsComponent {
  10. private readonly operations=inject(OperationsService);open=signal(false);busy=signal(false);error=signal('');message=signal('');items=signal<Grant[]>([]);reason='';
  11. close(){if(!this.busy())this.open.set(false);}
  12. async show(){this.open.set(true);this.error.set('');this.message.set('');this.busy.set(true);try{await this.load();}catch(e){this.error.set(e instanceof Error?e.message:'授权信息读取失败');}finally{this.busy.set(false);}}
  13. private async load(){this.items.set((await this.operations.request<{items:Grant[]}>('ops/stores/credit-grants/list')).items);}
  14. async save(row:Grant){if(this.busy())return;if(this.reason.trim().length<2){this.error.set('请填写至少两个字的授权或撤销原因');return;}this.busy.set(true);this.error.set('');try{await this.operations.mutate('ops/stores/credit-grants/save',row.objectId,this.reason.trim(),{enabled:!row.enabled});this.message.set(row.enabled?'充值权限已撤销':'充值权限已授予');await this.load();}catch(e){this.error.set(e instanceof Error?e.message:'授权变更失败');}finally{this.busy.set(false);}}
  15. }