MainController.php 17 KB

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