User.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\admin\model;
  3. use app\BaseModel;
  4. use think\model\concern\SoftDelete;
  5. use thans\jwt\facade\JWTAuth;
  6. class User extends BaseModel
  7. {
  8. use SoftDelete;
  9. protected $pk = "user_id";
  10. public static $defaultField = 'user_id,realname,realname as displayName,account,avatar,name_py,email,last_login_ip';
  11. protected $json = ['setting'];
  12. protected $jsonAssoc = true;
  13. public function getAvatarAttr($value,$data)
  14. {
  15. return avatarUrl($data['avatar'],$data['realname'],$data['user_id']);
  16. }
  17. public function getSettingAttr($value)
  18. {
  19. if (!$value) return null;
  20. $setting = $value;
  21. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  22. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  23. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  24. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  25. $setting['sendKey']=(int)$setting['sendKey'];
  26. return $setting;
  27. }
  28. /**
  29. * 刷新用户token 之前token将被拉黑
  30. * 修改用户数据后 调用该方法 并返回前台更新token
  31. * @param array $info 用户信息
  32. * @param string $terminal 客户端标识
  33. * @return string
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public static function refreshToken($info,$terminal, $admin = [])
  39. {
  40. $info = str_encipher(json_encode($info),true, config('app.aes_token_key'));
  41. $authToken = 'bearer '.JWTAuth::builder(['info' => $info, 'terminal' => $terminal, 'admin' => $admin]);
  42. return $authToken;
  43. }
  44. //添加客服用户
  45. public static function addCs($params) {
  46. self::create([
  47. 'account' => $params['username'],
  48. 'realname' => $params['nickname'] ?? '',
  49. 'password' => $params['password'],
  50. 'sex' => $params['sex'],
  51. 'role' => $params['id'] == 1 ? 1 : 2,
  52. 'remark' => $params['remark'] ?? '',
  53. 'cs_uid' => $params['id'],
  54. 'friend_limit' => 500,
  55. 'group_limit' => 500,
  56. 'phone' => $params['phone'] ?? '',
  57. ]);
  58. }
  59. }