ywtc_api/application/api/controller/Api.php

362 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\api\controller;
use app\common\controller\Common;
use think\Db;
use think\Request;
use util\Aes;
use app\admin\model\Attachment as AttachmentModel;
use app\member\model\Member as MemberModel;
class Api extends Common
{
protected $params = null; //传参
protected $user_id = 0; //登录用户id
protected $user_info = 0; //登录用户信息
protected $base_url = null; //网站域名
public function initialize()
{
header('Content-Type:application/json; charset=utf-8');
parent::initialize();
$this->params = $this->request->param(); //传参
$this->base_url = 'https://' . $_SERVER['HTTP_HOST']; //当前域名
//获取头部信息
$header = $this->request->header();
//非登录模块操作需要验证用户信息
if ( $this->request->controller() != 'Login' && $this->request->controller() != 'Uploadfile' && $this->request->controller() != 'Wxpaynotify') {
if($this->request->action()!='index' && $this->request->action()!='groupdetail' && $this->request->action()!='messageboard'){
//根据token获取用户信息
if (!isset($header['user-token']) || empty($header['user-token'])) {
apiReturn(500,'token信息不存在');
}
$user_token = $header['user-token'];
$this->user_info = MemberModel::where('userToken',$user_token)->find();
if (!$this->user_info || empty($user_token)) {
apiReturn(500,'token参数错误或登录超时');
}
// 检查账号有效性
if (!MemberModel::where(['id' => $this->user_info['id'], 'is_delete' => 0])->value('id')) {
apiReturn(500,'账号不存在或已被禁用');
}
//登录是否超时(一周登录时间)
$time_compa = abs(time() - $this->user_info['lastlogin_time']);
if ($time_compa > 604800) {
//清空token
MemberModel::update(['user_token' => ''], ['id' => $this->user_info['id']]);
apiReturn(500,'登录超时');
}
//登录用户id
$this->user_id = $this->user_info['id'];
}else{
if (isset($header['user-token']) && !empty($header['user-token'])) {
$user_token = $header['user-token'];
$this->user_info = MemberModel::where('userToken',$user_token)->find();
if (!$this->user_info || empty($user_token)) {
apiReturn(500,'token参数错误或登录超时');
}
// 检查账号有效性
if (!MemberModel::where(['id' => $this->user_info['id'], 'is_delete' => 0])->value('id')) {
apiReturn(500,'账号不存在或已被禁用');
}
//登录是否超时(一周登录时间)
$time_compa = abs(time() - $this->user_info['lastlogin_time']);
if ($time_compa > 604800) {
//清空token
MemberModel::update(['user_token' => ''], ['id' => $this->user_info['id']]);
apiReturn(500,'登录超时');
}
//登录用户id
$this->user_id = $this->user_info['id'];
}
}
}
}
/**
* 获取附件地址
*/
public function getFileUrl($attactId = 0)
{
$url = $this->base_url . get_file_path($attactId);
return $url;
}
/**
* 产生随机字符串
* 产生一个指定长度的随机字符串,并返回给用户
* @access public
* @param int $len 产生字符串的位数
* @return string
*/
function genNumberString($len = 6)
{
$chars = array(
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
//非法字符判断
public function illegalcharacters($str)
{
$strarr = array(
'"',
'<',
'>',
'<>',
'(',
')',
'()',
',',
',',
'script',
'svg',
'alert',
'confirm',
'prompt',
'onload',
'onmouseover',
'onfocus',
'onerror',
'xss',
);
if (in_array($str, $strarr)) {
return true;
} else {
return false;
}
}
//计算两个时间之前存在几个小时
public function hours_min($start_time, $end_time)
{
$sec = strtotime($end_time) - strtotime($start_time);
$sec = round($sec / 60);
$min = str_pad($sec % 60, 2, 0, STR_PAD_LEFT);
$hours_min = floor($sec / 60);
$min != 0 && $hours_min .= ':' . $min;
return $hours_min;
}
public function encryptPhone($phone) {
$maskedPhone = substr_replace($phone, '****', 3, 4);
return $maskedPhone;
}
//更改时间显示
protected function format_date($time)
{
$t = time() - $time;
$f = array(
//'31536000'=>'年',
//'2592000'=>'个月',
//'604800'=>'星期',
'86400' => '天',
'3600' => '小时',
'60' => '分钟',
'1' => '秒'
);
foreach ($f as $k => $v) {
if (0 != $c = floor($t / (int)$k)) {
return $c . $v;
}
}
}
/**
* 生成用户token信息
* @param int uid 用户id
* @param string secret 盐值信息
*/
public function getToken($uid, $secret = 'bodybreakthrough'){
//将签名密钥拼接到签名字符串最后面
$str = $uid . $secret . time();
//通过md5算法为签名字符串生成一个md5签名该签名就是我们要追加的sign参数值
return md5($str);
}
public function get_access_token()
{
$appid = 'wxd5a3c4538c00a549';
$secret = '6f3cd9ac49b8501f1db38e2f5f11bfd6';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$result = file_get_contents($url);
$data = json_decode($result, true);
if ($data['access_token']) {
return $data['access_token'];
} else {
return $data['errmsg'];
}
}
public function _requestPost($url, $data, $ssl = true)
{
//curl完成
$curl = curl_init();
//设置curl选项
curl_setopt($curl, CURLOPT_URL, $url); //URL
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 FirePHP/0.7.4';
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); //user_agent请求代理信息
curl_setopt($curl, CURLOPT_AUTOREFERER, true); //referer头请求来源
curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超时时间
//SSL相关
if ($ssl) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //禁用后cURL将终止从服务端进行验证
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //检查服务器SSL证书中是否存在一个公用名(common name)。
}
// 处理post相关选项
curl_setopt($curl, CURLOPT_POST, true); // 是否为POST请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // 处理请求数据
// 处理响应结果
curl_setopt($curl, CURLOPT_HEADER, false); //是否处理响应头
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //curl_exec()是否返回响应结果
// 发出请求
$response = curl_exec($curl);
if (false === $response) {
echo '<br>', curl_error($curl), '<br>';
return false;
}
curl_close($curl);
return $response;
}
/**
* 判断微信图片
*/
public function judge_wx_pic($id)
{
$headimg = AttachmentModel::where('id', $id)->value('path');
if (!strstr($headimg, 'http')) {
$headimg = $this->getFileUrl($id);
}
return $headimg;
}
/**
* 根据经纬度和半径计算出范围
* @param string $lat 纬度
* @param String $lng 经度
* @param float $radius 半径(单位米)
* @return Array 范围数组
*/
public function calcScope($lat, $lng, $radius)
{
$radius = $radius * 1000;
$degree = (24901 * 1609) / 360.0;
$dpmLat = 1 / $degree;
$radiusLat = $dpmLat * $radius;
$minLat = $lat - $radiusLat; // 最小纬度
$maxLat = $lat + $radiusLat; // 最大纬度
$mpdLng = $degree * cos($lat * (pi() / 180));
$dpmLng = 1 / $mpdLng;
$radiusLng = $dpmLng * $radius;
$minLng = $lng - $radiusLng; // 最小经度
$maxLng = $lng + $radiusLng; // 最大经度
/** 返回范围数组 */
$scope = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng
);
return $scope;
}
/**
* 计算两个经纬度之间的距离
*/
function distance($lat1, $lon1, $lat2, $lon2)
{
$radius = 6378.137;
$rad = floatval(M_PI / 180.0);
$lat1 = floatval($lat1) * $rad;
$lon1 = floatval($lon1) * $rad;
$lat2 = floatval($lat2) * $rad;
$lon2 = floatval($lon2) * $rad;
$theta = $lon2 - $lon1;
$dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
if ($dist < 0) {
$dist += M_PI;
}
return $dist = $dist * $radius;
}
/**
*
* @param $latitude 纬度
* @param $longitude 经度
* @param $raidus 半径范围(单位:米)
* @return multitype:number
*/
public function getAround($latitude,$longitude,$raidus){
$PI = 3.14159265;
$degree = (24901*1609)/360.0;
$dpmLat = 1/$degree;
$radiusLat = $dpmLat*$raidus;
$minLat = $latitude - $radiusLat;
$maxLat = $latitude + $radiusLat;
$mpdLng = $degree*cos($latitude * ($PI/180));
$dpmLng = 1 / $mpdLng;
$radiusLng = $dpmLng*$raidus;
$minLng = $longitude - $radiusLng;
$maxLng = $longitude + $radiusLng;
return [
'minLat'=>$minLat,
'maxLat'=>$maxLat,
'minLng'=>$minLng,
'maxLng'=>$maxLng
];
}
//根据秒数转换时分秒
function secondsToHMS($seconds) {
$hours = gmdate("H", $seconds);
$minutes = gmdate("i", $seconds);
$seconds = gmdate("s", $seconds);
$hours = !empty(intval($hours))?$hours.'时':null;
$minutes = !empty(intval($minutes))?$minutes.'分':null;
$seconds = !empty(intval($seconds))?$seconds.'秒':null;
return $hours.$minutes.$seconds;
}
}