User.php 12 KB

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