成就大师你就看vn

1 <?php
2 namespace app\index\controller;
3
4 use think\Controller;
5 use think\Db;
6 use think\Picture;
7
8 class Chat extends Controller
9 {
10 public function privateChat()
11 {
12 $id = $this->request->param('id');
13
14 if(!$id){
15 $this->error('请求错误,请重新登录');
16 }
17 $res = Db::name('accounts')->where(['id'=>$id,'status'=>1])->field('type,info_id,id')->find();
18 $head = new Circle();
19 $re = $head->adopt_id_get_userInfo($id);
20 $res['name'] = $re['cname'];
21 $res['photo']= $re['photo'];
22
23
24 $user = session('da');
25 Db::name('chat_record')->where(['form'=>$user['id'],'to'=>$res['id'],'type'=>1])->setField('read',1);
26
27 $sql = "SELECT * FROM `ams_chat_record` WHERE (`to` = ".$res['id']." AND `form` = ".$user['id']." AND `type` = 1) OR (`to` = ".$user['id']." AND `form` = ".$res['id']." AND `type` = 1) ";
28 $msg = Db::query($sql);
29
30 /*$redis = new Redis();
31 $hand = $redis->handler();
32 // $redis->clear();
33 $msg = $redis->get($id.'_'.session('da.id'))?$redis->get($id.'_'.session('da.id')):[];
34 $msg1 = $redis->get(session('da.id').'_'.$id)?$redis->get(session('da.id').'_'.$id):[];
35 // dump($msg);
36 // dump($msg1);
37 $msg = array_merge($msg,$msg1);
38 // 排序
39 array_multisort(array_column($msg,'sent'),SORT_ASC,$msg );*/
40 $nickname = Db::name('remake')->where(['user'=>$user['id'],'friend'=>$res['id']])->field('remake')->find();
41 if($nickname){
42 $res['name'] = $nickname['remake'];
43 }
44
45 $this->assign('user',$user);
46 $this->assign('res',$res);
47 $this->assign('msg',$msg);
48 return $this->fetch();
49 }
50
51 public function new_privateChat()
52 {
53 $id = $this->request->param('id');
54 if(!$id){
55 $this->error('请求错误,请重新登录');
56 }
57 $res = Db::name('accounts')->where(['id'=>$id,'status'=>1])->field('type,info_id,id')->find();
58 $head = new Circle();
59 $re = $head->adopt_id_get_userInfo($id);
60 $res['name'] = $re['cname'];
61 $res['photo']= $re['photo'];
62
63 $user = session('da');
64 Db::name('chat_record')->where(['form'=>$user['id'],'to'=>$res['id'],'type'=>1])->setField('read',1);
65
66 $sql = "SELECT * FROM `ams_chat_record` WHERE (`to` = ".$res['id']." AND `form` = ".$user['id']." AND `type` = 1) OR (`to` = ".$user['id']." AND `form` = ".$res['id']." AND `type` = 1) ";
67 $msg = Db::query($sql);
68 /*$redis = new Redis();
69 $hand = $redis->handler();
70 // $redis->clear();
71 $msg = $redis->get($id.'_'.session('da.id'))?$redis->get($id.'_'.session('da.id')):[];
72 $msg1 = $redis->get(session('da.id').'_'.$id)?$redis->get(session('da.id').'_'.$id):[];
73 // dump($msg);
74 // dump($msg1);
75 $msg = array_merge($msg,$msg1);
76 // 排序
77 array_multisort(array_column($msg,'sent'),SORT_ASC,$msg );*/
78 $nickname = Db::name('remake')->where(['user'=>$user['id'],'friend'=>$res['id']])->field('remake')->find();
79 if($nickname){
80 $res['name'] = $nickname['remake'];
81 }
82
83 return $this->success($msg);
84 }
85
86 /**
87 * to 消息接收人
88 * form 消息发送人
89 * sent 消息发送时间
90 * recd 消息状态
91 */
92 public function save_msg()
93 {
94 if($this->request->isAjax()){
95 // $redis = new Redis();
96 // $redis->connect('127.0.0.1',6379);
97 $data = $this->request->post();
98 if($data['type'] == 1){
99 $data['form'] = session('da.id');
100 }
101 if($data['type'] == 3){
102 $data['type'] = 1;
103 }
104 $data['create_time'] = time();
105 $data['content'] = $data['msg'];
106 unset($data['msg']);
107 if($data['type'] == 2){
108 $res = Db::name('chat_record')->insertGetId($data);
109 }else{
110 $res = Db::name('chat_record')->insert($data);
111 }
112
113 if($res){
114 if($data['type'] == 2){
115 $this->success($res);
116 }else{
117 $this->success();
118 }
119 }else{
120 $this->error();
121 }
122 // $redis->lPush('name',$data);
123 // $msg = $redis->get($data['form'].'_'.$data['to'])?$redis->get($data['form'].'_'.$data['to']):[];
124 // array_push($msg,$data);
125 // $redis->set($data['form'].'_'.$data['to'],$msg);
126 }
127 }
128
129 public function group_chat()
130 {
131 $id = $this->request->param('group');
132 if(!$id){
133 $this->error('请选择正确的群');
134 }
135 $res = Db::name('circle_group')->where(['id'=>$id,'status'=>1])->find();
136
137 $user = session('da');
138 $head = new Circle();
139 $result = $head->adopt_id_get_userInfo($user['id']);
140 $user['name'] = $result['cname'];
141 $msg = Db::name('chat_record')->where(['form'=>$id,'type'=>2])->select();
142 foreach($msg as $k=>$value){
143 $re = $head->adopt_id_get_userInfo($value['to']);
144 $msg[$k]['name'] = $re['cname'];
145 $msg[$k]['photo'] = $re['photo'];
146 }
147 if($msg){
148 $rs = Db::name('record_user_group')->where(['user'=>$user['id'],'group'=>$res['id']])->find();
149 if($rs){
150 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$msg[count($msg)-1]['id']);
151 }else{
152 Db::name('record_user_group')->insert(['user'=>$user['id'],'group'=>$res['id'],'last_id'=>$msg[count($msg)-1]['id']]);
153 }
154 }
155 $this->assign('user',$user);
156 $this->assign('res',$res);
157 $this->assign('msg',$msg);
158 return $this->fetch();
159 }
160 public function new_group_chat()
161 {
162 $id = $this->request->param('group');
163 if(!$id){
164 $this->error('请选择正确的群');
165 }
166 $res = Db::name('circle_group')->where(['id'=>$id,'status'=>1])->find();
167
168 $user = session('da');
169 $head = new Circle();
170 $result = $head->adopt_id_get_userInfo($user['id']);
171 $user['name'] = $result['cname'];
172 $msg = Db::name('chat_record')->where(['form'=>$id,'type'=>2])->select();
173 foreach($msg as $k=>$value){
174 $re = $head->adopt_id_get_userInfo($value['to']);
175 $msg[$k]['name'] = $re['cname'];
176 $msg[$k]['photo'] = $re['photo'];
177 }
178 if($msg){
179 $rs = Db::name('record_user_group')->where(['user'=>$user['id'],'group'=>$res['id']])->find();
180 if($rs){
181 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$msg[count($msg)-1]['id']);
182 }else{
183 Db::name('record_user_group')->insert(['user'=>$user['id'],'group'=>$res['id'],'last_id'=>$msg[count($msg)-1]['id']]);
184 }
185 }
186 // $this->assign('user',$user);
187 // $this->assign('res',$res);
188 // $this->assign('msg',$msg);
189
190
191 $this->success($msg);
192 }
193
194
195
196 /**
197 * 记录用户最后看到的群聊消息id
198 */
199 public function record_group()
200 {
201 $data = $this->request->post();
202 $data['user'] = session('da.id');
203 $data['group'] = substr($data['group'],5);
204 $rs = Db::name('record_user_group')->where(['user'=>$data['user'],'group'=>$data['group']])->find();
205 if($rs){
206 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$data['last_id']);
207 }else{
208 Db::name('record_user_group')->insert($data);
209 }
210 }
211
212 public function uploadImg()
213 {
214 if($this->request->isAjax() && $this->request->isPost())
215 {
216 $percent = 1;
217 $file = $_FILES['file'];
218 if ($file["error"] > 0){
219 $this->error('上传错误');
220 }
221 if ((($file["type"] == "image/jpg") || ($file["type"] == "image/jpeg") ||($file["type"] == "image/png") ||($file["type"] == "image/gif"))){
222 $upload_path = "chat/".date('Ymd',time()).'/';//上传文件的存放路径
223 if(!is_dir('upload/'.$upload_path)){
224 mkdir('upload/'.$upload_path,0777,true);
225 }
226 $new_file = time().'.'.substr(strrchr($file['name'], '.'), 1);
227 $new_path = $upload_path.$new_file;
228 if(!move_uploaded_file($file['tmp_name'],'upload/'.$new_path)) {
229 $this->error('发送失败!');
230 }
231 if($file['size'] > 1024000){
232 $pic = new Picture('upload/'.$new_path,$percent);
233 $img = $pic->compressImg('upload/'.$new_path);
234 if($img != null){
235 $this->success($img);
236 }else{
237 $this->error('图片发送失败');
238 }
239 }else{
240 $this->success('upload/'.$new_path);
241 }
242 }else{
243 $this->error('目前只支持发送jpg,jpeg,png,gif格式的图片');
244 }
245 }
246 }
247 public function uploadText()
248 {
249 if($this->request->isAjax() && $this->request->isPost())
250 {
251 $file = $_FILES['text'];
252 if ($file["error"] > 0)$this->error('上传错误');
253 if($file['size'] > 10240000)$this->error('上传文件过大。');
254 if ((($file["type"] == "application/octet-stream") || ($file['type'] == "application/x-msdownload")))$this->error('上传文件格式不正确!');
255 $upload_path = "chat/".date('Ymd',time()).'/';//上传文件的存放路径
256 if(!is_dir('upload/'.$upload_path)){
257 mkdir('upload/'.$upload_path,0777,true);
258 }
259 $new_file = time().'.'.substr(strrchr($file['name'], '.'), 1);
260 $new_path = $upload_path.$new_file;
261 if(!move_uploaded_file($file['tmp_name'],'upload/'.$new_path)) {
262 $this->error('发送失败!');
263 }else{
264 $this->success('upload/'.$new_path);
265 }
266
267 }
268 }
269 }
270 ?>
第三方科技的设计费
范德萨发的
1 <?php
2 namespace app\index\controller;
3
4 use think\Controller;
5 use think\Db;
6 use think\Picture;
7
8 class Chat extends Controller
9 {
10 public function privateChat()
11 {
12 $id = $this->request->param('id');
13
14 if(!$id){
15 $this->error('请求错误,请重新登录');
16 }
17 $res = Db::name('accounts')->where(['id'=>$id,'status'=>1])->field('type,info_id,id')->find();
18 $head = new Circle();
19 $re = $head->adopt_id_get_userInfo($id);
20 $res['name'] = $re['cname'];
21 $res['photo']= $re['photo'];
22
23
24 $user = session('da');
25 Db::name('chat_record')->where(['form'=>$user['id'],'to'=>$res['id'],'type'=>1])->setField('read',1);
26
27 $sql = "SELECT * FROM `ams_chat_record` WHERE (`to` = ".$res['id']." AND `form` = ".$user['id']." AND `type` = 1) OR (`to` = ".$user['id']." AND `form` = ".$res['id']." AND `type` = 1) ";
28 $msg = Db::query($sql);
29
30 /*$redis = new Redis();
31 $hand = $redis->handler();
32 // $redis->clear();
33 $msg = $redis->get($id.'_'.session('da.id'))?$redis->get($id.'_'.session('da.id')):[];
34 $msg1 = $redis->get(session('da.id').'_'.$id)?$redis->get(session('da.id').'_'.$id):[];
35 // dump($msg);
36 // dump($msg1);
37 $msg = array_merge($msg,$msg1);
38 // 排序
39 array_multisort(array_column($msg,'sent'),SORT_ASC,$msg );*/
40 $nickname = Db::name('remake')->where(['user'=>$user['id'],'friend'=>$res['id']])->field('remake')->find();
41 if($nickname){
42 $res['name'] = $nickname['remake'];
43 }
44
45 $this->assign('user',$user);
46 $this->assign('res',$res);
47 $this->assign('msg',$msg);
48 return $this->fetch();
49 }
50
51 public function new_privateChat()
52 {
53 $id = $this->request->param('id');
54 if(!$id){
55 $this->error('请求错误,请重新登录');
56 }
57 $res = Db::name('accounts')->where(['id'=>$id,'status'=>1])->field('type,info_id,id')->find();
58 $head = new Circle();
59 $re = $head->adopt_id_get_userInfo($id);
60 $res['name'] = $re['cname'];
61 $res['photo']= $re['photo'];
62
63 $user = session('da');
64 Db::name('chat_record')->where(['form'=>$user['id'],'to'=>$res['id'],'type'=>1])->setField('read',1);
65
66 $sql = "SELECT * FROM `ams_chat_record` WHERE (`to` = ".$res['id']." AND `form` = ".$user['id']." AND `type` = 1) OR (`to` = ".$user['id']." AND `form` = ".$res['id']." AND `type` = 1) ";
67 $msg = Db::query($sql);
68 /*$redis = new Redis();
69 $hand = $redis->handler();
70 // $redis->clear();
71 $msg = $redis->get($id.'_'.session('da.id'))?$redis->get($id.'_'.session('da.id')):[];
72 $msg1 = $redis->get(session('da.id').'_'.$id)?$redis->get(session('da.id').'_'.$id):[];
73 // dump($msg);
74 // dump($msg1);
75 $msg = array_merge($msg,$msg1);
76 // 排序
77 array_multisort(array_column($msg,'sent'),SORT_ASC,$msg );*/
78 $nickname = Db::name('remake')->where(['user'=>$user['id'],'friend'=>$res['id']])->field('remake')->find();
79 if($nickname){
80 $res['name'] = $nickname['remake'];
81 }
82
83 return $this->success($msg);
84 }
85
86 /**
87 * to 消息接收人
88 * form 消息发送人
89 * sent 消息发送时间
90 * recd 消息状态
91 */
92 public function save_msg()
93 {
94 if($this->request->isAjax()){
95 // $redis = new Redis();
96 // $redis->connect('127.0.0.1',6379);
97 $data = $this->request->post();
98 if($data['type'] == 1){
99 $data['form'] = session('da.id');
100 }
101 if($data['type'] == 3){
102 $data['type'] = 1;
103 }
104 $data['create_time'] = time();
105 $data['content'] = $data['msg'];
106 unset($data['msg']);
107 if($data['type'] == 2){
108 $res = Db::name('chat_record')->insertGetId($data);
109 }else{
110 $res = Db::name('chat_record')->insert($data);
111 }
112
113 if($res){
114 if($data['type'] == 2){
115 $this->success($res);
116 }else{
117 $this->success();
118 }
119 }else{
120 $this->error();
121 }
122 // $redis->lPush('name',$data);
123 // $msg = $redis->get($data['form'].'_'.$data['to'])?$redis->get($data['form'].'_'.$data['to']):[];
124 // array_push($msg,$data);
125 // $redis->set($data['form'].'_'.$data['to'],$msg);
126 }
127 }
128
129 public function group_chat()
130 {
131 $id = $this->request->param('group');
132 if(!$id){
133 $this->error('请选择正确的群');
134 }
135 $res = Db::name('circle_group')->where(['id'=>$id,'status'=>1])->find();
136
137 $user = session('da');
138 $head = new Circle();
139 $result = $head->adopt_id_get_userInfo($user['id']);
140 $user['name'] = $result['cname'];
141 $msg = Db::name('chat_record')->where(['form'=>$id,'type'=>2])->select();
142 foreach($msg as $k=>$value){
143 $re = $head->adopt_id_get_userInfo($value['to']);
144 $msg[$k]['name'] = $re['cname'];
145 $msg[$k]['photo'] = $re['photo'];
146 }
147 if($msg){
148 $rs = Db::name('record_user_group')->where(['user'=>$user['id'],'group'=>$res['id']])->find();
149 if($rs){
150 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$msg[count($msg)-1]['id']);
151 }else{
152 Db::name('record_user_group')->insert(['user'=>$user['id'],'group'=>$res['id'],'last_id'=>$msg[count($msg)-1]['id']]);
153 }
154 }
155 $this->assign('user',$user);
156 $this->assign('res',$res);
157 $this->assign('msg',$msg);
158 return $this->fetch();
159 }
160 public function new_group_chat()
161 {
162 $id = $this->request->param('group');
163 if(!$id){
164 $this->error('请选择正确的群');
165 }
166 $res = Db::name('circle_group')->where(['id'=>$id,'status'=>1])->find();
167
168 $user = session('da');
169 $head = new Circle();
170 $result = $head->adopt_id_get_userInfo($user['id']);
171 $user['name'] = $result['cname'];
172 $msg = Db::name('chat_record')->where(['form'=>$id,'type'=>2])->select();
173 foreach($msg as $k=>$value){
174 $re = $head->adopt_id_get_userInfo($value['to']);
175 $msg[$k]['name'] = $re['cname'];
176 $msg[$k]['photo'] = $re['photo'];
177 }
178 if($msg){
179 $rs = Db::name('record_user_group')->where(['user'=>$user['id'],'group'=>$res['id']])->find();
180 if($rs){
181 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$msg[count($msg)-1]['id']);
182 }else{
183 Db::name('record_user_group')->insert(['user'=>$user['id'],'group'=>$res['id'],'last_id'=>$msg[count($msg)-1]['id']]);
184 }
185 }
186 // $this->assign('user',$user);
187 // $this->assign('res',$res);
188 // $this->assign('msg',$msg);
189
190
191 $this->success($msg);
192 }
193
194
195
196 /**
197 * 记录用户最后看到的群聊消息id
198 */
199 public function record_group()
200 {
201 $data = $this->request->post();
202 $data['user'] = session('da.id');
203 $data['group'] = substr($data['group'],5);
204 $rs = Db::name('record_user_group')->where(['user'=>$data['user'],'group'=>$data['group']])->find();
205 if($rs){
206 Db::name('record_user_group')->where('id',$rs['id'])->setField('last_id',$data['last_id']);
207 }else{
208 Db::name('record_user_group')->insert($data);
209 }
210 }
211
212 public function uploadImg()
213 {
214 if($this->request->isAjax() && $this->request->isPost())
215 {
216 $percent = 1;
217 $file = $_FILES['file'];
218 if ($file["error"] > 0){
219 $this->error('上传错误');
220 }
221 if ((($file["type"] == "image/jpg") || ($file["type"] == "image/jpeg") ||($file["type"] == "image/png") ||($file["type"] == "image/gif"))){
222 $upload_path = "chat/".date('Ymd',time()).'/';//上传文件的存放路径
223 if(!is_dir('upload/'.$upload_path)){
224 mkdir('upload/'.$upload_path,0777,true);
225 }
226 $new_file = time().'.'.substr(strrchr($file['name'], '.'), 1);
227 $new_path = $upload_path.$new_file;
228 if(!move_uploaded_file($file['tmp_name'],'upload/'.$new_path)) {
229 $this->error('发送失败!');
230 }
231 if($file['size'] > 1024000){
232 $pic = new Picture('upload/'.$new_path,$percent);
233 $img = $pic->compressImg('upload/'.$new_path);
234 if($img != null){
235 $this->success($img);
236 }else{
237 $this->error('图片发送失败');
238 }
239 }else{
240 $this->success('upload/'.$new_path);
241 }
242 }else{
243 $this->error('目前只支持发送jpg,jpeg,png,gif格式的图片');
244 }
245 }
246 }
247 public function uploadText()
248 {
249 if($this->request->isAjax() && $this->request->isPost())
250 {
251 $file = $_FILES['text'];
252 if ($file["error"] > 0)$this->error('上传错误');
253 if($file['size'] > 10240000)$this->error('上传文件过大。');
254 if ((($file["type"] == "application/octet-stream") || ($file['type'] == "application/x-msdownload")))$this->error('上传文件格式不正确!');
255 $upload_path = "chat/".date('Ymd',time()).'/';//上传文件的存放路径
256 if(!is_dir('upload/'.$upload_path)){
257 mkdir('upload/'.$upload_path,0777,true);
258 }
259 $new_file = time().'.'.substr(strrchr($file['name'], '.'), 1);
260 $new_path = $upload_path.$new_file;
261 if(!move_uploaded_file($file['tmp_name'],'upload/'.$new_path)) {
262 $this->error('发送失败!');
263 }else{
264 $this->success('upload/'.$new_path);
265 }
266
267 }
268 }
269 }
270 ?>
