123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\service\controller\api;
- use app\service\service\WechatService;
- use think\Controller;
- use think\Db;
- class Client extends Controller
- {
-
- protected $config = [];
-
- protected $appid = '';
-
- protected $name = '';
-
- protected $type = '';
-
- protected $message = '';
-
- public function yar($param)
- {
- try {
- $instance = $this->create($param);
- $service = new \Yar_Server(empty($instance) ? $this : $instance);
- $service->handle();
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
-
- public function soap($param)
- {
- try {
- $instance = $this->create($param);
- $service = new \SoapServer(null, ['uri' => strtolower($this->name)]);
- $service->setObject(empty($instance) ? $this : $instance);
- $service->handle();
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
-
- private function create($token)
- {
- if ($this->auth($token)) {
- $weminiClassName = 'Account,Basic,Code,Domain,Tester,User,Crypt,Plugs,Poi,Qrcode,Template,Total';
- $wechatClassName = 'Card,Custom,Limit,Media,Menu,Oauth,Pay,Product,Qrcode,Receive,Scan,Script,Shake,Tags,Template,User,Wifi';
- if ($this->type === 'wechat' && stripos($wechatClassName, $this->name) !== false) {
- $instance = WechatService::instance($this->name, $this->appid, 'WeChat');
- } elseif ($this->type === 'wemini' && stripos($weminiClassName, $this->name) !== false) {
- $instance = WechatService::instance($this->name, $this->appid, 'WeMini');
- } elseif (stripos('Service,MiniApp', $this->name) !== false) {
- $instance = WechatService::instance($this->name, $this->appid, 'WeOpen');
- } elseif (stripos('Wechat,Config,Handler', $this->name) !== false) {
- $className = "\\app\\service\\handler\\WechatHandler";
- $instance = new $className($this->config);
- }
- if (!empty($instance)) return $instance;
- }
- throw new \think\Exception($this->message);
- }
-
- private function auth($token = '')
- {
- list($this->name, $this->appid, $appkey, $this->type) = explode('-', $token . '---');
- if (empty($this->name) || empty($this->appid) || empty($appkey)) {
- $this->message = '缺少必要的参数AppId或AppKey';
- return false;
- }
- $where = ['authorizer_appid' => $this->appid, 'status' => '1', 'is_deleted' => '0'];
- $this->config = Db::name('WechatServiceConfig')->where($where)->find();
- if (empty($this->config)) {
- $this->message = '无效的微信绑定对象';
- return false;
- }
- if (strtolower($this->config['appkey']) !== strtolower($appkey)) {
- $this->message = '授权AppId与AppKey不匹配';
- return false;
- }
- $this->message = '';
- $this->name = ucfirst(strtolower($this->name));
- $this->type = strtolower(empty($this->type) ? 'WeChat' : $this->type);
- Db::name('WechatServiceConfig')->where($where)->setInc('total', 1);
- return true;
- }
- }
|