qian.go 11 KB

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