Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /www/wwwroot/dev.zhalaotie.com/wp-includes/wp-db.php on line 3215

Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /www/wwwroot/dev.zhalaotie.com/wp-includes/wp-db.php on line 3215
Angular Framework 前端实现本地模拟服务端提交&响应功能 – 扎老铁
Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /www/wwwroot/dev.zhalaotie.com/wp-includes/wp-db.php on line 3215

Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /www/wwwroot/dev.zhalaotie.com/wp-includes/wp-db.php on line 3215
Warning: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given in /www/wwwroot/dev.zhalaotie.com/wp-includes/wp-db.php on line 3215
class="post-52830 post type-post status-publish format-standard has-post-thumbnail hentry">

Angular Framework 前端实现本地模拟服务端提交&响应功能

import {UserSignInEvent} from './UserSignIn';
import {UserSignInResult} from './UserSignInResult';
import {Observable, Subject} from 'rxjs';
import {AjaxActionResult} from './components/forms/core/AjaxActionResult';
import {Injectable} from '@angular/core';
import {User} from './User';
import {ServiceBase} from './ServiceBase';
import {DataLoadStatus} from './components/core';
import {HttpApi} from './HttpApi';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {isNullOrUndefined} from 'util';
import {LocalizationProvider} from './i18n_provider';
import {List} from 'linqts';

//
// declare  interface Permission{
//
// }
//


export declare interface DevicePropertyPermissionSet {
  /**
   * 允许所有
   */
  allowAll?: boolean;
  /**
   * 0 不允许
   * 1 允许读
   * 5 允许写
   */
  keys?: { [id: string]: number }
}

@Injectable()
export class MembershipService extends ServiceBase {
  l = LocalizationProvider.current;

  constructor(httpApi: HttpApi) {
    super(httpApi);
    // handle 页面刷新

    User.current = this.users['0'];

    let s = this.getUserFromStorage();
    if (s) {
      // debugger
      s.subscribe(x => {
        this.signIn(x);
      }, err => {
        console.error(`Error load user form localStorage ${err.message}`, err)
      });
    } else {

    }
  }

  /**
   * 上位机建议分为3级权限,
   * 初级权限:用户只能查看灯的工作状态,只读不能修改任何参数;
   * 中级权限:用户可以添加场景,添加设备,修改modbusdata寄存器表格里标黄的部分寄存器值。
   * 高级权限:只对厂家开放,可以修改所有寄存器的值。
   */
  users = [
    {id: '0', username: 'Monitoring user', name: this.l.localize('user_anonymous'), password: '201802', userType: 'anonymous'},
    {id: '2', username: 'Admin user', name: this.l.localize('user_normal-admin'), password: '02139126868', userType: 'user-admin'},
    {id: '1', username: 'www.nanhua.com', name: this.l.localize('user_super-admin'), password: '4008217000', userType: 'super-admin'},
    {id: 'dev', username: 'dev', name: this.l.localize('user_dev-admin'), password: 'dev', userType: 'dev-admin'},
  ];


  normalUserPermissions = {
    'act://iot/device/read': 1,
    'act://iot/scene/read': 1,
    'act://iot/group/read': 1,
    'act://iot/system/configuration/read': 1,//权限设置
  };

  userAdminPermissions = {
    ...this.normalUserPermissions,
    // 'url:///iot/scene/create': 1,
    // 'url:///iot/device/create': 1,
    // 'url:///iot/device/update': 1,
    'act://iot/device/create': 1,//设备新增按钮
    'act://iot/device/delete': 1,//设备删除按钮
    'act://iot/device/update': 1,//设备编辑按钮
    // 'act://iot/device/factoryReset': 1, //设备恢复出厂设置按钮
    'act://iot/scene/create': 1, //场景创建图标
    'act://iot/scene/delete': 1, //场景删除图标
    'act://iot/scene/update': 1, //场景编辑图标
    'act://iot/scene/updateisok': 1, //场景编辑确认/ok按钮
    'act://iot/scene/setposition': 1,
    'act://iot/scene/setimage': 1,
    'act://iot/group/create': 1, //分组创建图标
    'act://iot/group/update': 1, //分组编辑图标
    'act://iot/group/updateisok': 1, //分组编辑确认/ok按钮
    'act://iot/group/delete': 1, //分组删除图标
    'act://iot/device-control': 1, //控制灯的确认/ok按钮
    'act://iot/system/configuration': 1,
    'act://iot/diagnostics/test': 1,
    'act://iot/device/factoryReset': 1,
  };

  superAdminPermissions = {
    ...this.userAdminPermissions,
    // 'url:///iot/scene/create': 1,
    // 'url:///iot/device/create': 1,
    // 'url:///iot/device/update': 1,
    // 'act://iot/device/create': 1,//设备新增按钮
    // 'act://iot/device/delete': 1,//设备删除按钮
    // 'act://iot/device/update': 1,//设备编辑按钮
    //设备恢复出厂设置按钮
    'act://iot/system/configuration/update': 1,
    'act://iot/setGroupNoAndItemNo': 1,
    'act://iot/stopService': 1,
    'act://iot/restartService': 1,
    // 'act://iot/scene/create': 1, //场景创建图标
    // 'act://iot/scene/delete': 1, //场景删除图标
    // 'act://iot/scene/update': 1, //场景编辑图标
    // 'act://iot/scene/updateisok': 1, //场景编辑确认/ok按钮
    // 'act://iot/group/create': 1, //分组创建图标
    // 'act://iot/group/update': 1, //分组编辑图标
    // 'act://iot/group/updateisok': 1, //分组编辑确认/ok按钮
    // 'act://iot/group/delete': 1, //分组删除图标
    // 'act://iot/configuration': 1,//权限设置导航
    // 'act://iot/control': 1, //控制灯的确认/ok按钮
  };


  devAdminPermissions = {
    ...this.superAdminPermissions,
    'act://iot/pauseSyncTask': 1,
    'act://iot/resumeSyncTask': 1,
    'act://iot/sceneBatchControl': 1,
  };


  userTypePermissions: { [id: string]: { [id: string]: number } } = {
    'normal': this.normalUserPermissions,
    'user-admin': this.userAdminPermissions,
    'super-admin': this.superAdminPermissions,
    'dev-admin': this.devAdminPermissions,
  };

  getFeaturePermissionList(user: User) {
    if (!user) return null;
    let ps = this.userTypePermissions[user.userType];
    return ps;
  }

  getFeaturePermission(key: string) {
    let ps = this.getFeaturePermissionList(User.current);
    if (!ps) return false;
    let v = ps[key];
    // debugger
    if (isNullOrUndefined(v)) return false;
    return v;
  }


  static userAdminProps = 'mdslave_add,prdct_type_item_idx,mmdd_as_spring,mmdd_as_summer,mmdd_as_autumn,mmdd_as_winter,GPS_hard_ware_on_line,OPT_hard_ware_on_line,hhmm_as_dawm_for_spring,hhmm_as_day_for_spring,hhmm_as_even_for_spring,hhmm_as_night_for_spring,hhmm_as_dawm_for_summer,hhmm_as_day_for_summer,hhmm_as_even_for_summer,hhmm_as_night_for_summer,hhmm_as_dawm_for_autumn,hhmm_as_day_for_autumn,hhmm_as_even_for_autumn,hhmm_as_night_for_autumn,hhmm_as_dawm_for_winter,hhmm_as_day_for_winter,hhmm_as_even_for_winter,hhmm_as_night_for_winter,turn_on_qtm_when_dawn,strength_when_dawn,blink_freq_when_dawn,turn_on_qtm_when_day,strength_when_day,blink_freq_when_day,turn_on_qtm_when_even,strength_when_even,blink_freq_when_even,turn_on_qtm_when_night,strength_when_night,blink_freq_when_night,turn_on_qtm_for_crt,strength_for_crt,blink_freq_for_crt'
    .split(',');
  static userAdminPropsPermissions;

  static get1() {
    let p = this.userAdminPropsPermissions;
    if (!p) {
      p = {};
      let props = this.userAdminProps;
      for (let i of props) p[i] = 5;
      this.userAdminPropsPermissions = p;
    }
    return p;
  }

  userTypePropPermissions: { [id: string]: DevicePropertyPermissionSet } = {
    'dev-admin': {
      allowAll: true,
    },
    'super-admin': {
      allowAll: true,
    },
    'user-admin': {
      keys: MembershipService.get1(),
    },
  };

  getPropPermission(userType: string, type?: string) {
    let ps = this.getPropPermissionList(User.current);
    if (!ps) return 0;
    let v = ps[userType];
    if (isNullOrUndefined(v)) return 0;
    return v;
  }

  getPropPermissionList(user: User) {
    if (!user) return null;
    let ps = this.userTypePropPermissions[user.userType];
    return ps;
  }


  isLoggedIn(): boolean {
    let u = User.current;
    return u && u.userType != 'anonymous';
  }

  currentUser() {
    return User.current;
  }

  private getUserFromStorage(): Observable<User> {
    let str = localStorage.getItem('_user');
    if (!str) return null;
    try {
      return Observable.of(JSON.parse(str));
    } catch (e) {
      return null;
      // return Observable.throw(e);
    }
  }

  //
  // // store the URL so we can redirect after logging in
  // redirectUrl: string;

  // login(user: any): Observable<boolean> {
  //   return Observable.of(true).delay(1000).do(val => this.isLoggedIn = true);
  // }

  logout(): void {
    this.unset();
  }


  /**
   * 输入用户名密码,本地模拟登录服务端返回
   * @param info
   * @returns {any}
   */
  signIn(info: UserSignInEvent): Observable<AjaxActionResult<UserSignInResult>> {
    let username = info.username.trim().toLowerCase();
    var u = this.users.find(x => x.username && x.username == username || x.username.toLowerCase() == username);
    let act = this.getUserAjaxResult(info, u);
    if (act.operationStatus == DataLoadStatus.done) {
      this.set(u);
    }
    return Observable.of(act);
  }

  private getUserAjaxResult(info: UserSignInEvent, userResult: User) {
    let act = new AjaxActionResult<UserSignInResult>();
    let r = new UserSignInResult();
    act.data = r;
    if (!userResult) {
      r.resultType = 'Membership_Signin_UserNotExists';
      act.message = this.l.localize('This user does not exist');
      act.operationStatus = DataLoadStatus.fail;
    } else if (userResult.password != info.password) {
      r.resultType = 'Membership_Signin_PasswordIncorrect';
      act.message = this.l.localize('The password is incorrect');
      act.operationStatus = DataLoadStatus.fail;
    } else {
      r.resultType = 'Membership_Signin_Success';
      act.operationStatus = DataLoadStatus.done;
      act.message = this.l.localize('Login successful');
      r.user = userResult;
    }
    return act;
  }

  private set(u: User) {
    // 避免 json 序列号循环引用
    User.current = null;
    localStorage.setItem('_user', JSON.stringify(u));
    User.current = u;
  }

  private unset() {
    localStorage.removeItem('_user');
    User.current = this.users['0'];
  }
}

 

发表评论

电子邮件地址不会被公开。