Wireless sensor networks are not only used for environmental monitoring, medical research, and industrial production, but also increasingly focused on security. While traditional key security mechanisms can withstand external network attacks, they are unable to resist internal network attacks after a node has been compromised by an enemy. To address this issue, the TMSDF algorithm is proposed in this paper, which improves the trust value calculation through adjustments to the adjustment function, penalty function, and node connection degree in the direct and indirect trust. The algorithm also utilizes a dynamic balance weight factor to adjust the direct and indirect trust in the calculation of the integrated trust value. In network deployment using the leach clustering routing method, the trust value and residual energy are integrated into the cluster head election process. The cluster head is responsible for managing trust and electing the fusion node, which performs data fusion to balance the network's energy. Simulation results demonstrate that the proposed method can identify internal attacks by malicious nodes, resist manipulation by malicious nodes, and perform safe data fusion.