SmartMurphytt/chainmaker_go/controller/rightConfirmationController.go

197 lines
5.2 KiB
Go

package controller
import (
"chainmaker.org/chainmaker/pb-go/v2/common"
sdk "chainmaker.org/chainmaker/sdk-go/v2"
"chainmaker.org/chainmaker/sdk-go/v2/examples"
"chainmaker_go/database"
"chainmaker_go/model"
"fmt"
"github.com/gin-gonic/gin"
"log"
"strconv"
)
func RightConfirmation(ctx *gin.Context) {
DB := database.GetDB()
cookie, err := ctx.Cookie("username")
if err != nil {
cookie = "NotSet"
}
fmt.Printf("Cookie value: %s \n", cookie)
//获取参数
user_id = cookie
//查询该用户所有资源
var rightConfirm []model.RightConfirmation
DB.Where("Owner_userid = ? and Is_right_confirmation = ?", user_id, "否").Find(&rightConfirm)
log.Println(rightConfirm)
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"msg": "登录成功",
"data": rightConfirm,
})
}
func RightConfirmationRow(ctx *gin.Context) {
DB := database.GetDB()
cookie, err := ctx.Cookie("username")
if err != nil {
cookie = "NotSet"
}
fmt.Printf("Cookie value: %s \n", cookie)
//获取参数
user_id = cookie
//获取参数
var requestRow = model.Row{}
ctx.Bind(&requestRow)
id := requestRow.Id
int_id, err := strconv.Atoi(id)
if err != nil {
fmt.Println("error")
}
//查询该用户所有资源
var rightConfirms []model.RightConfirmation
DB.Where("Owner_userid = ? and Is_right_confirmation = ?", user_id, "否").Find(&rightConfirms)
var rightConfirm model.RightConfirmation
rightConfirm = rightConfirms[int_id]
var rightConfirmationYes model.RightConfirmationYes
rightConfirmationYes.Right_type = rightConfirm.Right_type
rightConfirmationYes.Resource_uniqueid = rightConfirm.Resource_uniqueid
rightConfirmationYes.User_id = rightConfirm.Owner_userid
rightConfirmationYes.Time = ""
rightConfirmationYes.Data_sign = ""
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"msg": "登录成功",
"data": rightConfirmationYes,
})
}
func RightConfirmationYes(ctx *gin.Context) {
DB := database.GetDB()
//获取参数
var requestRightConfirm = model.RightConfirmationYes{}
ctx.Bind(&requestRightConfirm)
//获取参数
user_id = requestRightConfirm.User_id
right_type = requestRightConfirm.Right_type
resource_id = requestRightConfirm.Resource_uniqueid
time = requestRightConfirm.Time
//data_sign := requestRightConfirm.Data_sign
//将该资源的is_right_confirmation设为"是"
/* var resource model.Resource
DB.Model(&resource).Find(&resource, "user_id = ? and right_type = ? and resource_uniqueid = ?",
user_id, right_type, resource_uniqueid).UpdateColumn("Is_right_confirmation", "是")*/
//将该资源从待确权列表中删除
var rightConfirm model.RightConfirmation
DB.Model(&rightConfirm).Find(&rightConfirm, "right_type = ? and resource_uniqueid = ?",
right_type, resource_id).UpdateColumn("Is_right_confirmation", "是")
var resource model.Resource
DB.Where("resource_uniqueid = ?", resource_id).First(&resource)
newResource := model.Resource{
User_id: user_id,
Right_type: right_type,
Resource_name: resource.Resource_name,
Resource_describe: resource.Resource_describe,
Resource_data_hash: resource.Resource_data_hash,
Resource_refer: resource.Resource_refer,
Resource_root: resource.Resource_root,
Time: time,
Data_sign: "",
Resource_uniqueid: resource_id,
Is_right_confirmation: "是",
}
DB.Create(&newResource)
//注册信息上链
fmt.Println("====================== create client ======================")
client, err := examples.CreateChainClientWithSDKConf(sdkConfigOrg1Client1Path)
if err != nil {
log.Fatalln(err)
}
fmt.Println("====================== 调用合约 ======================")
err = testUserContractClaimInvoke2(client, "invoke_contract", true)
if err != nil {
log.Fatalln(err)
}
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"msg": "确权成功",
})
}
func testUserContractClaimInvoke2(client *sdk.ChainClient,
method string, withSyncResult bool) error {
kvs := []*common.KeyValuePair{
{
Key: "method",
Value: []byte("confirm_right"),
},
{
Key: "user_id",
Value: []byte(user_id),
},
{
Key: "resource_id",
Value: []byte(resource_id),
},
{
Key: "right_type",
Value: []byte(right_type),
},
{
Key: "time",
Value: []byte(time),
},
}
err := invokeUserContract2(client, claimContractName1, method, "", kvs, withSyncResult, &common.Limit{GasLimit: 200000})
if err != nil {
return err
}
return nil
}
func invokeUserContract2(client *sdk.ChainClient, contractName, method, txId string,
kvs []*common.KeyValuePair, withSyncResult bool, limit *common.Limit) error {
resp, err := client.InvokeContractWithLimit(contractName, method, txId, kvs, -1, withSyncResult, limit)
if err != nil {
return err
}
if resp.Code != common.TxStatusCode_SUCCESS {
return fmt.Errorf("invoke contract failed, [code:%d]/[msg:%s]\n", resp.Code, resp.Message)
}
if !withSyncResult {
fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[txId:%s]\n", resp.Code, resp.Message, resp.ContractResult.Result)
} else {
fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[contractResult:%s]\n", resp.Code, resp.Message, resp.ContractResult)
}
return nil
}