Browse Source

优化账号管理相关接口

boweniac 7 months ago
parent
commit
4e74edff24

+ 14 - 1
desc/wechat/wx.api

@@ -50,6 +50,12 @@ type (
 
 		// 模式信息
 		AgentInfo *AgentInfo `json:"agentInfo,optional"`
+
+		// 大模型服务地址
+        ApiBase *string `json:"apiBase,optional"`
+
+        // 大模型服务密钥
+        ApiKey *string `json:"apiKey,optional"`
     }
 
     // The response data of wx list | Wx列表数据
@@ -78,6 +84,9 @@ type (
         // 租户id
         OrganizationId  *uint64 `json:"organizationId,optional"`
 
+        // 租户名称
+        OrganizationName  *string `json:"organizationName,optional"`
+
         // 端口号 
         Port  *string `json:"port,optional"`
 
@@ -108,13 +117,17 @@ service Wechat {
     @handler createWx
     post /wx/create (WxInfo) returns (BaseMsgResp)
 
+    // Check wx information | 检查并更新Wx状态
+    @handler checkWx
+    post /wx/check (WxInfo) returns (BaseMsgResp)
+
     // Update wx information | 更新Wx
     @handler updateWx
     post /wx/update (WxInfo) returns (BaseMsgResp)
 
     // Delete wx information | 删除Wx信息
     @handler deleteWx
-    post /wx/delete (IDsReq) returns (BaseMsgResp)
+    post /wx/delete (IDReq) returns (BaseMsgResp)
 
     // Get wx list | 获取Wx列表
     @handler getWxList

+ 5 - 3
ent/migrate/schema.go

@@ -598,6 +598,8 @@ var (
 		{Name: "tel", Type: field.TypeString, Comment: "手机号", Default: ""},
 		{Name: "head_big", Type: field.TypeString, Comment: "微信头像", Default: ""},
 		{Name: "organization_id", Type: field.TypeUint64, Nullable: true, Comment: "机构 ID", Default: 1},
+		{Name: "api_base", Type: field.TypeString, Nullable: true, Comment: "大模型服务地址", Default: ""},
+		{Name: "api_key", Type: field.TypeString, Nullable: true, Comment: "大模型服务密钥", Default: ""},
 		{Name: "agent_id", Type: field.TypeUint64, Comment: "模式ID", Default: 0},
 		{Name: "server_id", Type: field.TypeUint64, Nullable: true, Comment: "服务器id", Default: 0},
 	}
@@ -609,13 +611,13 @@ var (
 		ForeignKeys: []*schema.ForeignKey{
 			{
 				Symbol:     "wx_agent_wx_agent",
-				Columns:    []*schema.Column{WxColumns[14]},
+				Columns:    []*schema.Column{WxColumns[16]},
 				RefColumns: []*schema.Column{AgentColumns[0]},
 				OnDelete:   schema.NoAction,
 			},
 			{
 				Symbol:     "wx_server_wxs",
-				Columns:    []*schema.Column{WxColumns[15]},
+				Columns:    []*schema.Column{WxColumns[17]},
 				RefColumns: []*schema.Column{ServerColumns[0]},
 				OnDelete:   schema.SetNull,
 			},
@@ -624,7 +626,7 @@ var (
 			{
 				Name:    "wx_server_id_port",
 				Unique:  true,
-				Columns: []*schema.Column{WxColumns[15], WxColumns[5]},
+				Columns: []*schema.Column{WxColumns[17], WxColumns[5]},
 			},
 			{
 				Name:    "wx_wxid",

+ 147 - 1
ent/mutation.go

@@ -19224,6 +19224,8 @@ type WxMutation struct {
 	head_big           *string
 	organization_id    *uint64
 	addorganization_id *int64
+	api_base           *string
+	api_key            *string
 	clearedFields      map[string]struct{}
 	server             *uint64
 	clearedserver      bool
@@ -19972,6 +19974,104 @@ func (m *WxMutation) ResetAgentID() {
 	m.agent = nil
 }
 
+// SetAPIBase sets the "api_base" field.
+func (m *WxMutation) SetAPIBase(s string) {
+	m.api_base = &s
+}
+
+// APIBase returns the value of the "api_base" field in the mutation.
+func (m *WxMutation) APIBase() (r string, exists bool) {
+	v := m.api_base
+	if v == nil {
+		return
+	}
+	return *v, true
+}
+
+// OldAPIBase returns the old "api_base" field's value of the Wx entity.
+// If the Wx object wasn't provided to the builder, the object is fetched from the database.
+// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
+func (m *WxMutation) OldAPIBase(ctx context.Context) (v string, err error) {
+	if !m.op.Is(OpUpdateOne) {
+		return v, errors.New("OldAPIBase is only allowed on UpdateOne operations")
+	}
+	if m.id == nil || m.oldValue == nil {
+		return v, errors.New("OldAPIBase requires an ID field in the mutation")
+	}
+	oldValue, err := m.oldValue(ctx)
+	if err != nil {
+		return v, fmt.Errorf("querying old value for OldAPIBase: %w", err)
+	}
+	return oldValue.APIBase, nil
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (m *WxMutation) ClearAPIBase() {
+	m.api_base = nil
+	m.clearedFields[wx.FieldAPIBase] = struct{}{}
+}
+
+// APIBaseCleared returns if the "api_base" field was cleared in this mutation.
+func (m *WxMutation) APIBaseCleared() bool {
+	_, ok := m.clearedFields[wx.FieldAPIBase]
+	return ok
+}
+
+// ResetAPIBase resets all changes to the "api_base" field.
+func (m *WxMutation) ResetAPIBase() {
+	m.api_base = nil
+	delete(m.clearedFields, wx.FieldAPIBase)
+}
+
+// SetAPIKey sets the "api_key" field.
+func (m *WxMutation) SetAPIKey(s string) {
+	m.api_key = &s
+}
+
+// APIKey returns the value of the "api_key" field in the mutation.
+func (m *WxMutation) APIKey() (r string, exists bool) {
+	v := m.api_key
+	if v == nil {
+		return
+	}
+	return *v, true
+}
+
+// OldAPIKey returns the old "api_key" field's value of the Wx entity.
+// If the Wx object wasn't provided to the builder, the object is fetched from the database.
+// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
+func (m *WxMutation) OldAPIKey(ctx context.Context) (v string, err error) {
+	if !m.op.Is(OpUpdateOne) {
+		return v, errors.New("OldAPIKey is only allowed on UpdateOne operations")
+	}
+	if m.id == nil || m.oldValue == nil {
+		return v, errors.New("OldAPIKey requires an ID field in the mutation")
+	}
+	oldValue, err := m.oldValue(ctx)
+	if err != nil {
+		return v, fmt.Errorf("querying old value for OldAPIKey: %w", err)
+	}
+	return oldValue.APIKey, nil
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (m *WxMutation) ClearAPIKey() {
+	m.api_key = nil
+	m.clearedFields[wx.FieldAPIKey] = struct{}{}
+}
+
+// APIKeyCleared returns if the "api_key" field was cleared in this mutation.
+func (m *WxMutation) APIKeyCleared() bool {
+	_, ok := m.clearedFields[wx.FieldAPIKey]
+	return ok
+}
+
+// ResetAPIKey resets all changes to the "api_key" field.
+func (m *WxMutation) ResetAPIKey() {
+	m.api_key = nil
+	delete(m.clearedFields, wx.FieldAPIKey)
+}
+
 // ClearServer clears the "server" edge to the Server entity.
 func (m *WxMutation) ClearServer() {
 	m.clearedserver = true
@@ -20060,7 +20160,7 @@ func (m *WxMutation) Type() string {
 // order to get all numeric fields that were incremented/decremented, call
 // AddedFields().
 func (m *WxMutation) Fields() []string {
-	fields := make([]string, 0, 15)
+	fields := make([]string, 0, 17)
 	if m.created_at != nil {
 		fields = append(fields, wx.FieldCreatedAt)
 	}
@@ -20106,6 +20206,12 @@ func (m *WxMutation) Fields() []string {
 	if m.agent != nil {
 		fields = append(fields, wx.FieldAgentID)
 	}
+	if m.api_base != nil {
+		fields = append(fields, wx.FieldAPIBase)
+	}
+	if m.api_key != nil {
+		fields = append(fields, wx.FieldAPIKey)
+	}
 	return fields
 }
 
@@ -20144,6 +20250,10 @@ func (m *WxMutation) Field(name string) (ent.Value, bool) {
 		return m.OrganizationID()
 	case wx.FieldAgentID:
 		return m.AgentID()
+	case wx.FieldAPIBase:
+		return m.APIBase()
+	case wx.FieldAPIKey:
+		return m.APIKey()
 	}
 	return nil, false
 }
@@ -20183,6 +20293,10 @@ func (m *WxMutation) OldField(ctx context.Context, name string) (ent.Value, erro
 		return m.OldOrganizationID(ctx)
 	case wx.FieldAgentID:
 		return m.OldAgentID(ctx)
+	case wx.FieldAPIBase:
+		return m.OldAPIBase(ctx)
+	case wx.FieldAPIKey:
+		return m.OldAPIKey(ctx)
 	}
 	return nil, fmt.Errorf("unknown Wx field %s", name)
 }
@@ -20297,6 +20411,20 @@ func (m *WxMutation) SetField(name string, value ent.Value) error {
 		}
 		m.SetAgentID(v)
 		return nil
+	case wx.FieldAPIBase:
+		v, ok := value.(string)
+		if !ok {
+			return fmt.Errorf("unexpected type %T for field %s", value, name)
+		}
+		m.SetAPIBase(v)
+		return nil
+	case wx.FieldAPIKey:
+		v, ok := value.(string)
+		if !ok {
+			return fmt.Errorf("unexpected type %T for field %s", value, name)
+		}
+		m.SetAPIKey(v)
+		return nil
 	}
 	return fmt.Errorf("unknown Wx field %s", name)
 }
@@ -20366,6 +20494,12 @@ func (m *WxMutation) ClearedFields() []string {
 	if m.FieldCleared(wx.FieldOrganizationID) {
 		fields = append(fields, wx.FieldOrganizationID)
 	}
+	if m.FieldCleared(wx.FieldAPIBase) {
+		fields = append(fields, wx.FieldAPIBase)
+	}
+	if m.FieldCleared(wx.FieldAPIKey) {
+		fields = append(fields, wx.FieldAPIKey)
+	}
 	return fields
 }
 
@@ -20392,6 +20526,12 @@ func (m *WxMutation) ClearField(name string) error {
 	case wx.FieldOrganizationID:
 		m.ClearOrganizationID()
 		return nil
+	case wx.FieldAPIBase:
+		m.ClearAPIBase()
+		return nil
+	case wx.FieldAPIKey:
+		m.ClearAPIKey()
+		return nil
 	}
 	return fmt.Errorf("unknown Wx nullable field %s", name)
 }
@@ -20445,6 +20585,12 @@ func (m *WxMutation) ResetField(name string) error {
 	case wx.FieldAgentID:
 		m.ResetAgentID()
 		return nil
+	case wx.FieldAPIBase:
+		m.ResetAPIBase()
+		return nil
+	case wx.FieldAPIKey:
+		m.ResetAPIKey()
+		return nil
 	}
 	return fmt.Errorf("unknown Wx field %s", name)
 }

+ 8 - 0
ent/runtime/runtime.go

@@ -776,6 +776,14 @@ func init() {
 	wxDescAgentID := wxFields[10].Descriptor()
 	// wx.DefaultAgentID holds the default value on creation for the agent_id field.
 	wx.DefaultAgentID = wxDescAgentID.Default.(uint64)
+	// wxDescAPIBase is the schema descriptor for api_base field.
+	wxDescAPIBase := wxFields[11].Descriptor()
+	// wx.DefaultAPIBase holds the default value on creation for the api_base field.
+	wx.DefaultAPIBase = wxDescAPIBase.Default.(string)
+	// wxDescAPIKey is the schema descriptor for api_key field.
+	wxDescAPIKey := wxFields[12].Descriptor()
+	// wx.DefaultAPIKey holds the default value on creation for the api_key field.
+	wx.DefaultAPIKey = wxDescAPIKey.Default.(string)
 }
 
 const (

+ 6 - 0
ent/schema/wx.go

@@ -52,6 +52,12 @@ func (Wx) Fields() []ent.Field {
 		field.Uint64("agent_id").Default(0).
 			Comment("模式ID").
 			Annotations(entsql.WithComments(true)),
+		field.String("api_base").Optional().Default("").
+			Annotations(entsql.WithComments(true)).
+			Comment("大模型服务地址"),
+		field.String("api_key").Optional().Default("").
+			Annotations(entsql.WithComments(true)).
+			Comment("大模型服务密钥"),
 	}
 }
 

+ 48 - 0
ent/set_not_nil.go

@@ -4278,3 +4278,51 @@ func (w *WxCreate) SetNotNilAgentID(value *uint64) *WxCreate {
 	}
 	return w
 }
+
+// set field if value's pointer is not nil.
+func (w *WxUpdate) SetNotNilAPIBase(value *string) *WxUpdate {
+	if value != nil {
+		return w.SetAPIBase(*value)
+	}
+	return w
+}
+
+// set field if value's pointer is not nil.
+func (w *WxUpdateOne) SetNotNilAPIBase(value *string) *WxUpdateOne {
+	if value != nil {
+		return w.SetAPIBase(*value)
+	}
+	return w
+}
+
+// set field if value's pointer is not nil.
+func (w *WxCreate) SetNotNilAPIBase(value *string) *WxCreate {
+	if value != nil {
+		return w.SetAPIBase(*value)
+	}
+	return w
+}
+
+// set field if value's pointer is not nil.
+func (w *WxUpdate) SetNotNilAPIKey(value *string) *WxUpdate {
+	if value != nil {
+		return w.SetAPIKey(*value)
+	}
+	return w
+}
+
+// set field if value's pointer is not nil.
+func (w *WxUpdateOne) SetNotNilAPIKey(value *string) *WxUpdateOne {
+	if value != nil {
+		return w.SetAPIKey(*value)
+	}
+	return w
+}
+
+// set field if value's pointer is not nil.
+func (w *WxCreate) SetNotNilAPIKey(value *string) *WxCreate {
+	if value != nil {
+		return w.SetAPIKey(*value)
+	}
+	return w
+}

+ 23 - 1
ent/wx.go

@@ -49,6 +49,10 @@ type Wx struct {
 	OrganizationID uint64 `json:"organization_id,omitempty"`
 	// 模式ID
 	AgentID uint64 `json:"agent_id,omitempty"`
+	// 大模型服务地址
+	APIBase string `json:"api_base,omitempty"`
+	// 大模型服务密钥
+	APIKey string `json:"api_key,omitempty"`
 	// Edges holds the relations/edges for other nodes in the graph.
 	// The values are being populated by the WxQuery when eager-loading is set.
 	Edges        WxEdges `json:"edges"`
@@ -95,7 +99,7 @@ func (*Wx) scanValues(columns []string) ([]any, error) {
 		switch columns[i] {
 		case wx.FieldID, wx.FieldStatus, wx.FieldServerID, wx.FieldOrganizationID, wx.FieldAgentID:
 			values[i] = new(sql.NullInt64)
-		case wx.FieldPort, wx.FieldProcessID, wx.FieldCallback, wx.FieldWxid, wx.FieldAccount, wx.FieldNickname, wx.FieldTel, wx.FieldHeadBig:
+		case wx.FieldPort, wx.FieldProcessID, wx.FieldCallback, wx.FieldWxid, wx.FieldAccount, wx.FieldNickname, wx.FieldTel, wx.FieldHeadBig, wx.FieldAPIBase, wx.FieldAPIKey:
 			values[i] = new(sql.NullString)
 		case wx.FieldCreatedAt, wx.FieldUpdatedAt, wx.FieldDeletedAt:
 			values[i] = new(sql.NullTime)
@@ -210,6 +214,18 @@ func (w *Wx) assignValues(columns []string, values []any) error {
 			} else if value.Valid {
 				w.AgentID = uint64(value.Int64)
 			}
+		case wx.FieldAPIBase:
+			if value, ok := values[i].(*sql.NullString); !ok {
+				return fmt.Errorf("unexpected type %T for field api_base", values[i])
+			} else if value.Valid {
+				w.APIBase = value.String
+			}
+		case wx.FieldAPIKey:
+			if value, ok := values[i].(*sql.NullString); !ok {
+				return fmt.Errorf("unexpected type %T for field api_key", values[i])
+			} else if value.Valid {
+				w.APIKey = value.String
+			}
 		default:
 			w.selectValues.Set(columns[i], values[i])
 		}
@@ -300,6 +316,12 @@ func (w *Wx) String() string {
 	builder.WriteString(", ")
 	builder.WriteString("agent_id=")
 	builder.WriteString(fmt.Sprintf("%v", w.AgentID))
+	builder.WriteString(", ")
+	builder.WriteString("api_base=")
+	builder.WriteString(w.APIBase)
+	builder.WriteString(", ")
+	builder.WriteString("api_key=")
+	builder.WriteString(w.APIKey)
 	builder.WriteByte(')')
 	return builder.String()
 }

+ 160 - 0
ent/wx/where.go

@@ -130,6 +130,16 @@ func AgentID(v uint64) predicate.Wx {
 	return predicate.Wx(sql.FieldEQ(FieldAgentID, v))
 }
 
+// APIBase applies equality check predicate on the "api_base" field. It's identical to APIBaseEQ.
+func APIBase(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEQ(FieldAPIBase, v))
+}
+
+// APIKey applies equality check predicate on the "api_key" field. It's identical to APIKeyEQ.
+func APIKey(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEQ(FieldAPIKey, v))
+}
+
 // CreatedAtEQ applies the EQ predicate on the "created_at" field.
 func CreatedAtEQ(v time.Time) predicate.Wx {
 	return predicate.Wx(sql.FieldEQ(FieldCreatedAt, v))
@@ -930,6 +940,156 @@ func AgentIDNotIn(vs ...uint64) predicate.Wx {
 	return predicate.Wx(sql.FieldNotIn(FieldAgentID, vs...))
 }
 
+// APIBaseEQ applies the EQ predicate on the "api_base" field.
+func APIBaseEQ(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEQ(FieldAPIBase, v))
+}
+
+// APIBaseNEQ applies the NEQ predicate on the "api_base" field.
+func APIBaseNEQ(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldNEQ(FieldAPIBase, v))
+}
+
+// APIBaseIn applies the In predicate on the "api_base" field.
+func APIBaseIn(vs ...string) predicate.Wx {
+	return predicate.Wx(sql.FieldIn(FieldAPIBase, vs...))
+}
+
+// APIBaseNotIn applies the NotIn predicate on the "api_base" field.
+func APIBaseNotIn(vs ...string) predicate.Wx {
+	return predicate.Wx(sql.FieldNotIn(FieldAPIBase, vs...))
+}
+
+// APIBaseGT applies the GT predicate on the "api_base" field.
+func APIBaseGT(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldGT(FieldAPIBase, v))
+}
+
+// APIBaseGTE applies the GTE predicate on the "api_base" field.
+func APIBaseGTE(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldGTE(FieldAPIBase, v))
+}
+
+// APIBaseLT applies the LT predicate on the "api_base" field.
+func APIBaseLT(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldLT(FieldAPIBase, v))
+}
+
+// APIBaseLTE applies the LTE predicate on the "api_base" field.
+func APIBaseLTE(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldLTE(FieldAPIBase, v))
+}
+
+// APIBaseContains applies the Contains predicate on the "api_base" field.
+func APIBaseContains(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldContains(FieldAPIBase, v))
+}
+
+// APIBaseHasPrefix applies the HasPrefix predicate on the "api_base" field.
+func APIBaseHasPrefix(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldHasPrefix(FieldAPIBase, v))
+}
+
+// APIBaseHasSuffix applies the HasSuffix predicate on the "api_base" field.
+func APIBaseHasSuffix(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldHasSuffix(FieldAPIBase, v))
+}
+
+// APIBaseIsNil applies the IsNil predicate on the "api_base" field.
+func APIBaseIsNil() predicate.Wx {
+	return predicate.Wx(sql.FieldIsNull(FieldAPIBase))
+}
+
+// APIBaseNotNil applies the NotNil predicate on the "api_base" field.
+func APIBaseNotNil() predicate.Wx {
+	return predicate.Wx(sql.FieldNotNull(FieldAPIBase))
+}
+
+// APIBaseEqualFold applies the EqualFold predicate on the "api_base" field.
+func APIBaseEqualFold(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEqualFold(FieldAPIBase, v))
+}
+
+// APIBaseContainsFold applies the ContainsFold predicate on the "api_base" field.
+func APIBaseContainsFold(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldContainsFold(FieldAPIBase, v))
+}
+
+// APIKeyEQ applies the EQ predicate on the "api_key" field.
+func APIKeyEQ(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEQ(FieldAPIKey, v))
+}
+
+// APIKeyNEQ applies the NEQ predicate on the "api_key" field.
+func APIKeyNEQ(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldNEQ(FieldAPIKey, v))
+}
+
+// APIKeyIn applies the In predicate on the "api_key" field.
+func APIKeyIn(vs ...string) predicate.Wx {
+	return predicate.Wx(sql.FieldIn(FieldAPIKey, vs...))
+}
+
+// APIKeyNotIn applies the NotIn predicate on the "api_key" field.
+func APIKeyNotIn(vs ...string) predicate.Wx {
+	return predicate.Wx(sql.FieldNotIn(FieldAPIKey, vs...))
+}
+
+// APIKeyGT applies the GT predicate on the "api_key" field.
+func APIKeyGT(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldGT(FieldAPIKey, v))
+}
+
+// APIKeyGTE applies the GTE predicate on the "api_key" field.
+func APIKeyGTE(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldGTE(FieldAPIKey, v))
+}
+
+// APIKeyLT applies the LT predicate on the "api_key" field.
+func APIKeyLT(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldLT(FieldAPIKey, v))
+}
+
+// APIKeyLTE applies the LTE predicate on the "api_key" field.
+func APIKeyLTE(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldLTE(FieldAPIKey, v))
+}
+
+// APIKeyContains applies the Contains predicate on the "api_key" field.
+func APIKeyContains(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldContains(FieldAPIKey, v))
+}
+
+// APIKeyHasPrefix applies the HasPrefix predicate on the "api_key" field.
+func APIKeyHasPrefix(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldHasPrefix(FieldAPIKey, v))
+}
+
+// APIKeyHasSuffix applies the HasSuffix predicate on the "api_key" field.
+func APIKeyHasSuffix(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldHasSuffix(FieldAPIKey, v))
+}
+
+// APIKeyIsNil applies the IsNil predicate on the "api_key" field.
+func APIKeyIsNil() predicate.Wx {
+	return predicate.Wx(sql.FieldIsNull(FieldAPIKey))
+}
+
+// APIKeyNotNil applies the NotNil predicate on the "api_key" field.
+func APIKeyNotNil() predicate.Wx {
+	return predicate.Wx(sql.FieldNotNull(FieldAPIKey))
+}
+
+// APIKeyEqualFold applies the EqualFold predicate on the "api_key" field.
+func APIKeyEqualFold(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldEqualFold(FieldAPIKey, v))
+}
+
+// APIKeyContainsFold applies the ContainsFold predicate on the "api_key" field.
+func APIKeyContainsFold(v string) predicate.Wx {
+	return predicate.Wx(sql.FieldContainsFold(FieldAPIKey, v))
+}
+
 // HasServer applies the HasEdge predicate on the "server" edge.
 func HasServer() predicate.Wx {
 	return predicate.Wx(func(s *sql.Selector) {

+ 20 - 0
ent/wx/wx.go

@@ -45,6 +45,10 @@ const (
 	FieldOrganizationID = "organization_id"
 	// FieldAgentID holds the string denoting the agent_id field in the database.
 	FieldAgentID = "agent_id"
+	// FieldAPIBase holds the string denoting the api_base field in the database.
+	FieldAPIBase = "api_base"
+	// FieldAPIKey holds the string denoting the api_key field in the database.
+	FieldAPIKey = "api_key"
 	// EdgeServer holds the string denoting the server edge name in mutations.
 	EdgeServer = "server"
 	// EdgeAgent holds the string denoting the agent edge name in mutations.
@@ -85,6 +89,8 @@ var Columns = []string{
 	FieldHeadBig,
 	FieldOrganizationID,
 	FieldAgentID,
+	FieldAPIBase,
+	FieldAPIKey,
 }
 
 // ValidColumn reports if the column name is valid (part of the table columns).
@@ -135,6 +141,10 @@ var (
 	DefaultOrganizationID uint64
 	// DefaultAgentID holds the default value on creation for the "agent_id" field.
 	DefaultAgentID uint64
+	// DefaultAPIBase holds the default value on creation for the "api_base" field.
+	DefaultAPIBase string
+	// DefaultAPIKey holds the default value on creation for the "api_key" field.
+	DefaultAPIKey string
 )
 
 // OrderOption defines the ordering options for the Wx queries.
@@ -220,6 +230,16 @@ func ByAgentID(opts ...sql.OrderTermOption) OrderOption {
 	return sql.OrderByField(FieldAgentID, opts...).ToFunc()
 }
 
+// ByAPIBase orders the results by the api_base field.
+func ByAPIBase(opts ...sql.OrderTermOption) OrderOption {
+	return sql.OrderByField(FieldAPIBase, opts...).ToFunc()
+}
+
+// ByAPIKey orders the results by the api_key field.
+func ByAPIKey(opts ...sql.OrderTermOption) OrderOption {
+	return sql.OrderByField(FieldAPIKey, opts...).ToFunc()
+}
+
 // ByServerField orders the results by server field.
 func ByServerField(field string, opts ...sql.OrderTermOption) OrderOption {
 	return func(s *sql.Selector) {

+ 164 - 0
ent/wx_create.go

@@ -234,6 +234,34 @@ func (wc *WxCreate) SetNillableAgentID(u *uint64) *WxCreate {
 	return wc
 }
 
+// SetAPIBase sets the "api_base" field.
+func (wc *WxCreate) SetAPIBase(s string) *WxCreate {
+	wc.mutation.SetAPIBase(s)
+	return wc
+}
+
+// SetNillableAPIBase sets the "api_base" field if the given value is not nil.
+func (wc *WxCreate) SetNillableAPIBase(s *string) *WxCreate {
+	if s != nil {
+		wc.SetAPIBase(*s)
+	}
+	return wc
+}
+
+// SetAPIKey sets the "api_key" field.
+func (wc *WxCreate) SetAPIKey(s string) *WxCreate {
+	wc.mutation.SetAPIKey(s)
+	return wc
+}
+
+// SetNillableAPIKey sets the "api_key" field if the given value is not nil.
+func (wc *WxCreate) SetNillableAPIKey(s *string) *WxCreate {
+	if s != nil {
+		wc.SetAPIKey(*s)
+	}
+	return wc
+}
+
 // SetID sets the "id" field.
 func (wc *WxCreate) SetID(u uint64) *WxCreate {
 	wc.mutation.SetID(u)
@@ -349,6 +377,14 @@ func (wc *WxCreate) defaults() error {
 		v := wx.DefaultAgentID
 		wc.mutation.SetAgentID(v)
 	}
+	if _, ok := wc.mutation.APIBase(); !ok {
+		v := wx.DefaultAPIBase
+		wc.mutation.SetAPIBase(v)
+	}
+	if _, ok := wc.mutation.APIKey(); !ok {
+		v := wx.DefaultAPIKey
+		wc.mutation.SetAPIKey(v)
+	}
 	return nil
 }
 
@@ -475,6 +511,14 @@ func (wc *WxCreate) createSpec() (*Wx, *sqlgraph.CreateSpec) {
 		_spec.SetField(wx.FieldOrganizationID, field.TypeUint64, value)
 		_node.OrganizationID = value
 	}
+	if value, ok := wc.mutation.APIBase(); ok {
+		_spec.SetField(wx.FieldAPIBase, field.TypeString, value)
+		_node.APIBase = value
+	}
+	if value, ok := wc.mutation.APIKey(); ok {
+		_spec.SetField(wx.FieldAPIKey, field.TypeString, value)
+		_node.APIKey = value
+	}
 	if nodes := wc.mutation.ServerIDs(); len(nodes) > 0 {
 		edge := &sqlgraph.EdgeSpec{
 			Rel:     sqlgraph.M2O,
@@ -765,6 +809,42 @@ func (u *WxUpsert) UpdateAgentID() *WxUpsert {
 	return u
 }
 
+// SetAPIBase sets the "api_base" field.
+func (u *WxUpsert) SetAPIBase(v string) *WxUpsert {
+	u.Set(wx.FieldAPIBase, v)
+	return u
+}
+
+// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
+func (u *WxUpsert) UpdateAPIBase() *WxUpsert {
+	u.SetExcluded(wx.FieldAPIBase)
+	return u
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (u *WxUpsert) ClearAPIBase() *WxUpsert {
+	u.SetNull(wx.FieldAPIBase)
+	return u
+}
+
+// SetAPIKey sets the "api_key" field.
+func (u *WxUpsert) SetAPIKey(v string) *WxUpsert {
+	u.Set(wx.FieldAPIKey, v)
+	return u
+}
+
+// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
+func (u *WxUpsert) UpdateAPIKey() *WxUpsert {
+	u.SetExcluded(wx.FieldAPIKey)
+	return u
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (u *WxUpsert) ClearAPIKey() *WxUpsert {
+	u.SetNull(wx.FieldAPIKey)
+	return u
+}
+
 // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
 // Using this option is equivalent to using:
 //
@@ -1054,6 +1134,48 @@ func (u *WxUpsertOne) UpdateAgentID() *WxUpsertOne {
 	})
 }
 
+// SetAPIBase sets the "api_base" field.
+func (u *WxUpsertOne) SetAPIBase(v string) *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.SetAPIBase(v)
+	})
+}
+
+// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
+func (u *WxUpsertOne) UpdateAPIBase() *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.UpdateAPIBase()
+	})
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (u *WxUpsertOne) ClearAPIBase() *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.ClearAPIBase()
+	})
+}
+
+// SetAPIKey sets the "api_key" field.
+func (u *WxUpsertOne) SetAPIKey(v string) *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.SetAPIKey(v)
+	})
+}
+
+// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
+func (u *WxUpsertOne) UpdateAPIKey() *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.UpdateAPIKey()
+	})
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (u *WxUpsertOne) ClearAPIKey() *WxUpsertOne {
+	return u.Update(func(s *WxUpsert) {
+		s.ClearAPIKey()
+	})
+}
+
 // Exec executes the query.
 func (u *WxUpsertOne) Exec(ctx context.Context) error {
 	if len(u.create.conflict) == 0 {
@@ -1509,6 +1631,48 @@ func (u *WxUpsertBulk) UpdateAgentID() *WxUpsertBulk {
 	})
 }
 
+// SetAPIBase sets the "api_base" field.
+func (u *WxUpsertBulk) SetAPIBase(v string) *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.SetAPIBase(v)
+	})
+}
+
+// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
+func (u *WxUpsertBulk) UpdateAPIBase() *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.UpdateAPIBase()
+	})
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (u *WxUpsertBulk) ClearAPIBase() *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.ClearAPIBase()
+	})
+}
+
+// SetAPIKey sets the "api_key" field.
+func (u *WxUpsertBulk) SetAPIKey(v string) *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.SetAPIKey(v)
+	})
+}
+
+// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
+func (u *WxUpsertBulk) UpdateAPIKey() *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.UpdateAPIKey()
+	})
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (u *WxUpsertBulk) ClearAPIKey() *WxUpsertBulk {
+	return u.Update(func(s *WxUpsert) {
+		s.ClearAPIKey()
+	})
+}
+
 // Exec executes the query.
 func (u *WxUpsertBulk) Exec(ctx context.Context) error {
 	if u.create.err != nil {

+ 104 - 0
ent/wx_update.go

@@ -256,6 +256,46 @@ func (wu *WxUpdate) SetNillableAgentID(u *uint64) *WxUpdate {
 	return wu
 }
 
+// SetAPIBase sets the "api_base" field.
+func (wu *WxUpdate) SetAPIBase(s string) *WxUpdate {
+	wu.mutation.SetAPIBase(s)
+	return wu
+}
+
+// SetNillableAPIBase sets the "api_base" field if the given value is not nil.
+func (wu *WxUpdate) SetNillableAPIBase(s *string) *WxUpdate {
+	if s != nil {
+		wu.SetAPIBase(*s)
+	}
+	return wu
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (wu *WxUpdate) ClearAPIBase() *WxUpdate {
+	wu.mutation.ClearAPIBase()
+	return wu
+}
+
+// SetAPIKey sets the "api_key" field.
+func (wu *WxUpdate) SetAPIKey(s string) *WxUpdate {
+	wu.mutation.SetAPIKey(s)
+	return wu
+}
+
+// SetNillableAPIKey sets the "api_key" field if the given value is not nil.
+func (wu *WxUpdate) SetNillableAPIKey(s *string) *WxUpdate {
+	if s != nil {
+		wu.SetAPIKey(*s)
+	}
+	return wu
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (wu *WxUpdate) ClearAPIKey() *WxUpdate {
+	wu.mutation.ClearAPIKey()
+	return wu
+}
+
 // SetServer sets the "server" edge to the Server entity.
 func (wu *WxUpdate) SetServer(s *Server) *WxUpdate {
 	return wu.SetServerID(s.ID)
@@ -396,6 +436,18 @@ func (wu *WxUpdate) sqlSave(ctx context.Context) (n int, err error) {
 	if wu.mutation.OrganizationIDCleared() {
 		_spec.ClearField(wx.FieldOrganizationID, field.TypeUint64)
 	}
+	if value, ok := wu.mutation.APIBase(); ok {
+		_spec.SetField(wx.FieldAPIBase, field.TypeString, value)
+	}
+	if wu.mutation.APIBaseCleared() {
+		_spec.ClearField(wx.FieldAPIBase, field.TypeString)
+	}
+	if value, ok := wu.mutation.APIKey(); ok {
+		_spec.SetField(wx.FieldAPIKey, field.TypeString, value)
+	}
+	if wu.mutation.APIKeyCleared() {
+		_spec.ClearField(wx.FieldAPIKey, field.TypeString)
+	}
 	if wu.mutation.ServerCleared() {
 		edge := &sqlgraph.EdgeSpec{
 			Rel:     sqlgraph.M2O,
@@ -700,6 +752,46 @@ func (wuo *WxUpdateOne) SetNillableAgentID(u *uint64) *WxUpdateOne {
 	return wuo
 }
 
+// SetAPIBase sets the "api_base" field.
+func (wuo *WxUpdateOne) SetAPIBase(s string) *WxUpdateOne {
+	wuo.mutation.SetAPIBase(s)
+	return wuo
+}
+
+// SetNillableAPIBase sets the "api_base" field if the given value is not nil.
+func (wuo *WxUpdateOne) SetNillableAPIBase(s *string) *WxUpdateOne {
+	if s != nil {
+		wuo.SetAPIBase(*s)
+	}
+	return wuo
+}
+
+// ClearAPIBase clears the value of the "api_base" field.
+func (wuo *WxUpdateOne) ClearAPIBase() *WxUpdateOne {
+	wuo.mutation.ClearAPIBase()
+	return wuo
+}
+
+// SetAPIKey sets the "api_key" field.
+func (wuo *WxUpdateOne) SetAPIKey(s string) *WxUpdateOne {
+	wuo.mutation.SetAPIKey(s)
+	return wuo
+}
+
+// SetNillableAPIKey sets the "api_key" field if the given value is not nil.
+func (wuo *WxUpdateOne) SetNillableAPIKey(s *string) *WxUpdateOne {
+	if s != nil {
+		wuo.SetAPIKey(*s)
+	}
+	return wuo
+}
+
+// ClearAPIKey clears the value of the "api_key" field.
+func (wuo *WxUpdateOne) ClearAPIKey() *WxUpdateOne {
+	wuo.mutation.ClearAPIKey()
+	return wuo
+}
+
 // SetServer sets the "server" edge to the Server entity.
 func (wuo *WxUpdateOne) SetServer(s *Server) *WxUpdateOne {
 	return wuo.SetServerID(s.ID)
@@ -870,6 +962,18 @@ func (wuo *WxUpdateOne) sqlSave(ctx context.Context) (_node *Wx, err error) {
 	if wuo.mutation.OrganizationIDCleared() {
 		_spec.ClearField(wx.FieldOrganizationID, field.TypeUint64)
 	}
+	if value, ok := wuo.mutation.APIBase(); ok {
+		_spec.SetField(wx.FieldAPIBase, field.TypeString, value)
+	}
+	if wuo.mutation.APIBaseCleared() {
+		_spec.ClearField(wx.FieldAPIBase, field.TypeString)
+	}
+	if value, ok := wuo.mutation.APIKey(); ok {
+		_spec.SetField(wx.FieldAPIKey, field.TypeString, value)
+	}
+	if wuo.mutation.APIKeyCleared() {
+		_spec.ClearField(wx.FieldAPIKey, field.TypeString)
+	}
 	if wuo.mutation.ServerCleared() {
 		edge := &sqlgraph.EdgeSpec{
 			Rel:     sqlgraph.M2O,

+ 44 - 0
internal/handler/Wx/check_wx_handler.go

@@ -0,0 +1,44 @@
+package Wx
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/Wx"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /wx/check Wx CheckWx
+//
+// Check wx information | 检查并更新Wx状态
+//
+// Check wx information | 检查并更新Wx状态
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: WxInfo
+//
+// Responses:
+//  200: BaseMsgResp
+
+func CheckWxHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.WxInfo
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := Wx.NewCheckWxLogic(r.Context(), svcCtx)
+		resp, err := l.CheckWx(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 1 - 1
internal/handler/Wx/delete_wx_handler.go

@@ -27,7 +27,7 @@ import (
 
 func DeleteWxHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
-		var req types.IDsReq
+		var req types.IDReq
 		if err := httpx.Parse(r, &req, true); err != nil {
 			httpx.ErrorCtx(r.Context(), w, err)
 			return

+ 5 - 0
internal/handler/routes.go

@@ -88,6 +88,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
+					Path:    "/wx/check",
+					Handler: Wx.CheckWxHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
 					Path:    "/wx/update",
 					Handler: Wx.UpdateWxHandler(serverCtx),
 				},

+ 115 - 0
internal/logic/Wx/check_wx_logic.go

@@ -0,0 +1,115 @@
+package Wx
+
+import (
+	"context"
+	"errors"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/ent"
+	agentModel "wechat-api/ent/agent"
+	"wechat-api/ent/wx"
+	"wechat-api/hook"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type CheckWxLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewCheckWxLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckWxLogic {
+	return &CheckWxLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *CheckWxLogic) CheckWx(req *types.WxInfo) (resp *types.BaseMsgResp, err error) {
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	if !isAdmin {
+		l.Error("没有权限,请联系管理员", nil)
+		return nil, nil
+	}
+
+	// 获取服务器信息
+	server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
+	if err != nil {
+		l.Error("获取服务器信息失败", err)
+		return nil, err
+	}
+
+	hook := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
+
+	//  判断端口是否被占用
+	portOccupied, err := hook.GetPortOccupiedInfo(*req.Port)
+	if err != nil {
+		l.Error("获取端口占用信息失败", err)
+		return nil, err
+	}
+
+	if portOccupied.Occupied != "1" {
+		l.Error("端口未使用,请重新扫码登陆", err)
+		return nil, err
+	}
+
+	// 判断所选模式是否可用
+	if req.AgentId != nil {
+		agent, err := l.svcCtx.DB.Agent.Query().Where(agentModel.Status(1), agentModel.ID(*req.AgentId)).First(l.ctx)
+		if err != nil && ent.IsNotFound(err) || agent == nil {
+			return nil, errors.New("所选模式不存在或者已被禁用,请选择其他模式")
+		}
+	}
+
+	loginStatus, err := hook.IsLoginStatus()
+	if err != nil {
+		return nil, err
+	}
+
+	if loginStatus.Onlinestatus == "3" {
+		selfInfo, err := hook.GetSelfLoginInfo()
+		if err != nil {
+			return nil, err
+		}
+
+		wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.ServerIDEQ(server.ID)).Where(wx.PortEQ(*req.Port)).Limit(1).Only(l.ctx)
+		if err != nil && !ent.IsNotFound(err) {
+			l.Error("查询微信信息失败", err)
+			return nil, err
+		}
+
+		if req.Callback != nil && *req.Callback != "" {
+			_ = hook.ConfigureMsgRecive("1", *req.Callback)
+		}
+
+		var dbErr error
+		if ent.IsNotFound(err) {
+			l.Error("账号不存在,请联系管理员", err)
+			return nil, err
+		} else {
+			_, dbErr = l.svcCtx.DB.Wx.UpdateOneID(wxInfo.ID).
+				SetNotNilProcessID(&selfInfo.ProcessID).
+				SetNotNilCallback(req.Callback).
+				SetNotNilWxid(&selfInfo.Wxid).
+				SetNotNilAccount(&selfInfo.Account).
+				SetNotNilNickname(&selfInfo.Nickname).
+				SetNotNilTel(&selfInfo.Tel).
+				SetNotNilHeadBig(&selfInfo.HeadBig).
+				SetStatus(1).
+				Save(l.ctx)
+		}
+		if dbErr != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, dbErr, req)
+		}
+	} else {
+		l.Error("微信未登录", "loginStatus", loginStatus)
+		return nil, errorx.NewDefaultError("微信未登陆,当前登陆状态:" + loginStatus.Onlinestatus + "," + loginStatus.Msg)
+	}
+
+	return &types.BaseMsgResp{Msg: errormsg.Success}, nil
+}

+ 33 - 66
internal/logic/Wx/create_wx_logic.go

@@ -2,10 +2,11 @@ package Wx
 
 import (
 	"context"
-	"errors"
 	"github.com/zeromicro/go-zero/core/errorx"
+	"math/rand"
+	"strconv"
+	"time"
 	"wechat-api/ent"
-	agentModel "wechat-api/ent/agent"
 	"wechat-api/ent/wx"
 	"wechat-api/hook"
 	"wechat-api/internal/utils/dberrorhandler"
@@ -32,7 +33,12 @@ func NewCreateWxLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWx
 }
 
 func (l *CreateWxLogic) CreateWx(req *types.WxInfo) (*types.BaseMsgResp, error) {
-	organizationId := l.ctx.Value("organizationId").(uint64)
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	if !isAdmin {
+		l.Error("没有权限,请联系管理员", nil)
+		return nil, errorx.NewDefaultError("没有权限,请联系管理员")
+	}
+	// 获取服务器信息
 	server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
 	if err != nil {
 		l.Error("获取服务器信息失败", err)
@@ -41,82 +47,43 @@ func (l *CreateWxLogic) CreateWx(req *types.WxInfo) (*types.BaseMsgResp, error)
 
 	hook := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
 
+	// 判断端口是否被占用
 	portOccupied, err := hook.GetPortOccupiedInfo(*req.Port)
 	if err != nil {
 		l.Error("获取端口占用信息失败", err)
 		return nil, err
 	}
 
-	if portOccupied.Occupied != "1" {
-		l.Error("端口未使用,请重新扫码登陆", err)
+	if portOccupied.Occupied == "1" {
+		l.Error("端口已占用,请改用其他端口", err)
 		return nil, err
 	}
 
-	// 判断所选模式是否可用
-	if req.AgentId != nil {
-		agent, err := l.svcCtx.DB.Agent.Query().Where(agentModel.Status(1), agentModel.ID(*req.AgentId)).First(l.ctx)
-		if err != nil && ent.IsNotFound(err) || agent == nil {
-			return nil, errors.New("所选模式不存在或者已被禁用,请选择其他模式")
-		}
-	}
-
-	loginStatus, err := hook.IsLoginStatus()
-	if err != nil {
+	_, err = l.svcCtx.DB.Wx.Query().Where(wx.ServerIDEQ(server.ID)).Where(wx.PortEQ(*req.Port)).Limit(1).Only(l.ctx)
+	if err != nil && !ent.IsNotFound(err) {
+		l.Error("查询微信信息失败", err)
 		return nil, err
 	}
 
-	if loginStatus.Onlinestatus == "3" {
-		selfInfo, err := hook.GetSelfLoginInfo()
-		if err != nil {
-			return nil, err
-		}
-
-		wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.ServerIDEQ(server.ID)).Where(wx.PortEQ(*req.Port)).Limit(1).Only(l.ctx)
-		if err != nil && !ent.IsNotFound(err) {
-			l.Error("查询微信信息失败", err)
-			return nil, err
-		}
-
-		var dbErr error
-		if ent.IsNotFound(err) {
-			if req.Callback != nil && *req.Callback != "" {
-				_ = hook.ConfigureMsgRecive("1", *req.Callback)
-			}
-			_, dbErr = l.svcCtx.DB.Wx.Create().
-				SetNotNilServerID(req.ServerId).
-				SetNotNilPort(req.Port).
-				SetNotNilProcessID(&selfInfo.ProcessID).
-				SetNotNilCallback(req.Callback).
-				SetNotNilWxid(&selfInfo.Wxid).
-				SetNotNilAccount(&selfInfo.Account).
-				SetNotNilNickname(&selfInfo.Nickname).
-				SetNotNilTel(&selfInfo.Tel).
-				SetNotNilHeadBig(&selfInfo.HeadBig).
-				SetStatus(1).
-				SetOrganizationID(organizationId).
-				SetNotNilAgentID(req.AgentId).
-				Save(l.ctx)
-		} else {
-			if req.Callback != nil && *req.Callback != "" {
-				_ = hook.ConfigureMsgRecive("1", *req.Callback)
-			}
-			_, dbErr = l.svcCtx.DB.Wx.UpdateOneID(wxInfo.ID).
-				SetNotNilProcessID(&selfInfo.ProcessID).
-				SetNotNilCallback(req.Callback).
-				SetNotNilWxid(&selfInfo.Wxid).
-				SetNotNilAccount(&selfInfo.Account).
-				SetNotNilNickname(&selfInfo.Nickname).
-				SetNotNilTel(&selfInfo.Tel).
-				SetNotNilHeadBig(&selfInfo.HeadBig).
-				SetStatus(1).
-				Save(l.ctx)
-		}
-		if dbErr != nil {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, dbErr, req)
-		}
+	var dbErr error
+	if ent.IsNotFound(err) {
+		wxid := strconv.Itoa(rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000))
+		_, dbErr = l.svcCtx.DB.Wx.Create().
+			SetServerID(*req.ServerId).
+			SetPort(*req.Port).
+			SetCallback(*req.Callback).
+			SetOrganizationID(*req.OrganizationId).
+			SetNotNilAPIBase(req.ApiBase).
+			SetNotNilAPIKey(req.ApiKey).
+			SetWxid(wxid).
+			SetStatus(0).
+			Save(l.ctx)
 	} else {
-		l.Error("微信未登录", "loginStatus", loginStatus)
-		return nil, errorx.NewDefaultError("微信未登陆,当前登陆状态:" + loginStatus.Onlinestatus + "," + loginStatus.Msg)
+		l.Error("端口已使用,请使用其他端口", err)
+		return nil, err
+	}
+	if dbErr != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, dbErr, req)
 	}
 
 	return &types.BaseMsgResp{Msg: errormsg.Success}, nil

+ 58 - 4
internal/logic/Wx/delete_wx_logic.go

@@ -2,6 +2,10 @@ package Wx
 
 import (
 	"context"
+	"math/rand"
+	"strconv"
+	"time"
+	"wechat-api/hook"
 
 	"wechat-api/ent/wx"
 	"wechat-api/internal/svc"
@@ -26,14 +30,64 @@ func NewDeleteWxLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteWx
 	}
 }
 
-func (l *DeleteWxLogic) DeleteWx(req *types.IDsReq) (resp *types.BaseMsgResp, err error) {
+func (l *DeleteWxLogic) DeleteWx(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
 	isAdmin := l.ctx.Value("isAdmin").(bool)
+
+	// 获取账号信息
+	wxInfo, err := l.svcCtx.DB.Wx.Query().
+		Where(
+			wx.IDEQ(req.Id),
+		).
+		Only(l.ctx)
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	// 获取服务器信息
+	serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
+	if err != nil {
+		l.Error("获取服务器信息失败", err)
+		return nil, err
+	}
+
+	hook := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
+
+	//  判断端口是否被占用
+	portOccupied, err := hook.GetPortOccupiedInfo(wxInfo.Port)
+	if err != nil {
+		l.Error("获取端口占用信息失败", err)
+		return nil, err
+	}
+
+	if portOccupied.Occupied == "1" {
+		// 判断账号是否正在登录
+		loginStatus, err := hook.IsLoginStatus()
+		if err != nil {
+			return nil, err
+		}
+
+		if loginStatus.Onlinestatus == "3" {
+			l.Error("请先退出账号再删除", nil)
+			return nil, nil
+		}
+	}
+
+	randNum := strconv.Itoa(rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000))
+
 	if isAdmin {
-		_, err = l.svcCtx.DB.Wx.Delete().Where(wx.IDIn(req.Ids...)).Exec(l.ctx)
+		err = l.svcCtx.DB.Wx.UpdateOneID(req.Id).
+			SetWxid(randNum).
+			SetPort(randNum).
+			Exec(l.ctx)
+		_, err = l.svcCtx.DB.Wx.Delete().Where(wx.IDEQ(req.Id)).Exec(l.ctx)
 	} else {
 		organizationId := l.ctx.Value("organizationId").(uint64)
-
-		_, err = l.svcCtx.DB.Wx.Delete().Where(wx.IDIn(req.Ids...), wx.OrganizationIDEQ(organizationId)).Exec(l.ctx)
+		err = l.svcCtx.DB.Wx.UpdateOneID(req.Id).
+			Where(wx.OrganizationID(organizationId)).
+			SetWxid(randNum).
+			SetPort(randNum).
+			Exec(l.ctx)
+		_, err = l.svcCtx.DB.Wx.Delete().Where(wx.IDEQ(req.Id), wx.OrganizationIDEQ(organizationId)).Exec(l.ctx)
 	}
 
 	if err != nil {

+ 8 - 0
internal/logic/Wx/get_wx_list_logic.go

@@ -50,6 +50,14 @@ func (l *GetWxListLogic) GetWxList(req *types.WxListReq) (*types.WxListResp, err
 		if req.OrganizationId != nil {
 			predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
 		}
+		if req.OrganizationName != nil {
+			departmentList, _ := l.svcCtx.CoreRpc.GetDepartmentList(l.ctx, &core.DepartmentListReq{Name: req.OrganizationName})
+			organizationIds := make([]uint64, 0)
+			for _, department := range departmentList.Data {
+				organizationIds = append(organizationIds, *department.Id)
+			}
+			predicates = append(predicates, wx.OrganizationIDIn(organizationIds...))
+		}
 	}
 	if req.ServerId != nil {
 		predicates = append(predicates, wx.ServerIDEQ(*req.ServerId))

+ 3 - 19
internal/logic/Wx/update_wx_logic.go

@@ -30,32 +30,16 @@ func (l *UpdateWxLogic) UpdateWx(req *types.WxInfo) (resp *types.BaseMsgResp, er
 
 	if isAdmin {
 		err = l.svcCtx.DB.Wx.UpdateOneID(*req.Id).
-			SetNotNilStatus(req.Status).
-			SetNotNilServerID(req.ServerId).
-			SetNotNilPort(req.Port).
-			SetNotNilProcessID(req.ProcessId).
 			SetNotNilCallback(req.Callback).
-			SetNotNilWxid(req.Wxid).
-			SetNotNilAccount(req.Account).
-			SetNotNilNickname(req.Nickname).
-			SetNotNilTel(req.Tel).
-			SetNotNilHeadBig(req.HeadBig).
 			SetNotNilAgentID(req.AgentId).
+			SetNotNilOrganizationID(req.OrganizationId).
+			SetNotNilAPIBase(req.ApiBase).
+			SetNotNilAPIKey(req.ApiKey).
 			Exec(l.ctx)
 	} else {
 		organizationId := l.ctx.Value("organizationId").(uint64)
 		err = l.svcCtx.DB.Wx.UpdateOneID(*req.Id).
 			Where(wx.OrganizationID(organizationId)).
-			SetNotNilStatus(req.Status).
-			SetNotNilServerID(req.ServerId).
-			SetNotNilPort(req.Port).
-			SetNotNilProcessID(req.ProcessId).
-			SetNotNilCallback(req.Callback).
-			SetNotNilWxid(req.Wxid).
-			SetNotNilAccount(req.Account).
-			SetNotNilNickname(req.Nickname).
-			SetNotNilTel(req.Tel).
-			SetNotNilHeadBig(req.HeadBig).
 			SetNotNilAgentID(req.AgentId).
 			Exec(l.ctx)
 	}

+ 2 - 4
internal/logic/Wxhook/refresh_login_q_r_logic.go

@@ -3,7 +3,6 @@ package Wxhook
 import (
 	"context"
 	"github.com/zeromicro/go-zero/core/errorx"
-	"time"
 	"wechat-api/hook"
 
 	"wechat-api/internal/svc"
@@ -28,6 +27,7 @@ func NewRefreshLoginQRLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Re
 
 func (l *RefreshLoginQRLogic) RefreshLoginQR(req *types.RefreshLoginQRReq) (resp *types.RefreshLoginQRResp, err error) {
 
+	// 获取服务器信息
 	server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
 	if err != nil {
 		l.Error("获取服务器信息失败", err)
@@ -35,6 +35,7 @@ func (l *RefreshLoginQRLogic) RefreshLoginQR(req *types.RefreshLoginQRReq) (resp
 	}
 	hookClient := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
 
+	// 如端口未被占用则启动微信
 	portOccupied, err := hookClient.GetPortOccupiedInfo(*req.Port)
 	if err != nil {
 		l.Error("获取端口占用信息失败", err)
@@ -53,9 +54,6 @@ func (l *RefreshLoginQRLogic) RefreshLoginQR(req *types.RefreshLoginQRReq) (resp
 		}
 	}
 
-	// 间隔一秒钟
-	time.Sleep(1 * time.Second)
-
 	loginStatus, err := hookClient.IsLoginStatus()
 	if err != nil {
 		l.Error("查询登录状态失败", err)

+ 6 - 0
internal/types/types.go

@@ -321,6 +321,10 @@ type WxInfo struct {
 	AgentId *uint64 `json:"agentId,optional"`
 	// 模式信息
 	AgentInfo *AgentInfo `json:"agentInfo,optional"`
+	// 大模型服务地址
+	ApiBase *string `json:"apiBase,optional"`
+	// 大模型服务密钥
+	ApiKey *string `json:"apiKey,optional"`
 }
 
 // The response data of wx list | Wx列表数据
@@ -347,6 +351,8 @@ type WxListReq struct {
 	ServerId *uint64 `json:"serverId,optional"`
 	// 租户id
 	OrganizationId *uint64 `json:"organizationId,optional"`
+	// 租户名称
+	OrganizationName *string `json:"organizationName,optional"`
 	// 端口号
 	Port *string `json:"port,optional"`
 	// 进程号