User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use Closure;
  5. use library\basic\BaseModel;
  6. use think\db\exception\DbException;
  7. use app\model\api\ShowTemplate;
  8. use app\model\api\InfoAudit;
  9. use think\Model;
  10. use think\facade\Db;
  11. /**
  12. * @mixin \think\Model
  13. */
  14. class User extends BaseModel
  15. {
  16. //
  17. protected $pk = 'uid';
  18. /**
  19. * 获取列表数据
  20. * @param $page
  21. * @param $where
  22. * @param $pageCount
  23. * @param $desc
  24. */
  25. public function getList($page, $where = [], $pageCount = 20, $filed = '*', $desc = '')
  26. {
  27. $today = strtotime(date("Y-m-d"));
  28. $data = $this
  29. ->alias("m")
  30. ->field("m.*,u.nickname as i_nickname,u.uuid as i_uuid,t.name as i_tname,t.tuuid as i_tuuid,s.day_surplus_talk,s.day_surplus_video,s.time as surplus_time"
  31. . ",(select count(*) from table_user_friends as p where p.uid = m.uid) as friendsCount"//朋友数
  32. . ",(select count(*) from table_user_attention as a where a.uid = m.uid) as attentionCount"//关注数
  33. . ",(select count(*) from table_user_attention as f where f.i_uid = m.uid) as fasCount"//粉丝数
  34. . ",(select count(*) from table_user_visitors as v where v.uid = m.uid) as visitorsCount"//访客数
  35. . "")
  36. ->leftJoin("user u","u.uid = m.i_uid")
  37. ->leftJoin("user_surplus s","s.uid = m.uid and s.time = {$today}")
  38. ->leftJoin("tuser t","t.tuid = m.i_tuid")
  39. ->when(!empty($where), function ($query) use ($where) {
  40. if (!empty($where['mobile'])) {
  41. $query->where('m.mobile', $where['mobile']);
  42. }
  43. if (!empty($where['nickname'])) {
  44. $query->whereLike('m.nickname', "%{$where['nickname']}%");
  45. }
  46. if (is_numeric($where['levelid']) && in_array(strval($where['levelid']),['0','1','2'])) {
  47. $query->where('m.levelid', intval($where['levelid']));
  48. }
  49. if (is_numeric($where['is_certification']) && in_array(strval($where['is_certification']),['0','1','2'])) {
  50. $query->where('m.is_certification', intval($where['is_certification']));
  51. }
  52. if (is_numeric($where['is_goddess']) && in_array(strval($where['is_goddess']),['0','1','2'])) {
  53. $query->where('m.is_goddess', intval($where['is_goddess']));
  54. }
  55. if (!empty($where['address'])) {
  56. $query->whereLike('m.address', "%{$where['address']}%");
  57. }
  58. if (!empty($where['now_address'])) {
  59. $query->whereLike('m.now_address', "%{$where['now_address']}%");
  60. }
  61. if (!empty($where['i_uid'])) {
  62. $query->where('m.i_uid', $where['i_uid']);
  63. }
  64. if (!empty($where['i_tuid'])) {
  65. $query->where('m.i_tuid', $where['i_tuid']);
  66. }
  67. if (is_numeric($where["sex"]) && in_array(strval($where["sex"]),["0","1","2"])) {
  68. $query->where('m.sex', $where["sex"]);
  69. }
  70. if (is_numeric($where["is_im"]) && in_array(strval($where["is_im"]),["0","1"])) {
  71. $query->where('m.is_im', $where["is_im"]);
  72. }
  73. if (is_numeric($where["is_online"]) && in_array(strval($where["is_online"]),["0","1"])) {
  74. $query->where('m.is_online', $where["is_online"]);
  75. }
  76. if (is_numeric($where["status"]) && in_array(strval($where["status"]),["0","1","-1"])) {
  77. $query->where('m.status', $where["status"]);
  78. }
  79. if (!empty($where['uid'])) {
  80. $query->where('m.uid', $where['uid']);
  81. }
  82. if (!empty($where['uuid'])) {
  83. $query->where('m.uuid', $where['uuid']);
  84. }
  85. //注册时间
  86. $startTime = "";
  87. $endTime = "";
  88. if(!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
  89. $startTime = strtotime($where['time'][0]);
  90. $endTime = strtotime($where['time'][1]);
  91. }
  92. if (!empty($startTime) && !empty($endTime)) {
  93. $query->whereBetween("m.regtime","{$startTime},{$endTime}");
  94. }
  95. })
  96. ->order($desc)
  97. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  98. ->toArray();
  99. return [$data['total'], $data['data']];
  100. }
  101. /**
  102. * 保存数据
  103. * @param $data
  104. * @param $admin_id
  105. * @return array
  106. */
  107. public function saveUpdate($data, $admin_id)
  108. {
  109. try {
  110. self::beginTrans();
  111. if(!is_numeric($data["levelid"]) || !in_array(strval($data["levelid"]),["0","1","2"])){
  112. unset($data["levelid"]);
  113. }
  114. if(empty($data["mobile"])){
  115. unset($data["mobile"]);
  116. }else{
  117. if(!isMobile($data["mobile"])){
  118. return [0, '请输入正确的手机号码'];
  119. }
  120. }
  121. if(empty($data["nickname"])){
  122. unset($data["nickname"]);
  123. }
  124. if(empty($data["address"])){
  125. unset($data["address"]);
  126. }
  127. if(empty($data["now_address"])){
  128. unset($data["now_address"]);
  129. }
  130. if(!is_numeric($data["status"]) || !in_array(strval($data["status"]),["0","1","-1"])){
  131. unset($data["status"]);
  132. }
  133. if(!is_numeric($data["is_shield"]) || !in_array(strval($data["is_shield"]),["0","1"])){
  134. unset($data["is_shield"]);
  135. }
  136. //密码
  137. if (!empty($data['password'])) {
  138. $data['password'] = md5($data['password']);
  139. }else{
  140. unset($data["password"]);
  141. }
  142. if(empty($data["avatar"])){
  143. unset($data["avatar"]);
  144. }
  145. if(!is_numeric($data["sex"]) || !in_array(strval($data["sex"]),["0","1","2"])){
  146. unset($data["sex"]);
  147. }
  148. //特殊设置
  149. if(empty($data["msg_price"]) || !is_numeric($data["msg_price"])){
  150. unset($data["msg_price"]);
  151. }
  152. if(empty($data["video_price"]) || !is_numeric($data["video_price"])){
  153. unset($data["video_price"]);
  154. }
  155. if(empty($data["audio_price"]) || !is_numeric($data["audio_price"])){
  156. unset($data["audio_price"]);
  157. }
  158. if(empty($data["phone_price"]) || !is_numeric($data["phone_price"])){
  159. unset($data["phone_price"]);
  160. }
  161. if(empty($data["wx_price"]) || !is_numeric($data["wx_price"])){
  162. unset($data["wx_price"]);
  163. }
  164. if(empty($data["qq_price"]) || !is_numeric($data["qq_price"])){
  165. unset($data["qq_price"]);
  166. }
  167. //开关设置
  168. if(!is_numeric($data["is_rank"]) || !in_array(strval($data["is_rank"]),["0","1"])){
  169. unset($data["is_rank"]);
  170. }
  171. if(!is_numeric($data["is_gift"]) || !in_array(strval($data["is_gift"]),["0","1"])){
  172. unset($data["is_gift"]);
  173. }
  174. if(!is_numeric($data["is_msg"]) || !in_array(strval($data["is_msg"]),["0","1"])){
  175. unset($data["is_msg"]);
  176. }
  177. if(!is_numeric($data["is_video"]) || !in_array(strval($data["is_video"]),["0","1"])){
  178. unset($data["is_video"]);
  179. }
  180. if(!is_numeric($data["is_audio"]) || !in_array(strval($data["is_audio"]),["0","1"])){
  181. unset($data["is_audio"]);
  182. }
  183. //uid
  184. if (!empty($data['uid'])) {
  185. $uData = $this->where('uid', $data['uid'])->find()->toArray();
  186. if (empty($uData)) {
  187. return [0, '会员不存在'];
  188. }
  189. if(!empty($data["mobile"])){
  190. //判断手机是否重复
  191. $count = $this
  192. ->where('mobile', $data['mobile'])
  193. ->where('uid', '<>', $data['uid'])
  194. ->count();
  195. if ($count > 0) {
  196. return [0, '手机号码已经存在'];
  197. }
  198. }
  199. } else {
  200. $data['regtime'] = time();
  201. }
  202. $this->saveModel($data);
  203. self::commitTrans();
  204. return [1, ''];
  205. } catch (DbException $db) {
  206. self::rollbackTrans();
  207. return [0, '操作失败1'];
  208. } catch (\Exception $e) {
  209. self::rollbackTrans();
  210. return [0, '操作失败2'];
  211. }
  212. return [0, '操作失败'];
  213. }
  214. /**
  215. * 获取列表数据
  216. * @param $page
  217. * @param $where
  218. * @param $pageCount
  219. * @param $desc
  220. */
  221. public function getItem($uid)
  222. {
  223. $data = $this
  224. ->field("m.*,(select mobile from table_user where uid = m.i_uid) as i_name")
  225. ->alias("m")
  226. ->where('uid', $uid)
  227. ->select();
  228. return $data;
  229. }
  230. /**
  231. * 获取小程序在职展示列表
  232. * @return type
  233. */
  234. public function getApiWorkerList($post){
  235. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  236. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  237. $where=[];
  238. $where[]=["u.work_type_id",">",0];
  239. $where[]=["u.status","=",1];
  240. if(!empty($post['work_type_id'])){
  241. $where[]=["u.work_type_id","=",$post['work_type_id']];
  242. }
  243. $totalCount = $this->alias("u")->where($where)->count();
  244. $data=null;
  245. if($totalCount>0){
  246. $data = $this
  247. ->alias("u")
  248. ->field("u.uid,ut.show_template_id")
  249. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//默认模板
  250. ->where($where)
  251. ->order("u.show_temp_seq", "desc")
  252. ->order("u.uid", "desc")
  253. ->page($post["page"], $post["pageSize"])
  254. ->select();
  255. if(!empty($data)){
  256. $data = $data->toArray();
  257. }
  258. // 遍历列表数据,查询每个从业人员的用户信息,添加 is_type_audit 和 ancestral_place 字段
  259. foreach ($data as &$worker) {
  260. $user = User::where('uid', $worker['uid'])->first();
  261. $worker['is_type_audit'] = $user->is_type_audit;
  262. $worker['ancestral_place'] = $user->ancestral_place;
  263. }
  264. $infoAuditDb = new InfoAudit();
  265. foreach($data as $k=>$v){
  266. $item=[
  267. "name" =>"",
  268. "avatar" =>"",
  269. "age" =>"",
  270. "service_project_ar" =>[],
  271. "user_work_type_title" =>"",
  272. "service_area_all" =>[],
  273. "birthday" =>""
  274. ];
  275. $infoData = $infoAuditDb->getItem(["status"=>1,"uid"=>$v["uid"]]);
  276. if(!empty($infoData)){
  277. foreach($item as $k2=>$v2){
  278. $item[$k2] = $infoData[$k2];
  279. }
  280. }
  281. $data[$k] = array_merge($v,$item);
  282. }
  283. }
  284. $data = empty($data)?[]:$data;
  285. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  286. }
  287. /**
  288. * 获取从业人员列表
  289. * @param type $post
  290. * @param type $field
  291. * @param type $is_admin
  292. */
  293. public function getWorkerList($post,$field="*",$is_admin=0){
  294. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  295. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  296. $where=[];
  297. $where[]=["u.work_type_id",">",0];
  298. //用户uid
  299. if(!empty($post['uid'])){
  300. $where[]=["u.uid","=",$post['uid']];
  301. }
  302. if(!empty($post['work_type_id'])){
  303. $where[]=["u.work_type_id","=",$post['work_type_id']];
  304. }
  305. //手机号码
  306. if(!empty($post["mobile"])){
  307. $where[]=["u.mobile","=",$post["mobile"]];
  308. }
  309. //状态
  310. if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
  311. $where[]=["u.status","=",(int)$post["status"]];
  312. }
  313. //父级uid
  314. if(!empty($post["parent_uid"])){
  315. $where[]=["u.parent_uid","=",$post["parent_uid"]];
  316. }
  317. if($is_admin==1){
  318. $field = "u.*"
  319. . ",ut.show_template_id"
  320. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  321. . ",wt.title as work_type_title"
  322. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  323. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  324. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  325. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  326. }
  327. $totalCount = $this->alias("u")->where($where)->count();
  328. $data=null;
  329. if($totalCount>0){
  330. $data = $this
  331. ->alias("u")
  332. ->field($field)
  333. ->leftJoin("user p","p.uid = u.parent_uid")
  334. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  335. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//职称
  336. ->where($where)
  337. ->order("u.show_temp_seq", "desc")
  338. ->order("u.uid", "desc")
  339. ->page($post["page"], $post["pageSize"])
  340. ->select();
  341. if(!empty($data)){
  342. $data = $data->toArray();
  343. }
  344. }
  345. $data = empty($data)?[]:$data;
  346. $ShowTemplateDb = new ShowTemplate();
  347. foreach($data as $k=>$v){
  348. if(!empty($v["regtime"])){
  349. $data[$k]["regtime"] = date("Y-m-d H:i:s",$v["regtime"]);
  350. }
  351. if(!empty($v["parent_time"])){
  352. $data[$k]["parent_time"] = date("Y-m-d H:i:s",$v["parent_time"]);
  353. }
  354. }
  355. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  356. }
  357. /**
  358. * 前端获取用户列表
  359. * @param type $post
  360. * @param type $field
  361. * @return type
  362. */
  363. public function getDataList($post,$field="*",$is_admin=0){
  364. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  365. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  366. $where=[];
  367. //用户uid
  368. if(!empty($post['uid'])){
  369. $where[]=["u.uid","=",$post['uid']];
  370. }
  371. //状态
  372. if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
  373. $where[]=["u.status","=",(int)$post["status"]];
  374. }
  375. //昵称
  376. if(!empty($post["nickname"])){
  377. $where[]=["u.nickname","like","%{$post["nickname"]}%"];
  378. }
  379. //手机号码
  380. if(!empty($post["mobile"])){
  381. $where[]=["u.mobile","=",$post["mobile"]];
  382. }
  383. //父级uid
  384. if(!empty($post["parent_uid"])){
  385. $where[]=["u.parent_uid","=",$post["parent_uid"]];
  386. }
  387. //注册时间
  388. if(!empty($post['time']) && !empty($post['time'][0]) && !empty($post['time'][1])) {
  389. $startTime = strtotime($post['time'][0]);
  390. $endTime = strtotime($post['time'][1]);
  391. $where[]=["u.regtime","between","{$startTime},{$endTime}"];
  392. }
  393. if($is_admin==1){
  394. $field = "u.*"
  395. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  396. . ",wt.title as work_type_title"
  397. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  398. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  399. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  400. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  401. }
  402. $totalCount = $this->alias("u")->where($where)->count();
  403. $data=null;
  404. if($totalCount>0){
  405. $data = $this
  406. ->alias("u")
  407. ->field($field)
  408. ->leftJoin("user p","p.uid = u.parent_uid")
  409. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  410. ->where($where)
  411. ->order("u.uid", "desc")
  412. ->page($post["page"], $post["pageSize"])
  413. ->select();
  414. if(!empty($data)){
  415. $data = $data->toArray();
  416. }
  417. }
  418. $data = empty($data)?[]:$data;
  419. foreach($data as $k=>$v){
  420. if(!empty($v["regtime"])){
  421. $data[$k]["regtime"] = date("Y-m-d H:i:s",$v["regtime"]);
  422. }
  423. if(!empty($v["parent_time"])){
  424. $data[$k]["parent_time"] = date("Y-m-d H:i:s",$v["parent_time"]);
  425. }
  426. }
  427. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  428. }
  429. }