UserAdminService.php 5.1 KB

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