UserModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Models;
  12. use App\Services\Api\MemberService;
  13. use App\Services\Common\UserRoleService;
  14. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  15. use Earnp\GoogleAuthenticator\Librarys\GoogleAuthenticator as GoogleSecretAuthenticator;
  16. /**
  17. * 会员管理-模型
  18. * @author laravel开发员
  19. * @since 2020/11/10
  20. * Class AdminModel
  21. * @package App\Models
  22. */
  23. class UserModel extends BaseModel
  24. {
  25. // 设置数据表
  26. protected $table = 'user';
  27. /**
  28. * 获取数据信息
  29. * @param int $id 用户ID
  30. * @return array|string
  31. * @author laravel开发员
  32. * @since 2020/11/10
  33. */
  34. public function getInfo($id)
  35. {
  36. $info = parent::getInfo($id); // TODO: Change the autogenerated stub
  37. if ($info) {
  38. // 头像
  39. if ($info['avatar']) {
  40. $info['avatar'] = get_image_url($info['avatar']);
  41. }
  42. // 性别
  43. if ($info['gender']) {
  44. $info['gender_name'] = config('admin.gender_list')[$info['gender']];
  45. }
  46. // 谷歌验证码处理
  47. $info['google_qrcode'] = '';
  48. $username = $info['username'];
  49. if(empty($info['google_key'])){
  50. $google = new GoogleSecretAuthenticator();
  51. $googleKey = $google->createSecret();//创建一个Secret
  52. $googleUrl="otpauth://totp/sbt@{$username}?secret=".$googleKey;//二维码中填充的内容
  53. if($googleKey && $googleUrl){
  54. UserModel::where(['id'=>$id])->update(['google_key'=> $googleKey,'update_time'=>time()]);
  55. $info['google_key'] = $googleKey;
  56. $info['google_qrcode'] = MemberService::make()->makeQrcode($googleUrl,'google',true);
  57. $info['google_qrcode'] = get_image_url($info['google_qrcode']);
  58. }
  59. }else{
  60. $googleUrl = "otpauth://totp/SBT@{$username}?secret={$info['google_key']}";
  61. $info['google_qrcode'] = MemberService::make()->makeQrcode($googleUrl,'google',true);
  62. $info['google_qrcode'] = get_image_url($info['google_qrcode']);
  63. }
  64. // 获取用户角色列表
  65. $userRoleService = new UserRoleService();
  66. $roleList = $userRoleService->getUserRoleList($id);
  67. $info['roles'] = $roleList;
  68. }
  69. return $info;
  70. }
  71. }