qian.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package tencent
  2. import (
  3. "fmt"
  4. "io"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/logger"
  8. "mtp2_if/models"
  9. "mtp2_if/services/tencent"
  10. "mtp2_if/utils"
  11. "net/http"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // 腾讯电子签
  15. type QueryUsereSignRecordsReq struct {
  16. UserId int `form:"userId" binding:"required"` // 用户ID
  17. MemberUserId int `form:"memberUserId" binding:"required"` // 所属会员ID
  18. RecordId *int `form:"recordId"` // 记录ID
  19. TemplateConfigId *int `form:"templateConfigId"` // 模板配置ID
  20. Templatetype *int `form:"templatetype"` // 模板类型 - 1:实名认证 2:开户协议 3:日结算单 4:交易协议
  21. }
  22. // QueryUsereSignRecords 查询用户电子签记录表
  23. // @Summary 查询用户电子签记录表
  24. // @Produce json
  25. // @Security ApiKeyAuth
  26. // @accept application/json
  27. // @Param userId query int true "用户ID"
  28. // @Param memberUserId query int true "所属会员ID"
  29. // @Param recordId query int false "记录ID"
  30. // @Param templateConfigId query int false "模板配置ID"
  31. // @Param templatetype query int false "模板类型 - 1:实名认证 2:开户协议 3:日结算单 4:交易协议"
  32. // @Success 200 {array} models.Useresignrecord
  33. // @Failure 500 {object} app.Response
  34. // @Router /Tencent/QueryUsereSignRecords [get]
  35. // @Tags 腾讯电子签
  36. func QueryUsereSignRecords(c *gin.Context) {
  37. appG := app.Gin{C: c}
  38. // 获取请求参数
  39. var req QueryUsereSignRecordsReq
  40. if err := appG.C.ShouldBindQuery(&req); err != nil {
  41. logger.GetLogger().Errorf("QueryUsereSignRecords failed: %s", err.Error())
  42. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  43. return
  44. }
  45. if rsp, err := models.QueryUsereSignRecords(req.UserId, req.MemberUserId, req.RecordId, req.TemplateConfigId, req.Templatetype); err == nil {
  46. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  47. } else {
  48. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  49. }
  50. }
  51. // QianNotice 腾讯电子签回调接口
  52. // @Summary 腾讯电子签回调接口
  53. // @Produce json
  54. // @accept application/json
  55. // @Success 200 {object} app.Response
  56. // @Failure 500 {object} app.Response
  57. // @Router /Tencent/QianNotice [post]
  58. // @Tags 腾讯电子签
  59. func QianNotice(c *gin.Context) {
  60. appG := app.Gin{C: c}
  61. // 获取推送内容
  62. b, err := io.ReadAll(appG.C.Request.Body)
  63. if err != nil {
  64. return
  65. }
  66. payload := string(b)
  67. // 验签
  68. if payload != "" {
  69. signFromHeader := appG.C.Request.Header.Get("Content-Signature")
  70. if signFromHeader != "" {
  71. if tencent.VerifySign(payload, signFromHeader) {
  72. // 验签成功
  73. // 内容解密
  74. content, err := tencent.DecryptContent(payload)
  75. if err == nil {
  76. // 解密成功
  77. tencent.ProcessNotice(content)
  78. }
  79. }
  80. }
  81. }
  82. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  83. }
  84. // CreateConsoleLoginUrl 创建电子签控制台登录链接
  85. // @Summary 创建电子签控制台登录链接
  86. // @Produce json
  87. // @Security ApiKeyAuth
  88. // @accept application/json
  89. // @Success 200 {object} app.Response
  90. // @Failure 500 {object} app.Response
  91. // @Router /Tencent/CreateConsoleLoginUrl [post]
  92. // @Tags 腾讯电子签
  93. func CreateConsoleLoginUrl(c *gin.Context) {
  94. appG := app.Gin{C: c}
  95. agent := utils.SetAgent()
  96. proxyOrganizationName := "天津麦顿"
  97. response, _ := tencent.CreateConsoleLoginUrl(agent, proxyOrganizationName)
  98. appG.Response(http.StatusOK, e.SUCCESS, response.ToJsonString())
  99. }
  100. type InitTencentESSReq struct {
  101. UserId int `json:"userId" binding:"required"` // 用户ID
  102. MemberUserId int `json:"memberUserId" binding:"required"` // 所属会员ID
  103. }
  104. // InitTencentESS 按用户ID和机构ID创建腾讯电子签业务信息
  105. // @Summary 按用户ID和机构ID创建腾讯电子签业务信息
  106. // @Produce json
  107. // @Security ApiKeyAuth
  108. // @accept application/json
  109. // @Param data body InitTencentESSReq true "入参"
  110. // @Failure 500 {object} app.Response
  111. // @Router /Tencent/InitTencentESS [post]
  112. // @Tags 腾讯电子签
  113. func InitTencentESS(c *gin.Context) {
  114. appG := app.Gin{C: c}
  115. // 获取请求参数
  116. var req InitTencentESSReq
  117. if err := appG.C.ShouldBindJSON(&req); err != nil {
  118. logger.GetLogger().Errorf("InitTencentESS failed: %s", err.Error())
  119. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  120. return
  121. }
  122. if err := tencent.InitTencentESS(req.UserId, req.MemberUserId); err == nil {
  123. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  124. } else {
  125. appG.Response(http.StatusBadRequest, e.ERROR, nil)
  126. }
  127. }
  128. // InitMdUserSwapProtocol 按用户ID和机构ID创建机构交易协议申请信息
  129. // @Summary 按用户ID和机构ID创建机构交易协议申请信息
  130. // @Produce json
  131. // @Security ApiKeyAuth
  132. // @accept application/json
  133. // @Param data body InitTencentESSReq true "入参"
  134. // @Failure 500 {object} app.Response
  135. // @Router /Tencent/InitMdUserSwapProtocol [post]
  136. // @Tags 腾讯电子签
  137. func InitMdUserSwapProtocol(c *gin.Context) {
  138. appG := app.Gin{C: c}
  139. // 获取请求参数
  140. var req InitTencentESSReq
  141. if err := appG.C.ShouldBindJSON(&req); err != nil {
  142. logger.GetLogger().Errorf("InitTencentESS failed: %s", err.Error())
  143. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  144. return
  145. }
  146. if err := tencent.InitMdUserSwapProtocol(req.UserId, req.MemberUserId); err == nil {
  147. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  148. } else {
  149. appG.Response(http.StatusBadRequest, e.ERROR, nil)
  150. }
  151. }
  152. // PersonInfo 签署人信息
  153. type PersonInfo struct {
  154. Name string `json:"name"` // 姓名
  155. Mobile string `json:"mobile"` // 手机号码
  156. IdCardNumber string `json:"idCardNumber"` // 身份证号码,目前只支持身份证
  157. }
  158. // OrganizationInfo 签署企业信息
  159. type OrganizationInfo struct {
  160. Name string `json:"name"` // 企业签署方工商营业执照上的企业名称
  161. }
  162. // CreateFlowByTemplateDirectlyReq 通过合同模板名称直接发起签署流程请求
  163. type CreateFlowByTemplateDirectlyReq struct {
  164. UserESignRecordID uint64 `json:"userESignRecordID" binding:"required"` // 用户电子签记录表ID 只有当前状态是1和4的电子签记录才能发起合同签署
  165. UserType int `json:"userType" binding:"required"` // 用户类型 1-个人 2-企业
  166. PersonInfo *PersonInfo `json:"personInfo"` // 签署人信息,用户类型为个人时必填
  167. OrganizationInfo *OrganizationInfo `json:"organizationInfo"` // 签署企业信息,用户类型为企业时必填
  168. }
  169. // CreateFlowByTemplateDirectlyRsp 通过合同模板名称直接发起签署流程回复
  170. type CreateFlowByTemplateDirectlyRsp struct {
  171. FlowId string `json:"flowId"` // 流程编号
  172. SignUrl string `json:"signUrl"` // 合同签署小程序URL
  173. }
  174. // CreateFlowByTemplateDirectly 通过合同模板名称直接发起签署流程
  175. // @Summary 通过合同模板名称直接发起签署流程
  176. // @Security ApiKeyAuth
  177. // @accept application/json
  178. // @Produce application/json
  179. // @Param data body CreateFlowByTemplateDirectlyReq true "入参"
  180. // @Success 200 {object} app.Response{Data=CreateFlowByTemplateDirectlyRsp} "出参"
  181. // @Failure 500 {object} app.Response
  182. // @Router /Tencent/CreateFlowByTemplateDirectly [post]
  183. // @Tags 腾讯电子签
  184. func CreateFlowByTemplateDirectly(c *gin.Context) {
  185. appG := app.Gin{C: c}
  186. // 获取请求参数
  187. var req CreateFlowByTemplateDirectlyReq
  188. if err := appG.C.ShouldBindJSON(&req); err != nil {
  189. logger.GetLogger().Errorf("CreateFlowByTemplateDirectly failed: %s", err.Error())
  190. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  191. return
  192. }
  193. // 进阶判断请求参数
  194. if req.UserType == 1 {
  195. if req.PersonInfo == nil {
  196. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少PersonInfo", nil)
  197. return
  198. }
  199. }
  200. if req.UserType == 2 {
  201. if req.OrganizationInfo == nil {
  202. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少OrganizationInfo", nil)
  203. return
  204. }
  205. }
  206. // 获取电子签信息表记录
  207. record, err := models.GetUseresignRecord(req.UserESignRecordID)
  208. if err != nil {
  209. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "获取电子签信息失败", nil)
  210. return
  211. }
  212. // 确认电子签信息状态
  213. // 记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝
  214. if record.RECORDSTATUS != 1 && record.RECORDSTATUS != 4 {
  215. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "电子签信息状态异常", nil)
  216. return
  217. }
  218. // 构建电子签平台合同模板名称
  219. // 格式:类型标志_合同模板名称;例如 "P_风险提示书",个人为P企业为E
  220. flag := "P"
  221. if req.UserType == 2 {
  222. flag = "E"
  223. }
  224. tmplateName := fmt.Sprintf("%s_%s", flag, record.TEMPLATENAME)
  225. personName := ""
  226. personMobile := ""
  227. idCardNumber := ""
  228. if req.PersonInfo != nil {
  229. personName = req.PersonInfo.Name
  230. personMobile = req.PersonInfo.Mobile
  231. idCardNumber = req.PersonInfo.IdCardNumber
  232. }
  233. organizationName := ""
  234. if req.OrganizationInfo != nil {
  235. organizationName = req.OrganizationInfo.Name
  236. }
  237. flowId, signUrl, err := tencent.CreateFlowByTemplateDirectly(tmplateName, req.UserType, personName, personMobile, idCardNumber, organizationName, record)
  238. if err == nil {
  239. appG.Response(http.StatusOK, e.SUCCESS, CreateFlowByTemplateDirectlyRsp{
  240. FlowId: flowId,
  241. SignUrl: signUrl,
  242. })
  243. } else {
  244. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, err.Error(), nil)
  245. }
  246. }
  247. type GetFlowStatusReq struct {
  248. ContractNo string `form:"contractno" binding:"required"` // 合同编号
  249. }
  250. // GetFlowStatus 获取合同状态
  251. // @Summary 获取合同状态
  252. // @Produce json
  253. // @Security ApiKeyAuth
  254. // @accept application/json
  255. // @Param contractno query string true "合同编号"
  256. // @Success 200 {object} int "记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝"
  257. // @Failure 500 {object} app.Response
  258. // @Router /Tencent/GetFlowStatus [get]
  259. // @Tags 腾讯电子签
  260. func GetFlowStatus(c *gin.Context) {
  261. appG := app.Gin{C: c}
  262. // 获取请求参数
  263. var req GetFlowStatusReq
  264. if err := appG.C.ShouldBindQuery(&req); err != nil {
  265. logger.GetLogger().Errorf("GetFlowStatus failed: %s", err.Error())
  266. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  267. return
  268. }
  269. if recordStatus, err := tencent.GetFlowStatus(req.ContractNo); err == nil {
  270. appG.Response(http.StatusOK, e.SUCCESS, recordStatus)
  271. } else {
  272. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  273. }
  274. }