Mbchat.php
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: hsj
* Date: 2015-09-09
*/
namespace app\mchat\controller;
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Validate;
use think\Verify;
use think\Db;
use think\Cookie;
use app\mobile\controller\MobileBase;
use qcloudcos\Myqcloudcos;
class Mbchat extends MobileBase{
public $user_id = 0;
public $user = array();
public function _initialize()
{
// parent::__construct();
mlog('12121','Mbchat/'.getMobileStoId());
$sid = I('stoid');
if (session('?user')) {
$user = session('user');
$user = M('users')->where("user_id", $user['user_id'])->where("store_id", $sid)->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
mlog('56565','Mbchat/'.getMobileStoId());
} else {
mlog('78787878','Mbchat/'.getMobileStoId());
$usrid=Cookie::get('user_id');
if($usrid){
$user = M('users')->where("user_id", $usrid)->where("store_id", $sid)->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}
}
if (!$this->user_id ) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
//$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId()););
exit;
}
$this->chat_db=Db::connect('chat_db');
}
private $sub_name = array('date', 'Y/m-d');
private $savePath = 'temp/';
private $savePath1 = '';
//保存聊天内容
public function save_msg()
{
$data=input('post.');
$data['admin_id']=$data['admin_id'];
$data['store_id']=getMobileStoId();
$data['chat_type']=$data['chat_type'];
$data['msg_type']=$data['msg_type'];
$data['msg']=$data['msg'];
$data['add_time']=time();
$res=$this->chat_db->name('chat_msg')->insert($data);
$res_history=$this->chat_db->name('chat_msg_history')->insert($data);
return $res;
}
public function save_image()
{
$file = request()->file('imgfile');
$result = $this->validate(
['file2' => $file],
['file2'=>'image','file2'=>'fileSize:10000000000'],
['file2.image' => '上传文件必须为图片','file2.fileSize' => '上传文件过大']
);
if(true !== $result){
return json($result);
} else {
// 移动到框架应用根目录/public/uploads/ 目录下
if (empty($this->savePath1))
$this->savePath = $this->savePath . date('Y') . '/' . date('m-d') . '/';
else
$this->savePath = $this->savePath . $this->savePath1 . '/' . date('Y') . '/' . date('m-d') . '/';
$info = $file
// ->rule(function ($file) {
// $saveFileName = md5(mt_rand());
// return $saveFileName; // 使用自定义的文件保存规则
// })
->move('public/upload/' . $this->savePath);
if ($info) {
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "public/upload/" . $saveFileName;
//保存到腾讯云
vendor ('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0){//不存在创建
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
$saveFileName=$this->savePath.$info->getSaveName();
m_cut_img(basename($saveFileName),ROOT_PATH.'/public/upload/'.dirname($saveFileName).'/');
//上传到腾讯云
$localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
$ypath='/'.UPLOAD_PATH.'mchat/'.dirname($saveFileName).'/'.basename($saveFileName);
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
if ($res && $res['code']==0)//成功
{
// mdelFile($localpath);
}
return json(['code'=>1,'msg'=>'上传成功','localpath'=>$localpath,'ypath'=>$ypath]);
} else {
return json(['code'=>-1,'msg'=>$file->getError()]);
}
}
}
public function del_image(){
$url = I('url');
mdelFile($url);
}
private $ybpath='wxd';
public function save_img()
{
// //上传到腾讯云
// $localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
// $ypath='/'.UPLOAD_PATH.'mchat/'.dirname($saveFileName).'/'.basename($saveFileName);
$contents = I('POST.contents');
$uploadpath='/'.UPLOAD_PATH.'/mchat/mbchat/';
$ypath='/'.UPLOAD_PATH.'/mchat/mbchat/'.date('Ym',time()).'/';
$img = base64_decode($contents);
if (!file_exists(ROOT_PATH.$uploadpath)){
mkdir($uploadpath);
}
$simg=time();
$imgname=$uploadpath.$simg;
$a = file_put_contents($imgname, $img);//返回的是字节数
//生成小图
m_cut_img($simg,ROOT_PATH .$uploadpath, 100, 1);
//保存到存储云
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder($this->ybpath,$ypath);
if ($resfolder && $resfolder['code']!=0){
//不存在创建
Myqcloudcos::createFolder($this->ybpath,$ypath);
}
$res=Myqcloudcos::upload($this->ybpath,ROOT_PATH .$uploadpath.$simg,$ypath.$simg);
if ($res && $res['code']==0){//成功
//删除本地
mdelFile(ROOT_PATH . $uploadpath . $simg);
// return $simg;
return json(['code'=>1,'msg'=>'上传成功','localpath'=>ROOT_PATH .$uploadpath.$simg,'ypath'=>$ypath.$simg]);
} else {
return json(['code'=>-1,'msg'=>'上传失败']);
}
}
public function test(){
return $this->fetch();
}
public function index(){
//这两个缓存都得删除 暂时应用于分配方法中
$user_id= $this->user_id;
$user = $this->user;
$stoid = I('stoid');
$goods_id = I('goods_id/d',0);
$sto_log=tpCache('shop_info',$stoid);
if ($goods_id) {
$goods = M('goods')->where("goods_id", $goods_id)->where("store_id", $stoid)->find();
$this->assign('goods',$goods);
$this->assign('goods_id', $goods_id);
}
//查询该会员是否在线聊天
$where['user_id']= $user_id;
$is_online=$this->chat_db->name('chat_receive')->where($where)->find();
if(isset($is_online['state']))
{
if($is_online['state']!=0)
{
$this->assign('is_online','1');
}
}else
{
$this->assign('is_online','1');
}
//商家在getinfo设置了定值记得修改
// $this->assign('admin_id',$this->admin_info['admin_id']);
$user['part_time']=time();
$this->assign('user_id', $user_id);
$this->assign('store_logo', $sto_log['store_logo']);
$this->assign('user',json_encode($user));
$this->assign('head_pic',$user['head_pic']);
$this->assign('nickname',$user['nickname']);
$this->assign('stoid', $stoid);
return $this->fetch();
}
public function dis_online_man()
{
$data=input('post.');
$stoid=I('stoid');
$user_id=$data['user_id'];
$get_ip=$data['user_ip'];
//$where['user_id']=session('user_id')
mlog('00'.$data['user_id'],'dis_online_man/'.$stoid);
$where['user_id']=$user_id;
$where['store_id']=$stoid;
$res=$this->chat_db->name('chat_receive')->where($where)->find();
//是否存在于接待表
if(!empty($res))
{
mlog('11'.$data['user_id'],'dis_online_man/'.$stoid);
//判断admin_id是否为空
if(!empty($res['admin_id'])&&$res['state']==0)
{
return $res;
//不需要分配 直接返回admin_id
// $dis_admin_id['admin_id'] =$res['admin_id'];
// $dis_admin_id['admin_ip'] =$res['admin_ip'];
}
if(empty($res['admin_id'])||$res['state']=1)
{
$help = new \app\mchat\controller\Getinfo();
$get_admin_id = $help -> user_to_admin($stoid);
//分配在线客服
if(!empty($get_admin_id))
{
//确保admin是同一个stoid
$admin_f = M('admin')
->where("admin_id", $get_admin_id['admin_id'])
->where("store_id", $stoid)
->field('admin_id')
->find();
if($admin_f) {
$dis_admin_id['admin_id'] = $get_admin_id['admin_id'];
$dis_admin_id['admin_ip'] = $get_admin_id['admin_id'] . '111';
$dis_admin_id['state'] = 0;
$dis_admin_id['num'] = $res['num'] + 1;
$dis_admin_id['add_time'] = time();
$dis_admin_id['store_id'] = $stoid;
//更新接待表和聊天记录表
$result = $this->chat_db->name('chat_receive')->where('user_id=' . $user_id . '')
->where('store_id', $stoid)->update($dis_admin_id);
$result = $this->chat_db->name('chat_msg')->where('user_id=' . $user_id . '')->where('is_read=0')
->where('store_id', $stoid)->save(['admin_id' => $get_admin_id['admin_id']]);
}
}else{
// 没有客服登录 怎么办
}
}
}else {
mlog('22'.$data['user_id'],'dis_online_man/'.$stoid);
$help = new \app\mchat\controller\Getinfo();
$get_admin_id = $help -> user_to_admin($stoid);
$dis_admin_id['state']=0;
$dis_admin_id['user_id']=$user_id;
$dis_admin_id['user_ip']=$get_ip;
$dis_admin_id['add_time']=time();
$dis_admin_id['num']=1;
$dis_admin_id['store_id']=$stoid;
if(!empty($get_admin_id))
{
$dis_admin_id['admin_id']=$get_admin_id['admin_id'];
$dis_admin_id['admin_ip']=$get_admin_id['admin_id'].'111';
}else{
$dis_admin_id['admin_id']='';
$dis_admin_id['admin_ip']='';
}
$result=$this->chat_db->name('chat_receive')->insert($dis_admin_id);
}
$return_data['admin_id']=$dis_admin_id['admin_id'];
$return_data['admin_ip']=$dis_admin_id['admin_id'].'111';
return $return_data;
}
//分配
public function dis_admin_id($user_id='',$user_ip='',$type='',$num='')
{
$help = new \app\mchat\controller\Getinfo();
$res = $help -> user_to_admin();
if(isset($res['admin_id']))
{
$dis_admin_id['admin_id']=$res['admin_id'];
$dis_admin_id['admin_ip']=$res['admin_ip'];
}else{
// $online_man=$this->chat_db->query('select * ,max(id) as id from wxd_chat_login_state where state=0 and login_timeout>UNIX_TIMESTAMP()');
$where['state']=0;
$where['login_timeout']=['>',time()];
$where['admin_ip']=['!=',''];
$online_man=$this->chat_db->name('chat_login_state')->where($where)->order('login_time desc')->find();
$dis_admin_id['admin_id']=$online_man['admin_id'];
$dis_admin_id['admin_ip']=$online_man['admin_ip'];
}
$dis_admin_id['user_id']=$user_id;
$dis_admin_id['user_ip']=$user_ip;
$dis_admin_id['state']=0;
$dis_admin_id['add_time']=time();
//$dis_admin_id['session_id']=uuid();
$dis_admin_id['msg_uc']=1;
//历史有接待过的
if(!empty($type))
{
$dis_admin_id['num']=$num+1;
$result=$this->chat_db->name('chat_receive')->where('user_id='.$user_id.'')->update($dis_admin_id);
}else{
$dis_admin_id['num']=1;
$result=$this->chat_db->name('chat_receive')->insert($dis_admin_id);
}
$data['admin_ip']=$dis_admin_id['admin_ip'];
$data['admin_id']=$dis_admin_id['admin_id'];
return $data;
}
public function sel_history_list()
{
$data=input('post.');
// $where['admin_id']=$data['admin_id'];
$where['user_id']=$data['user_id'];
//$where['add_time']=$data['date'];
//var_dump($were);die;
$result=$this->chat_db->name('chat_msg_history')->where($where)->select();
return json_encode($result);
}
//判断是否在线
public function is_online(){
$where['admin_id']=I('tadmin_id');
$where['store_id']=I('stoid');
$where['state']=0;
$rs=$this->chat_db->name('chat_login_state')->where($where)->find();
if($rs){
return json(['ison'=>1]);
}else{
return json(['ison'=>0]);
}
}
}