User.php 18 KB

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