区块链小程序安装教程 区块链设计教程

 网络   2022-10-26 21:18   43
channelId beego.AppConfig.String( channel_id_gaj ) chainCodeName beego.AppConfig.String( chaincode_id_auth ) userId beego.AppConfig.String( user_id ) ccs,err : models.Initialize(channelId,chainCodeName,userId) if err ! nil{ resp[ errno ] utils.RECODE_DATAERR resp[ errmsg ] utils.RECODE_DATAERR return defer ccs.Close() args : [][]byte{[]byte(name.(string)),[]byte(id.(string))} response,err : ccs.ChainCodeQuery( check ,args) if err ! nil{ resp[ errno ] utils.RECODE_DATAERR resp[ errmsg ] utils.RECODE_DATAERR return beego.Info( response: ,response)

测试

证书下载及解压配置文件下载及解压Host文件改动app.conf 配置文件改动 参考test项目 告竣key以及value的同时修盖 经过欣赏器摹拟get恳求 前端集体信息认证 !DOCTYPE html html lang en head meta charset UTF-8 title 集体认证 /title script src vue.js /script script src vue-resource.js /script /head body div id app table td 姓名: /td td input type text v-model name /td /tr td 身份证: /td td input type text v-model id /td /tr /table button v-on:click check 认证 /button /div /body script var vm new Vue({ el: #app , data:{ name: , id: methods:{ check:function () { var url http://localhost:8080/auth var params { name :this.name, id :this.id} this.$http.get( url, {params:params}, {emulateJSON:true} ).then( function (response) { console.log(response) function (response) { console.log(response) /script /html 后端上传 CSV文件操作

写操作

func main() { file, _ : os.OpenFile( test.csv , os.O_WRONLY|os.O_CREATE, os.ModePerm) w : csv.NewWriter(file) w.Write([]string{ 123 , 234234 , 345345 , 234234 }) w.Write([]string{ 123 , 234234 , 345345 , 234234 }) w.Flush() file.Close()

读操作

func main() { file, _ : os.Open( test.csv ) r : csv.NewReader(file) for { strs, err : r.Read() if err io.EOF{ break if err ! nil{ beego.Error(err) for _, str : range strs { fmt.Print(str, \t ) fmt.Println() CSV文件上传

猎取上传文件,遗失到要地,经过义务办理文件实质写入区块链。

//上传文件func (this *AuthController) RecordAuth() { beego.Debug( receive file ) //file 这是一个key值 对于应上传Form中的name信息 f, h, e : this.GetFile( file ) if e ! nil { // 假设呈现极度了,f实质为nil handleResponse(this.Ctx.ResponseWriter, 400, e.Error()) return //合拢上传的文件 没有然的话会呈现且自文件没有能消除的状况 defer f.Close() //失去文件的称号 fileName : h.Filename fmt.Println( 文件称号: ) fmt.Println(fileName) //遗失文件到指定的位置 //static/uploadfile,这个是文件的地方 第一个static前方没有要有/ err : this.SaveToFile( file , path.Join( static/uploadfile , fileName)) if err ! nil { handleResponse(this.Ctx.ResponseWriter, 500, err.Error()) return handleResponse(this.Ctx.ResponseWriter, 200, ok )

开放异步义务

//上传文件func (this *AuthController) RecordAuth() { beego.Debug( receive file ) //file 这是一个key值 对于应上传Form中的name信息 f, h, e : this.GetFile( auth ) if e ! nil { // 假设呈现极度了,f实质为nil handleResponse(this.Ctx.ResponseWriter, 400, e.Error()) return //合拢上传的文件 没有然的话会呈现且自文件没有能消除的状况 defer f.Close() //失去文件的称号 fileName : h.Filename fmt.Println( 文件称号: ) fmt.Println(fileName) //遗失文件到指定的位置 //static/uploadfile,这个是文件的地方 第一个static前方没有要有/ err : this.SaveToFile( file , path.Join( static/uploadfile , fileName)) if err ! nil { handleResponse(this.Ctx.ResponseWriter, 500, err.Error()) return // 开放义务 // 正在现在时光的5秒钟后开放 t : time.Now().Add(5 * time.Second) second : t.Second() minute : t.Minute() hour : t.Hour() spec : fmt.Sprintf( %d %d %d * * * , second, minute, hour) tk : toolbox.NewTask( myTask , spec, func() error { beego.Info( start task ) // 当咱们的义务施行告竣后,休止 defer toolbox.StopTask() return myTask(fileName) // 讲义务推广到容器 toolbox.AddTask( myTask , tk) // 开放义务施行 toolbox.StartTask() handleResponse(this.Ctx.ResponseWriter, 200, ok )

myTast实质

func myTask(fileName string) error{ rf, _ : os.Open(path.Join( static/uploadfile , fileName)) r : csv.NewReader(rf) for { strs, err : r.Read() if err io.EOF { break if err ! nil { beego.Error(err) for _, str : range strs { fmt.Print(str, \t ) fmt.Println() return nil

纪录信息到区块

func myTask(fileName string) error { // 读文件 // 写数据到区块 var ( channelId beego.AppConfig.String( channel_id_gaj ) chainCodeId beego.AppConfig.String( chaincode_id_auth ) userId beego.AppConfig.String( user_id ) ccs, e : models.Initialize(channelId, chainCodeId, userId) defer ccs.Close() if e ! nil { beego.Error(e.Error()) return e file, _ : os.Open(path.Join( static/upload , fileName)) reader : csv.NewReader(file) // 并没有停止,缘由:大概某行数据有课题,当并没有是一切的都有课题,只须要纪录有课题的行信息就也许 // 还有一种大概触及极度,某行数据的写区块堕落 // 纪录行数:大概会有多个行数据出课题,没有会利用行的字符串拼接办法 // 会定义一个字符串的数组,最平生成字符串时也许利用 , 施行宰割行数 var line 0 var lines []string for { line 1 linestr : strconv.Itoa(line) record, err : reader.Read() if err io.EOF { // 文件末尾 break if err ! nil { // 有极度须要处置 lines append(lines, linestr) continue if len(record) ! 3 { // 有极度须要处置 lines append(lines, linestr) continue var args [][]byte for _, str : range record { //fmt.Print(str, \t ) args append(args, []byte(str)) //fmt.Println() _, err ccs.ChainCodeUpdate( add , args) if err ! nil { // 有极度须要处置 lines append(lines, linestr) if len(lines) 0 { beego.Error( Error lines: , strings.Join(lines, , )) }else{ // 施行的全部顺遂 beego.Info( write data success ) return nil

区块链小程序安装教程 区块链设计教程

前端上传(代码)

!DOCTYPE html html lang en head meta charset UTF-8 title 认证办理 /title script src vue.js /script script src vue-resource.js /script style .app-con{ width: 500px; margin: 50px auto; /style /head body div id app class app-con img src images/timg.jpg alt 身份 上传认证文件: input type file change onUpload button v-on:click upload 上传 /button /div /body script var vm new Vue({ el: #app , data:{ formData:new FormData() methods:{ onUpload(e){ // 当上传文件实质变动时分 this.formData.append( auth ,e.target.files[0]) this.formData.append( type , csv ) upload:function () { // 上传文件 var url http://10.211.55.4:8080/ogaj this.$http.post(url,this.formData).then( function (repponse) { console.log(repponse) function (repponse) { console.log(repponse) /script /html

, linestr) } }

if len(lines) 0 { beego.Error( Error lines: , strings.Join(lines, , ))}else{ // 施行的全部顺遂 beego.Info( write data success )return nil

}

前端上传(代码) html !DOCTYPE html html lang en head meta charset UTF-8 title 认证办理 /title script src vue.js /script script src vue-resource.js /script style .app-con{ width: 500px; margin: 50px auto; /style /head body div id app class app-con img src images/timg.jpg alt 身份 上传认证文件: input type file change onUpload button v-on:click upload 上传 /button /div /body script var vm new Vue({ el: #app , data:{ formData:new FormData() methods:{ onUpload(e){ // 当上传文件实质变动时分 this.formData.append( auth ,e.target.files[0]) this.formData.append( type , csv ) upload:function () { // 上传文件 var url http://10.211.55.4:8080/ogaj this.$http.post(url,this.formData).then( function (repponse) { console.log(repponse) function (repponse) { console.log(repponse) /script /html 收起 进展全文
本文地址:http://yz.ziyouea.com/p/45433.html
版权声明:本站文章来自网络,如有违规侵权请联系我们下架。