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. "strconv"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // 腾讯电子签
  15. type QueryUsereSignRecordsReq struct {
  16. UserId int `form:"userId" binding:"required"` // 用户ID
  17. MemberUserId int `form:"memberUserId" binding:"required"` // 所属会员ID
  18. RecordId *int `form:"recordId"` // 记录ID
  19. TemplateConfigId *int `form:"templateConfigId"` // 模板配置ID
  20. Templatetype *int `form:"templatetype"` // 模板类型 - 1:实名认证 2:开户协议 3:日结算单 4:交易协议
  21. }
  22. // QueryUsereSignRecords 查询用户电子签记录表
  23. // @Summary 查询用户电子签记录表
  24. // @Produce json
  25. // @Security ApiKeyAuth
  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. type CreateConsoleLoginUrlReq struct {
  85. AreaUserId int `form:"areaUserId" binding:"required"` // 机构ID
  86. }
  87. type CreateConsoleLoginUrlRsp struct {
  88. ConsoleUrl string `json:"consoleUrl"` // 电子签控制台登录链接
  89. }
  90. // CreateConsoleLoginUrl 创建电子签控制台登录链接
  91. // @Summary 创建电子签控制台登录链接
  92. // @Produce json
  93. // @Security ApiKeyAuth
  94. // @accept application/json
  95. // @Success 200 {object} app.Response
  96. // @Failure 500 {object} app.Response
  97. // @Router /Tencent/CreateConsoleLoginUrl [post]
  98. // @Tags 腾讯电子签
  99. func CreateConsoleLoginUrl(c *gin.Context) {
  100. appG := app.Gin{C: c}
  101. // 获取请求参数
  102. var req CreateConsoleLoginUrlReq
  103. if err := appG.C.ShouldBindQuery(&req); err != nil {
  104. logger.GetLogger().Errorf("CreateConsoleLoginUrl failed: %s", err.Error())
  105. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  106. return
  107. }
  108. if response, err := tencent.CreateConsoleLoginUrl(req.AreaUserId); err != nil {
  109. unquotedStr, _ := strconv.Unquote(response.ToJsonString())
  110. appG.Response(http.StatusOK, e.SUCCESS, CreateConsoleLoginUrlRsp{ConsoleUrl: unquotedStr})
  111. } else {
  112. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, err.Error(), nil)
  113. }
  114. }
  115. type InitTencentESSReq struct {
  116. UserId int `json:"userId" binding:"required"` // 用户ID
  117. MemberUserId int `json:"memberUserId" binding:"required"` // 所属会员ID
  118. }
  119. // InitTencentESS 按用户ID和机构ID创建腾讯电子签业务信息
  120. // @Summary 按用户ID和机构ID创建腾讯电子签业务信息
  121. // @Produce json
  122. // @Security ApiKeyAuth
  123. // @accept application/json
  124. // @Param data body InitTencentESSReq true "入参"
  125. // @Failure 500 {object} app.Response
  126. // @Router /Tencent/InitTencentESS [post]
  127. // @Tags 腾讯电子签
  128. func InitTencentESS(c *gin.Context) {
  129. appG := app.Gin{C: c}
  130. // 获取请求参数
  131. var req InitTencentESSReq
  132. if err := appG.C.ShouldBindJSON(&req); err != nil {
  133. logger.GetLogger().Errorf("InitTencentESS failed: %s", err.Error())
  134. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  135. return
  136. }
  137. if err := tencent.InitTencentESS(req.UserId, req.MemberUserId); err == nil {
  138. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  139. } else {
  140. appG.Response(http.StatusBadRequest, e.ERROR, nil)
  141. }
  142. }
  143. // PersonInfo 签署人信息
  144. type PersonInfo struct {
  145. Name string `json:"name"` // 姓名
  146. Mobile string `json:"mobile"` // 手机号码
  147. IdCardNumber string `json:"idCardNumber"` // 身份证号码,目前只支持身份证
  148. }
  149. // OrganizationInfo 签署企业信息
  150. type OrganizationInfo struct {
  151. Name string `json:"name"` // 企业签署方工商营业执照上的企业名称
  152. }
  153. // CreateFlowByTemplateDirectlyReq 通过合同模板名称直接发起签署流程请求
  154. type CreateFlowByTemplateDirectlyReq struct {
  155. UserESignRecordID uint64 `json:"userESignRecordID" binding:"required"` // 用户电子签记录表ID 只有当前状态是1和4的电子签记录才能发起合同签署
  156. UserType int `json:"userType" binding:"required"` // 用户类型 1-个人 2-企业
  157. PersonInfo *PersonInfo `json:"personInfo"` // 签署人信息,用户类型为个人时必填
  158. OrganizationInfo *OrganizationInfo `json:"organizationInfo"` // 签署企业信息,用户类型为企业时必填
  159. }
  160. // CreateFlowByTemplateDirectlyRsp 通过合同模板名称直接发起签署流程回复
  161. type CreateFlowByTemplateDirectlyRsp struct {
  162. FlowId string `json:"flowId"` // 流程编号
  163. SignUrl string `json:"signUrl"` // 合同签署小程序URL
  164. }
  165. // CreateFlowByTemplateDirectly 通过合同模板名称直接发起签署流程
  166. // @Summary 通过合同模板名称直接发起签署流程
  167. // @Security ApiKeyAuth
  168. // @accept application/json
  169. // @Produce application/json
  170. // @Param data body CreateFlowByTemplateDirectlyReq true "入参"
  171. // @Success 200 {object} app.Response{Data=CreateFlowByTemplateDirectlyRsp} "出参"
  172. // @Failure 500 {object} app.Response
  173. // @Router /Tencent/CreateFlowByTemplateDirectly [post]
  174. // @Tags 腾讯电子签
  175. func CreateFlowByTemplateDirectly(c *gin.Context) {
  176. appG := app.Gin{C: c}
  177. // 获取请求参数
  178. var req CreateFlowByTemplateDirectlyReq
  179. if err := appG.C.ShouldBindJSON(&req); err != nil {
  180. logger.GetLogger().Errorf("CreateFlowByTemplateDirectly failed: %s", err.Error())
  181. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  182. return
  183. }
  184. // 进阶判断请求参数
  185. if req.UserType == 1 {
  186. if req.PersonInfo == nil {
  187. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少PersonInfo", nil)
  188. return
  189. }
  190. }
  191. if req.UserType == 2 {
  192. if req.OrganizationInfo == nil {
  193. appG.ResponseByMsg(http.StatusBadRequest, e.INVALID_PARAMS, "缺少OrganizationInfo", nil)
  194. return
  195. }
  196. }
  197. // 获取电子签信息表记录
  198. record, err := models.GetUseresignRecord(req.UserESignRecordID)
  199. if err != nil {
  200. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "获取电子签信息失败", nil)
  201. return
  202. }
  203. // 确认电子签信息状态
  204. // 记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝
  205. if record.RECORDSTATUS != 1 && record.RECORDSTATUS != 4 {
  206. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, "电子签信息状态异常", nil)
  207. return
  208. }
  209. // 构建电子签平台合同模板名称
  210. // 格式:合同模板名称_类型标志;例如 "风险提示书_P",个人为P企业为E
  211. flag := "P"
  212. if req.UserType == 2 {
  213. flag = "E"
  214. }
  215. tmplateName := fmt.Sprintf("%s_%s", record.TEMPLATENAME, flag)
  216. personName := ""
  217. personMobile := ""
  218. idCardNumber := ""
  219. if req.PersonInfo != nil {
  220. personName = req.PersonInfo.Name
  221. personMobile = req.PersonInfo.Mobile
  222. idCardNumber = req.PersonInfo.IdCardNumber
  223. }
  224. organizationName := ""
  225. if req.OrganizationInfo != nil {
  226. organizationName = req.OrganizationInfo.Name
  227. }
  228. flowId, signUrl, err := tencent.CreateFlowByTemplateDirectly(tmplateName, req.UserType, personName, personMobile, idCardNumber, organizationName, record)
  229. if err == nil {
  230. appG.Response(http.StatusOK, e.SUCCESS, CreateFlowByTemplateDirectlyRsp{
  231. FlowId: flowId,
  232. SignUrl: signUrl,
  233. })
  234. } else {
  235. appG.ResponseByMsg(http.StatusBadRequest, e.ERROR, err.Error(), nil)
  236. }
  237. }
  238. type GetFlowStatusReq struct {
  239. ContractNo string `form:"contractno" binding:"required"` // 合同编号
  240. AreaUserId int `form:"areaUserId" binding:"required"` // 机构ID
  241. }
  242. // GetFlowStatus 获取合同状态
  243. // @Summary 获取合同状态
  244. // @Produce json
  245. // @Security ApiKeyAuth
  246. // @accept application/json
  247. // @Param contractno query string true "合同编号"
  248. // @Param areaUserId query int true "机构ID"
  249. // @Success 200 {object} int "记录状态 - 1:未签署 2:签署中 3:已签署 4:签署拒绝"
  250. // @Failure 500 {object} app.Response
  251. // @Router /Tencent/GetFlowStatus [get]
  252. // @Tags 腾讯电子签
  253. func GetFlowStatus(c *gin.Context) {
  254. appG := app.Gin{C: c}
  255. // 获取请求参数
  256. var req GetFlowStatusReq
  257. if err := appG.C.ShouldBindQuery(&req); err != nil {
  258. logger.GetLogger().Errorf("GetFlowStatus failed: %s", err.Error())
  259. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  260. return
  261. }
  262. if recordStatus, err := tencent.GetFlowStatus(req.ContractNo, req.AreaUserId); err == nil {
  263. appG.Response(http.StatusOK, e.SUCCESS, recordStatus)
  264. } else {
  265. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  266. }
  267. }