getConfig()); } else { list($appid, $appkey) = [sysconf('wechat.thr_appid'), sysconf('wechat.thr_appkey')]; $data = ['class' => $name, 'appid' => $appid, 'time' => time(), 'nostr' => uniqid()]; $data['sign'] = md5("{$data['class']}#{$appid}#{$appkey}#{$data['time']}#{$data['nostr']}"); $token = enbase64url(json_encode($data, JSON_UNESCAPED_UNICODE)); if (class_exists('Yar_Client')) { $url = "http://open.cuci.cc/service/api.client/yar?not_init_session=1&token={$token}"; $client = new \Yar_Client($url); } else { $url = "http://open.cuci.cc/service/api.client/soap?not_init_session=1&token={$token}"; $client = new \SoapClient(null, ['location' => $url, 'uri' => "thinkadmin"]); } try { $exception = new \think\Exception($client->getMessage(), $client->getCode()); } catch (\Exception $exception) { $exception = null; } if ($exception instanceof \Exception) { throw $exception; } return $client; } } /** * 获取当前微信APPID * @return bool|string * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAppid() { if ($this->getType() === 'api') { return sysconf('wechat.appid'); } else { return sysconf('wechat.thr_appid'); } } /** * 获取接口授权模式 * @return string * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getType() { $type = strtolower(sysconf('wechat.type')); if (in_array($type, ['api', 'thr'])) return $type; throw new \think\Exception('请在后台配置微信对接授权模式'); } /** * 获取公众号配置参数 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getConfig() { return [ 'token' => sysconf('wechat.token'), 'appid' => sysconf('wechat.appid'), 'appsecret' => sysconf('wechat.appsecret'), 'encodingaeskey' => sysconf('wechat.encodingaeskey'), 'cache_path' => $this->app->getRuntimePath() . 'wechat', ]; } /** * 通过网页授权获取粉丝信息 * @param string $source 回跳URL地址 * @param integer $isfull 获取资料模式 * @param boolean $redirect 是否直接跳转 * @return array * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getWebOauthInfo($source, $isfull = 0, $redirect = true) { $appid = $this->getAppid(); $openid = $this->app->session->get("{$appid}_openid"); $fansinfo = $this->app->session->get("{$appid}_fansinfo"); if ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($fansinfo))) { empty($fansinfo) || FansService::instance()->set($fansinfo); return ['openid' => $openid, 'fansinfo' => $fansinfo]; } if ($this->getType() === 'api') { $wechat = self::WeChatOauth(); if (input('state') !== $appid) { $snsapi = empty($isfull) ? 'snsapi_base' : 'snsapi_userinfo'; $param = (strpos($source, '?') !== false ? '&' : '?') . 'rcode=' . encode($source); $OauthUrl = $wechat->getOauthRedirect($source . $param, $appid, $snsapi); if ($redirect) redirect($OauthUrl, 301)->send(); exit("window.location.href='{$OauthUrl}'"); } if (($token = $wechat->getOauthAccessToken()) && isset($token['openid'])) { $this->app->session->set("{$appid}_openid", $openid = $token['openid']); if (empty($isfull) && input('rcode')) { redirect(enbase64url(input('rcode')), 301)->send(); } $this->app->session->set("{$appid}_fansinfo", $fansinfo = $wechat->getUserInfo($token['access_token'], $openid)); empty($fansinfo) || FansService::instance()->set($fansinfo); } redirect(enbase64url(input('rcode')), 301)->send(); } else { $result = self::ThinkAdminConfig()->oauth($this->app->session->getId(), $source, $isfull); $this->app->session->set("{$appid}_openid", $openid = $result['openid']); $this->app->session->set("{$appid}_fansinfo", $fansinfo = $result['fans']); if ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($fansinfo))) { empty($fansinfo) || FansService::instance()->set($fansinfo); return ['openid' => $openid, 'fansinfo' => $fansinfo]; } if ($redirect && !empty($result['url'])) { redirect($result['url'], 301)->send(); } exit("window.location.href='{$result['url']}'"); } } /** * 获取微信网页JSSDK * @param string $url JS签名地址 * @return array * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getWebJssdkSign($url = null) { $url = is_null($url) ? $this->app->request->url(true) : $url; if ($this->getType() === 'api') { return self::WeChatScript()->getJsSign($url); } else { return self::ThinkAdminConfig()->jsSign($url); } } }