123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Home\Controller;
- use Think\Controller;
- class ComController extends Controller
- {
- public $owner_name;
- public function _initialize()
- {
- header("Access-Control-Allow-Origin:*");
- header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
- header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
- $_SESSION['phone'] = '17621295894';
- }
- //获取设备编号
- public function getDeviceCode($company)
- {
- $device = M()->query("SELECT o.owner_code,c.owner_name FROM sp_owner o join sp_owner_company c ON o.company =c.owner_id WHERE o.company in(".$company.")");
- $this->owner_name = $device[0]['owner_name'];
- $str = "";
- foreach ($device as $val) {
- $str .= "'" . $val['owner_code'] . "',";
- }
- $newstr = substr($str, 0, strlen($str) - 1);
- return $newstr;
- }
- public function getCompileUrl()
- {
- $Data = I('');
- if (empty($Data['curl'])) {
- $R['flag'] = false;
- $R['msg'] = '缺少关键参数curl';
- echo json_encode($R);
- exit();
- }
- $curl = $Data['curl'];
- $tourl = urlencode($curl);
- $R['flag'] = true;
- $R['turl'] = $tourl;
- $R['msg'] = '当前路径编译成功';
- echo json_encode($R);
- exit();
- }
- public function getPageAuthorization()
- {
- if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
- //跳转登录页
- $R['msg'] = '页面授权未通过,跳转登录页面';
- $R['flag'] = false;
- echo json_encode($R);
- exit();
- }
- if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
- $Data = I('');
- $code = $Data['code'];
- if (empty($code)) {
- $R['msg'] = '路径编译成功';
- $R['flag'] = false;
- echo json_encode($R);
- exit();
- } else {
- $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
- $res = M()->query("SELECT phone FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "' and type='wx'");
- if (count($res) > 0) {
- $_SESSION['idopenid'] = $user_openid['openid'];
- $_SESSION['openid'] = $user_openid['openid'];
- $this->iphone = $res[0]['phone'];
- $_GET['phone'] = $res[0]['phone'];
- $_SESSION['phone'] = $res[0]['phone'];
- $R['msg'] = '页面授权通过,用户权限通过';
- $R['flag'] = true;
- echo json_encode($R);
- exit();
- } else {
- unset($_SESSION['idopenid']);
- unset($_SESSION['phone']);
- $R['msg'] = '页面授权未通过,跳转登录页面';
- $R['flag'] = false;
- echo json_encode($R);
- exit();
- }
- }
- } else {
- $res = M()->query("SELECT phone FROM sp_wx_customuser WHERE openid='" . $_SESSION['idopenid'] . "' and type='wx'");
- if (count($res) > 0) {
- $this->iphone = $res[0]['phone'];
- $_GET['phone'] = $res[0]['phone'];
- $_SESSION['phone'] = $res[0]['phone'];
- $R['msg'] = '页面授权通过,用户权限通过';
- $R['flag'] = true;
- echo json_encode($R);
- exit();
- } else {
- unset($_SESSION['idopenid']);
- unset($_SESSION['phone']);
- $R['msg'] = '页面授权未通过,跳转登录页面';
- $R['flag'] = false;
- echo json_encode($R);
- exit();
- }
- }
- }
- public function getPageAuthorization1()
- {
- $http = 'https://';
- $code = I('get.code');
- // print_r("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $_SESSION['app_token'] . "' and type='app' and phone='".$_SESSION['phone']."'");
- // exit();
- if (isset($_SESSION['app_token'])&&isset($_SESSION['phone'])){
- $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE type='app' and phone='".$_SESSION['phone']."'");
- header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);//项目首页
- }else{
- if (!strpos($_SERVER['HTTP_USER_AGENT'], "MicroMessenger") != false) {
- header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
- exit();
- }
- if (!isset($_SESSION['idopenid']) && empty($_SESSION['idopenid'])) {
- if (empty($code)) {
- $tourl = urlencode($http . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]);
- header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx41e478e592f9e04a&redirect_uri=' . $tourl . '&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect ');
- } else {
- $user_openid = $this->s_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx41e478e592f9e04a&secret=fc165de4aa5cd24b628baa30b100e146&code=' . $code . '&grant_type=authorization_code');
- $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $user_openid['openid'] . "' and type='wx'");
- if (count($res) > 0) {
- $_SESSION['idopenid'] = $user_openid['openid'];
- $_SESSION['openid'] = $user_openid['openid'];
- $this->iphone = $res[0]['phone'];
- $_GET['phone'] = $res[0]['phone'];
- $_SESSION['phone'] = $res[0]['phone'];
- // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
- header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);//项目首页
- } else {
- unset($_SESSION['idopenid']);
- unset($_SESSION['phone']);
- // header('Location:https://iot.usky.cn/ceshi.html?');//登录页
- header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
- }
- }
- } else {
- $res = M()->query("SELECT phone,openid FROM sp_wx_customuser WHERE openid='" . $_SESSION['idopenid'] . "' and type='wx'");
- if (count($res) > 0) {
- $this->iphone = $res[0]['phone'];
- $_GET['phone'] = $res[0]['phone'];
- $_SESSION['phone'] = $res[0]['phone'];
- // print_r($res);
- // exit();
- // header('Location:https://iot.usky.cn/ceshi.html?op='.$res['openid']);//项目首页
- header('Location:https://qhome.usky.cn/work/#/?op='.$res[0]['openid']);
- } else {
- unset($_SESSION['idopenid']);
- unset($_SESSION['phone']);
- header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');//登录页
- // header('Location:https://qhome.usky.cn/work/index.html#/pages/login/login');
- }
- }
- }
- }
- private function s_curl($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- $arr = json_decode($file_contents, true);
- return $arr;
- }
- }
|