$$ \boldsymbol{M_{1}}\cdot\boldsymbol{r} = M_{1} r \cos\left(\theta_{1} \right) \tag{1} $$
$$ \boldsymbol{M_{2}}\cdot\boldsymbol{r} = M_{2} r \cos\left(\theta_{2} \right) \tag{2} $$
$$ \boldsymbol{M_{1}}\cdot\boldsymbol{M_{2}} = M_{1} M_{2}\left( \cos\left(\theta_{1} \right) \cos\left(\theta_{2} \right) + \sin\left(\theta_{1} \right) \sin\left(\theta_{2} \right) \cos\left(\phi\right) \right) \tag{3} $$
About the third tag , totally I can't get what is going on.
Which website(s) should I refer?

From the figure, you see the plane created by $\vec r$ and $\vec M_1$. You project $\vec M_2$ onto this plane (a vector component perpendicular to the plane is perpendicular to $\vec M_1$ and does not contribute to the dot product). The length is $M_2\cos\phi$. In this plane, the relative angle between $\vec M_1$ and the projection of $\vec M_2$ is $\theta_1-\theta_2$. Then use $$\cos(\theta_1-\theta_2)=\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2$$ You will then get the answer.