qian.go 11 KB

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