OtherController.php
15.8 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/9/6
* Time: 15:59
*/
namespace api\index\controller;
use api\index\model\JobModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;
use think\Validate;
/**
* @title 杂项
* @description
*/
class OtherController extends RestBaseController
{
/**
* @title 联系我们
* @description
* @author GuoSheng
* @url /index/Other/index
* @method GET
*
* @return contactus:联系我们
*
*/
public function index()
{
$data = Db::name('other')
->where('id',1)
->field('contactus')
->find();
$data['contactus'] = strip_tags(cmf_replace_content_file_url(htmlspecialchars_decode($data['contactus'])));
$this->success('SUCCESS',$data);
}
/**
* @title 关于我们
* @description
* @author GuoSheng
* @url /index/Other/about
* @method GET
*
* @return aboutus:关于我们
*
*/
public function about()
{
$data = Db::name('other')
->where('id',1)
->field('aboutus')
->find();
$data['aboutus'] = strip_tags(cmf_replace_content_file_url(htmlspecialchars_decode($data['aboutus'])));
$this->success('SUCCESS',$data);
}
/**
* @title 账户信息
* @description
* @author GuoSheng
* @url /index/Other/account
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:city type:string require:1 desc: 城市
* @param name:infostatus type:int require:1 desc:消息通知 (1关闭 2开启)
* @param name:company type:string require:1 desc:所在企业
* @param name:position type:string require:1 desc:职位
* @param name:content type:string require:1 desc:企业简介
*
* @return city:所选城市
* @return infostatus:消息通知 (0关闭 1开启)
* @return company:所在企业
* @return position:职位
* @return content:企业简介
*
*/
public function account()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$res = Db::name('account')
->where('user_id',$user_id)
->find();
if($res) {
$param['update_time'] = time();
Db::name('account')
->where('user_id', $user_id)
->update($param);
$data = Db::name('account')
->where('user_id',$user_id)
->find();
$data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
$this->success('SUCCESS',$data);
}else{
$param['create_time'] = time();
$param['user_id'] = $user_id;
Db::name('account')
->insert($param);
$data = Db::name('account')
->where('user_id',$user_id)
->find();
$data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
$this->success('SUCCESS',$data);
}
}
/**
* @title 账户信息携带token
* @description
* @author GuoSheng
* @url /index/Other/account_s
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @return city:所选城市
* @return infostatus:消息通知 (1关闭 2开启)
* @return company:所在企业
* @return position:职位
* @return content:企业简介
*
*/
public function account_s()
{
$user_id = $this->getUserId();
$res = Db::name('account')
->where('user_id',$user_id)
->find();
if($res) {
$res['status'] = 1;
$res['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($res['content']));
$this->success('SUCCESS',$res);
}else{
$res['status'] = 0;
$this->success('SUCCESS',$res);
}
}
/**
* @title 收藏
* @description 收藏信息
* @author GuoSheng
* @url /index/Other/collect
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:job_id type:int require:1 other: desc:信息ID
*
*/
public function collect()
{
$param['user_id'] = $this->getUserId();
$param['job_id'] = $this->request->param('job_id');
$param['create_time'] = time();
$res = Db::name('collect')
->where('user_id',$param['user_id'])
->where('job_id',$param['job_id'])
->find();
if($res){
$this->error(['code'=>40001,'msg'=>'您已经收藏过了']);
}else{
$data = Db::name('collect')
->insert($param);
$res = Db::name('job')
->where('id',$param['job_id'])
->find();
$good_num = $res['good_num']+1;
Db::name('job')
->where('id',$param['job_id'])
->update(['good_num'=>$good_num]);
if($data){
$this->success('SUCCESS');
}else{
$this->error('sql执行失败');
}
}
}
/**
* @title 收藏数量
* @description 收藏信息
* @author GuoSheng
* @url /index/Other/redHeat
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:job_id type:int require:1 other: desc:信息ID
*
* @return good_num:收藏数量
*
*/
public function redHeat()
{
$user_id= $this->getUserId();
$job_id = $this->request->param('job_id');
$res = Db::name('job')
->where('id',$job_id)
->find();
$data['good_num'] = $res['good_num'];
$this->success('SUCCESS',$data);
}
/**
* @title 取消收藏
* @description 取消收藏
* @author GuoSheng
* @url /index/Other/cancelCollect
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:job_id type:int require:1 other: desc:信息ID
*
*/
public function cancelCollect()
{
$user_id = $this->getUserId();
$job_id = $this->request->param('job_id');
$res = Db::name('collect')
->where('user_id',$user_id)
->where('job_id',$job_id)
->find();
if($res){
$data = Db::name('collect')
->where('user_id',$user_id)
->where('job_id',$job_id)
->delete();
$res = Db::name('job')
->where('id',$job_id)
->find();
$good_num = $res['good_num']-1;
Db::name('job')
->where('id',$job_id)
->update(['good_num'=>$good_num]);
if($data){
$this->success('SUCCESS');
}else{
$this->error('sql执行失败');
}
}else{
$this->error(['code'=>40001,'msg'=>'您已经取消过了']);
}
}
/**
* @title 分享
* @description 分享
* @author GuoSheng
* @url /index/Other/share
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:job_id type:int require:1 other: desc:信息ID
*
* @return share_num:分享数量
*
*/
public function share()
{
$user_id= $this->getUserId();
$job_id = $this->request->param('job_id');
$res = Db::name('job')
->where('id',$job_id)
->find();
$data['share_num'] = $res['share_num']+1;
Db::name('job')
->where('id',$job_id)
->update($data);
$this->success('SUCCESS',$data);
}
/**
* @title 生成海报
* @description 生成海报
* @author GuoSheng
* @url /index/Other/getPoster
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:job_id type:int require:1 other: desc:信息id
* @param name:route type:string require:1 other: desc:页面路径与参数
* @param name:avatar type:string require:1 other: desc:用户头像
*
* @return url:海报地址
*
*/
public function getPoster(){
$user_id= $this->getUserId();
$job_id = $this->request->param('job_id');
$url = $this->request->param('route');
if(empty($user_id) || empty($job_id) || empty($url)){
$this->error(['code'=>40005,'msg'=>'缺少必要参数']);
}
$savePath = './../upload/poster/';
if (!file_exists($savePath)){
mkdir($savePath, 0777,true);
}
if(!file_exists("./upload/qrcode/code_$user_id.jpg")){
$code = $this->getCode($user_id,$job_id,$url);
}else{
$code = "qrcode/code_$user_id.jpg";
}
$codeImage = \think\Image::open(ROOT_PATH."public/upload/$code");
$codeImage->thumb(120,120,\think\Image::THUMB_CENTER)->save(ROOT_PATH."public/upload/$code");
$jobModel = new JobModel();
$where['id'] = ['eq',$job_id];
$job = $jobModel->getOne($where);
$user = Db::name('user')
->field('avatar,user_nickname')
->where('id',$job['user_id'])
->find();
$avatar = $this->request->param('avatar');
dump($avatar);
$image = \think\Image::open(ROOT_PATH."public/bg.png");
$text = "期待你的加入";
$remark = "长按识别二维码识别小程序";
$result = $image
->water(ROOT_PATH."public/upload/$code",[456,590],100)
->water($user['avatar'],1,100)
->text($remark,ROOT_PATH.'public/static/font-awesome/fonts/PingFang Medium.ttf',14,'#FFFFFF',[66,659])
->text($text,ROOT_PATH.'public/static/font-awesome/fonts/PingFang Medium.ttf',12,'#FFFFFF',[132,100])
->text($job['job_title'],ROOT_PATH.'public/static/font-awesome/fonts/PingFang Medium.ttf',20,'#FFFFFF',[100,200])
->text($user['user_nickname'],ROOT_PATH.'public/static/font-awesome/fonts/PingFang Medium.ttf',16,'#FFFFFF',[132,60]);
$result->save($savePath."poster_".$user_id."_$job_id.png");
$this->success('SUCCESS',['url'=>cmf_get_image_url("poster/poster_".$user_id."_$job_id.png")]);
}
public function getCode($user_id,$job_id,$url){
$savePath = './../upload/get_code/';
$filename = "code_$user_id.jpg";
if(!file_exists($savePath.$filename)){
$access_token = $this->get_access_token();
$qrcode = $this->imgShare($user_id,$job_id,$access_token,$url);
}elsE{
$qrcode = "qrcode/code_".$user_id."_$job_id.jpg";
}
return $qrcode;
}
public function imgShare($user_id,$job_id,$access_token,$path){
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
$post['path'] = $path;
$post['width'] = 280;
$result = $this->curl($url, json_encode($post));
$array = json_decode($result, true);
if($array['errmsg']) {
return false;
}
// 分享二维码保存到用户数据
if (!file_exists(ROOT_PATH . 'public' . DS . 'upload/qrcode/')) {
mkdir(ROOT_PATH . 'public' . DS . 'upload/qrcode/', 0777, true);
}
$filename = "code_".$user_id."_$job_id.jpg";
$furl = ROOT_PATH . 'public' . DS . 'upload/qrcode/'.$filename;
file_put_contents($furl, $result);
return "qrcode/".$filename;
}
// curl方法
protected function curl($url, $post = null, $header = null) {
// 初始化
$curl = curl_init();
// 设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
// 请求头
if($header) {
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
}
// 设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_NOBODY, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
//把POST的变量加上
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
// 执行命令
$data = curl_exec($curl);
if (curl_errno($curl)) {
$data = curl_error($curl);
}
return $data;
}
//获取access_token
public function get_access_token()
{
$app_id = config('app_id');
$app_secret = config('app_secret');
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_id.'&secret='.$app_secret;
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
$token = $json_arr['access_token'];
return $token;
}
//curl get请求
public function http_get($url){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
//curl post请求
public function http_post($url,$data){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
public function yuanjiao($imgpath){
$ext= pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefrompng($imgpath);
break;
}
$wh= getimagesize($imgpath);
$w=$wh[0];
$h= $wh[1];
$w= min($w, $h);
$h= $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r= $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
}