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