qian.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }
  153. // OrganizationInfo 签署企业信息
  154. type OrganizationInfo struct {
  155. Name string `json:"name"` // 企业签署方工商营业执照上的企业名称
  156. }
  157. // CreateFlowByTemplateDirectlyReq 通过合同模板名称直接发起签署流程请求
  158. type CreateFlowByTemplateDirectlyReq struct {
  159. UserESignRecordID uint64 `json:"userESignRecordID" binding:"required"` // 用户电子签记录表ID 只有当前状态是1和4的电子签记录才能发起合同签署
  160. UserType int `json:"userType" binding:"required"` // 用户类型 1-个人 2-企业
  161. PersonInfo *PersonInfo `json:"personInfo"` // 签署人信息,用户类型为个人时必填
  162. OrganizationInfo *OrganizationInfo `json:"organizationInfo"` // 签署企业信息,用户类型为企业时必填
  163. }
  164. // CreateFlowByTemplateDirectlyRsp 通过合同模板名称直接发起签署流程回复
  165. type CreateFlowByTemplateDirectlyRsp struct {
  166. FlowId string `json:"flowId"` // 流程编号
  167. SignUrl string `json:"signUrl"` // 合同签署小程序URL
  168. }
  169. // CreateFlowByTemplateDirectly 通过合同模板名称直接发起签署流程
  170. // @Summary 通过合同模板名称直接发起签署流程
  171. // @Security ApiKeyAuth
  172. // @accept application/json
  173. // @Produce application/json
  174. // @Param data body CreateFlowByTemplateDirectlyReq true "入参"
  175. // @Success 200 {object} app.Response{Data=CreateFlowByTemplateDirectlyRsp} "出参"
  176. // @Failure 500 {object} app.Response
  177. // @Router /Tencent/CreateFlowByTemplateDirectly [post]
  178. // @Tags 腾讯电子签
  179. func CreateFlowByTemplateDirectly(c *gin.Context) {
  180. appG := app.Gin{C: c}
  181. // 获取请求参数
  182. var req CreateFlowByTemplateDirectlyReq
  183. if err := appG.C.ShouldBindJSON(&req); err != nil {
  184. logger.GetLogger().Errorf("CreateFlowByTemplateDirectly failed: %s", err.Error())
  185. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  186. return
  187. }
  188. // 进阶判断请求参数
  189. if req.UserType == 1 {
  190. if req.PersonInfo == nil {
  191. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少PersonInfo", nil)
  192. return
  193. }
  194. }
  195. if req.UserType == 2 {
  196. if req.OrganizationInfo == nil {
  197. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少OrganizationInfo", nil)
  198. return
  199. }
  200. }
  201. // 获取电子签信息表记录
  202. record, err := models.GetUseresignRecord(req.UserESignRecordID)
  203. if err != nil {
  204. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "获取电子签信息失败", nil)
  205. return
  206. }
  207. // 确认电子签信息状态
  208. // 记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝
  209. if record.RECORDSTATUS != 1 && record.RECORDSTATUS != 4 {
  210. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "电子签信息状态异常", nil)
  211. return
  212. }
  213. // 构建电子签平台合同模板名称
  214. // 格式:类型标志_合同模板名称;例如 "P_风险提示书",个人为P企业为E
  215. flag := "P"
  216. if req.UserType == 2 {
  217. flag = "E"
  218. }
  219. tmplateName := fmt.Sprintf("%s_%s", flag, record.TEMPLATENAME)
  220. personName := ""
  221. personMobile := ""
  222. idCardNumber := ""
  223. if req.PersonInfo != nil {
  224. personName = req.PersonInfo.Name
  225. personMobile = req.PersonInfo.Mobile
  226. idCardNumber = req.PersonInfo.IdCardNumber
  227. }
  228. organizationName := ""
  229. if req.OrganizationInfo != nil {
  230. organizationName = req.OrganizationInfo.Name
  231. }
  232. flowId, signUrl, err := tencent.CreateFlowByTemplateDirectly(tmplateName, req.UserType, personName, personMobile, idCardNumber, organizationName, record)
  233. if err == nil {
  234. appG.Response(http.StatusOK, e.SUCCESS, CreateFlowByTemplateDirectlyRsp{
  235. FlowId: flowId,
  236. SignUrl: signUrl,
  237. })
  238. } else {
  239. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, err.Error(), nil)
  240. }
  241. }
  242. type GetFlowStatusReq struct {
  243. ContractNo string `form:"contractno" binding:"required"` // 合同编号
  244. }
  245. // GetFlowStatus 获取合同状态
  246. // @Summary 获取合同状态
  247. // @Produce json
  248. // @Security ApiKeyAuth
  249. // @accept application/json
  250. // @Param contractno query string true "合同编号"
  251. // @Success 200 {object} int "记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝"
  252. // @Failure 500 {object} app.Response
  253. // @Router /Tencent/GetFlowStatus [get]
  254. // @Tags 腾讯电子签
  255. func GetFlowStatus(c *gin.Context) {
  256. appG := app.Gin{C: c}
  257. // 获取请求参数
  258. var req GetFlowStatusReq
  259. if err := appG.C.ShouldBindQuery(&req); err != nil {
  260. logger.GetLogger().Errorf("GetFlowStatus failed: %s", err.Error())
  261. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  262. return
  263. }
  264. if recordStatus, err := tencent.GetFlowStatus(req.ContractNo); err == nil {
  265. appG.Response(http.StatusOK, e.SUCCESS, recordStatus)
  266. } else {
  267. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  268. }
  269. }