应用集成平台服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

573 lines
18 KiB

package workWechat
import (
"appPlatform/api/version1/user"
"appPlatform/middleware/grocerystore"
"appPlatform/models/modelshr"
"appPlatform/overall"
"appPlatform/overall/publicmethod"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
/*
*
@ 作者: 秦东
@ 时间: 2024-01-18 14:11:11
@ 功能: 获取token
@ 参数
#systemApp 系统
#key 身份KEy
#isAgain 重新授权 1:否,2:是
@ 返回值
#
@ 方法原型
#token token值
#err 状态
*/
func GainWechatToken(systemApp, key string, isAgain int) (token string, err error) {
companyId := overall.CONSTANT_CONFIG.WechatCompany.CompanyId
redisFileKey := fmt.Sprintf("Wechat:Token:%v_%v_%v", companyId, key, overall.CONSTANT_CONFIG.RedisPrefixStr.Alias)
var secretStr string
switch systemApp {
case "kpi":
redisFileKey = fmt.Sprintf("%v_%v_%v", redisFileKey, systemApp, overall.CONSTANT_CONFIG.WechatKpi.Agentid)
secretStr = overall.CONSTANT_CONFIG.WechatKpi.Secret
case "school":
redisFileKey = fmt.Sprintf("%v_%v_%v", redisFileKey, systemApp, overall.CONSTANT_CONFIG.WechatSchool.Agentid)
secretStr = overall.CONSTANT_CONFIG.WechatSchool.Secret
case "stzl":
redisFileKey = fmt.Sprintf("%v_%v_%v", redisFileKey, systemApp, overall.CONSTANT_CONFIG.ShuTongZhiLian.Agentid)
secretStr = overall.CONSTANT_CONFIG.ShuTongZhiLian.Secret
default:
redisFileKey = fmt.Sprintf("%v_%v", redisFileKey, systemApp)
}
redisClient := grocerystore.RunRedis(overall.CONSTANT_REDIS4) //设定redis库
if isAgain != 1 {
token, err = getWechatServer(companyId, secretStr)
if err != nil {
return
}
redisClient.SetRedisTime(7200)
redisClient.Set(redisFileKey, token)
} else {
isTrue, tokens := redisClient.Get(redisFileKey)
if isTrue && token != "" {
err = nil
token = tokens
return
} else {
token, err = getWechatServer(companyId, secretStr)
if err != nil {
return
}
redisClient.SetRedisTime(7200)
redisClient.Set(redisFileKey, token)
}
}
return
}
/*
*
@ 作者: 秦东
@ 时间: 2024-01-18 14:23:24
@ 功能: 获取微信Token(链接微信服务器)
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func getWechatServer(companyId, secretStr string) (token string, err error) {
getTokenUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%v&corpsecret=%v", companyId, secretStr)
// getTokenUrl := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + companyId + "&corpsecret=" + secretStr
tokenByte := publicmethod.CurlGet(getTokenUrl)
var callBackCont WeChatCallBack
err = json.Unmarshal(tokenByte, &callBackCont)
if err != nil {
return
}
if callBackCont.Errcode != 0 {
err = errors.New("未能获得到TOKEN!")
return
}
token = callBackCont.Accesstoken
return
}
/*
*
@ 作者: 秦东
@ 时间: 2024-01-24 11:30:15
@ 功能: 获取人员信息单页(手机)查看权限
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) LookOnePeopleArchives(c *gin.Context) {
var requestData LookOneMan
err := c.ShouldBindJSON(&requestData)
if err != nil {
publicmethod.Result(100, err, c)
return
}
var sendData SendPower
sendData.IsOk = 2
if requestData.SurveyMan == "" {
publicmethod.Result(0, sendData, c)
return
}
if requestData.UnSurveyMan == "" {
requestData.UnSurveyMan = requestData.SurveyMan
}
var surverManInfo modelshr.ManCont
err = surverManInfo.GetCont(map[string]interface{}{"`number`": requestData.SurveyMan}, "`emp_type`", "`company`", "`maindeparment`", "`admin_org`", "`position`", "`position`", "`role`")
if err != nil {
publicmethod.Result(0, sendData, c)
return
}
var roleList []string
if surverManInfo.Role != "" {
roleList = strings.Split(surverManInfo.Role, ",")
}
// fmt.Printf("人员信息--->%v\n", surverManInfo)
//获取权限
var orgList []string
sendData.Purview, _, orgList, sendData.Level = user.GetUserPower("appsystem", surverManInfo.Position, roleList)
// fmt.Printf("人员信息--->Purview:%v--->orgList:%v--->Level:%v\n", sendData.Purview, orgList, sendData.Level)
if publicmethod.IsInTrue[string]("210700510225772544", sendData.Purview) {
//获取被查看人行政组织
var lookManInfo modelshr.PersonArchives
lookManInfo.GetCont(map[string]interface{}{"`number`": requestData.UnSurveyMan}, "`company`", "`maindeparment`", "`admin_org`")
switch sendData.Level {
case 1:
var sunOrg publicmethod.GetOrgAllParent
sunOrg.GetOrgSun(surverManInfo.AdminOrg)
sunOrg.Id = append(sunOrg.Id, surverManInfo.AdminOrg)
if publicmethod.IsInTrue[int64](lookManInfo.Company, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.MainDeparment, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.AdminOrg, sunOrg.Id) {
sendData.IsOk = 1
}
case 2:
var sunOrg publicmethod.GetOrgAllParent
sunOrg.GetOrgSun(surverManInfo.MainDeparment)
sunOrg.Id = append(sunOrg.Id, surverManInfo.MainDeparment)
if publicmethod.IsInTrue[int64](lookManInfo.Company, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.MainDeparment, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.AdminOrg, sunOrg.Id) {
sendData.IsOk = 1
}
case 3:
var sunOrg publicmethod.GetOrgAllParent
sunOrg.GetOrgSun(surverManInfo.Company)
sunOrg.Id = append(sunOrg.Id, surverManInfo.Company)
if publicmethod.IsInTrue[int64](lookManInfo.Company, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.MainDeparment, sunOrg.Id) || publicmethod.IsInTrue[int64](lookManInfo.AdminOrg, sunOrg.Id) {
sendData.IsOk = 1
}
case 4:
departMent := strconv.FormatInt(lookManInfo.MainDeparment, 10)
orgId := strconv.FormatInt(lookManInfo.AdminOrg, 10)
companyId := strconv.FormatInt(lookManInfo.Company, 10)
if publicmethod.IsInTrue[string](companyId, orgList) || publicmethod.IsInTrue[string](departMent, orgList) || publicmethod.IsInTrue[string](orgId, orgList) {
sendData.IsOk = 1
}
case 5:
sendData.IsOk = 1
default:
sendData.IsOk = 2
}
}
publicmethod.Result(0, sendData, c)
}
/*
*
@ 作者: 秦东
@ 时间: 2024-10-23 15:04:38
@ 功能: 构造企业微信网页授权code
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) ObtainAuthorization(c *gin.Context) {
//保障获取渠道一致
md5Token := GainTokenKey(c)
//来源于哪个系统
systemApp := c.Query("systemapp")
if systemApp == "" {
systemApp = "stzl"
}
isAgain := c.Query("isagain") //是否强制重新授权
isAgainInt, _ := strconv.Atoi(isAgain)
if isAgainInt == 0 {
isAgainInt = 1
}
//获取token
token, err := GainWechatToken(systemApp, md5Token, isAgainInt)
if err != nil {
publicmethod.Result(1, err, c, "身份认证失败")
return
}
//组装企业微信重定向地址参数
var urlRedirectKey []string
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("systemapp=%v", systemApp))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("isagain=%v", isAgainInt))
userNum := c.Query("userid")
if userNum != "" {
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("userid=%v", userNum))
}
urlParameter := strings.Join(urlRedirectKey, "&") //合并成参数字符串
//企业微信重定向地址
watchWorkCallBackUrl := url.QueryEscape(fmt.Sprintf("%v/systemapi/wechat/wechatCallBack?%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, urlParameter))
redirectUrl := fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%v&redirect_uri=%v&response_type=code&scope=snsapi_base&state=%v#wechat_redirect", overall.CONSTANT_CONFIG.WechatCompany.CompanyId, watchWorkCallBackUrl, token)
c.Redirect(http.StatusMovedPermanently, redirectUrl)
}
/*
*
@ 作者: 秦东
@ 时间: 2024-10-23 14:41:12
@ 功能: 企业微信参数地址重定向回调
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) WechatCallBack(c *gin.Context) {
code := c.Query("code")
state := c.Query("state")
if code == "" || state == "" {
publicmethod.Result(0, code, c, "未能查询到您的信息!企业微信授权失败!")
return
}
systemApp := c.Query("systemapp")
if systemApp == "" {
systemApp = "stzl"
}
isAgain := c.Query("isagain")
isAgainInt, _ := strconv.Atoi(isAgain)
userNum := c.Query("userid")
md5Token := GainTokenKey(c) //保障获取渠道一致
token, _ := GainWechatToken(systemApp, md5Token, isAgainInt)
gainWechatInfo := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%v&code=%v", token, code)
wechatInfoByte := publicmethod.CurlGet(gainWechatInfo)
var callBackWechatInfo WorkWechatUserAuter
err := json.Unmarshal(wechatInfoByte, &callBackWechatInfo)
if err != nil {
publicmethod.Result(0, err, c, "未能查询到您的信息!企业微信授权失败!2")
return
}
if callBackWechatInfo.Errcode != 0 {
if callBackWechatInfo.Errcode == 42001 {
a.ObtainAuthorization(c)
return
}
publicmethod.Result(12, callBackWechatInfo, c, "未能查询到您的信息!企业微信授权失败!3")
return
}
var userWechatId string
if callBackWechatInfo.OpenId != "" {
userWechatId = callBackWechatInfo.OpenId
}
if callBackWechatInfo.Userid != "" {
userWechatId = callBackWechatInfo.Userid
}
if userWechatId == "" {
publicmethod.Result(1, err, c, "未能查询到您的信息!企业微信授权失败!")
return
}
wechatCont, err := SetUpWechatInfo(userWechatId)
if err != nil {
publicmethod.Result(13, err, c, "未能查询到您的信息!企业微信授权失败!")
return
}
// callBackLoginUrl := fmt.Sprintf("%v/#/?callback=1&usernum=%v&openid=%v&userkey=%v&token=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, userNum, wechatCont.UserInfo.Number, wechatCont.UserKey, wechatCont.Token)
callBackLoginUrl := fmt.Sprintf("%v?callback=1&usernum=%v&openid=%v&userkey=%v&token=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, userNum, wechatCont.UserInfo.Number, wechatCont.UserKey, wechatCont.Token)
c.Redirect(http.StatusMovedPermanently, callBackLoginUrl)
}
// 获取token Key
func GainTokenKey(c *gin.Context) (md5Token string) {
host := c.Request.Header.Get("Host")
userAgent := c.Request.Header.Get("User-Agent")
wechatTokenStr := fmt.Sprintf("%v_%v", host, userAgent)
var md5JiaMi publicmethod.Md5Encryption
md5JiaMi.Md5EncryptionInit(wechatTokenStr)
md5Token = md5JiaMi.Md5EncryptionAlgorithm()
return
}
/*
*
@ 作者: 秦东
@ 时间: 2024-01-20 14:06:00
@ 功能: 获取登陆人员信息
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func SetUpWechatInfo(wechatOpenId string) (sendData WechatVerifyIdentity, err error) {
err = overall.CONSTANT_DB_HR.Where("`wechat` = ? OR `work_wechat` = ?", wechatOpenId, wechatOpenId).First(&sendData.UserInfo).Error
if err != nil {
return
}
if !publicmethod.IsInTrue[int](sendData.UserInfo.EmpType, []int{1, 3, 4, 5, 6, 7, 8, 9, 10}) {
err = errors.New("对不起!你没有权限进入!")
return
}
// uuIdVal := overallhandle.OnlyOneNumber(3)
userAgent := overall.CONSTANT_CONFIG.Appsetup.AppKey
var md5JiaMi publicmethod.Md5Encryption
md5JiaMi.Md5EncryptionInit(userAgent)
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
//工号MD5加密
var md5JiaMiNumber publicmethod.Md5Encryption
md5JiaMiNumber.Md5EncryptionInit(sendData.UserInfo.Number)
sendData.UserKey = md5JiaMiNumber.Md5EncryptionAlgorithm()
sha1Str := fmt.Sprintf("%v%v%v%v", sendData.UserKey, sendData.UserInfo.Number, sendData.UserInfo.Password, md5Token)
sendData.Token = publicmethod.Sha1Encryption(sha1Str)
//组成Token字符串进行
wechatUserToken := fmt.Sprintf("%v%v", sendData.UserKey, sendData.Token)
var md5JiaMiWechat publicmethod.Md5Encryption
md5JiaMiWechat.Md5EncryptionInit(wechatUserToken)
wechatRedisKey := md5JiaMiWechat.Md5EncryptionAlgorithm()
wechatRedisToekn := fmt.Sprintf("Wechat:UserToken:%v_%v", wechatRedisKey, overall.CONSTANT_CONFIG.RedisPrefixStr.Alias)
saveInfo := publicmethod.MapOut[string]()
structValue := reflect.ValueOf(sendData.UserInfo)
structType := structValue.Type()
for i := 0; i < structValue.NumField(); i++ {
fieldValue := structValue.Field(i)
fieldType := structType.Field(i)
// fmt.Printf("%s: %v\n", fieldType.Name, fieldValue.Interface())
saveInfo[fieldType.Name] = fieldValue.Interface()
}
redisClient := grocerystore.RunRedis(overall.CONSTANT_REDIS4) //设定redis库
redisClient.SetRedisTime(7200)
redisClient.HashMsetAdd(wechatRedisToekn, saveInfo)
return
}
/*
*
@ 作者: 秦东
@ 时间: 2024-12-19 13:42:30
@ 功能: 查看手机流程表单详情单页企业微信网页授权code
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) ObtaOnePage(c *gin.Context) {
//保障获取渠道一致
md5Token := GainTokenKey(c)
//来源于哪个系统
systemApp := c.Query("systemapp")
if systemApp == "" {
systemApp = "stzl"
}
callBackType := c.Query("call_back_type")
if callBackType == "" {
callBackType = "work_wechat"
}
isAgain := c.Query("isagain") //是否强制重新授权
isAgainInt, _ := strconv.Atoi(isAgain)
if isAgainInt == 0 {
isAgainInt = 1
}
//获取token
token, err := GainWechatToken(systemApp, md5Token, isAgainInt)
if err != nil {
publicmethod.Result(1, err, c, "身份认证失败")
return
}
//组装企业微信重定向地址参数
var urlRedirectKey []string
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("systemapp=%v", systemApp))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("isagain=%v", isAgainInt))
userNum := c.Query("userid")
if userNum != "" {
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("userid=%v", userNum))
}
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("appKey=%v", c.Query("appKey")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("appId=%v", c.Query("appId")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("taskTitle=%v", c.Query("taskTitle")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("taskId=%v", c.Query("taskId")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("taskKey=%v", c.Query("taskKey")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("call_back_type=%v", callBackType))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("masters_key=%v", c.Query("masters_key")))
urlParameter := strings.Join(urlRedirectKey, "&") //合并成参数字符串
//企业微信重定向地址
watchWorkCallBackUrl := url.QueryEscape(fmt.Sprintf("%v/systemapi/wechat/wechatCallBackPage?%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, urlParameter))
redirectUrl := fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%v&redirect_uri=%v&response_type=code&scope=snsapi_base&state=%v#wechat_redirect", overall.CONSTANT_CONFIG.WechatCompany.CompanyId, watchWorkCallBackUrl, token)
c.Redirect(http.StatusMovedPermanently, redirectUrl)
}
/*
*
@ 作者: 秦东
@ 时间: 2024-10-23 14:41:12
@ 功能: 查看手机流程表单企业微信参数地址重定向回调
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) WechatCallBackPage(c *gin.Context) {
code := c.Query("code")
state := c.Query("state")
if code == "" || state == "" {
publicmethod.Result(0, code, c, "未能查询到您的信息!企业微信授权失败!")
return
}
systemApp := c.Query("systemapp")
if systemApp == "" {
systemApp = "stzl"
}
isAgain := c.Query("isagain")
isAgainInt, _ := strconv.Atoi(isAgain)
userNum := c.Query("userid")
md5Token := GainTokenKey(c) //保障获取渠道一致
token, _ := GainWechatToken(systemApp, md5Token, isAgainInt)
gainWechatInfo := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%v&code=%v", token, code)
wechatInfoByte := publicmethod.CurlGet(gainWechatInfo)
var callBackWechatInfo WorkWechatUserAuter
err := json.Unmarshal(wechatInfoByte, &callBackWechatInfo)
if err != nil {
publicmethod.Result(0, err, c, "未能查询到您的信息!企业微信授权失败!2")
return
}
if callBackWechatInfo.Errcode != 0 {
if callBackWechatInfo.Errcode == 42001 {
a.ObtainAuthorization(c)
return
}
publicmethod.Result(12, callBackWechatInfo, c, "未能查询到您的信息!企业微信授权失败!3")
return
}
var userWechatId string
if callBackWechatInfo.OpenId != "" {
userWechatId = callBackWechatInfo.OpenId
}
if callBackWechatInfo.Userid != "" {
userWechatId = callBackWechatInfo.Userid
}
if userWechatId == "" {
publicmethod.Result(1, err, c, "未能查询到您的信息!企业微信授权失败!")
return
}
wechatCont, err := SetUpWechatInfo(userWechatId)
if err != nil {
publicmethod.Result(13, err, c, "未能查询到您的信息!企业微信授权失败!")
return
}
var urlRedirectKey []string
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("callback=%v", 1))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("usernum=%v", userNum))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("openid=%v", wechatCont.UserInfo.Number))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("userkey=%v", wechatCont.UserKey))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("token=%v", wechatCont.Token))
// urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("appKey=%v", c.Query("appKey")))
// urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("appId=%v", c.Query("appId")))
// urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("taskId=%v", c.Query("taskId")))
// urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("taskKey=%v", c.Query("taskKey")))
urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("masters_key=%v", c.Query("masters_key")))
// urlRedirectKey = append(urlRedirectKey, fmt.Sprintf("runFlowId=%v", c.Query("runFlowId")))
urlParameter := strings.Join(urlRedirectKey, "&") //合并成参数字符串
// callBackLoginUrl := fmt.Sprintf("%v/#/?callback=1&usernum=%v&openid=%v&userkey=%v&token=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, userNum, wechatCont.UserInfo.Number, wechatCont.UserKey, wechatCont.Token)
// callBackLoginUrl := fmt.Sprintf("%v?callback=1&usernum=%v&openid=%v&userkey=%v&token=%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, userNum, wechatCont.UserInfo.Number, wechatCont.UserKey, wechatCont.Token)
callBackType := c.Query("call_back_type")
if callBackType == "" {
callBackType = "work_wechat"
}
callBackLoginUrl := fmt.Sprintf("%v/#/work_wechat?%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, urlParameter)
switch callBackType {
case "login":
callBackLoginUrl = fmt.Sprintf("%v/#/login?%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, urlParameter)
default:
callBackLoginUrl = fmt.Sprintf("%v/#/work_wechat?%v", overall.CONSTANT_CONFIG.Appsetup.WebKpiUrl, urlParameter)
}
fmt.Printf("\n\n返回路径:%v\n\n", callBackLoginUrl)
c.Redirect(http.StatusMovedPermanently, callBackLoginUrl)
}