FriendsController.php
7.0 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
<?php
/**
* Created by PhpStorm.
* auther: sgj
* Date: 2018/12/20
* Time: 16:55
*/
namespace app\portal\controller;
use app\portal\model\GroupModel;
use app\user\model\UserModel;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Log;
use think\Request;
class FriendsController extends WeChatBaseController
{
protected $GroupModel;
public function __construct(GroupModel $GroupModel)
{
parent::__construct();
$this->GroupModel = $GroupModel;
}
public function index(){
$User=new UserModel();
$user=$User->getUserInfo();
if (empty($user['user_login'])){
$targetUrl=url('portal/index/register');
header('location:'. $targetUrl);
}
$group=$this->GroupModel->getMyGroup();
$this->assign('group',$group);
return $this->fetch();
}
/**
* 圈子详情页
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function friendList(){
$config=config('wechat_config');
$Wechat=new Application($config);
$js = $Wechat->js;
$api[]='chooseImage';
$api[]='previewImage';
$api[]='uploadImage';
$api[]='downloadImage';
$sdk=$js->config($api, false);
$group_id=input('id');
/*检查权限*/
$user_id=cmf_get_current_user_id();
$User=new UserModel();
$user=$User->getMyself()->toArray();
$isin=$this->GroupModel->checkUser($user_id,$group_id);
if (empty($isin)){
$this->error();
}
$silence=$this->GroupModel->silenceCheck($user_id,$group_id);
$group=$this->GroupModel->getGroup($group_id);
$info=$this->GroupModel->getFriendsInfo($group_id);
$article=$this->GroupModel->getArticleList($group_id)->toArray();
foreach ($article as $k=>$v){
$article[$k]['pic']=cmf_get_image_url(json_decode($v['amore'],true)['thumbnail']);
}
$this->assign('sdk',$sdk);
$this->assign('silence',$silence);
$this->assign('info',$info);
$this->assign('userinfo',$user);
$this->assign('group',$group);
$this->assign('article',$article);
return $this->fetch();
}
public function addTalk(){
$group_id=input('gid');
$config=config('wechat_config');
$Wechat=new Application($config);
$js = $Wechat->js;
$api[]='chooseImage';
$api[]='previewImage';
$api[]='uploadImage';
$api[]='downloadImage';
$sdk=$js->config($api, false);
$this->assign(cmf_get_option('site_info'));
$this->assign('sdk',$sdk);
$this->assign('group_id',$group_id);
return $this->fetch();
}
/**
* 微信上传照片
*/
public function upload_wx_pic_mul()
{
$config=config('wechat_config');
$Wechat=new Application($config);
$js = $Wechat->js;
$access_token = $js->getAccessToken();
$img_str = $this->request->param('media', '');
$img_str = rtrim($img_str, ',');
$img_arr = explode(',', $img_str);
$temporary = $Wechat->material_temporary;
$dir=ROOT_PATH.'public/'.'upload/friend'.'/';
foreach ($img_arr as $k => $v) {
$name=date('YmdHis',time()).rand(1000,9999).'.jpg';
$data[$k]=$temporary->download($v, "$dir", "$name");
}
return $data;
}
// public function getUser(){
// require_once VENDOR_PATH . "jssdk/jssdk.php";
// $jssdk = new \JSSDK(Config('WX_APPID'), Config('WX_APP_SECRET'));
// $access_token = $jssdk->getAccessToken();
// $this->redirect('https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid=o1FU71IKa-M6q5vNF9sTd2tv-GEg&lang=zh_CN');
// }
/**
* 根据media_id下载微信图片
* @param $access_token
* @param $media_id
* @param $foldername
* @return string
*/
private function getmedia($access_token, $media_id, $foldername)
{
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $media_id;
if (!file_exists("./upload/" . $foldername)) {
mkdir("./upload/" . $foldername, 0777, true);
}
$file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
$targetName = './upload/' . $foldername . '/' . $file_name;
$saveName = $foldername . '/' . $file_name;
$ch = curl_init($url); // 初始化
$fp = fopen($targetName, 'wb'); // 打开写入
curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $saveName;
}
//发布文章提交
public function addPost()
{
// Log::init([
// 'type' => 'File',
// 'path' => APP_PATH.'logs/'
// ]);
$param = $this->request->param();
if (empty($param['media'])&&empty($param['post_content'])){
return 0;
}
if (!empty($param['media'])) {
$param['more']['photos'] = [];
foreach ($this->upload_wx_pic_mul($param['media']) as $key => $url) {
$photoUrl = cmf_asset_relative_url($url);
array_push($param['more']['photos'], $photoUrl);
$insert['more'] = json_encode($param['more']);
unset($param['picture']);
}
}
/* Log::write($param);
log('11111');*/
$insert['user_id'] = cmf_get_current_user_id();
$insert['group_id'] = input('group_id');
$insert['pic_num']=$param['num'];
$insert['add_time'] = time();
$insert['content'] = input('post_content');
$re = db('group_text')->insert($insert);
if ($re==1){
$site=cmf_get_option('site_info');
addScore($insert['user_id'],$site['friend'],'发布圈子动态');
}
return $re;
}
/**
* 点击赞
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\PDOException
*/
public function postLike(){
$id=input('text_id');
$user_id=cmf_get_current_user_id();
$result=$this->GroupModel->postLike($user_id,$id);
if ($result==1){
$this->success('');
}else{
$this->error();
}
}
/**
* 发布提交内容
*/
public function postContent(){
$id=input('id');
$content=input('content');
$user_id=cmf_get_current_user_id();
$result=$this->GroupModel->postContent($user_id,$content,$id);
if ($result==1){
$this->success('');
}else{
$this->error();
}
}
}