123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use app\common\library\Keyworld;
- use think\Exception;
- class Topicdongtai extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
-
- public function addone(){
- $content = input('content','');
- $images = input('images','');
- $audio_file = input('audio_file','');
- $topic_ids = input('topic_ids','');
- $aite = input('aite','','htmlspecialchars_decode');
- $type = input('type',1);
- $audio_second = input('audio_second',0);
- if(!$content && !$images && !$audio_file){
- $this->error(__('Invalid parameters'));
- }
-
- $content = Keyworld::sensitive($content);
-
- if($type == 1){
- $audio_file = '';
- $audio_second = 0;
- }else{
- $images = '';
- }
- $data = [
- 'topic_ids' => $topic_ids,
- 'user_id' => $this->auth->id,
- 'content' => $content,
- 'images' => $images,
- 'audio_file' => $audio_file,
- 'type' => $type,
- 'cityname' => input('cityname',''),
- 'aite' => $aite,
- 'is_public' => input('is_public',1),
- 'audio_second' => $audio_second,
- 'longitude' => input('longitude',0),
- 'latitude' => input('latitude',0),
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- Db::startTrans();
- $id = Db::name('topic_dongtai')->insertGetId($data);
-
- $rs = Db::name('topic_hub')->where('id','IN',$topic_ids)->setInc('t_number');
- Db::commit();
- $this->success('发布成功',$id);
- }
-
-
- public function my_lists(){
- $uid = input('uid',$this->auth->id);
- if (empty($uid)) {
- $uid = $this->auth->id;
- }
- $where = ['dt.user_id'=>$uid];
- if($uid != $this->auth->id){
- $where['dt.is_public'] = 1;
- }
- $list = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->join('user_wallet uw','user.id = uw.user_id','LEFT')
- ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
- ->where($where)
- ->order('dt.id desc')->autopage()->select();
- $list = list_domain_image($list,['images','audio_file','avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
- $val['aite'] = json_decode($val['aite'],true);
-
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
-
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- unset($val['vip_endtime']);
-
- $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
-
- $val['createtime_text'] = get_last_time($val['createtime']);
-
- $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
-
- $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
-
- $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
- }
- }
- $this->success('success',$list);
- }
-
- public function delete(){
- $id = input('id',0);
- $where['id'] = $id;
- $where['user_id'] = $this->auth->id;
- $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->where($where)->find();
- if (empty($dongtai)) {
- $this->error('未找到动态信息');
- }
- Db::startTrans();
- $delRes = Db::name('topic_dongtai')->where('id',$id)->where('user_id',$this->auth->id)->delete();
- if (!$delRes) {
- Db::rollback();
- $this->error('动态删除失败');
- }
-
- if (!empty($dongtai['topic_ids'])) {
- $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
- if (!$res) {
- Db::rollback();
- $this->error('更新话题数量失败');
- }
- }
-
- Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
-
- Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
-
- Db::name('topic_answer_good')->where('dt_id',$id)->delete();
- Db::commit();
- $this->success('删除成功');
- }
-
- public function topic_list(){
- $where = ['dt.is_public' => 1];
-
- $topic_id = input('topic_id',0);
- $where_exp = [];
- if($topic_id){
- $where_exp[] = ['exp',Db::raw("FIND_IN_SET('".$topic_id."',dt.topic_ids)")];
- }
-
- $order = input('orderby','new');
- $orderby = 'dt.id desc';
-
- if($order == 'follow'){
- $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
- $where['dt.user_id'] = ['IN',$follow_user_ids];
- }
-
- if($order == 'near'){
- $orderby = 'distance asc';
- }
-
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
-
- $attribute = input('attribute','all');
- if($attribute != 'all' && $attribute != 'BOTH'){
- $where['user.attribute'] = $attribute;
- }
-
- $where2 = [];
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where2['dt.user_id'] = ['NOTIN',$black_ids];
- }
-
- $field = 'dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime';
- if($order == 'near'){
- $field .= ',(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(dt.longitude,dt.latitude))*111195) as distance';
- }
- $list = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->join('user_wallet uw','user.id = uw.user_id','LEFT')
- ->field($field)
- ->where($where)
- ->where($where2)
- ->where($where_exp)
- ->order($orderby)
- ->autopage()->select();
- $list = list_domain_image($list,['images','audio_file','avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
-
- $val['aite'] = json_decode($val['aite'],true);
-
- $val['distance'] = isset($val['distance']) ? $val['distance'] : 0;
- $val['distance'] = bcdiv(intval($val['distance']),1000,2).'km';
-
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
-
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- unset($val['vip_endtime']);
-
- $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
-
- $val['createtime_text'] = get_last_time($val['createtime']);
-
- $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
-
- $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
-
- $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
-
- }
- }
- $this->success('success',$list);
- }
-
- public function info(){
- $id = input('id');
- $info = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->join('user_wallet uw','user.id = uw.user_id','LEFT')
- ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
- ->where('dt.id',$id)->find();
- $info = info_domain_image($info,['images','audio_file','avatar']);
- if($info){
- $info['aite'] = json_decode($info['aite'],true);
-
- $info['age'] = birthtime_to_age($info['birthday']);
- unset($info['birthday']);
-
- $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
- unset($info['vip_endtime']);
-
- $info['isgood'] = $this->is_good($id,$this->auth->id);
-
- $info['createtime_text'] = get_last_time($info['createtime']);
-
- $info['is_follow'] = $this->is_follow($info['user_id'],$this->auth->id);
-
- $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
-
- $info['topic_text'] = Db::name('topic_hub')->where('id','IN',$info['topic_ids'])->column('name');
- }
- $this->success('success',$info);
- }
-
- public function good(){
- $id = input('id');
- $where = [
- 'dt_id' => $id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('topic_dongtai_good')->where($where)->find();
- if($check){
- $this->error('已经赞过了');
- }
- Db::startTrans();
- $where['createtime'] = time();
- $rs = Db::name('topic_dongtai_good')->insertGetId($where);
- if(!$rs){
- Db::rollback();
- $this->error('点赞失败');
- }
- $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
- if($up === false){
- Db::rollback();
- $this->error('点赞失败');
- }
- Db::commit();
- $this->success('点赞成功');
- }
-
- public function answer(){
- $id = input('id',0);
- $content = input('content','');
- $to_user_id = input('to_user_id',0);
- $level = input('level',1);
- $floor = input('floor',0);
- if(empty($content) || empty($id)){
- $this->error();
- }
-
- $content = Keyworld::sensitive($content);
-
- if($level == 2 && $floor == 0){
- $this->error('楼层错误');
- }
-
- if($level == 1 || $floor == 0){
- $to_user_id = 0;
- $floor = 1;
- $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
- if($last_floor){
- $floor = $last_floor + 1;
- }
- }
-
- if($to_user_id){
- $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
- if(empty($to_user)){
- $this->error('被回复的用户不存在');
- }
- }
-
- $data = [
- 'dt_id' => $id,
- 'floor' => $floor,
- 'user_id' => $this->auth->id,
- 'content' => $content,
- 'to_user_id' => $to_user_id,
- 'level' => $level,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- Db::startTrans();
- $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
- Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
- Db::commit();
- $this->success('评价成功');
- }
-
- public function answer_good(){
- $dt_id = input('dt_id',0);
- $answer_id = input('answer_id',0);
- $where = [
- 'dt_id' => $dt_id,
- 'answer_id' => $answer_id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('topic_answer_good')->where($where)->find();
- if($check){
- $this->error('已经赞过了');
- }
- Db::startTrans();
- $where['createtime'] = time();
- $rs = Db::name('topic_answer_good')->insertGetId($where);
- $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
- if($rs && $up !== false){
- Db::commit();
- $this->success('点赞成功');
- }
- Db::rollback();
- $this->error('点赞失败');
- }
-
-
-
-
-
- public function collect(){
- $where = [
- 'user_id' => $this->auth->id,
- 'table' => 'topic_dongtai',
- 'table_id' => input('id',0),
- ];
- $check = Db::name('user_collect')->where($where)->find();
- if($check){
- $this->success('已经收藏过了');
- }else{
- Db::name('user_collect')->insertGetId($where);
- $this->success('收藏成功');
- }
- }
-
-
-
- public function answer_list(){
- $dt_id = input('dt_id',0);
-
- $floor_list = Db::name('topic_dongtai_answer')
- ->alias('a')
- ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
- ->join('user','a.user_id = user.id','LEFT')
- ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
- $floor_list = list_domain_image($floor_list,['avatar']);
-
- if(!empty($floor_list)){
- foreach($floor_list as $key => &$val){
-
- $val['childremark'] = '';
- $map = [
- 'a.dt_id' => $dt_id,
- 'a.floor' => $val['floor'],
- 'a.level' => 2,
- ];
- $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
- if($number > 0){
- $answer_info = Db::name('topic_dongtai_answer')
- ->alias('a')
- ->field('user.nickname')
- ->join('user','a.user_id = user.id','LEFT')
- ->where($map)->order('a.id desc')->find();
- $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
- }
-
- $val['createtime_text'] = get_last_time($val['createtime']);
-
- $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
-
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
- }
- }
- $this->success(1,$floor_list);
- }
-
- public function answer_info(){
- $answer_id = input('answer_id');
-
- $floor_info = Db::name('topic_dongtai_answer')
- ->alias('a')
- ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
- ->join('user','a.user_id = user.id','LEFT')
- ->where(['a.id'=>$answer_id])->find();
- $floor_info = info_domain_image($floor_info,['avatar']);
- $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
-
- $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
- unset($floor_info['birthday']);
-
- $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
- $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
-
- $floors = $floor_info['floor'];
- $child_lists = Db::name('topic_dongtai_answer')->alias('a')
- ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
- ->join('user','a.user_id = user.id','LEFT')
- ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
- $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
- if(!empty($child_lists)){
- foreach($child_lists as $key => &$answer){
-
- $answer['age'] = birthtime_to_age($answer['birthday']);
- unset($answer['birthday']);
- $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
- $answer['createtime_text'] = get_last_time($answer['createtime']);
- }
- }
-
- $floor_info['child'] = $child_lists;
- $this->success('success',$floor_info);
- }
-
- private function is_good($dt_id,$uid){
- $where = [
- 'dt_id' => $dt_id,
- 'user_id' => $uid,
- ];
- $check = Db::name('topic_dongtai_good')->where($where)->find();
- if($check){
- return 1;
- }else{
- return 0;
- }
- }
-
- private function answer_is_good($answer_id,$uid){
- $where = [
- 'answer_id' => $answer_id,
- 'user_id' => $uid,
- ];
- $check = Db::name('topic_answer_good')->where($where)->find();
- if($check){
- return 1;
- }else{
- return 0;
- }
- }
-
- private function is_follow($uid,$follow_uid){
- $where = [
- 'uid' => $uid,
- 'follow_uid' => $follow_uid,
- ];
- $check = Db::name('user_follow')->where($where)->find();
- if($check){
- return 1;
- }else{
- return 0;
- }
- }
-
- public function my_answer(){
- $map = [
- 'dt.user_id' => $this->auth->id,
- 'a.level' => 1,
- ];
- $list = Db::name('topic_dongtai_answer')->alias('a')
- ->field('a.id,a.createtime,a.content,
- dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,
- user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
- ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
- ->join('user dtuser','dt.user_id = dtuser.id','LEFT')
- ->join('user','a.user_id = user.id','LEFT')
- ->join('user_wallet uw','user.id = uw.user_id','LEFT')
- ->where($map)->order('a.id desc')->autopage()->select();
- $list = list_domain_image($list,['avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
-
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
-
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- unset($val['vip_endtime']);
- }
- }
- $this->success(1,$list);
- }
-
- public function delete_answer(){
- $id = input('id',0);
- Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->delete();
- $this->success();
- }
-
- public function my_good(){
- $where = ['good.user_id'=>$this->auth->id];
- $list = Db::name('topic_dongtai')->alias('dt')
- ->join('user','dt.user_id = user.id','LEFT')
- ->join('user_wallet uw','user.id = uw.user_id','LEFT')
- ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')
- ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
- ->where($where)
- ->order('dt.id desc')->autopage()->select();
- $list = list_domain_image($list,['images','audio_file','avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
-
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
-
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- unset($val['vip_endtime']);
-
- $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
-
- $val['createtime_text'] = get_last_time($val['createtime']);
-
- $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
-
- $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
-
- $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
- }
- }
- $this->success('success',$list);
- }
- }
|