| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- declare (strict_types = 1);
- namespace app\model\api;
- use Closure;
- use library\basic\BaseModel;
- use think\db\exception\DbException;
- use think\Model;
- use think\facade\Db;
- /**
- * @mixin \think\Model
- */
- class User extends BaseModel
- {
- //
- protected $pk = 'uid';
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getList($page, $where = [], $pageCount = 20, $filed = '*', $desc = '')
- {
- $today = strtotime(date("Y-m-d"));
- $data = $this
- ->alias("m")
- ->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"
- . ",(select count(*) from table_user_friends as p where p.uid = m.uid) as friendsCount"//朋友数
- . ",(select count(*) from table_user_attention as a where a.uid = m.uid) as attentionCount"//关注数
- . ",(select count(*) from table_user_attention as f where f.i_uid = m.uid) as fasCount"//粉丝数
- . ",(select count(*) from table_user_visitors as v where v.uid = m.uid) as visitorsCount"//访客数
- . "")
- ->leftJoin("user u","u.uid = m.i_uid")
- ->leftJoin("user_surplus s","s.uid = m.uid and s.time = {$today}")
- ->leftJoin("tuser t","t.tuid = m.i_tuid")
- ->when(!empty($where), function ($query) use ($where) {
- if (!empty($where['mobile'])) {
- $query->where('m.mobile', $where['mobile']);
- }
-
- if (!empty($where['nickname'])) {
- $query->whereLike('m.nickname', "%{$where['nickname']}%");
- }
-
- if (is_numeric($where['levelid']) && in_array(strval($where['levelid']),['0','1','2'])) {
- $query->where('m.levelid', intval($where['levelid']));
- }
-
- if (is_numeric($where['is_certification']) && in_array(strval($where['is_certification']),['0','1','2'])) {
- $query->where('m.is_certification', intval($where['is_certification']));
- }
-
- if (is_numeric($where['is_goddess']) && in_array(strval($where['is_goddess']),['0','1','2'])) {
- $query->where('m.is_goddess', intval($where['is_goddess']));
- }
-
- if (!empty($where['address'])) {
- $query->whereLike('m.address', "%{$where['address']}%");
- }
-
- if (!empty($where['now_address'])) {
- $query->whereLike('m.now_address', "%{$where['now_address']}%");
- }
- if (!empty($where['i_uid'])) {
- $query->where('m.i_uid', $where['i_uid']);
- }
- if (!empty($where['i_tuid'])) {
- $query->where('m.i_tuid', $where['i_tuid']);
- }
-
- if (is_numeric($where["sex"]) && in_array(strval($where["sex"]),["0","1","2"])) {
- $query->where('m.sex', $where["sex"]);
- }
-
- if (is_numeric($where["is_im"]) && in_array(strval($where["is_im"]),["0","1"])) {
- $query->where('m.is_im', $where["is_im"]);
- }
-
- if (is_numeric($where["is_online"]) && in_array(strval($where["is_online"]),["0","1"])) {
- $query->where('m.is_online', $where["is_online"]);
- }
-
- if (is_numeric($where["status"]) && in_array(strval($where["status"]),["0","1","-1"])) {
- $query->where('m.status', $where["status"]);
- }
- if (!empty($where['uid'])) {
- $query->where('m.uid', $where['uid']);
- }
-
- if (!empty($where['uuid'])) {
- $query->where('m.uuid', $where['uuid']);
- }
- //注册时间
- $startTime = "";
- $endTime = "";
- if(!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
- $startTime = strtotime($where['time'][0]);
- $endTime = strtotime($where['time'][1]);
- }
- if (!empty($startTime) && !empty($endTime)) {
- $query->whereBetween("m.regtime","{$startTime},{$endTime}");
- }
-
- })
- ->order($desc)
- ->paginate(['list_rows' => $pageCount, 'page' => $page])
- ->toArray();
- return [$data['total'], $data['data']];
- }
- /**
- * 保存数据
- * @param $data
- * @param $admin_id
- * @return array
- */
- public function saveUpdate($data, $admin_id)
- {
- try {
- self::beginTrans();
-
- if(!is_numeric($data["levelid"]) || !in_array(strval($data["levelid"]),["0","1","2"])){
- unset($data["levelid"]);
- }
- if(empty($data["mobile"])){
- unset($data["mobile"]);
- }else{
- if(!isMobile($data["mobile"])){
- return [0, '请输入正确的手机号码'];
- }
- }
- if(empty($data["nickname"])){
- unset($data["nickname"]);
- }
- if(empty($data["address"])){
- unset($data["address"]);
- }
- if(empty($data["now_address"])){
- unset($data["now_address"]);
- }
- if(!is_numeric($data["status"]) || !in_array(strval($data["status"]),["0","1","-1"])){
- unset($data["status"]);
- }
- if(!is_numeric($data["is_shield"]) || !in_array(strval($data["is_shield"]),["0","1"])){
- unset($data["is_shield"]);
- }
-
- //密码
- if (!empty($data['password'])) {
- $data['password'] = md5($data['password']);
- }else{
- unset($data["password"]);
- }
- if(empty($data["avatar"])){
- unset($data["avatar"]);
- }
- if(!is_numeric($data["sex"]) || !in_array(strval($data["sex"]),["0","1","2"])){
- unset($data["sex"]);
- }
- //特殊设置
- if(empty($data["msg_price"]) || !is_numeric($data["msg_price"])){
- unset($data["msg_price"]);
- }
- if(empty($data["video_price"]) || !is_numeric($data["video_price"])){
- unset($data["video_price"]);
- }
- if(empty($data["audio_price"]) || !is_numeric($data["audio_price"])){
- unset($data["audio_price"]);
- }
- if(empty($data["phone_price"]) || !is_numeric($data["phone_price"])){
- unset($data["phone_price"]);
- }
- if(empty($data["wx_price"]) || !is_numeric($data["wx_price"])){
- unset($data["wx_price"]);
- }
- if(empty($data["qq_price"]) || !is_numeric($data["qq_price"])){
- unset($data["qq_price"]);
- }
- //开关设置
- if(!is_numeric($data["is_rank"]) || !in_array(strval($data["is_rank"]),["0","1"])){
- unset($data["is_rank"]);
- }
- if(!is_numeric($data["is_gift"]) || !in_array(strval($data["is_gift"]),["0","1"])){
- unset($data["is_gift"]);
- }
- if(!is_numeric($data["is_msg"]) || !in_array(strval($data["is_msg"]),["0","1"])){
- unset($data["is_msg"]);
- }
- if(!is_numeric($data["is_video"]) || !in_array(strval($data["is_video"]),["0","1"])){
- unset($data["is_video"]);
- }
- if(!is_numeric($data["is_audio"]) || !in_array(strval($data["is_audio"]),["0","1"])){
- unset($data["is_audio"]);
- }
- //uid
- if (!empty($data['uid'])) {
- $uData = $this->where('uid', $data['uid'])->find()->toArray();
- if (empty($uData)) {
- return [0, '会员不存在'];
- }
- if(!empty($data["mobile"])){
- //判断手机是否重复
- $count = $this
- ->where('mobile', $data['mobile'])
- ->where('uid', '<>', $data['uid'])
- ->count();
- if ($count > 0) {
- return [0, '手机号码已经存在'];
- }
- }
- } else {
- $data['regtime'] = time();
- }
- $this->saveModel($data);
- self::commitTrans();
- return [1, ''];
- } catch (DbException $db) {
- self::rollbackTrans();
- return [0, '操作失败1'];
- } catch (\Exception $e) {
- self::rollbackTrans();
- return [0, '操作失败2'];
- }
- return [0, '操作失败'];
- }
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getItem($uid)
- {
- $data = $this
- ->field("m.*,(select mobile from table_user where uid = m.i_uid) as i_name")
- ->alias("m")
- ->where('uid', $uid)
- ->select();
- return $data;
- }
-
-
-
-
- /**
- * 前端获取用户列表
- * @param type $post
- * @param type $field
- * @return type
- */
- public function getDataList($post,$field="*"){
- $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
- $post["page"] = $post["page"]<=0 ? 1 : (int)$post["page"];
- $where=[];
- if(isset($post["status"]) && in_array((string)$post["status"], ["0","1"])){
- $where[]=["status","=",(int)$post["status"]];
- }
-
- if(!empty($post["nickname"])){
- $where[]=["nickname","like","%{$post["nickname"]}%"];
- }
-
- if(!empty($post["mobile"])){
- $where[]=["mobile","=",$post["mobile"]];
- }
-
- if(!empty($post["parent_id"])){
- $where[]=["parent_id","=",$post["parent_id"]];
- }
-
- $totalCount = $this->where($where)->count();
- $data=null;
- if($totalCount>0){
- $data = $this
- ->field($field)
- ->where($where)
- ->order("uid", "desc")
- ->page($post["page"], $post["pageSize"])
- ->select();
- if(!empty($data)){
- $data = $data->toArray();
- }
- }
- $data = empty($data)?[]:$data;
- return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
- }
-
-
-
- }
|