p2p开户模块功能实现

开户模块简介

p2p平台-》点击开户-》填写信息(身份证,银行卡采用百度的图片文字识别)-》同意协议(来自数据库)-》点击提交–》在p2p平台表中存一分,把开户表中唯一id写入redis list队列中,返回资料已提交审核中–》任务接口:,从队列中取出id,根据id获取数据库信息-》去调用银行接口-》回调p2p平台-》p2p平台更新开户表状态

百度智能云文字识别

百度智能云平台有文字识别系统 可以识别图片 身份证 银行卡等信息
创建一个百度账号 登录到百度智能云 产品服务》人工智能》文字识别
创建一个应用 领取文字识别模块进行测试

上传图片百度云文字识别

在创建好的controller文件夹创建open_controller.go文件中编写
上传图片 保存到本地static文件里 返回路径名
鉴权认证机制,鉴权接口解析出的的access_token
获取access_token需要两个参数 分别是应用的API Key和应用的Secret Key
文字识别接口
基于业界领先的深度学习技术,提供多场景、多语种、高精度的整图文字检测和识别服务
需要access_token,通过API Key和Secret Key获取的access_token。
p2p开户模块功能实现_第1张图片

package controller

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"myproject/model"
	"myproject/utils"
	"net/http"
	"net/url"
	"strings"
)
func Open(openGrp *gin.RouterGroup) {
	openGrp.Use().GET("/zinentiqu", zinentiqu)
	openGrp.Use().GET("/wenzi", Mywenzi)
	openGrp.Use().POST("/upload", upload)
	openGrp.Use().POST("/open", OpenBankViews)
}
// 上传文件
func upload(c *gin.Context) {
	f, _ := c.FormFile("file")
	// 生成文件
	img := "static/" + f.Filename
	err := c.SaveUploadedFile(f, img)
	if err != nil {
		c.JSON(200, gin.H{"code": "10021", "msg": "保存失败"})
		return
	}
	c.JSON(200, gin.H{"code": "200", "msg": "保存成功", "img": img})

}

func zinentiqu(c *gin.Context) {
	var host = "https://aip.baidubce.com/oauth/2.0/token"
	var param = map[string]string{
		"grant_type":    "client_credentials",
		"client_id":     "P9U6qt71Rg39P6htA4gAycTw",
		"client_secret": "VC9rwcBZYmOEmZ6063o98TWAwQlgauhF",
	}
	uri, err := url.Parse(host)
	if err != nil {
		fmt.Println(err)
	}
	query := uri.Query()
	for k, v := range param {
		query.Set(k, v)
	}
	uri.RawQuery = query.Encode()

	response, err := http.Get(uri.String())
	if err != nil {
		fmt.Println(err)
	}
	result, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println(err)
	}
	//fmt.Println(">>", string(result))
	// 将string转换为json数据
	var v interface{}
	json.Unmarshal([]byte(string(result)), &v)
	data := v.(map[string]interface{})

	c.JSON(200, gin.H{
		"code":         200,
		"access_token": data["access_token"],
	})
}

func Mywenzi(c *gin.Context) {
	var host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
	var accessToken = c.Query("accessToken")
	var img = c.Query("img")

	uri, err := url.Parse(host)
	if err != nil {
		fmt.Println(err)
	}
	query := uri.Query()
	query.Set("access_token", accessToken)
	uri.RawQuery = query.Encode()

	filebytes, err := ioutil.ReadFile(img)
	if err != nil {
		fmt.Println(err)
	}

	image := base64.StdEncoding.EncodeToString(filebytes)
	//fmt.Println("===", image)
	sendBody := http.Request{}
	sendBody.ParseForm()
	sendBody.Form.Add("image", image)
	sendData := sendBody.Form.Encode()

	client := &http.Client{}
	request, err := http.NewRequest("POST", uri.String(), strings.NewReader(sendData))
	if err != nil {
		fmt.Println(err)
	}
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	response, err := client.Do(request)
	defer response.Body.Close()
	result, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(result))

	// 数据转换
	var v interface{}
	json.Unmarshal([]byte(string(result)), &v)
	data := v.(map[string]interface{})
	// 格式化数据
	var list2 []string
	for _, i := range data["words_result"].([]interface{}) {
		item := i.(map[string]interface{})
		for _, vm := range item {
			list2 = append(list2, vm.(string))
		}
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": list2,
	})
}

上传图片
p2p开户模块功能实现_第2张图片
生成Access token
p2p开户模块功能实现_第3张图片
文字识别
p2p开户模块功能实现_第4张图片

开户接口

在创建好的controller文件夹创建open_controller.go文件中编写
用户输入身份证 银行卡确认无误后进行开户 将信息添加到开户表

package controller

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"myproject/model"
	"myproject/utils"
	"net/http"
	"net/url"
	"strings"
)

func Open(openGrp *gin.RouterGroup) {
	openGrp.Use().GET("/zinentiqu", GetAcestoken)
	openGrp.Use().GET("/wenzi", Mywenzi)
	openGrp.Use().POST("/upload", upload)
	openGrp.Use().POST("/open", OpenBankViews)
}
...
// 添加开户表
func OpenBankViews(c *gin.Context) {
	var data = make(map[string]interface{})
	_ = c.ShouldBind(&data)

	var open_info = &model.OpenBank{
		Name:       fmt.Sprint(data["Name"]), //姓名
		CardNumber: fmt.Sprint(data["CardNumber"]), //身份证号
		BankCode:   fmt.Sprint(data["BankCode"]),// 银行卡号
		Mobile:     fmt.Sprint(data["Mobile"]),// 手机号
		Userid:     fmt.Sprint(data["Userid"]),// 用户id
	}
	db := model.GetDb()
	db.Create(open_info)

	// 添加到redis中
	openId := open_info.ID
	fmt.Println(">>>>>>>>>>>>", openId)
	if openId != 0 {
		r := utils.GetRedis()
		r.ListAdd("openuser", int(openId))
	}

	c.JSON(200, gin.H{"code": 200, "msg": "已经提交,等待审核"})
}

前端

vue测试效果演示

vue代码参考

<template>
  <div>
    <div>
      <van-nav-bar
        title="开通村管"
        left-text=""
        left-arrow
        @click-left="onClickLeft"
      />
    div>
    
    <font color="aaaaaa" size=2>您正在使用招商银行开户功能font>
    <div class="groupIndex">
      <div>
        <van-cell-group inset>
          <van-field
            v-model="otype"
            label="身份类型"
            placeholder="请输入身份类型"
          />
        van-cell-group>
      div>
      
      <div>
        <van-uploader class="uploadIndex" :after-read="afterRead" />
      div>
    div>
    
    <div>
      <van-cell-group inset>
        <van-field
          v-model="name"
          label="客户姓名"
          placeholder="请输入客户姓名"
        />
      van-cell-group>
    div>
    
    <div>
      <van-cell-group inset>
        <van-field v-model="code" label="证件号" placeholder="请输入证件号" />
      van-cell-group>
    div>
    
    <div>
      <font color="aaaaaa" size=2 
        >温馨提示:若银行卡位二类账号,将会影响提现,具体账号类型可咨询发卡行font
      >
    div>
    
    <div>
      <van-cell-group inset>
        <van-field
          v-model="number"
          label="银行卡号"
          placeholder="请输入本人储蕃卡卡号"
        />
      van-cell-group>
    div>
    
    <div>
      <van-cell-group inset>
        <van-field
          v-model="mobile"
          label="手机号"
          placeholder="请输入银行预留手机号"
        />
      van-cell-group>
    div>
    
    <br /><br />
    <div>
      <van-button @click="OpenBankViews" round block type="info">
        确认协议并注册
      van-button>
    div>
  div>
template>
<script>
export default {
  name: "",
  data() {
    return {
      otype: "身份证",
      img: "",
      access_token: "",
      name: "",
      code: "",
      number: "",
      mobile: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.push("/");
    },
    // 上传图片
    afterRead(file) {
      var formData = new FormData();
      formData.append("file", file.file);

      console.log(">>>", file.file.name, formData);
      this.axios.post("/upload", formData).then((res) => {
        console.log(">>>", res);
        this.img = res.data.img;
        this.tokenUpolad();
      });
    },
    // 获取上传文件的token
    tokenUpolad() {
      this.axios.get("/zinentiqu").then((res) => {
        this.access_token = res.data.access_token;
        console.log("access_token:", this.access_token);
        console.log("img", this.img);
        this.dataSiedStr();
      });
    },
    // 文字识别
    dataSiedStr() {
      this.axios
        .get("/wenzi", {
          params: {
            accessToken: this.access_token,
            img: this.img,
          },
        })
        .then((res) => {
          console.log("生成文字的响应: ", res);
          this.name = String(res.data.data[0]).substring(2);
          var counts = String(res.data.data[6]).length;
          console.log(">>>", counts);
          if (counts > 18) {
            this.code = String(res.data.data[6]).substring(6);
          } else {
            this.code = res.data.data[6];
          }
        });
    },
    // 开户用户《添加开户》 
    OpenBankViews() {
      this.axios.post("/open", {
        Name: this.name,
        CardNumber: this.code,
        BankCode: this.number,
        Mobile: this.mobile,
        Userid: localStorage.userid
      }).then(res =>{
        console.log("开户用户《添加开户》的响应:", res)
        this.$message({
            type: "success",
            message: res.data.msg
        })
        this.$router.push("/")
      });
    },
  },
};
script>

<style scoped >
* {
  margin: 0;
}
.top100 {
  margin: 15px;
}
.groupIndex {
  display: flex;
}
style>

你可能感兴趣的:(p2p,网络协议,网络)