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.
350 lines
9.3 KiB
350 lines
9.3 KiB
package workWechat
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"hr_server/grocerystore"
|
|
"hr_server/overall"
|
|
"hr_server/overall/overallhandle"
|
|
"net/http"
|
|
"net/url"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-01-18 15:56:15
|
|
@ 功能: 获取Token
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (a *ApiMethod) GainWechatToken(c *gin.Context) {
|
|
var requestData WechatTokanVal
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
overallhandle.Result(100, err, c)
|
|
return
|
|
}
|
|
host := c.Request.Header.Get("Host")
|
|
userAgent := c.Request.Header.Get("User-Agent")
|
|
wechatTokenStr := fmt.Sprintf("%v_%v", host, userAgent)
|
|
var md5JiaMi overallhandle.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(wechatTokenStr)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
token, err := GainWechatToken(requestData.SystemApp, md5Token, requestData.IsAgain)
|
|
if err != nil {
|
|
overallhandle.Result(107, err, c)
|
|
return
|
|
}
|
|
overallhandle.Result(0, token, c)
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-01-19 16:00:09
|
|
@ 功能: 获取企业的jsapi_ticket 或 获取应用的jsapi_ticket
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (a *ApiMethod) WechatJsapiTicket(c *gin.Context) {
|
|
var requestData WechatTokanVal
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
overallhandle.Result(100, err, c)
|
|
return
|
|
}
|
|
host := c.Request.Header.Get("Host")
|
|
userAgent := c.Request.Header.Get("User-Agent")
|
|
wechatTokenStr := fmt.Sprintf("%v_%v", host, userAgent)
|
|
var md5JiaMi overallhandle.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(wechatTokenStr)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
jsApiTicker, err := GainJsapiTicket(requestData.SystemApp, md5Token, requestData.IsAgain)
|
|
if err != nil {
|
|
overallhandle.Result(107, err, c)
|
|
return
|
|
}
|
|
overallhandle.Result(0, jsApiTicker, c)
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-01-20 08:57:14
|
|
@ 功能: 获取身份认证
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (a *ApiMethod) AuthenticationUser(c *gin.Context) {
|
|
host := c.Request.Header.Get("Host")
|
|
userAgent := c.Request.Header.Get("User-Agent")
|
|
wechatTokenStr := fmt.Sprintf("%v_%v", host, userAgent)
|
|
var md5JiaMi overallhandle.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(wechatTokenStr)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
|
|
systemApp := c.Query("systemapp")
|
|
if systemApp == "" {
|
|
systemApp = "hr"
|
|
}
|
|
isAgain := c.Query("isagain")
|
|
if isAgain == "" {
|
|
isAgain = "1"
|
|
}
|
|
isAgainInt, _ := strconv.Atoi(isAgain)
|
|
if isAgainInt == 0 {
|
|
isAgainInt = 1
|
|
}
|
|
token, err := GainWechatToken(systemApp, md5Token, isAgainInt)
|
|
if err != nil {
|
|
overallhandle.Result(1, token, c, "身份认证失败")
|
|
return
|
|
}
|
|
var additional []string
|
|
additional = append(additional, fmt.Sprintf("systemapp=%v", systemApp))
|
|
additional = append(additional, fmt.Sprintf("isagain=%v", isAgainInt))
|
|
userNum := c.Query("usernum")
|
|
if userNum != "" {
|
|
additional = append(additional, fmt.Sprintf("usernum=%v", userNum))
|
|
}
|
|
|
|
urlParameter := strings.Join(additional, "&")
|
|
//重定向身份认证
|
|
callBackUrl := url.QueryEscape(fmt.Sprintf("%v/kpiapi/wechat/callbackauthuser?%v", overall.CONSTANT_CONFIG.Appsetup.WebUrl, 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, callBackUrl, token)
|
|
// formUrl := c.Request.URL.Path
|
|
// formUrls := c.Request.RequestURI
|
|
|
|
// sendData := overallhandle.MapOut()
|
|
// sendData["formUrl"] = formUrl
|
|
// sendData["formUrls"] = formUrls
|
|
// sendData["userNum"] = userNum
|
|
// sendData["token"] = token
|
|
// sendData["callBackUrl"] = callBackUrl
|
|
// sendData["urlParameter"] = urlParameter
|
|
// sendData["redirectUrl"] = redirectUrl
|
|
// overallhandle.Result(0, sendData, c)
|
|
c.Redirect(http.StatusMovedPermanently, redirectUrl)
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-01-20 13:53:51
|
|
@ 功能: 企业微信身份回调认证
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (a *ApiMethod) CallBackAuthUser(c *gin.Context) {
|
|
code := c.Query("code")
|
|
state := c.Query("state")
|
|
if code == "" || state == "" {
|
|
overallhandle.Result(1, code, c, "未能查询到您的信息!企业微信授权失败!")
|
|
return
|
|
}
|
|
systemApp := c.Query("systemapp")
|
|
if systemApp == "" {
|
|
systemApp = "hr"
|
|
}
|
|
isAgain := c.Query("isagain")
|
|
if isAgain == "" {
|
|
isAgain = "1"
|
|
}
|
|
userNum := c.Query("usernum")
|
|
gainWechatInfo := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%v&code=%v", state, code)
|
|
wechatInfoByte := overallhandle.CurlGet(gainWechatInfo)
|
|
var callBackWechatInfo WorkWechatUserAuter
|
|
err := json.Unmarshal(wechatInfoByte, &callBackWechatInfo)
|
|
if err != nil {
|
|
overallhandle.Result(1, err, c, "未能查询到您的信息!企业微信授权失败!2")
|
|
return
|
|
}
|
|
if callBackWechatInfo.Errcode != 0 {
|
|
|
|
if callBackWechatInfo.Errcode == 42001 {
|
|
AgainEmpower(c)
|
|
return
|
|
}
|
|
overallhandle.Result(1, callBackWechatInfo, c, "未能查询到您的信息!企业微信授权失败!3")
|
|
return
|
|
}
|
|
var userWechatId string
|
|
if callBackWechatInfo.OpenId != "" {
|
|
userWechatId = callBackWechatInfo.OpenId
|
|
}
|
|
if callBackWechatInfo.Userid != "" {
|
|
userWechatId = callBackWechatInfo.Userid
|
|
}
|
|
if userWechatId == "" {
|
|
overallhandle.Result(1, err, c, "未能查询到您的信息!企业微信授权失败!")
|
|
return
|
|
}
|
|
wechatCont, err := SetUpWechatInfo(code)
|
|
if err != nil {
|
|
overallhandle.Result(1, err, c)
|
|
return
|
|
}
|
|
callBackLoginUrl := fmt.Sprintf("%v/#/?usernum=%v&openid=%v&userkey=%v&token=%v", overall.CONSTANT_CONFIG.Appsetup.WebUrl, userNum, wechatCont.UserInfo.Number, wechatCont.UserKey, wechatCont.Token)
|
|
c.Redirect(http.StatusMovedPermanently, callBackLoginUrl)
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 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 !overallhandle.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 overallhandle.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(userAgent)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
//工号MD5加密
|
|
var md5JiaMiNumber overallhandle.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 = overallhandle.Sha1Encryption(sha1Str)
|
|
//组成Token字符串进行
|
|
|
|
wechatUserToken := fmt.Sprintf("%v%v", sendData.UserKey, sendData.Token)
|
|
var md5JiaMiWechat overallhandle.Md5Encryption
|
|
md5JiaMiWechat.Md5EncryptionInit(wechatUserToken)
|
|
wechatRedisKey := md5JiaMiWechat.Md5EncryptionAlgorithm()
|
|
wechatRedisToekn := fmt.Sprintf("Wechat:UserToken:%v_%v", wechatRedisKey, overall.CONSTANT_CONFIG.RedisPrefixStr.Alias)
|
|
|
|
saveInfo := overallhandle.MapOut()
|
|
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-01-20 14:03:26
|
|
@ 功能: 重新授权
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func AgainEmpower(c *gin.Context) {
|
|
host := c.Request.Header.Get("Host")
|
|
userAgent := c.Request.Header.Get("User-Agent")
|
|
wechatTokenStr := fmt.Sprintf("%v_%v", host, userAgent)
|
|
var md5JiaMi overallhandle.Md5Encryption
|
|
md5JiaMi.Md5EncryptionInit(wechatTokenStr)
|
|
md5Token := md5JiaMi.Md5EncryptionAlgorithm()
|
|
|
|
systemApp := c.Query("systemapp")
|
|
if systemApp == "" {
|
|
systemApp = "hr"
|
|
}
|
|
isAgain := c.Query("isagain")
|
|
if isAgain == "" {
|
|
isAgain = "1"
|
|
}
|
|
isAgainInt, _ := strconv.Atoi(isAgain)
|
|
if isAgainInt == 0 {
|
|
isAgainInt = 1
|
|
}
|
|
token, err := GainWechatToken(systemApp, md5Token, isAgainInt)
|
|
if err != nil {
|
|
overallhandle.Result(1, token, c, "身份认证失败")
|
|
return
|
|
}
|
|
var additional []string
|
|
additional = append(additional, fmt.Sprintf("systemapp=%v", systemApp))
|
|
additional = append(additional, fmt.Sprintf("isagain=%v", isAgainInt))
|
|
userNum := c.Query("usernum")
|
|
if userNum != "" {
|
|
additional = append(additional, fmt.Sprintf("usernum=%v", userNum))
|
|
}
|
|
|
|
urlParameter := strings.Join(additional, "&")
|
|
//重定向身份认证
|
|
callBackUrl := url.QueryEscape(fmt.Sprintf("%v/kpiapi/wechat/callbackauthuser?%v", overall.CONSTANT_CONFIG.Appsetup.WebUrl, 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, callBackUrl, token)
|
|
c.Redirect(http.StatusMovedPermanently, redirectUrl)
|
|
}
|
|
|