ShareController.php
12.4 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/6/8
* Time: 18:59
*/
namespace app\portal\controller;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
class ShareController extends WeChatBaseController
{
/**
* 我的推广页面
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(){
//判断用户是否填写手机号
$user_id = cmf_get_current_user_id();
$my_user = Db::name('my_user')->where('uid',$user_id)->find();
$user = Db::name('user')->where('id',$user_id)->find();
if(empty($my_user)){
$this->error('查询为空','','','');
}
if($my_user['is_pro'] != 1){
$this->error('无权限','','','');
}
/* if(empty($my_user['phone'])){
$this->redirect('information');
}*/
//判断有没有生成过
// if(!file_exists('./upload/poster/poster_'.$user_id.".png")){
$url = $this->get_url($my_user['id'],$my_user['status']);
$webPath = 'qrcode/code_img'.$user_id.$my_user['status'].'.png';
$savePath = './upload/'.$webPath;
//生成带参二维码
if(!file_exists($savePath)){
$prcode_url = $this->code_img($my_user['id'],$my_user['status']);
}
$savePath = './upload/poster';
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
if($my_user['status'] == 3){
//老师
$image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png");
$image->thumb(300,300,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png");
$image = \think\Image::open(ROOT_PATH."public/bg2.png");
$image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[410,1015],100)
// ->water(ROOT_PATH."public/bg2.png",\think\Image::WATER_CENTER,100)
->save('./upload/poster/poster_'.$user_id.".png");
}else{
//业务员
//保存头像
$avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']);
if($avatar['code']==1){
$this->error($avatar['msg']);
}
$image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg");
$image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png");
$image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png");
$image->thumb(360,360,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png");
$image = \think\Image::open(ROOT_PATH."public/white.png");
$image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[195,725],100)
->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100)
->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100)
->save('./upload/poster/poster_'.$user_id.".png");
}
// }
$share_image = cmf_get_image_url('poster/poster_'.$user_id.".png");
$this->assign('share_image',$share_image);
//分享
// $options = config('wechat_config');
// $app = new Application($options);
// $js = $app->js;
// $jssdk = $js->config(array('onMenuShareAppMessage', 'onMenuShareTimeline','onMenuShareQQ'), false, false, true);
$this->assign('jssdk','$jssdk');
//分享内容
$share_url = url('to_user',array('user_id'=>$user_id),false,true);
$share['share_title'] = "$user[user_nickname]的海报";
$share['share_desc'] = '快来加入我的团队';
$share['share_image'] = $share_image;
$share['share_url'] = $share_url;
$this->assign('status',$my_user['status']);
$this->assign('share',$share);
return $this->fetch();
}
public function to_user(){
$user_id = $this->request->param('user_id',0,'intval');
if(empty($user_id)){
$this->error('缺少必要参数');
}
$my_user = Db::name('my_user')->where('uid',$user_id)->find();
$user = Db::name('user')->where('id',$user_id)->find();
if(empty($my_user)){
$this->error('查询为空','','','');
}
if($my_user['is_pro'] != 1){
$this->error('无权限','','','');
}
$url = $this->get_url($my_user['id'],$my_user['status']);
$webPath = 'qrcode/code_img'.$user_id.$my_user['status'].'.png';
$savePath = './upload/'.$webPath;
//生成带参二维码
if(!file_exists($savePath)){
$prcode_url = $this->get_code($my_user['id'],$my_user['status']);
}
//保存头像
$avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']);
if($avatar['code']==1){
$this->error($avatar['msg']);
}
$savePath = './upload/poster';
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg");
$image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png");
$image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png");
$image->thumb(360,360,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png");
$image = \think\Image::open(ROOT_PATH."public/white.png");
$image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[195,725],100)
->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100)
->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100)
->save('./upload/poster/poster_'.$user_id.".png");
$share_image = cmf_get_image_url('poster/poster_'.$user_id.".png");
$this->assign('share_image',$share_image);
return $this->fetch();
}
public function information(){
$user_id = cmf_get_current_user_id();
$data = Db::name('my_user')->where(['uid'=>$user_id])->find();
$this->assign('data',$data);
return $this->fetch();
}
public function information_save(){
$param = $this->request->param();
if(empty($param['name'])){
$this->error('姓名不能为空');
}
if(empty($param['phone'])){
$this->error('手机号不能为空');
}
$data['name'] = $param['name'];
$data['phone'] = $param['phone'];
$user_id = cmf_get_current_user_id();
$result = Db::name('my_user')->where('uid',$user_id)->update($data);
if(empty($result)){
$this->error('保存失败');
}else{
$this->success('保存成功');
}
}
/**
* 分享链接
* @param $user_id
* @param $status
* @return string
*/
public function get_url($user_id,$status){
//http://xkeasy.w.bronet.cn/portal/index/index/my_user_id/用户id/status_id/身份id
$url = url('portal/index/index',['my_user_id'=>$user_id,'status_id'=>$status],true,true);
return $url;
}
/**
* 生成二维码
* @param $user_id
* @param $qrData
* @return bool|string
*/
public function get_code($user_id,$qrData){
/*存储本地*/
$webPath = 'qrcode/';
$savePath = './upload/'.$webPath;
if(!file_exists($savePath)){
mkdir ($savePath,0777,true);
}
$saveName = "share_".$user_id;
$qrLevel = 'H';
$qrSize = '8';
$savePrefix = "";
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix,$saveName)){
$pic = $webPath .$savePrefix. $saveName . '.png';
return $pic;
}else{
return false;
}
}
public function code_img($my_user_id,$status){
//生成带参二维码
$savePath=ROOT_PATH.'public/upload/qrcode/';
if (!file_exists($savePath)){
mkdir($savePath, 0777,true);
}
$code_img = $this->code_img1($my_user_id,$status);
$code = file_get_contents($code_img);
$user_id = Db::name('my_user')->where('id',$my_user_id)->value('uid');
file_put_contents(ROOT_PATH."public/upload/qrcode/code_img$user_id$status.png",$code);
return $savePath."code_img$user_id$status.png";
}
/**
* 生成带参二维码
*/
public function code_img1($admin_id,$status)
{
$options=config('wechat_config');
$app = new Application($options);
$accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
$token = $accessToken->getToken(false);
$url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $token;
$param = [
'expire_seconds' => '',
'action_name' => 'QR_LIMIT_STR_SCENE',
'action_info' => ['scene' => ['scene_str' => "$admin_id"."-"."$status"]]
];
$result = $this->api_notice_increment($url, json_encode($param));
$data = json_decode($result, true);
$ticket = urlencode($data['ticket']);
$qr_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $ticket;
return $qr_url;
}
/**
* 获取二维码提交
*/
public function api_notice_increment($url, $data)
{
// $data=json_encode($data);
$ch = curl_init();
$header = array("Accept-Charset: utf-8");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return $ch;
} else {
curl_close($ch);
return $tmpInfo;
}
}
/**
* 下载微信头像保存到本地
* @param $url
* @param $filename
* @return array
*/
function getImage($url,$filename){
$save_dir='./upload/avatar/';
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return ['code'=>1,'msg'=>'图片路径错误'];
}
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}else{
return ['code'=>1,'msg'=>'图片转换失败'];
}
$img_content=$imgBase64Code;//图片内容
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
$type = $result[2];
$new_file = $save_dir.$filename.'.'.$type;
$save_path= 'avatar/'.$filename.'.'.$type;
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
return ['code'=>0,'file_path'=>$new_file,'save_path'=>$save_path];
}else{
return ['code'=>1,'msg'=>'图片保存失败'];
}
}else{
return ['code'=>1,'msg'=>'图片格式错误'];
}
}
}