فهرست منبع

生成小程序二维码,头像使用oss

icole 4 سال پیش
والد
کامیت
02b2fe2605

+ 0 - 1
asserts/qrcode/u3-320753572.png

@@ -1 +0,0 @@
-{"errcode":41001,"errmsg":"access_token missing rid: 60d59b78-595d0b73-04c0ba36"}

+ 1 - 1
etc/i2bill-api.yaml

@@ -26,4 +26,4 @@ AliYunOss:
   Bucket: i2-mp
   SignExpire: 30
   SignHost: https://i2-files.oss-cn-shanghai.aliyuncs.com
-  FileUrl: https://files-cdn.i2edu.net
+  FileUrl: https://mp-cdn.i2edu.net

+ 2 - 2
internal/logic/acquirer_mkt_qr/acquirer_mkt_qr_update_logic.go

@@ -91,12 +91,12 @@ func (l *AcquirerMktQrUpdateLogic) AcquirerMktQrUpdate(r *http.Request) (*types.
 	if acquirersQr.Id != 0 {
 		bean.LastUpdateTime = time.Now()
 		bean.LastUpdateBy = userId
-		_, err = l.svcCtx.DB.ID(acquirersQr.Id).AllCols().Update(bean)
+		_, err = l.svcCtx.DB.ID(acquirersQr.Id).Cols("school_id", "activity_id", "qudao_id", "last_update_time", "last_update_by").Update(bean)
 		if err != nil {
 			logx.Error(err.Error())
 			return &types.Response{500, err.Error(), nil}, nil
 		}
-		return &types.Response{200, "", nil}, nil
+		return &types.Response{200, "", bean}, nil
 	}
 	bean.LastUpdateBy = userId
 	bean.LastUpdateTime = time.Now()

+ 6 - 2
internal/logic/system/system_file_download_logic.go

@@ -2,7 +2,6 @@ package system
 
 import (
 	"context"
-	"git.i2edu.net/i2/i2-bill-api/internal/global"
 	"git.i2edu.net/i2/i2-bill-api/model"
 	"io"
 	"net/http"
@@ -41,7 +40,12 @@ func (l *SystemFileDownloadLogic) SystemFileDownload(w http.ResponseWriter, r *h
 	if attachment.Id == "" {
 		return &types.Response{200, "", nil}, nil
 	}
-	f, err := global.NewAliYunOssUpDownloader().Download(attachment)
+	oss, err := svc.GetAliYunOssInstance()
+	if err != nil {
+		logx.Error(err.Error())
+		return &types.Response{500, err.Error(), nil}, nil
+	}
+	f, err := oss.Download(attachment)
 	if err != nil {
 		logx.Error(err.Error())
 		return &types.Response{500, err.Error(), nil}, nil

+ 6 - 2
internal/logic/system/system_file_upload_logic.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
-	"git.i2edu.net/i2/i2-bill-api/internal/global"
 	"git.i2edu.net/i2/i2-bill-api/internal/svc"
 	"git.i2edu.net/i2/i2-bill-api/internal/types"
 	"io"
@@ -37,7 +36,12 @@ func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Respon
 		logx.Error(err.Error())
 		return &types.Response{500, err.Error(), nil}, nil
 	}
-	res, err := global.NewAliYunOssUpDownloader().Upload(file, fileHeader)
+	oss, err := svc.GetAliYunOssInstance()
+	if err != nil {
+		logx.Error(err.Error())
+		return &types.Response{500, err.Error(), nil}, nil
+	}
+	res, err := oss.Upload(file, fileHeader)
 	if err != nil {
 		logx.Error(err.Error())
 		return &types.Response{500, err.Error(), nil}, nil

+ 23 - 14
internal/logic/user/jz_qr_code_logic.go

@@ -3,12 +3,9 @@ package user
 import (
 	"context"
 	"fmt"
-	"time"
-
+	"git.i2edu.net/i2/go-zero/core/logx"
 	"git.i2edu.net/i2/i2-bill-api/internal/svc"
 	"git.i2edu.net/i2/i2-bill-api/internal/types"
-
-	"git.i2edu.net/i2/go-zero/core/logx"
 )
 
 type JzQrCodeLogic struct {
@@ -29,18 +26,30 @@ func NewJzQrCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) JzQrCodeL
 
 // JzQrCode 兼职人员兼职码
 func (l *JzQrCodeLogic) JzQrCode() (*types.JzQrcodeResponse, error) {
-	uid, err := l.svcCtx.Wechat.GenUserQrId(svc.QrcodeJzPrefix, l.UserId, map[string]interface{}{
-		"userId":    l.UserId,
-		"timestamp": time.Now().Unix(),
-		"type":      "JzQrCode",
-	})
-	fmt.Println("--uid=", uid)
+	//uid, err := l.svcCtx.Wechat.GenUserQrId(svc.QrcodeJzPrefix, l.UserId, map[string]interface{}{
+	//	"userId":    l.UserId,
+	//	"timestamp": time.Now().Unix(),
+	//	"type":      "JzQrCode",
+	//})
+	//fmt.Println("--uid=", uid)
 	// l.svcCtx.Wechat.GetQrParams(qrcodeJzPrefix, uid, map[string]interface{}{})
+	//if err != nil {
+	//	return nil, err
+	//}
+	userId := l.svcCtx.GetUserIdByJwt(l.ctx)
+	attr, err := l.svcCtx.Wechat.GenQrCode(fmt.Sprintf("%d", userId), "/pages/code/code")
+	tx := l.svcCtx.DB.NewSession()
+	_, err = tx.Where("user_id = ?", userId).Update(map[string]interface{}{"qr": attr.Url})
+	if err != nil {
+		tx.Rollback()
+		return &types.JzQrcodeResponse{}, err
+	}
+	_, err = tx.Insert(attr)
 	if err != nil {
-		return nil, err
+		tx.Rollback()
+		return &types.JzQrcodeResponse{}, err
 	}
-	uri, err := l.svcCtx.Wechat.GenQrCode(uid, "/pages/code/code")
 	return &types.JzQrcodeResponse{
-		FileId: uri,
-	}, err
+		FileId: attr.Url,
+	}, nil
 }

+ 21 - 9
internal/global/aliyun_oss.go → internal/svc/aliyun_oss.go

@@ -1,8 +1,7 @@
-package global
+package svc
 
 import (
 	"fmt"
-	"git.i2edu.net/i2/i2-bill-api/internal/svc"
 	"git.i2edu.net/i2/i2-bill-api/internal/utils"
 	"git.i2edu.net/i2/i2-bill-api/model"
 	"github.com/aliyun/aliyun-oss-go-sdk/oss"
@@ -13,27 +12,35 @@ import (
 	"time"
 )
 
-var aliYunBucket *oss.Bucket
+var AliYunOss *AliYunOssUpDownloader
 
 type AliYunOssUpDownloader struct {
 	bucket *oss.Bucket
 }
 
-func NewAliYunOssUpDownloader() *AliYunOssUpDownloader {
+func GetAliYunOssInstance() (*AliYunOssUpDownloader, error) {
+	var err error
+	if AliYunOss == nil {
+		AliYunOss, err = newAliYunOssUpDownloader()
+	}
+	return AliYunOss, err
+}
+
+func newAliYunOssUpDownloader() (*AliYunOssUpDownloader, error) {
 	// 创建OSSClient实例。
-	client, err := oss.New(svc.ServiceConfig.AliYunOss.EndPoint, svc.ServiceConfig.AliYunOss.KeyId, svc.ServiceConfig.AliYunOss.Secret)
+	client, err := oss.New(ServiceConfig.AliYunOss.EndPoint, ServiceConfig.AliYunOss.KeyId, ServiceConfig.AliYunOss.Secret)
 	if err != nil {
 		fmt.Println("Error:", err)
-		return nil
+		return nil, err
 	}
 
 	// 获取存储空间。
-	aliYunBucket, err = client.Bucket(svc.ServiceConfig.AliYunOss.Bucket)
+	aliYunBucket, err := client.Bucket(ServiceConfig.AliYunOss.Bucket)
 	if err != nil {
 		fmt.Println("Error:", err)
-		return nil
+		return nil, err
 	}
-	return &AliYunOssUpDownloader{bucket: aliYunBucket}
+	return &AliYunOssUpDownloader{bucket: aliYunBucket}, nil
 }
 
 /**
@@ -64,6 +71,8 @@ func (aoud *AliYunOssUpDownloader) Upload(file multipart.File, header *multipart
 	attach.Id = attachId
 	attach.Url = objectKey
 	attach.Hash = ""
+	attach.Ext = ext
+	attach.Name = header.Filename
 	attach.Size = header.Size
 
 	return attach, nil
@@ -91,6 +100,9 @@ func (aoud *AliYunOssUpDownloader) UploadRead(file io.Reader, ext string) (*mode
 	attach.Id = attachId
 	attach.Url = objectKey
 	attach.Hash = ""
+	attach.Ext = ext
+	attach.Name = attachId
+	attach.CreateTime = time.Now()
 	return attach, nil
 
 }

+ 8 - 3
internal/svc/wechat.go

@@ -134,10 +134,15 @@ func (wc *Wechat) GenQrCode(scene, page string) (*model.I2billSysAttachment, err
 	}
 	req := utils.Post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token)
 	req.JSONBody(map[string]interface{}{"scene": scene, "page": page})
-	//ph := path.Join("asserts", "qrcode", utils.Md5(scene)+".png")
-	attr, err := req.ToFile()
+	resp, err := req.Response()
 	if err != nil {
 		return nil, err
 	}
-	return attr, err
+	defer resp.Body.Close()
+	oss, err := GetAliYunOssInstance()
+	if err != nil {
+		return nil, err
+	}
+	return oss.UploadRead(resp.Body, ".png")
+
 }

+ 12 - 18
internal/utils/http.go

@@ -7,8 +7,6 @@ import (
 	"crypto/tls"
 	"encoding/json"
 	"encoding/xml"
-	"git.i2edu.net/i2/i2-bill-api/internal/global"
-	"git.i2edu.net/i2/i2-bill-api/model"
 	"io"
 	"io/ioutil"
 	"log"
@@ -554,30 +552,26 @@ func (b *HTTPRequest) Bytes() ([]byte, error) {
 
 // ToFile saves the body data in response to one file.
 // Calls Response inner.
-func (b *HTTPRequest) ToFile() (*model.I2billSysAttachment, error) {
+func (b *HTTPRequest) ToFile(filename string) error {
 	resp, err := b.getResponse()
 	if err != nil {
-		return nil, err
+		return err
 	}
 	if resp.Body == nil {
-		return nil, nil
+		return nil
 	}
 	defer resp.Body.Close()
-	//err = pathExistAndMkdir(filename)
-	//if err != nil {
-	//	return err
-	//}
-	//f, err := os.Create(filename)
-	//if err != nil {
-	//	return err
-	//}
-	//defer f.Close()
-	//_, err = io.Copy(f, resp.Body)
-	att, err := global.NewAliYunOssUpDownloader().UploadRead(resp.Body, ".png")
+	err = pathExistAndMkdir(filename)
 	if err != nil {
-		return nil, err
+		return err
 	}
-	return att, err
+	f, err := os.Create(filename)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+	_, err = io.Copy(f, resp.Body)
+	return err
 }
 
 // Check if the file directory exists. If it doesn't then it's created

+ 1 - 1
internal/utils/utils.go

@@ -135,7 +135,7 @@ func UnmarshalMysqlRow(row interface{}) map[string]interface{} {
 func TreePath(nodes []*transform.TreeNode, tagId int64, idPath, namePath *string) bool {
 	for _, node := range nodes {
 		if node.Id == tagId {
-			*idPath = *idPath + "," + fmt.Sprintf("%d", node.Id)
+			*idPath = strings.TrimLeft(*idPath+","+fmt.Sprintf("%d", node.Id), ",")
 			*namePath = strings.TrimLeft(*namePath+","+node.Text, ",")
 			return true
 		}