123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 小夏 < 449134904@qq.com>
- // +----------------------------------------------------------------------
- namespace app\web\controller;
- use think\Db;
- use app\common\plugin\Jwt;
- use app\common\plugin\Jssdk;
- use think\Controller;
- class MainController extends Controller
- {
- public $userid = 0;
- public $is_product = true;
- protected $token;
- protected $major;
- protected $ip;
- public $access_token;
- protected $redis;
- function __construct()
- {
- parent::__construct();
- $token=request()->request('sid');
- $major=request()->request('major');
- // $redis=new \Redis();
- // $redis->connect(config("config.redis.REDIS_HOST"),config("config.redis.REDIS_PORT"));
- // $this->redis=$redis;
- $ip=request()->ip();
- $this->ip=$ip;
- $this->token=$token;
- $this->major='aaa';
- $s = explode('\\', get_class($this));
- $module=request()->module();
- $controller = request()->controller();//控制器名
- $action=request()->action();
- $token='lyuis';
- if($token=='lyuis'){
- $this->userid=7;
- return;
- }
- if (end($s) != 'LoginController' && $module!="admin"&&$action!='login'&&$action!='datarealtime'&&$action!='home' ) {
- if(empty($token)){
- $this->api_return_json(['msg'=>'请先登录。']);
- }
- $result=Jwt::verify($token);
- // dump($token);
- // dump($result);
- if($result['code']!=1){
- $this->api_return_json(['msg'=>$result['message']]);
- }
- $data=(array)$result['message']['data'];
- if($data['major']!=$this->major){
- $this->api_return_json(['msg'=>'登录过期']);
- }
- /* $params=$this->request->param();
- unset($params['sign']);
- if(!$this->checkSign($this->request->param('sign'),$params)){
- $this->api_return_json(array('msg'=>'签名错误'));
- }
- if(((int)$params['time']+60)<time()){
- $this->api_return_json(array('msg'=>'签名过期'));
- } */
- $this->userid=$data['userid'];
- };
- // if (empty($sid)){
- //
- // }
- //
- // session_id($sid);
- // if(!isset($_SESSION)){
- // session_start();
- // }
- date_default_timezone_set('Asia/Shanghai');
- // if (isset($_SESSION['user_id'])) {
- // $this->userid = $_SESSION['user_id'];
- // }
- }
- /**
- * @param unknown_type $return_array
- * @param unknown_type $info_code 0代表失败 1代表成功 2代表其他
- */
- function api_return_json($return_array, $info_code = '0')
- {
- $callback = isset($_GET['callback']) ? trim($_GET['callback']) : '';
- $return_array['code'] = $info_code;
- if (!empty($callback)) {
- echo $callback . '(' . json_encode($return_array,JSON_UNESCAPED_UNICODE) . ')';
- exit();
- } else {
- echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
- exit();
- }
- }
- function curl_get($url)
- {
- $ch = curl_init();
- // 设置选项,包括URL
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- if (stripos($url, "https://") !== FALSE) {
- curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- } else {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
- }
- // 执行并获取HTML文档内容
- $output = curl_exec($ch);
- // 释放curl句柄
- $err_code = curl_errno($ch);
- curl_close($ch);
- return json_decode($output);
- }
- function getStartDay()
- {
- return strtotime(date('Y-m-d') . ' 00:00:00');
- }
- function getEndDay()
- {
- return strtotime(date('Y-m-d') . ' 23:59:59');
- }
-
- function getWeek($index)
- {
- $sdefaultDate = date("Y-m-d");
- //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
- $first=1;
- //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
- $w=date('w',strtotime($sdefaultDate));
- //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
- $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
- //本周结束日期
- $week_end=date('Y-m-d',strtotime("$week_start +6 days"));
- $week_start=strtotime($week_start . ' 00:00:00');
- $week_end=strtotime($week_end . ' 23:59:59');
- $tem=array($week_start,$week_end);
- return $tem[$index];
- }
-
- function getFirstDateMonth($month='',$year=''){
- if(empty($month)){
- return strtotime(date('Y-m').'-01 00:00:00');
- }elseif(empty($year)){
- return strtotime(date('Y-'.$month).'-01 00:00:00');
- }else{
- return strtotime(date($year.'-'.$month).'-01 00:00:00');
- }
- }
-
- function getEndDateMonth($month='',$year=''){
- if(empty($month)){
- $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
- $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
- return strtotime($date);
- }elseif(empty($year)){
- $BeginDate=date('Y-m-01', strtotime(date("Y-".$month."-d")));
- $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
- return strtotime($date);
- }else{
- $BeginDate=date('Y-m-01', strtotime(date($year."-".$month."-d")));
- $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
- return strtotime($date);
- }
- }
- //判断两个日期是否相连
- public function isNextDay($day1, $day2)
- {
- $date1 = date('Y-m-d', $day1);
- $date2 = date('Y-m-d', $day2);
- $is_next = (strtotime($date1 . ' 00:00:00') - strtotime($date2 . ' 00:00:00')) / 86400;
- return $is_next == 1;
- }
- public function getSignCount()
- {
- $days = Db::name('sign')->where(array('user_id' => $this->userid))->order('date desc')->limit(0, 30)->select()->toArray();
- $count = 0;
- if (sizeof($days) > 0)
- $count = 1;
- for ($i = 0; $i < sizeof($days) - 1; $i++) {
- if ($i == 0) {
- $yesterday = date('Y-m-d', strtotime("-1 day"));
- $today = date('Y-m-d');
- $sign_date = date('Y-m-d', $days[$i]['date']);
- if ($yesterday != $sign_date && $sign_date != $today) {
- $count = 0;
- break;
- }
- }
- if ($this->isNextDay($days[$i]['date'], $days[$i + 1]['date'])) {
- $count++;
- } else {
- break;
- }
- }
- return $count;
- }
- function curl_post($url, $data)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $return = curl_exec($ch);
- curl_close($ch);
- return $return;
- }
- //签名验证
- //将前端传参sign与除了sign以外的参数后台拼接成sign进行对比验证
- public function checkSign($sign, $params)
- {
- ksort($params);
- //var_dump($params);
- $param_str = '';
- $i = 0;
- foreach ($params as $key => $param) {
- if(!$this->haveEmojiChar($param)){
- if ($i == 0) {
- $param_str .= $key . '=' . $param;
- } else {
- $param_str .= "&" . $key . '=' . $param;
- }
- }
- $i++;
- }
- $signBackend = strtoupper(md5($param_str . '&key=' . md5($_SERVER['SERVER_NAME'])));
- file_put_contents(CMF_ROOT . 'data/sign.txt', "前端签名:" . $sign . ",后台原始数据," . $param_str . "签名:" . $signBackend . "\n生成签名前的参数:".$param_str . '&key=' . config()['config']['wxconfig']['secret']);
- if ($sign !== $signBackend) {
- return false;
- } else {
- return true;
- }
- }
- //判断是否有表情
- function haveEmojiChar($str)
- {
- $mbLen = mb_strlen($str);
-
- $strArr = [];
- for ($i = 0; $i < $mbLen; $i++) {
- $strArr[] = mb_substr($str, $i, 1, 'utf-8');
- if (strlen($strArr[$i]) >= 4) {
- return true;
- }
- }
-
- return false;
- }
- /**
- * 调试输出函数
- * @param mix $val 调试输出源数据
- * @param bool $dump 是否启用var_dump调试
- * @param bool $exit 是否在调试结束后设置断点
- * @return void
- */
- public function pre($val, $dump = true, $exit = true)
- {
- if ($dump) {
- $func = 'var_dump';
- } else {
- $func = (is_array($val) || is_object($val)) ? 'print_r' : 'printf';
- }
- //输出到html
- header("Content-type:text/html;charset=utf-8");
- echo '<pre>debug output:<hr />';
- $func($val);
- echo '</pre>';
- if ($exit) {
- exit;
- }
- }
- /**
- *计算某个经纬度的周围某段距离的正方形的四个点
- *
- * @param lng float 经度
- * @param lat float 纬度
- * @param 6371 地球半径
- * @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
- * @return array 正方形的四个点的经纬度坐标
- */
- public function returnSquarePoint($lng, $lat, $distance = 5)
- {
- $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
- $dlng = rad2deg($dlng);
- $dlat = $distance / 6371;
- $dlat = rad2deg($dlat);
- return array(
- 'left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
- 'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
- 'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
- 'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
- );
- }
- /**
- * 根据两点间的经纬度计算距离
- * @param $lat1 //纬度值
- * @param $lng1 //经度值
- * @param $lat2
- * @param $lng2
- * @return float 单位为(km)
- */
- public function getDistance($lat1, $lng1, $lat2, $lng2)
- {
- //近似地球半径(米)
- $earthRadius = 6367000;
- //纬度转换
- $lat1 = ($lat1 * pi()) / 180;
- $lng1 = ($lng1 * pi()) / 180;
- //经度转换
- $lat2 = ($lat2 * pi()) / 180;
- $lng2 = ($lng2 * pi()) / 180;
- //计算两点间距离返回(km)
- $differ_lng = $lng2 - $lng1;
- $differ_lat = $lat2 - $lat1;
- $stepOne = pow(sin($differ_lat / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($differ_lng / 2), 2);
- $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
- $distance = $earthRadius * $stepTwo / 1000;
- return round($distance,2);
- }
- /**
- * 拼团服务通知
- */
- public function noticeOrder($val,$p){
- }
- //发送统一服务消息
- public function sendNotice($data){
- if(empty($data))
- return false;
- $access_token=$this->getAccess_token();
- if(!$access_token)
- return false;
- $result=$this->curl_post('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token,$data);
- return $result;
- }
- function getAccess_token($refresh = false){
- $wxconfig=config()['config']['wxconfig'];
- $appid =$wxconfig['appid'];
- $appsecret =$wxconfig['secret'];
- //$appid='wxa334f8548511450e';
- //$appsecret='ba5bd1263065929f8831ce2cf8baf5b8';
- $path="small_access_token.json";
- if(!file_exists($path)){
- $datas['expire_time'] = 0;
- $datas['access_token'] = '';
- file_put_contents($path, json_encode($datas));
- chmod($path, 0777);
- }
- $data = json_decode(file_get_contents($path));
- if ($data->expire_time < time() || $refresh) {
- $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$appid.'&corpsecret='.$appsecret;
- $result = $this->curl_get($url);
- $access_token = $result->access_token;
-
- if (!$access_token) {
- return false;
- } else {
- $datas['expire_time'] = time() + 7000;
- $datas['access_token'] = $access_token;
- $fp = fopen($path, "w");
- fwrite($fp, json_encode($datas));
- fclose($fp);
-
- $this->access_token = $access_token;
- }
- } else if (!$this->access_token){
- $this->access_token = $data->access_token;
- }
- return $this->access_token;
- }
-
- public function FromXml($xml)
- {
- if(!$xml){
- throw new \Exception("xml数据异常!");
- }
- //将XML转为array
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $this->values;
- }
-
- public function ToXml($result){
- if(!is_array($result)
- || count($result) <= 0)
- {
- throw new \Exception("数组数据异常!");
- }
-
- $xml = "<xml>";
- foreach ($result as $key=>$val)
- {
- if (is_numeric($val)){
- $xml.="<".$key.">".$val."</".$key.">";
- }else{
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
- }
- }
- $xml.="</xml>";
- return $xml;
- }
-
- //BD-09(百度)坐标转换成GCJ-02(火星,高德)坐标
- //@param bd_lon 百度经度
- //@param bd_lat 百度纬度
- function bd_decrypt($bd_lon,$bd_lat){
- $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- $x = $bd_lon - 0.0065;
- $y = $bd_lat - 0.006;
- $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
- // $data['gg_lon'] = $z * cos($theta);
- // $data['gg_lat'] = $z * sin($theta);
- $gg_lon = $z * cos($theta);
- $gg_lat = $z * sin($theta);
- // 保留小数点后六位
- $data['gg_lon'] = round($gg_lon, 6);
- $data['gg_lat'] = round($gg_lat, 6);
- return $data;
- }
-
- //GCJ-02(火星,高德)坐标转换成BD-09(百度)坐标
- //@param bd_lon 百度经度
- //@param bd_lat 百度纬度
- function bd_encrypt($gg_lon,$gg_lat){
- $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- $x = $gg_lon;
- $y = $gg_lat;
- $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
- $bd_lon = $z * cos($theta) + 0.0065;
- $bd_lat = $z * sin($theta) + 0.006;
- // 保留小数点后六位
- $data['bd_lon'] = round($bd_lon, 6);
- $data['bd_lat'] = round($bd_lat, 6);
- return $data;
- }
-
- /**
- * 发送站内信息
- */
- public function siteMessage($content='',$siteurl='',$user_id=0,$type=1,$distributor_id=0,$membership_id=0){
- if(empty($content))
- return;
- if(!isset($user_id)||empty($user_id)){
- return;
- }
- $data['user_id']=$user_id;
- $data['add_time']=time();
- $data['content']=$content;
- $data['siteurl']=$siteurl;
- $data['type']=$type;
- $data['distributor_id']=$distributor_id;
- $data['membership_id']=$membership_id;
- Db::name('user_message')->insert($data);
- return true;
- //}
- }
- public function wasterJson($status='success',$msg='',array $data=[]){
- switch ($status){
- case 'success':
- $return=['status'=>$status];
- $return=array_merge($return,$data);
- break;
- case "error":
- $return=['status'=>$status,'msg'=>$msg];
- break;
- }
- file_put_contents('hyInterface.txt', '时间:'.date('Y-m-d H:i:s')."\n".json_encode($return,JSON_UNESCAPED_UNICODE),FILE_APPEND);
- echo json_encode($return,JSON_UNESCAPED_UNICODE);
- exit();
- }
- }
|