qian.go 9.7 KB

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