api.AddUser.php
3.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
<?php
/**
* 增加用户,对于程序设计,走的是修改逻辑
*/
include "Common/JsonResponse.php";
include "Common/mysqlHelper.php";
include "Common/WechatHelper.php";
include "Common/Encrypter.php";
// 验证注册通道
isStopSign();
// 头像,姓名必填
if (!isset($_POST['realname']) || !isset($_POST['mediaId'])) {
JsonResponse::error('姓名或照片不能为空');
}
// 坐标不能为空
if (!isset($_POST['latitude']) || !isset($_POST['longitude'])) {
JsonResponse::error('用户坐标不能为空');
}
// 登录验证,验证cookie是否有openid,没有前端走微信openid获取流程
if (!isset($_COOKIE['openid'])) {
JsonResponse::error('进入姿势不对?');
}
// 年会签到地址
$config = include('Common/config.php');
$partyLat = $config['latitude'];
$partyLng = $config['longitude'];
$distance = getDistance($partyLng, $partyLat, $_POST['longitude'], $_POST['latitude']);
// 签掉距离不能大于200m,配置文件设置
if ($distance > $config['distance']) {
JsonResponse::error('不在签到范围内');
}
// 姓名唯一
$realname = $_POST['realname'];
$mysql = new mysqlHelper();
$data = $mysql->fetch("SELECT id FROM user WHERE realname = ?", [ $realname ]);
if ($data) {
JsonResponse::error("Hello {$_POST['realname']} ,你的姓名重复了");
}
// 获取头像数据,从微信临时素材图片中。
$wechat = new WechatHelper();
$img = $wechat->getMedia($_POST['mediaId']);
// 保存图片
$filename = time() . rand(1000,9999) . ".jpg";
$tarfilename = "/party/upload/" .$filename;
$dir = str_replace('\\','/',realpath(dirname(__FILE__).'/'));
$dir = rtrim($dir, '/party/phpapi/');
$fp = fopen($dir . $tarfilename, "w");
fwrite($fp, $img);
fclose($fp);
// 组装新增用户需要的数据
$params[] = $_POST['realname'];
$params[] = 'http://' . $_SERVER['SERVER_NAME'] . $tarfilename;
// 这里openid解密
$enctypt = new Encrypter();
$params[] = $enctypt->decrypt($_COOKIE['openid']);
$time = time();
// pdo保存数据
$updateSql = "UPDATE user SET realname = ?, headimg = ?, `status` = 1, created = {$time} WHERE openid = ?;";
$rowCount = $mysql->update($updateSql, $params);
if ($rowCount) {
JsonResponse::result(['distance' => $distance]);
} else {
JsonResponse::error('报名失败');
}
function rad($dis)
{
return round($dis * (3.1415926535898 / 180), 6);
}
// 计算距离
function getDistance($lat1, $lng1, $lat2, $lng2)
{
$lat1 = round($lat1, 6);
$lng1 = round($lng1, 6);
$lat2 = round($lat2, 6);
$lng2 = round($lng2, 6);
$radLat1 = rad($lat1);
$radLat2 = rad($lat2);
$a = $radLat1 - $radLat2;
$b = rad($lng1) - rad($lng2);
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$s = round($s * 6378137, 0);
return $s;
}
// 验证注册通道是否关闭
function isStopSign()
{
$mysql = new mysqlHelper();
$data = $mysql->fetch("SELECT `value` FROM `setting` WHERE `key` = 'isStopSign' AND `value` = 1");
if ($data) {
JsonResponse::error("注册通道已经关闭,自己人联系颖慧");
}
}