qian.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. "strconv"
  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. signFromHeader := appG.C.Request.Header.Get("Content-Signature")
  71. if signFromHeader != "" {
  72. if tencent.VerifySign(payload, signFromHeader) {
  73. // 验签成功
  74. // 内容解密
  75. content, err := tencent.DecryptContent(payload)
  76. if err == nil {
  77. // 解密成功
  78. tencent.ProcessNotice(content)
  79. }
  80. }
  81. }
  82. }
  83. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  84. }
  85. // CreateConsoleLoginUrl 创建电子签控制台登录链接
  86. // @Summary 创建电子签控制台登录链接
  87. // @Produce json
  88. // @Security ApiKeyAuth
  89. // @accept application/json
  90. // @Success 200 {object} app.Response
  91. // @Failure 500 {object} app.Response
  92. // @Router /Tencent/CreateConsoleLoginUrl [post]
  93. // @Tags 腾讯电子签
  94. func CreateConsoleLoginUrl(c *gin.Context) {
  95. appG := app.Gin{C: c}
  96. agent := utils.SetAgent()
  97. proxyOrganizationName := "天津麦顿"
  98. response, _ := tencent.CreateConsoleLoginUrl(agent, proxyOrganizationName)
  99. appG.Response(http.StatusOK, e.SUCCESS, response.ToJsonString())
  100. }
  101. type InitTencentESSReq struct {
  102. UserId int `json:"userId" binding:"required"` // 用户ID
  103. MemberUserId int `json:"memberUserId" binding:"required"` // 所属会员ID
  104. }
  105. // InitTencentESS 按用户ID和机构ID创建腾讯电子签业务信息
  106. // @Summary 按用户ID和机构ID创建腾讯电子签业务信息
  107. // @Produce json
  108. // @Security ApiKeyAuth
  109. // @accept application/json
  110. // @Param data body InitTencentESSReq true "入参"
  111. // @Failure 500 {object} app.Response
  112. // @Router /Tencent/InitTencentESS [post]
  113. // @Tags 腾讯电子签
  114. func InitTencentESS(c *gin.Context) {
  115. appG := app.Gin{C: c}
  116. // 获取请求参数
  117. var req InitTencentESSReq
  118. if err := appG.C.ShouldBindJSON(&req); err != nil {
  119. logger.GetLogger().Errorf("InitTencentESS failed: %s", err.Error())
  120. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  121. return
  122. }
  123. if err := tencent.InitTencentESS(req.UserId, req.MemberUserId); err == nil {
  124. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  125. } else {
  126. appG.Response(http.StatusBadRequest, e.ERROR, nil)
  127. }
  128. }
  129. // PersonInfo 签署人信息
  130. type PersonInfo struct {
  131. Name string `json:"name"` // 姓名
  132. Mobile string `json:"mobile"` // 手机号码
  133. IdCardNumber string `json:"idCardNumber"` // 身份证号码,目前只支持身份证
  134. }
  135. // OrganizationInfo 签署企业信息
  136. type OrganizationInfo struct {
  137. Name string `json:"name"` // 企业签署方工商营业执照上的企业名称
  138. }
  139. // CreateFlowByTemplateDirectlyReq 通过合同模板名称直接发起签署流程请求
  140. type CreateFlowByTemplateDirectlyReq struct {
  141. UserESignRecordID uint64 `json:"userESignRecordID" binding:"required"` // 用户电子签记录表ID 只有当前状态是1和4的电子签记录才能发起合同签署
  142. UserType int `json:"userType" binding:"required"` // 用户类型 1-个人 2-企业
  143. PersonInfo *PersonInfo `json:"personInfo"` // 签署人信息,用户类型为个人时必填
  144. OrganizationInfo *OrganizationInfo `json:"organizationInfo"` // 签署企业信息,用户类型为企业时必填
  145. }
  146. // CreateFlowByTemplateDirectlyRsp 通过合同模板名称直接发起签署流程回复
  147. type CreateFlowByTemplateDirectlyRsp struct {
  148. FlowId string `json:"flowId"` // 流程编号
  149. SignUrl string `json:"signUrl"` // 合同签署小程序URL
  150. }
  151. // CreateFlowByTemplateDirectly 通过合同模板名称直接发起签署流程
  152. // @Summary 通过合同模板名称直接发起签署流程
  153. // @Security ApiKeyAuth
  154. // @accept application/json
  155. // @Produce application/json
  156. // @Param data body CreateFlowByTemplateDirectlyReq true "入参"
  157. // @Success 200 {object} app.Response{Data=CreateFlowByTemplateDirectlyRsp} "出参"
  158. // @Failure 500 {object} app.Response
  159. // @Router /Tencent/CreateFlowByTemplateDirectly [post]
  160. // @Tags 腾讯电子签
  161. func CreateFlowByTemplateDirectly(c *gin.Context) {
  162. appG := app.Gin{C: c}
  163. // 获取请求参数
  164. var req CreateFlowByTemplateDirectlyReq
  165. if err := appG.C.ShouldBindJSON(&req); err != nil {
  166. logger.GetLogger().Errorf("CreateFlowByTemplateDirectly failed: %s", err.Error())
  167. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  168. return
  169. }
  170. // 进阶判断请求参数
  171. if req.UserType == 1 {
  172. if req.PersonInfo == nil {
  173. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少PersonInfo", nil)
  174. return
  175. }
  176. }
  177. if req.UserType == 2 {
  178. if req.OrganizationInfo == nil {
  179. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少OrganizationInfo", nil)
  180. return
  181. }
  182. }
  183. // 获取电子签信息表记录
  184. record, err := models.GetUseresignRecord(req.UserESignRecordID)
  185. if err != nil {
  186. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "获取电子签信息失败", nil)
  187. return
  188. }
  189. // 确认电子签信息状态
  190. // 记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝
  191. if record.RECORDSTATUS != 1 && record.RECORDSTATUS != 4 {
  192. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "电子签信息状态异常", nil)
  193. return
  194. }
  195. // 构建电子签平台合同模板名称
  196. // 格式:所属会员ID_类型标志_合同模板名称;例如 "1000_P_风险提示书",个人为P企业为A
  197. flag := "P"
  198. if req.UserType == 2 {
  199. flag = "A"
  200. }
  201. tmplateName := fmt.Sprintf("%s_%s_%s", strconv.Itoa(int(record.AREAUSERID)), flag, record.TEMPLATENAME)
  202. personName := ""
  203. personMobile := ""
  204. idCardNumber := ""
  205. if req.PersonInfo != nil {
  206. personName = req.PersonInfo.Name
  207. personMobile = req.PersonInfo.Mobile
  208. idCardNumber = req.PersonInfo.IdCardNumber
  209. }
  210. organizationName := ""
  211. if req.OrganizationInfo != nil {
  212. organizationName = req.OrganizationInfo.Name
  213. }
  214. flowId, signUrl, err := tencent.CreateFlowByTemplateDirectly(tmplateName, req.UserType, personName, personMobile, idCardNumber, organizationName, record)
  215. if err == nil {
  216. appG.Response(http.StatusOK, e.SUCCESS, CreateFlowByTemplateDirectlyRsp{
  217. FlowId: flowId,
  218. SignUrl: signUrl,
  219. })
  220. } else {
  221. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, err.Error(), nil)
  222. }
  223. }
  224. type GetFlowStatusReq struct {
  225. ContractNo string `form:"contractno" binding:"required"` // 合同编号
  226. }
  227. // GetFlowStatus 获取合同状态
  228. // @Summary 获取合同状态
  229. // @Produce json
  230. // @Security ApiKeyAuth
  231. // @accept application/json
  232. // @Param contractno query string true "合同编号"
  233. // @Success 200 {object} int "记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝"
  234. // @Failure 500 {object} app.Response
  235. // @Router /Tencent/GetFlowStatus [get]
  236. // @Tags 腾讯电子签
  237. func GetFlowStatus(c *gin.Context) {
  238. appG := app.Gin{C: c}
  239. // 获取请求参数
  240. var req GetFlowStatusReq
  241. if err := appG.C.ShouldBindQuery(&req); err != nil {
  242. logger.GetLogger().Errorf("GetFlowStatus failed: %s", err.Error())
  243. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  244. return
  245. }
  246. if recordStatus, err := tencent.GetFlowStatus(req.ContractNo); err == nil {
  247. appG.Response(http.StatusOK, e.SUCCESS, recordStatus)
  248. } else {
  249. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  250. }
  251. }