User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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,u.is_type_audit,u.ancestral_place")
  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. $infoAuditDb = new InfoAudit();
  259. foreach($data as $k=>$v){
  260. $item=[
  261. "name" =>"",
  262. "avatar" =>"",
  263. "age" =>"",
  264. "service_project_ar" =>[],
  265. "user_work_type_title" =>"",
  266. "service_area_all" =>[],
  267. "birthday" =>""
  268. ];
  269. $infoData = $infoAuditDb->getItem(["status"=>1,"uid"=>$v["uid"]]);
  270. if(!empty($infoData)){
  271. foreach($item as $k2=>$v2){
  272. $item[$k2] = $infoData[$k2];
  273. }
  274. }
  275. $data[$k] = array_merge($v,$item);
  276. }
  277. }
  278. $data = empty($data)?[]:$data;
  279. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  280. }
  281. /**
  282. * 获取从业人员列表
  283. * @param type $post
  284. * @param type $field
  285. * @param type $is_admin
  286. */
  287. public function getWorkerList($post,$field="*",$is_admin=0){
  288. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  289. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  290. $where=[];
  291. $where[]=["u.work_type_id",">",0];
  292. //用户uid
  293. if(!empty($post['uid'])){
  294. $where[]=["u.uid","=",$post['uid']];
  295. }
  296. if(!empty($post['work_type_id'])){
  297. $where[]=["u.work_type_id","=",$post['work_type_id']];
  298. }
  299. //手机号码
  300. if(!empty($post["mobile"])){
  301. $where[]=["u.mobile","=",$post["mobile"]];
  302. }
  303. //状态
  304. if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
  305. $where[]=["u.status","=",(int)$post["status"]];
  306. }
  307. //父级uid
  308. if(!empty($post["parent_uid"])){
  309. $where[]=["u.parent_uid","=",$post["parent_uid"]];
  310. }
  311. if($is_admin==1){
  312. $field = "u.*"
  313. . ",ut.show_template_id"
  314. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  315. . ",wt.title as work_type_title"
  316. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  317. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  318. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  319. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  320. }
  321. $totalCount = $this->alias("u")->where($where)->count();
  322. $data=null;
  323. if($totalCount>0){
  324. $data = $this
  325. ->alias("u")
  326. ->field($field)
  327. ->leftJoin("user p","p.uid = u.parent_uid")
  328. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  329. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//职称
  330. ->where($where)
  331. ->order("u.show_temp_seq", "desc")
  332. ->order("u.uid", "desc")
  333. ->page($post["page"], $post["pageSize"])
  334. ->select();
  335. if(!empty($data)){
  336. $data = $data->toArray();
  337. }
  338. }
  339. $data = empty($data)?[]:$data;
  340. $ShowTemplateDb = new ShowTemplate();
  341. foreach($data as $k=>$v){
  342. if(!empty($v["regtime"])){
  343. $data[$k]["regtime"] = date("Y-m-d H:i:s",$v["regtime"]);
  344. }
  345. if(!empty($v["parent_time"])){
  346. $data[$k]["parent_time"] = date("Y-m-d H:i:s",$v["parent_time"]);
  347. }
  348. }
  349. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  350. }
  351. /**
  352. * 前端获取用户列表
  353. * @param type $post
  354. * @param type $field
  355. * @return type
  356. */
  357. public function getDataList($post,$field="*",$is_admin=0){
  358. $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
  359. $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
  360. $where=[];
  361. //用户uid
  362. if(!empty($post['uid'])){
  363. $where[]=["u.uid","=",$post['uid']];
  364. }
  365. //状态
  366. if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
  367. $where[]=["u.status","=",(int)$post["status"]];
  368. }
  369. //昵称
  370. if(!empty($post["nickname"])){
  371. $where[]=["u.nickname","like","%{$post["nickname"]}%"];
  372. }
  373. //手机号码
  374. if(!empty($post["mobile"])){
  375. $where[]=["u.mobile","=",$post["mobile"]];
  376. }
  377. //父级uid
  378. if(!empty($post["parent_uid"])){
  379. $where[]=["u.parent_uid","=",$post["parent_uid"]];
  380. }
  381. //注册时间
  382. if(!empty($post['time']) && !empty($post['time'][0]) && !empty($post['time'][1])) {
  383. $startTime = strtotime($post['time'][0]);
  384. $endTime = strtotime($post['time'][1]);
  385. $where[]=["u.regtime","between","{$startTime},{$endTime}"];
  386. }
  387. if($is_admin==1){
  388. $field = "u.*"
  389. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  390. . ",wt.title as work_type_title"
  391. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  392. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  393. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  394. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  395. }
  396. $totalCount = $this->alias("u")->where($where)->count();
  397. $data=null;
  398. if($totalCount>0){
  399. $data = $this
  400. ->alias("u")
  401. ->field($field)
  402. ->leftJoin("user p","p.uid = u.parent_uid")
  403. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  404. ->where($where)
  405. ->order("u.uid", "desc")
  406. ->page($post["page"], $post["pageSize"])
  407. ->select();
  408. if(!empty($data)){
  409. $data = $data->toArray();
  410. }
  411. }
  412. $data = empty($data)?[]:$data;
  413. foreach($data as $k=>$v){
  414. if(!empty($v["regtime"])){
  415. $data[$k]["regtime"] = date("Y-m-d H:i:s",$v["regtime"]);
  416. }
  417. if(!empty($v["parent_time"])){
  418. $data[$k]["parent_time"] = date("Y-m-d H:i:s",$v["parent_time"]);
  419. }
  420. }
  421. return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
  422. }
  423. }