User.php 18 KB

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