示例#1
0
 protected function _initialize()
 {
     //S('access_token',null);
     //如果没有access_token
     if (S('at_time') + 7200 <= time()) {
         $wx_info = C('wx_info');
         $Auth = new WechatAuth($wx_info['AppID'], $wx_info['Secret']);
         //获取access_token
         $access_token = $Auth->getAccessToken();
         if (!empty($access_token['access_token'])) {
             //保存到缓存7198秒
             S('access_token', $access_token['access_token'], 7198);
             S('at_time', time());
         } else {
             $this->error('获取信息失败,请通知管理员');
         }
     }
     if (empty(S('access_token'))) {
         $wx_info = C('wx_info');
         $Auth = new WechatAuth($wx_info['AppID'], $wx_info['Secret']);
         //获取access_token
         $access_token = $Auth->getAccessToken();
         if (!empty($access_token['access_token'])) {
             //保存到缓存7198秒
             S('access_token', $access_token['access_token'], 7198);
             S('at_time', time());
         } else {
             $this->error('获取信息失败,请通知管理员');
         }
     }
 }
 public function index()
 {
     $wechatauth = new WechatAuth(C('WEIXIN.APPID'), C('WEIXIN.SECRET'), C(''));
     $access_token = $wechatauth->getAccessToken();
     $result = $wechatauth->getServerIp($access_token);
     if (isset($result['errcode'])) {
         $this->error($result['errmsg']);
     }
     slog($result);
     $this->assign('ip', $result['ip_list']);
     $this->display();
 }
 /**
  * @return array|mixed
  * @throws \Exception
  * Judge weChat access_token
  */
 protected function tokenJudge()
 {
     $res = M('token')->where("id = 1")->find();
     $time = $res['m_time'];
     $now = time();
     if ($now - $time >= 3600) {
         $weChat = new WechatAuth();
         $data = $weChat->getAccessToken();
         $token = $data['access_token'];
         $save = ['token' => $token, 'm_time' => time()];
         M('token')->where("id = 1")->save($save);
     } else {
         $token = $res['token'];
     }
     return $token;
 }
示例#4
0
 /**
  * 资源文件上传方法
  * @param  string $type 上传的资源类型
  * @return string       媒体资源ID
  */
 private function upload($type)
 {
     $appid = 'wx58aebef2023e68cd';
     $appsecret = 'bf818ec2fb49c20a478bbefe9dc88c60';
     $token = session("token");
     if ($token) {
         $auth = new WechatAuth($appid, $appsecret, $token);
     } else {
         $auth = new WechatAuth($appid, $appsecret);
         $token = $auth->getAccessToken();
         session(array('expire' => $token['expires_in']));
         session("token", $token['access_token']);
     }
     switch ($type) {
         case 'image':
             $filename = './Public/image.jpg';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         case 'voice':
             $filename = './Public/voice.mp3';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         case 'video':
             $filename = './Public/video.mp4';
             $discription = array('title' => '视频标题', 'introduction' => '视频描述');
             $media = $auth->materialAddMaterial($filename, $type, $discription);
             break;
         case 'thumb':
             $filename = './Public/music.jpg';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         default:
             return '';
     }
     if ($media["errcode"] == 42001) {
         //access_token expired
         session("token", null);
         $this->upload($type);
     }
     return $media['media_id'];
 }
 public function tmpUploadVideo()
 {
     $appid = C('WEIXIN.APPID');
     $appsecret = C('WEIXIN.SECRET');
     $token = session("token");
     if ($token) {
         $auth = new WechatAuth($appid, $appsecret, $token);
     } else {
         $auth = new WechatAuth($appid, $appsecret);
         $token = $auth->getAccessToken();
         session(array('expire' => $token['expires_in']));
         session("token", $token['access_token']);
     }
     if (session('?weixin_video_id')) {
         return session('weixin_video_id');
     } else {
         $filename = './Public/video.mp4';
         $type = 'video';
         $discription = array('title' => '视频标题', 'introduction' => '视频描述');
         $media = $auth->materialAddMaterial($filename, $type, $discription);
         session('weixin_video_id', $media);
         slog($media);
         return session('weixin_video_id');
     }
 }
示例#6
0
 /**
  * 资源文件上传方法
  * @param  string $type 上传的资源类型
  * @return string       媒体资源ID
  */
 private function upload($type)
 {
     vendor('Weixinpay.WxPayJsApiPay');
     $appid = \WxPayConfig::APPID;
     $appsecret = \WxPayConfig::APPSECRET;
     $token = session("token");
     if ($token) {
         $auth = new WechatAuth($appid, $appsecret, $token);
     } else {
         $auth = new WechatAuth($appid, $appsecret);
         $token = $auth->getAccessToken();
         session(array('expire' => $token['expires_in']));
         session("token", $token['access_token']);
     }
     switch ($type) {
         case 'image':
             $filename = './Public/image.jpg';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         case 'voice':
             $filename = './Public/voice.mp3';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         case 'video':
             $filename = './Public/video.mp4';
             $discription = array('title' => '视频标题', 'introduction' => '视频描述');
             $media = $auth->materialAddMaterial($filename, $type, $discription);
             break;
         case 'thumb':
             $filename = './Public/music.jpg';
             $media = $auth->materialAddMaterial($filename, $type);
             break;
         default:
             return '';
     }
     if ($media["errcode"] == 42001) {
         //access_token expired
         session("token", null);
         $this->upload($type);
     }
     return $media['media_id'];
 }