UserAdminService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\DataUser;
  4. use app\data\model\DataUserToken;
  5. use think\admin\Exception;
  6. use think\admin\Service;
  7. /**
  8. * 用户数据管理服务
  9. * Class UserAdminService
  10. * @package app\data\service
  11. */
  12. class UserAdminService extends Service
  13. {
  14. const API_TYPE_WAP = 'wap';
  15. const API_TYPE_WEB = 'web';
  16. const API_TYPE_WXAPP = 'wxapp';
  17. const API_TYPE_WECHAT = 'wechat';
  18. const API_TYPE_IOSAPP = 'iosapp';
  19. const API_TYPE_ANDROID = 'android';
  20. const TYPES = [
  21. // 接口支付配置(不需要的直接注释)
  22. self::API_TYPE_WAP => [
  23. 'name' => '手机浏览器',
  24. 'auth' => 'phone',
  25. ],
  26. self::API_TYPE_WEB => [
  27. 'name' => '电脑浏览器',
  28. 'auth' => 'phone',
  29. ],
  30. self::API_TYPE_WXAPP => [
  31. 'name' => '微信小程序',
  32. 'auth' => 'openid1',
  33. ],
  34. self::API_TYPE_WECHAT => [
  35. 'name' => '微信服务号',
  36. 'auth' => 'openid2',
  37. ],
  38. self::API_TYPE_IOSAPP => [
  39. 'name' => '苹果APP应用',
  40. 'auth' => 'phone',
  41. ],
  42. self::API_TYPE_ANDROID => [
  43. 'name' => '安卓APP应用',
  44. 'auth' => 'phone',
  45. ],
  46. ];
  47. /**
  48. * 更新用户用户参数
  49. * @param mixed $map 查询条件
  50. * @param array $data 更新数据
  51. * @param string $type 接口类型
  52. * @param boolean $force 强刷令牌
  53. * @return array
  54. * @throws \think\admin\Exception
  55. * @throws \think\db\exception\DbException
  56. */
  57. public static function set($map, array $data, string $type, bool $force = false): array
  58. {
  59. unset($data['id'], $data['deleted'], $data['create_at']);
  60. $user = DataUser::mk()->where($map)->where(['deleted' => 0])->findOrEmpty();
  61. if (!$user->save($data)) throw new Exception("更新用户资料失败!");
  62. // 刷新用户认证令牌
  63. if ($force) UserTokenService::token($user['id'], $type);
  64. // 返回当前用户资料
  65. return static::get($user['id'], $type);
  66. }
  67. /**
  68. * 获取用户数据
  69. * @param integer $uuid 用户UID
  70. * @param string $type 接口类型
  71. * @return array
  72. * @throws \think\admin\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public static function get(int $uuid, string $type): array
  78. {
  79. $map = ['id' => $uuid, 'deleted' => 0];
  80. $user = DataUser::mk()->where($map)->findOrEmpty();
  81. if ($user->isEmpty()) throw new Exception('用户还没有注册!');
  82. // 用户认证令牌处理
  83. $map = ['uuid' => $uuid, 'type' => $type];
  84. if (!($access = DataUserToken::mk()->where($map)->find())) {
  85. [$state, $message, $access] = UserTokenService::token($uuid, $type);
  86. if (empty($state) || empty($access)) throw new Exception($message);
  87. }
  88. $user['token'] = ['token' => $access['token'], 'expire' => $access['time']];
  89. return $user->hidden(['deleted', 'password'])->toArray();
  90. }
  91. /**
  92. * 获取用户数据统计
  93. * @param int $uuid 用户UID
  94. * @return array
  95. */
  96. public static function total(int $uuid): array
  97. {
  98. return ['my_invite' => DataUser::mk()->where(['pid1' => $uuid])->count()];
  99. }
  100. /**
  101. * 获取用户查询条件
  102. * @param string $field 认证字段
  103. * @param string $openid 用户OPENID值
  104. * @param string $unionid 用户UNIONID值
  105. * @return array
  106. */
  107. public static function getUserUniMap(string $field, string $openid, string $unionid = ''): array
  108. {
  109. if (!empty($unionid)) {
  110. [$map1, $map2] = [[['unionid', '=', $unionid]], [[$field, '=', $openid]]];
  111. if ($uuid = DataUser::mk()->whereOr([$map1, $map2])->value('id')) {
  112. return ['id' => $uuid];
  113. }
  114. }
  115. return [$field => $openid];
  116. }
  117. /**
  118. * 列表绑定用户数据
  119. * @param array $list 原数据列表
  120. * @param string $keys 用户UID字段
  121. * @param string $bind 绑定字段名称
  122. * @param string $cols 返回用户字段
  123. * @return array
  124. */
  125. public static function buildByUid(array &$list, string $keys = 'uuid', string $bind = 'user', string $cols = '*'): array
  126. {
  127. if (count($list) < 1) return $list;
  128. $uids = array_unique(array_column($list, $keys));
  129. $users = DataUser::mk()->whereIn('id', $uids)->column($cols, 'id');
  130. foreach ($list as &$vo) $vo[$bind] = $users[$vo[$keys]] ?? [];
  131. return $list;
  132. }
  133. }