-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
445 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ce | ||
|
||
import ( | ||
"github.com/rosbit/go-wx-api/auth" | ||
"github.com/rosbit/go-wx-api/tools" | ||
"strconv" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// GET ${commonEndpoints.WxQr}?s=<service-name-in-conf>&t=<type-name,temp|forever>[&sceneid=xx][&e=<expire-secs-for-type-temp>] | ||
func CreateWxQr(w http.ResponseWriter, r *http.Request) { | ||
service := r.FormValue("s") | ||
if service == "" { | ||
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected") | ||
return | ||
} | ||
|
||
wxParams, ok := wxParamsCache[service] | ||
if !ok { | ||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", service)) | ||
return | ||
} | ||
|
||
qrType := r.FormValue("t") | ||
if qrType == "" { | ||
writeError(w, http.StatusBadRequest, "t(type) parameter expected") | ||
return | ||
} | ||
switch qrType { | ||
case "temp", "forever": | ||
default: | ||
writeError(w, http.StatusBadRequest, `t(ype) value must be "temp" or "forever"`) | ||
return | ||
} | ||
|
||
sceneid := r.FormValue("sceneid") | ||
if sceneid == "" { | ||
sceneid = "0" | ||
} | ||
|
||
accessToken, err := wxauth.NewAccessTokenWithParams(wxParams).Get() | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
var ticketURL2ShowQrCode, urlIncluedInQrcode string | ||
switch qrType { | ||
case "temp": | ||
expireSecs := 30 | ||
e := r.FormValue("e") | ||
if e == "" { | ||
expireSecs, _ := strconv.Atoi(e) | ||
if expireSecs <= 0 { | ||
expireSecs = 30 | ||
} | ||
} | ||
ticketURL2ShowQrCode, urlIncluedInQrcode, err = wxtools.CreateTempQrStrScene(accessToken, sceneid, expireSecs) | ||
case "forever": | ||
ticketURL2ShowQrCode, urlIncluedInQrcode, err = wxtools.CreateQrStrScene(accessToken, sceneid) | ||
} | ||
|
||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
writeJson(w, http.StatusOK, map[string]interface{}{ | ||
"code": http.StatusOK, | ||
"msg": "OK", | ||
"result": map[string]string { | ||
"ticketURL2ShowQrCode": ticketURL2ShowQrCode, | ||
"urlIncluedInQrcode": urlIncluedInQrcode, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ce | ||
|
||
import ( | ||
"wx-gateway/handlers" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// GET ${commonEndpoints.WxUser}?s=<service-name-in-conf>&o=<openId> | ||
func GetWxUserInfo(w http.ResponseWriter, r *http.Request) { | ||
service := r.FormValue("s") | ||
if service == "" { | ||
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected") | ||
return | ||
} | ||
|
||
wxParams, ok := wxParamsCache[service] | ||
if !ok { | ||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", service)) | ||
return | ||
} | ||
|
||
openId := r.FormValue("o") | ||
if openId == "" { | ||
writeError(w, http.StatusBadRequest, "o(penId) parameter expected") | ||
return | ||
} | ||
|
||
userInfo, err := gwhandlers.GetUserInfo(wxParams, openId) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
writeJson(w, http.StatusOK, map[string]interface{}{ | ||
"code": http.StatusOK, | ||
"msg": "OK", | ||
"userInfo": userInfo, | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ce | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func writeJson(w http.ResponseWriter, code int, data interface{}) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
switch data.(type) { | ||
case []byte: | ||
w.Write(data.([]byte)) | ||
default: | ||
enc := json.NewEncoder(w) | ||
enc.Encode(data) | ||
} | ||
} | ||
|
||
func writeError(w http.ResponseWriter, code int, msg string) { | ||
writeJson(w, code, map[string]interface{}{"code": code, "msg": msg}) | ||
} | ||
|
||
func readJson(r *http.Request, res interface{}) (status int, err error) { | ||
if r.Body == nil { | ||
return http.StatusBadRequest, fmt.Errorf("bad request") | ||
} | ||
defer r.Body.Close() | ||
|
||
if err = json.NewDecoder(r.Body).Decode(res); err != nil { | ||
return http.StatusBadRequest, err | ||
} | ||
return http.StatusOK, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ce | ||
|
||
import ( | ||
"github.com/rosbit/go-wx-api/conf" | ||
) | ||
|
||
var wxParamsCache = map[string]*wxconf.WxParamsT{} | ||
|
||
func CacheWxParams(service string, wxParams *wxconf.WxParamsT) { | ||
wxParamsCache[service] = wxParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ce | ||
|
||
import ( | ||
"github.com/rosbit/go-wx-api/auth" | ||
"github.com/rosbit/go-wx-api/tools" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// POST ${commonEndpoints.TmplMsg} | ||
// { | ||
// "s": "service-name-in-conf", | ||
// "to": "to-user-id", | ||
// "tid": "template-id", | ||
// "url": "optional url to jump", | ||
// "mp": { | ||
// "appid": "mini program appid", | ||
// "pagepath": "pagepath", | ||
// }, | ||
// "data": { | ||
// "k1": "v1", | ||
// "k2": "v2", | ||
// "....": "..." | ||
// } | ||
// } | ||
func SendTmplMsg(w http.ResponseWriter, r *http.Request) { | ||
var params struct { | ||
Service string `json:"s"` | ||
ToUserId string `json:"to"` | ||
TmplId string `json:"tid"` | ||
Url string `json:"url"` | ||
MiniProg struct { | ||
AppId string `json:"appid"` | ||
PagePath string `json:"pagepath"` | ||
} `json:"mp"` | ||
Data map[string]interface{} `json:"data"` | ||
} | ||
if status, err := readJson(r, ¶ms); err != nil { | ||
writeError(w, status, err.Error()) | ||
return | ||
} | ||
if params.Service == "" { | ||
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected") | ||
return | ||
} | ||
|
||
wxParams, ok := wxParamsCache[params.Service] | ||
if !ok { | ||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", params.Service)) | ||
return | ||
} | ||
if params.ToUserId == "" { | ||
writeError(w, http.StatusBadRequest, "to(user id) parameter expected") | ||
return | ||
} | ||
if params.TmplId == "" { | ||
writeError(w, http.StatusBadRequest, "tid(template id) parameter expected") | ||
return | ||
} | ||
if len(params.Data) == 0 { | ||
writeError(w, http.StatusBadRequest, "data parameter as a map expected") | ||
return | ||
} | ||
|
||
accessToken, err := wxauth.NewAccessTokenWithParams(wxParams).Get() | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
res, err := wxtools.SendTemplateMessage(accessToken, params.ToUserId, params.TmplId, params.Data, params.Url, params.MiniProg.AppId, params.MiniProg.PagePath) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
writeJson(w, http.StatusOK, res) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ce | ||
|
||
import ( | ||
"github.com/rosbit/go-wx-api/auth" | ||
"github.com/rosbit/go-wx-api/tools" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// POST ${commonEndpoints.ShortUrl} | ||
// s=<service-name-in-conf>&u=<long-url> | ||
func CreateShorturl(w http.ResponseWriter, r *http.Request) { | ||
service := r.FormValue("s") | ||
if service == "" { | ||
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected") | ||
return | ||
} | ||
|
||
wxParams, ok := wxParamsCache[service] | ||
if !ok { | ||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", service)) | ||
return | ||
} | ||
|
||
longUrl := r.FormValue("u") | ||
if longUrl == "" { | ||
writeError(w, http.StatusBadRequest, "u(rl) parameter expected") | ||
return | ||
} | ||
|
||
accessToken, err := wxauth.NewAccessTokenWithParams(wxParams).Get() | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
shortUrl, err := wxtools.MakeShorturl(accessToken, longUrl) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
writeJson(w, http.StatusOK, map[string]interface{}{ | ||
"code": http.StatusOK, | ||
"msg": "OK", | ||
"short-url": shortUrl, | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ce | ||
|
||
import ( | ||
"github.com/rosbit/go-wx-api/auth" | ||
"github.com/rosbit/go-wx-api/tools" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// POST ${commonEndpoints.SignJSAPI} | ||
// s=<service-name-in-conf>&u=<url-calling-jsapi-in-urlencoding> | ||
func SignJSAPI(w http.ResponseWriter, r *http.Request) { | ||
service := r.FormValue("s") | ||
if service == "" { | ||
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected") | ||
return | ||
} | ||
|
||
wxParams, ok := wxParamsCache[service] | ||
if !ok { | ||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", service)) | ||
return | ||
} | ||
|
||
url := r.FormValue("u") | ||
if url == "" { | ||
writeError(w, http.StatusBadRequest, "u(rl) parameter expected") | ||
return | ||
} | ||
|
||
accessToken, err := wxauth.NewAccessTokenWithParams(wxParams).Get() | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
nonce, timestamp, signature, err := wxtools.SignJSAPI(accessToken, url) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
writeJson(w, http.StatusOK, map[string]interface{}{ | ||
"code": http.StatusOK, | ||
"msg": "OK", | ||
"params": map[string]interface{}{ | ||
"nonce": nonce, | ||
"timestamp": timestamp, | ||
"signature": signature, | ||
}, | ||
}) | ||
} | ||
|
Oops, something went wrong.