Googlecode.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\cmgadm\controller\general;
  3. use app\cmgadm\model\Admin;
  4. use app\common\controller\Backend;
  5. use app\common\model\Config;
  6. /**
  7. * 谷歌验证码
  8. *
  9. * @icon fa fa-user
  10. */
  11. class Googlecode extends Backend
  12. {
  13. /**
  14. * @return string|\think\response\Json
  15. * @throws \think\Exception
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. * @throws \think\exception\DbException
  19. */
  20. public function index(){
  21. $google = new \PHPGangsta_GoogleAuthenticator();
  22. $admin = Admin::where(['id'=> $this->auth->id])->field('id,username,nickname,google_key')->find();
  23. $googleKey = isset($admin['google_key'])? $admin['google_key'] : '';
  24. if(empty($googleKey)){
  25. $secret = $google->createSecret();
  26. Admin::where(['id'=> $this->auth->id])->update(['google_key'=> $secret]);
  27. }
  28. $config = Config::getConfigByGroup('basic');
  29. $siteName = isset($config['name'])? $config['name']['value'] : '茶马古道';
  30. $qrcode = $google->getQRCodeGoogleUrl($admin['username'],$googleKey,$siteName);
  31. $this->view->assign('admin', $admin);
  32. $this->view->assign('qrcode', $qrcode);
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 绑定
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function bind()
  42. {
  43. if(Admin::where(['id'=> $this->auth->id])->update(['google_bind'=>1,'update_time'=>time()])){
  44. $this->success('绑定成功');
  45. }else{
  46. $this->error('绑定失败');
  47. }
  48. }
  49. }