MainController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\web\controller;
  12. use think\Db;
  13. use app\common\plugin\Jwt;
  14. use app\common\plugin\Jssdk;
  15. use think\Controller;
  16. class MainController extends Controller
  17. {
  18. public $userid = 0;
  19. public $is_product = true;
  20. protected $token;
  21. protected $major;
  22. protected $ip;
  23. public $access_token;
  24. protected $redis;
  25. function __construct()
  26. {
  27. parent::__construct();
  28. $token=request()->request('sid');
  29. $major=request()->request('major');
  30. // $redis=new \Redis();
  31. // $redis->connect(config("config.redis.REDIS_HOST"),config("config.redis.REDIS_PORT"));
  32. // $this->redis=$redis;
  33. $ip=request()->ip();
  34. $this->ip=$ip;
  35. $this->token=$token;
  36. $this->major='aaa';
  37. $s = explode('\\', get_class($this));
  38. $module=request()->module();
  39. $controller = request()->controller();//控制器名
  40. $action=request()->action();
  41. $token='lyuis';
  42. if($token=='lyuis'){
  43. $this->userid=7;
  44. return;
  45. }
  46. if (end($s) != 'LoginController' && $module!="admin"&&$action!='login'&&$action!='datarealtime'&&$action!='home' ) {
  47. if(empty($token)){
  48. $this->api_return_json(['msg'=>'请先登录。']);
  49. }
  50. $result=Jwt::verify($token);
  51. // dump($token);
  52. // dump($result);
  53. if($result['code']!=1){
  54. $this->api_return_json(['msg'=>$result['message']]);
  55. }
  56. $data=(array)$result['message']['data'];
  57. if($data['major']!=$this->major){
  58. $this->api_return_json(['msg'=>'登录过期']);
  59. }
  60. /* $params=$this->request->param();
  61. unset($params['sign']);
  62. if(!$this->checkSign($this->request->param('sign'),$params)){
  63. $this->api_return_json(array('msg'=>'签名错误'));
  64. }
  65. if(((int)$params['time']+60)<time()){
  66. $this->api_return_json(array('msg'=>'签名过期'));
  67. } */
  68. $this->userid=$data['userid'];
  69. };
  70. // if (empty($sid)){
  71. //
  72. // }
  73. //
  74. // session_id($sid);
  75. // if(!isset($_SESSION)){
  76. // session_start();
  77. // }
  78. date_default_timezone_set('Asia/Shanghai');
  79. // if (isset($_SESSION['user_id'])) {
  80. // $this->userid = $_SESSION['user_id'];
  81. // }
  82. }
  83. /**
  84. * @param unknown_type $return_array
  85. * @param unknown_type $info_code 0代表失败 1代表成功 2代表其他
  86. */
  87. function api_return_json($return_array, $info_code = '0')
  88. {
  89. $callback = isset($_GET['callback']) ? trim($_GET['callback']) : '';
  90. $return_array['code'] = $info_code;
  91. if (!empty($callback)) {
  92. echo $callback . '(' . json_encode($return_array,JSON_UNESCAPED_UNICODE) . ')';
  93. exit();
  94. } else {
  95. echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
  96. exit();
  97. }
  98. }
  99. function curl_get($url)
  100. {
  101. $ch = curl_init();
  102. // 设置选项,包括URL
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  105. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  106. curl_setopt($ch, CURLOPT_HEADER, 0);
  107. if (stripos($url, "https://") !== FALSE) {
  108. curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  109. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  110. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  111. } else {
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  113. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
  114. }
  115. // 执行并获取HTML文档内容
  116. $output = curl_exec($ch);
  117. // 释放curl句柄
  118. $err_code = curl_errno($ch);
  119. curl_close($ch);
  120. return json_decode($output);
  121. }
  122. function getStartDay()
  123. {
  124. return strtotime(date('Y-m-d') . ' 00:00:00');
  125. }
  126. function getEndDay()
  127. {
  128. return strtotime(date('Y-m-d') . ' 23:59:59');
  129. }
  130. function getWeek($index)
  131. {
  132. $sdefaultDate = date("Y-m-d");
  133. //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  134. $first=1;
  135. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  136. $w=date('w',strtotime($sdefaultDate));
  137. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  138. $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
  139. //本周结束日期
  140. $week_end=date('Y-m-d',strtotime("$week_start +6 days"));
  141. $week_start=strtotime($week_start . ' 00:00:00');
  142. $week_end=strtotime($week_end . ' 23:59:59');
  143. $tem=array($week_start,$week_end);
  144. return $tem[$index];
  145. }
  146. function getFirstDateMonth($month='',$year=''){
  147. if(empty($month)){
  148. return strtotime(date('Y-m').'-01 00:00:00');
  149. }elseif(empty($year)){
  150. return strtotime(date('Y-'.$month).'-01 00:00:00');
  151. }else{
  152. return strtotime(date($year.'-'.$month).'-01 00:00:00');
  153. }
  154. }
  155. function getEndDateMonth($month='',$year=''){
  156. if(empty($month)){
  157. $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
  158. $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
  159. return strtotime($date);
  160. }elseif(empty($year)){
  161. $BeginDate=date('Y-m-01', strtotime(date("Y-".$month."-d")));
  162. $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
  163. return strtotime($date);
  164. }else{
  165. $BeginDate=date('Y-m-01', strtotime(date($year."-".$month."-d")));
  166. $date=date('Y-m-d', strtotime("$BeginDate +1 month -1 day")).' 23:59:59';
  167. return strtotime($date);
  168. }
  169. }
  170. //判断两个日期是否相连
  171. public function isNextDay($day1, $day2)
  172. {
  173. $date1 = date('Y-m-d', $day1);
  174. $date2 = date('Y-m-d', $day2);
  175. $is_next = (strtotime($date1 . ' 00:00:00') - strtotime($date2 . ' 00:00:00')) / 86400;
  176. return $is_next == 1;
  177. }
  178. public function getSignCount()
  179. {
  180. $days = Db::name('sign')->where(array('user_id' => $this->userid))->order('date desc')->limit(0, 30)->select()->toArray();
  181. $count = 0;
  182. if (sizeof($days) > 0)
  183. $count = 1;
  184. for ($i = 0; $i < sizeof($days) - 1; $i++) {
  185. if ($i == 0) {
  186. $yesterday = date('Y-m-d', strtotime("-1 day"));
  187. $today = date('Y-m-d');
  188. $sign_date = date('Y-m-d', $days[$i]['date']);
  189. if ($yesterday != $sign_date && $sign_date != $today) {
  190. $count = 0;
  191. break;
  192. }
  193. }
  194. if ($this->isNextDay($days[$i]['date'], $days[$i + 1]['date'])) {
  195. $count++;
  196. } else {
  197. break;
  198. }
  199. }
  200. return $count;
  201. }
  202. function curl_post($url, $data)
  203. {
  204. $ch = curl_init();
  205. curl_setopt($ch, CURLOPT_URL, $url);
  206. curl_setopt($ch, CURLOPT_POST, 1);
  207. curl_setopt($ch, CURLOPT_HEADER, 0);
  208. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  209. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  210. $return = curl_exec($ch);
  211. curl_close($ch);
  212. return $return;
  213. }
  214. //签名验证
  215. //将前端传参sign与除了sign以外的参数后台拼接成sign进行对比验证
  216. public function checkSign($sign, $params)
  217. {
  218. ksort($params);
  219. //var_dump($params);
  220. $param_str = '';
  221. $i = 0;
  222. foreach ($params as $key => $param) {
  223. if(!$this->haveEmojiChar($param)){
  224. if ($i == 0) {
  225. $param_str .= $key . '=' . $param;
  226. } else {
  227. $param_str .= "&" . $key . '=' . $param;
  228. }
  229. }
  230. $i++;
  231. }
  232. $signBackend = strtoupper(md5($param_str . '&key=' . md5($_SERVER['SERVER_NAME'])));
  233. file_put_contents(CMF_ROOT . 'data/sign.txt', "前端签名:" . $sign . ",后台原始数据," . $param_str . "签名:" . $signBackend . "\n生成签名前的参数:".$param_str . '&key=' . config()['config']['wxconfig']['secret']);
  234. if ($sign !== $signBackend) {
  235. return false;
  236. } else {
  237. return true;
  238. }
  239. }
  240. //判断是否有表情
  241. function haveEmojiChar($str)
  242. {
  243. $mbLen = mb_strlen($str);
  244. $strArr = [];
  245. for ($i = 0; $i < $mbLen; $i++) {
  246. $strArr[] = mb_substr($str, $i, 1, 'utf-8');
  247. if (strlen($strArr[$i]) >= 4) {
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. /**
  254. * 调试输出函数
  255. * @param mix $val 调试输出源数据
  256. * @param bool $dump 是否启用var_dump调试
  257. * @param bool $exit 是否在调试结束后设置断点
  258. * @return void
  259. */
  260. public function pre($val, $dump = true, $exit = true)
  261. {
  262. if ($dump) {
  263. $func = 'var_dump';
  264. } else {
  265. $func = (is_array($val) || is_object($val)) ? 'print_r' : 'printf';
  266. }
  267. //输出到html
  268. header("Content-type:text/html;charset=utf-8");
  269. echo '<pre>debug output:<hr />';
  270. $func($val);
  271. echo '</pre>';
  272. if ($exit) {
  273. exit;
  274. }
  275. }
  276. /**
  277. *计算某个经纬度的周围某段距离的正方形的四个点
  278. *
  279. * @param lng float 经度
  280. * @param lat float 纬度
  281. * @param 6371 地球半径
  282. * @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
  283. * @return array 正方形的四个点的经纬度坐标
  284. */
  285. public function returnSquarePoint($lng, $lat, $distance = 5)
  286. {
  287. $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
  288. $dlng = rad2deg($dlng);
  289. $dlat = $distance / 6371;
  290. $dlat = rad2deg($dlat);
  291. return array(
  292. 'left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
  293. 'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
  294. 'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
  295. 'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
  296. );
  297. }
  298. /**
  299.      * 根据两点间的经纬度计算距离
  300.      * @param $lat1 //纬度值
  301.      * @param $lng1 //经度值
  302.      * @param $lat2
  303.      * @param $lng2
  304.      * @return float 单位为(km)
  305.      */
  306. public function getDistance($lat1, $lng1, $lat2, $lng2)
  307. {
  308. //近似地球半径(米)
  309. $earthRadius = 6367000;
  310. //纬度转换
  311. $lat1 = ($lat1 * pi()) / 180;
  312. $lng1 = ($lng1 * pi()) / 180;
  313. //经度转换
  314. $lat2 = ($lat2 * pi()) / 180;
  315. $lng2 = ($lng2 * pi()) / 180;
  316. //计算两点间距离返回(km)
  317. $differ_lng = $lng2 - $lng1;
  318. $differ_lat = $lat2 - $lat1;
  319. $stepOne = pow(sin($differ_lat / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($differ_lng / 2), 2);
  320. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  321. $distance = $earthRadius * $stepTwo / 1000;
  322. return round($distance,2);
  323. }
  324. /**
  325. * 拼团服务通知
  326. */
  327. public function noticeOrder($val,$p){
  328. }
  329. //发送统一服务消息
  330. public function sendNotice($data){
  331. if(empty($data))
  332. return false;
  333. $access_token=$this->getAccess_token();
  334. if(!$access_token)
  335. return false;
  336. $result=$this->curl_post('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token,$data);
  337. return $result;
  338. }
  339. function getAccess_token($refresh = false){
  340. $wxconfig=config()['config']['wxconfig'];
  341. $appid =$wxconfig['appid'];
  342. $appsecret =$wxconfig['secret'];
  343. //$appid='wxa334f8548511450e';
  344. //$appsecret='ba5bd1263065929f8831ce2cf8baf5b8';
  345. $path="small_access_token.json";
  346. if(!file_exists($path)){
  347. $datas['expire_time'] = 0;
  348. $datas['access_token'] = '';
  349. file_put_contents($path, json_encode($datas));
  350. chmod($path, 0777);
  351. }
  352. $data = json_decode(file_get_contents($path));
  353. if ($data->expire_time < time() || $refresh) {
  354. $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$appid.'&corpsecret='.$appsecret;
  355. $result = $this->curl_get($url);
  356. $access_token = $result->access_token;
  357. if (!$access_token) {
  358. return false;
  359. } else {
  360. $datas['expire_time'] = time() + 7000;
  361. $datas['access_token'] = $access_token;
  362. $fp = fopen($path, "w");
  363. fwrite($fp, json_encode($datas));
  364. fclose($fp);
  365. $this->access_token = $access_token;
  366. }
  367. } else if (!$this->access_token){
  368. $this->access_token = $data->access_token;
  369. }
  370. return $this->access_token;
  371. }
  372. public function FromXml($xml)
  373. {
  374. if(!$xml){
  375. throw new \Exception("xml数据异常!");
  376. }
  377. //将XML转为array
  378. //禁止引用外部xml实体
  379. libxml_disable_entity_loader(true);
  380. $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  381. return $this->values;
  382. }
  383. public function ToXml($result){
  384. if(!is_array($result)
  385. || count($result) <= 0)
  386. {
  387. throw new \Exception("数组数据异常!");
  388. }
  389. $xml = "<xml>";
  390. foreach ($result as $key=>$val)
  391. {
  392. if (is_numeric($val)){
  393. $xml.="<".$key.">".$val."</".$key.">";
  394. }else{
  395. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  396. }
  397. }
  398. $xml.="</xml>";
  399. return $xml;
  400. }
  401. //BD-09(百度)坐标转换成GCJ-02(火星,高德)坐标
  402. //@param bd_lon 百度经度
  403. //@param bd_lat 百度纬度
  404. function bd_decrypt($bd_lon,$bd_lat){
  405. $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  406. $x = $bd_lon - 0.0065;
  407. $y = $bd_lat - 0.006;
  408. $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
  409. $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
  410. // $data['gg_lon'] = $z * cos($theta);
  411. // $data['gg_lat'] = $z * sin($theta);
  412. $gg_lon = $z * cos($theta);
  413. $gg_lat = $z * sin($theta);
  414. // 保留小数点后六位
  415. $data['gg_lon'] = round($gg_lon, 6);
  416. $data['gg_lat'] = round($gg_lat, 6);
  417. return $data;
  418. }
  419. //GCJ-02(火星,高德)坐标转换成BD-09(百度)坐标
  420. //@param bd_lon 百度经度
  421. //@param bd_lat 百度纬度
  422. function bd_encrypt($gg_lon,$gg_lat){
  423. $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  424. $x = $gg_lon;
  425. $y = $gg_lat;
  426. $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
  427. $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
  428. $bd_lon = $z * cos($theta) + 0.0065;
  429. $bd_lat = $z * sin($theta) + 0.006;
  430. // 保留小数点后六位
  431. $data['bd_lon'] = round($bd_lon, 6);
  432. $data['bd_lat'] = round($bd_lat, 6);
  433. return $data;
  434. }
  435. /**
  436. * 发送站内信息
  437. */
  438. public function siteMessage($content='',$siteurl='',$user_id=0,$type=1,$distributor_id=0,$membership_id=0){
  439. if(empty($content))
  440. return;
  441. if(!isset($user_id)||empty($user_id)){
  442. return;
  443. }
  444. $data['user_id']=$user_id;
  445. $data['add_time']=time();
  446. $data['content']=$content;
  447. $data['siteurl']=$siteurl;
  448. $data['type']=$type;
  449. $data['distributor_id']=$distributor_id;
  450. $data['membership_id']=$membership_id;
  451. Db::name('user_message')->insert($data);
  452. return true;
  453. //}
  454. }
  455. public function wasterJson($status='success',$msg='',array $data=[]){
  456. switch ($status){
  457. case 'success':
  458. $return=['status'=>$status];
  459. $return=array_merge($return,$data);
  460. break;
  461. case "error":
  462. $return=['status'=>$status,'msg'=>$msg];
  463. break;
  464. }
  465. file_put_contents('hyInterface.txt', '时间:'.date('Y-m-d H:i:s')."\n".json_encode($return,JSON_UNESCAPED_UNICODE),FILE_APPEND);
  466. echo json_encode($return,JSON_UNESCAPED_UNICODE);
  467. exit();
  468. }
  469. }