ApiAuthUser.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Base\Services;
  3. use App\Api\Facades\ApiFacade;
  4. use App\User\Facades\UserFacade;
  5. use Illuminate\Support\Facades\Auth;
  6. trait ApiAuthUser
  7. {
  8. /** @var null 用户系统后台任务动态授权 */
  9. private $sysAuthUser = null;
  10. public function setSysAuthUser($sysAuthUser)
  11. {
  12. $this->sysAuthUser = $sysAuthUser;
  13. }
  14. /**
  15. * 获取登录用户ID
  16. * @return int
  17. */
  18. public function getAuthUserId()
  19. {
  20. $user = $this->getAuthUser();
  21. return $user['id'] ?? 0;
  22. }
  23. /**
  24. * 获取登录用户当前主页身份
  25. * @return int
  26. */
  27. public function getAuthCurrEnterpriseId()
  28. {
  29. $user = $this->getAuthUser();
  30. return $user['curr_enterprise_id'] ?? 0;
  31. }
  32. /**
  33. * 获取登录用户个人主页
  34. * @return int
  35. */
  36. public function getAuthPersonalEnterpriseId()
  37. {
  38. $user = $this->getAuthUser();
  39. return $user['enterprise_id'] ?? 0;
  40. }
  41. /**
  42. * 获取token
  43. * @return int
  44. */
  45. public function getAuthApiToken()
  46. {
  47. $user = $this->getAuthUser();
  48. return $user['api_token']??'';
  49. }
  50. /**
  51. * 获取token
  52. * @return int
  53. */
  54. public function getAuthSysApiToken()
  55. {
  56. $user = $this->getAuthUser();
  57. return $user['sys_api_token']??'';
  58. }
  59. /**
  60. * 获取手机
  61. * @return string
  62. */
  63. public function getAuthPhone()
  64. {
  65. $user = $this->getAuthUser();
  66. return $user['phone']??'';
  67. }
  68. /**
  69. * 获取国家编码
  70. * @return string
  71. */
  72. public function getAuthCountryCode()
  73. {
  74. $user = $this->getAuthUser();
  75. return $user['country_code']??'86';
  76. }
  77. /**
  78. * 获取邮箱
  79. * @return string
  80. */
  81. public function getAuthEmail()
  82. {
  83. $user = $this->getAuthUser();
  84. return $user['email']??'';
  85. }
  86. /**
  87. * 获取登录用户真实姓名
  88. * @return int
  89. */
  90. public function getAuthRealName()
  91. {
  92. $user = $this->getAuthUser();
  93. return $user['real_name'] ?? '';
  94. }
  95. /**
  96. * 获取登录用实名认证id
  97. * @return int
  98. */
  99. public function getAuthPersonalCertifiedId()
  100. {
  101. $user = $this->getAuthUser();
  102. return $user['personal_certified_id'] ?? 0;
  103. }
  104. /**
  105. * 获取登录用户企业认证id
  106. * @return int
  107. */
  108. public function getAuthEnterpriseCertifiedId()
  109. {
  110. $user = $this->getAuthUser();
  111. return $user['enterprise_certified_id'] ?? 0;
  112. }
  113. /**
  114. * 获取子账号id列表
  115. * @return string
  116. */
  117. public function getAuthEnterpriseIds()
  118. {
  119. $user = $this->getAuthUser();
  120. return $user['enterprise_ids']??[];
  121. }
  122. /**
  123. * 获取登录用户
  124. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  125. */
  126. public function getAuthUser()
  127. {
  128. // todo 用redis缓存来获取
  129. $user = Auth::user();
  130. if ($user == null) {
  131. $user = $this->sysAuthUser ?: null;
  132. }
  133. return $user;
  134. }
  135. /**
  136. * 获取登录设备语言
  137. * */
  138. public function getAuthDeviceLanguage(){
  139. $user = $this->getAuthUser();
  140. return empty($user['device_language'])?'zh-cn':$user['device_language'];
  141. }
  142. /**
  143. * 获取是否长登录
  144. * */
  145. public function getAuthlongLogin(){
  146. $user = $this->getAuthUser();
  147. return empty($user['long_login'])?0:$user['long_login'];
  148. }
  149. /**
  150. * 获取房间id
  151. * */
  152. public function getAuthRoomId(){
  153. $user = $this->getAuthUser();
  154. return empty($user['room_id'])?'':$user['room_id'];
  155. }
  156. /**
  157. * 更新用户的
  158. * */
  159. public function updateAuthUserData($isDetail=0){
  160. $user=Auth::user();
  161. if(!empty($user)){
  162. if($isDetail){
  163. $newUser = UserFacade::getDetailUserInfoById($user['id']);
  164. }else{
  165. $newUser = UserFacade::getBaseUserInfoById($user['id']);
  166. }
  167. $newUser['device_language']=empty($user['device_language'])?'zh-cn':$user['device_language'];
  168. $newUser['long_login']=empty($user['long_login'])?'0':$user['long_login'];
  169. $newUser['sys_api_token']=empty($user['sys_api_token'])?'':$user['sys_api_token'];
  170. $user = ApiFacade::updateUserInfoCache($user['api_token'], $newUser);
  171. }
  172. return $user;
  173. }
  174. /**
  175. * 获取token
  176. * @return int
  177. */
  178. public function getAuthIsUpdateUser()
  179. {
  180. $user = $this->getAuthUser();
  181. return empty($user['is_update_user'])?false:$user['is_update_user'];
  182. }
  183. }