ServerController.php
2.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
<?php
/**
* 服务类
* Author: xiaojie
* DateTime: 2018/12/03 18:01
*/
namespace app\portal\controller;
use app\portal\model\EdmModel;
use app\portal\model\ShowModel;
use app\portal\model\StatisticsModel;
use app\portal\model\UsersModel;
use cmf\controller\HomeBaseController;
use think\Config;
use think\Cookie;
use think\Session;
class ServerController extends HomeBaseController
{
/**
* 腾讯根据ip获取当前位置信息
* @param $ip 当前ip地址
* @return bool
*/
public function getAddress($ip)
{
$key = Config::get('TENGXUN_KEY');
$url = 'https://apis.map.qq.com/ws/location/v1/ip?ip=' . $ip . '&key=' .$key;
$data = json_decode(file_get_contents($url),true);
if($data['status'] !== 0){
return false;
}
return $data['result']['ad_info']['province'];
}
/**
* 访问数据统计
*/
public function statistics()
{
$statisticsModel = new StatisticsModel();
$ip = get_client_ip();
$address = $this->getAddress($ip);
if($address){
$status = 1;
}else{
$address = '用户拒绝授权地理位置';
$status = 0;
}
$statisticsModel->insert([
'ip' => $ip,
'address' => $address,
'create_time' => time(),
'create_date' => date('Y-m-d',time()),
'status' => $status,
]);
}
/**
* 订阅操作
*/
public function ajax_add_edm()
{
$email = $this->request->param('email');
$userModel = new UsersModel();
$edmModel = new EdmModel();
$user_mobile = Session::get('user_mobile');
if(empty($user_mobile)){
$this->apiResponse(-1,'请登录后操作');
}
$search = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
if (!preg_match($search,$email)) {
$this->apiResponse(0,'邮箱格式有误','');
}
$if_exist = $edmModel->where('email',$email)->find();
if($if_exist){
$this->apiResponse(0,'该邮箱已被订阅');
}
$user_info = $userModel->where('mobile',$user_mobile)->find();
$res = $edmModel->save([
'user_id' => $user_info['id'],
'email' => $email,
]);
if($res){
$this->apiResponse(1,'操作成功');
}
$this->apiResponse(0,'请稍后重试');
}
}