mirror of
https://github.com/dotnetcore/Util.git
synced 2026-10-21 14:51:58 +08:00
添加ts操作uuid
This commit is contained in:
@@ -16,11 +16,12 @@ namespace Util.Samples.Webs.Controllers {
|
||||
}
|
||||
|
||||
public IActionResult B() {
|
||||
var a = Request;
|
||||
return Content( "success" );
|
||||
}
|
||||
|
||||
public IActionResult Error() {
|
||||
return View();
|
||||
return Content( "" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Util.Samples.Webs.Models
|
||||
{
|
||||
public class ErrorViewModel
|
||||
{
|
||||
public string RequestId { get; set; }
|
||||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ namespace Util.Samples.Webs {
|
||||
app.UseMvc( routes => {
|
||||
routes.MapRoute( "areaRoute", "{area:exists}/{controller}/{action=Index}/{id?}" );
|
||||
routes.MapRoute( "default", "{controller=Home}/{action=Index}/{id?}" );
|
||||
routes.MapSpaFallbackRoute( "spa-fallback", "{controller=Home}/{action=Index}" );
|
||||
routes.MapSpaFallbackRoute( "spa-fallback", new { controller = "Home", action = "Index" } );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,6 @@ import { util } from "../../util";
|
||||
export class AComponent {
|
||||
target;
|
||||
constructor() {
|
||||
util.http.get("/home/b").handle(response => {
|
||||
this.target = response.text();
|
||||
});
|
||||
this.target = util.helper.uuid();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { NgModule, Injector } from '@angular/core'
|
||||
import { FrameworkModule } from '../framework.module'
|
||||
import { FrameworkModule } from './framework.module'
|
||||
import { routes as Routes } from './app.routes'
|
||||
import { util } from '../util'
|
||||
import { AppComponent } from './app.component'
|
||||
import { AComponent } from "./a/a";
|
||||
import { BComponent } from "./b/b";
|
||||
import { Router } from '../router'
|
||||
|
||||
/**
|
||||
* 应用根模块
|
||||
@@ -14,7 +14,7 @@ import { Router } from '../router'
|
||||
AppComponent, AComponent, BComponent
|
||||
],
|
||||
imports: [
|
||||
FrameworkModule, Router
|
||||
FrameworkModule, Routes
|
||||
],
|
||||
providers: [],
|
||||
bootstrap: [AppComponent]
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { ModuleWithProviders } from '@angular/core'
|
||||
import { RouterModule, Routes } from '@angular/router'
|
||||
import { AComponent } from "./a/a";
|
||||
import { BComponent } from "./b/b";
|
||||
|
||||
//路由配置
|
||||
let config: Routes = [
|
||||
{ path: 'a', component: AComponent },
|
||||
{ path: 'b', component: BComponent },
|
||||
{ path: '**', redirectTo: '/' }
|
||||
];
|
||||
|
||||
//导出路由
|
||||
export let routes: ModuleWithProviders = RouterModule.forRoot(config);
|
||||
@@ -1,13 +0,0 @@
|
||||
import { RouterModule } from '@angular/router'
|
||||
import { AComponent } from "./app/a/a";
|
||||
import { BComponent } from "./app/b/b";
|
||||
|
||||
//路由定义
|
||||
let router = RouterModule.forRoot([
|
||||
{ path: 'a', component: AComponent },
|
||||
{ path: 'b', component: BComponent },
|
||||
{ path: '**', redirectTo: '/' }
|
||||
]);
|
||||
|
||||
//导出路由
|
||||
export { router as Router }
|
||||
@@ -8,23 +8,33 @@ import { Observable } from "rxjs";
|
||||
export class HttpHelper {
|
||||
/**
|
||||
* get请求
|
||||
* @param url
|
||||
* @param url 请求地址
|
||||
*/
|
||||
public static get(url: string): HttpHandler {
|
||||
let response = ioc.get(Http).get(url);
|
||||
return new HttpHandler(response);
|
||||
public static get(url: string): HttpRequest {
|
||||
return new HttpRequest(url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Http响应处理器
|
||||
* Http请求操作
|
||||
*/
|
||||
export class HttpHandler {
|
||||
class HttpRequest {
|
||||
private _headers;
|
||||
|
||||
private _response: Observable<Response>;
|
||||
|
||||
/**
|
||||
* Http响应处理器
|
||||
* @param response 响应观察者
|
||||
* 初始化Http请求操作
|
||||
* @param url 请求地址
|
||||
*/
|
||||
constructor(private response: Observable<Response>) {
|
||||
constructor(private url: string) {
|
||||
this._headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' });
|
||||
}
|
||||
|
||||
private request() {
|
||||
if (this._response)
|
||||
return;
|
||||
this._response = ioc.get(Http).get(this.url, { headers: this._headers});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,6 +42,17 @@ export class HttpHandler {
|
||||
* @param handler 响应处理函数
|
||||
*/
|
||||
public handle(handler: (value: Response) => void) {
|
||||
this.response.subscribe(handler);
|
||||
this.request();
|
||||
this._response.subscribe(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Http内容类型
|
||||
*/
|
||||
export enum HttpContentType {
|
||||
/**
|
||||
* application/json
|
||||
*/
|
||||
Json
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
//============== 公共操作库 =================
|
||||
//Copyright 2017 何镇汐
|
||||
//Licensed under the MIT license
|
||||
//============================================
|
||||
import { UUID } from './uuid'
|
||||
|
||||
/**
|
||||
* 是否未定义
|
||||
@@ -78,4 +75,11 @@ export let isArray = (value): boolean => {
|
||||
*/
|
||||
export let isEmptyArray = (value): boolean => {
|
||||
return isArray(value) && value.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建唯一标识
|
||||
*/
|
||||
export let uuid = (): string => {
|
||||
return UUID.UUID();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//À´Ô´£ºhttps://github.com/wulfsolter/angular2-uuid/blob/master/src/uuid.ts
|
||||
"use strict";
|
||||
|
||||
export class UUID {
|
||||
|
||||
public static UUID(): string {
|
||||
if (typeof (window) !== "undefined" && typeof (window.crypto) !== "undefined" && typeof (window.crypto.getRandomValues) !== "undefined") {
|
||||
// If we have a cryptographically secure PRNG, use that
|
||||
// http://stackoverflow.com/questions/6906916/collisions-when-generating-uuids-in-javascript
|
||||
let buf: Uint16Array = new Uint16Array(8);
|
||||
window.crypto.getRandomValues(buf);
|
||||
return (this.pad4(buf[0]) + this.pad4(buf[1]) + "-" + this.pad4(buf[2]) + "-" + this.pad4(buf[3]) + "-" + this.pad4(buf[4]) + "-" + this.pad4(buf[5]) + this.pad4(buf[6]) + this.pad4(buf[7]));
|
||||
} else {
|
||||
// Otherwise, just use Math.random
|
||||
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
|
||||
// https://stackoverflow.com/questions/11605068/why-does-jshint-argue-against-bitwise-operators-how-should-i-express-this-code
|
||||
return this.random4() + this.random4() + "-" + this.random4() + "-" + this.random4() + "-" +
|
||||
this.random4() + "-" + this.random4() + this.random4() + this.random4();
|
||||
}
|
||||
}
|
||||
|
||||
private static pad4(num: number): string {
|
||||
let ret: string = num.toString(16);
|
||||
while (ret.length < 4) {
|
||||
ret = "0" + ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static random4(): string {
|
||||
return Math.floor((1 + Math.random()) * 0x10000)
|
||||
.toString(16)
|
||||
.substring(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,10 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Debug调试模式打包第三方框架 -->
|
||||
<Target Name="Debug_RunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
|
||||
<!-- 检测node js是否安装 -->
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Error";
|
||||
}
|
||||
|
||||
<h1 class="text-danger">Error.</h1>
|
||||
<h2 class="text-danger">An error occurred while processing your request.</h2>
|
||||
|
||||
|
||||
|
||||
<h3>Development Mode</h3>
|
||||
<p>
|
||||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
|
||||
</p>
|
||||
<p>
|
||||
<strong>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
|
||||
</p>
|
||||
Reference in New Issue
Block a user