Keyboard Maestro can do this very easily… under certain circumstances.
For example, if I was in such an environment, I would use a macro like this:
And the “trigger” would be unplugging my iPhone from my MacBook… which obviously means that in order for this to work, you have to plug your iPhone into your MacBook when you are sitting at your desk.
I would never leave my iPhone on my desk, so this would be a completely effective way for me to make sure my MacBook was locked when I left my desk.
However, what I actually use at my desk is a Qi-charger, so I don’t have to plug/unplug it ever, and I would find using a USB connection annoying.
That being said, it would be less annoying that buying pizza for the entire office.
The problem with Bluetooth solutions is that all of them I have used are incredibly unreliable. Someone mentioned Knock which is hugely out-of-date, not to mention that it was meant to be used to unlock your Mac, not lock it.
(You can have your USB device name be whatever you want, and it would work if you have an Android device, etc.)
Instead of this…
What I would do instead of this is assign a keyboard shortcut such as
command+control+option+L to run
"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession" -suspend and just press that key combo when I stood up, but that’s not the automated solution you were asking for.
Unlox (formerly MacID)
are supposed to be able to tell when your iPhone moves away from your Mac. I haven’t tried these but older apps like these were unreliable. YMMV.
I tried Unlox and can report that it worked ok but it absolutely destroyed my battery life on my brand new iPhone 11 Pro Max. So… I would not recommend that route.