ServerController.php 2.4 KB
<?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,'请稍后重试');
    }

}