User.php 16 KB

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