298 lines
7.1 KiB
Go
298 lines
7.1 KiB
Go
package weapp
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"hc/dbop"
|
||
"io"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/shockliu/logger"
|
||
)
|
||
|
||
func Opt(c *gin.Context) {
|
||
rows, err := dbop.MDb.Query("select union_id,open_id from wechat_member")
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
defer rows.Close()
|
||
for rows.Next() {
|
||
var oid, uid string
|
||
err = rows.Scan(&uid, &oid)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
fmt.Println(oid, uid)
|
||
_, err := dbop.MDb.Exec("update user_account set open_id = ? where uuID = ?;", oid, uid)
|
||
if err != nil {
|
||
logger.Debugf("%s", err.Error())
|
||
}
|
||
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"msg": err.Error(),
|
||
})
|
||
}
|
||
|
||
func UnlinkDev(c *gin.Context) {
|
||
//uid := c.MustGet("TK_User").(int)
|
||
uid := 10000014
|
||
|
||
type tmp struct {
|
||
DevId int `json:"dev_id"`
|
||
}
|
||
var data tmp
|
||
if err := c.ShouldBindJSON(&data); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
_, err := dbop.MDb.Exec("update user_device_bind set user_id = 0 where device_id = ? and user_id = ?;", data.DevId, uid)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"msg": "succ",
|
||
})
|
||
}
|
||
|
||
func BindDev(c *gin.Context) {
|
||
uid := c.MustGet("TK_User").(int)
|
||
//uid := 10000014
|
||
|
||
type tmp struct {
|
||
Devsn string `json:"devsn"`
|
||
DevMac string `json:"devmac"`
|
||
}
|
||
var data tmp
|
||
if err := c.ShouldBindJSON(&data); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
ts, _ := dbop.MDb.Begin()
|
||
var dev_id, dev_personal, binded_user int
|
||
is_new := false
|
||
err := ts.QueryRow("select d.device_id,d.bind_status,b.user_id from health_device d LEFT JOIN user_device_bind b on d.device_id=b.device_id where d.device_sn = ?;", data.Devsn).Scan(&dev_id, &dev_personal, &binded_user)
|
||
if err != nil { //不存在,新增
|
||
_, err = ts.Exec("insert into health_device (device_name,device_sn,device_mac,dev_type,create_time) values(?,?,?,6,now())", data.Devsn, data.Devsn, data.DevMac)
|
||
if err != nil {
|
||
ts.Rollback()
|
||
logger.Debugf(err.Error())
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
ts.QueryRow("select device_id from health_device where device_sn = ?;", data.Devsn).Scan(&dev_id)
|
||
is_new = true
|
||
_, err = ts.Exec("insert into user_device_bind (device_id,user_id) values (?,?)", dev_id, uid)
|
||
if err != nil {
|
||
ts.Rollback()
|
||
logger.Debugf(err.Error())
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -2,
|
||
"msg": "设备已经绑定,请先解除",
|
||
})
|
||
return
|
||
}
|
||
}
|
||
|
||
if !is_new {
|
||
if dev_personal == 0 && binded_user != 0 { //个人用户
|
||
ts.Rollback()
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -2,
|
||
"msg": "设备已经被绑定,请先解除",
|
||
})
|
||
return
|
||
}
|
||
|
||
_, err = ts.Exec("update user_device_bind set user_id = ? where device_id = ?;", uid, dev_id)
|
||
if err != nil {
|
||
ts.Rollback()
|
||
logger.Debugf(err.Error())
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -2,
|
||
"msg": "设备绑定失败",
|
||
})
|
||
return
|
||
}
|
||
}
|
||
|
||
ts.Commit()
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"msg": "succ",
|
||
})
|
||
}
|
||
|
||
func ReportNotify(c *gin.Context) {
|
||
type tmp struct {
|
||
UId int `json:"uid"`
|
||
}
|
||
var data tmp
|
||
if err := c.ShouldBindJSON(&data); err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
var open_id, uname string
|
||
err := dbop.MDb.QueryRow("select open_id,user_name from user_account where user_id = ?", data.UId).Scan(&open_id, &uname)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
//微信小程序id
|
||
appId := "wxe9c749a836dee2a8"
|
||
//微信小程序密钥
|
||
appSecret := "51cbc34547a97c661af7e8855a48d808"
|
||
//模板id
|
||
templateId := "FIggb-ZTIa_-NxV7-OS_OFEPWBFHv5TI0RGbqDJGelM"
|
||
//跳转页面地址
|
||
pageUrl := "pages/home/home"
|
||
//获取token接口
|
||
getTokenUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
|
||
//发送消息接口
|
||
sendWxUserMsgUrl := "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
|
||
|
||
resp, err := http.Get(getTokenUrl)
|
||
if err != nil {
|
||
logger.Warnf(err.Error())
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
body, _ := io.ReadAll(resp.Body)
|
||
//fmt.Println(string(body))
|
||
//{"access_token":"74_dSxu74890Vf1YvMkVzSafUgLgBLIBdBH_CegGeD8OWTuiCq7Lda3ddVdvkW-LdB-sKIigNwgXIA9J4DBp7G_7jbF-X4hk6bT3kgWooNv98zwWDvv3QeWSM0_4X4TLPhAHAJBF","expires_in":7200}
|
||
var access_token string
|
||
if len(body) > 0 {
|
||
c2j := make(map[string]any)
|
||
err = json.Unmarshal(body, &c2j)
|
||
access_token = c2j["access_token"].(string)
|
||
logger.Debugf("access_token %s", access_token)
|
||
|
||
tmp1 := gin.H{"value": uname}
|
||
tmp2 := gin.H{"value": time.Now().Format("2006-01-02 15:04:05")}
|
||
tmp3 := gin.H{"value": "进入【我的报告】可查看详情"}
|
||
tmp4 := gin.H{"value": "训练效果脑波分析报告"}
|
||
n_data := gin.H{"time2": tmp2, "thing3": tmp3, "thing4": tmp1, "thing5": tmp4}
|
||
client := &http.Client{}
|
||
ctx := gin.H{
|
||
"touser": open_id,
|
||
"template_id": templateId,
|
||
"page": pageUrl,
|
||
"miniprogram_state": "formal", //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
||
"lang": "zh_CN",
|
||
"data": n_data,
|
||
}
|
||
bytesData, err := json.Marshal(ctx)
|
||
if err != nil {
|
||
logger.Warnf("数据处理失败%s\n", err)
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": "数据处理失败",
|
||
})
|
||
return
|
||
}
|
||
req, _ := http.NewRequest("POST", fmt.Sprintf("%s%s", sendWxUserMsgUrl, access_token), bytes.NewReader(bytesData))
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
logger.Warnf(err.Error())
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": "下发失败",
|
||
})
|
||
return
|
||
}
|
||
body, err = io.ReadAll(resp.Body)
|
||
logger.Debugf("响应 %s", string(body))
|
||
if err != nil {
|
||
logger.Warnf("接口返回失败%s\n", err)
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": "下发失败",
|
||
})
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"msg": "succ",
|
||
})
|
||
|
||
} else {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": "信号发送失败",
|
||
})
|
||
}
|
||
|
||
}
|
||
|
||
func DevList(c *gin.Context) {
|
||
//uid := c.MustGet("TK_User").(int)
|
||
uid := 10000014
|
||
|
||
rows, err := dbop.MDb.Query("select d.device_id,d.device_sn,d.dev_type from health_device d left join user_device_bind b on d.device_id = b.device_id where b.user_id = ?", uid)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"message": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
|
||
defer rows.Close()
|
||
var rst []gin.H
|
||
for rows.Next() {
|
||
var dev_id, dev_name string
|
||
var dev_type int
|
||
err = rows.Scan(&dev_id, &dev_name, &dev_type)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": -1,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
rst = append(rst, gin.H{"dev_id": dev_id, "dev_name": dev_name, "dev_type": dev_type})
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"data": rst,
|
||
"msg": "succ",
|
||
})
|
||
}
|