swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

自定义服务类注解注入无效 #758

Closed lzmshow closed 5 years ago

lzmshow commented 5 years ago
Q A
Bug report? yes/no
Feature request? yes/no
Swoft version 2.0
Swoole version 4.3.4(by php --ri swoole)
PHP version 7.2 (by php -v)
Runtime environment Ubuntu etc.

Details

在app目录下新建目录Service 封装自己服务类 wechat.

在wecaht类中 有注入logic 注入的logic无效

在控制器中我是

$res = BeanFactory::getBean(Wechat::class); 这样实例化的 如果直接通过控制器注入logic是有效的

请问下这是什么原因

控制器

<?php
namespace App\Http\Controller\Auth;

use App\Service\Message;
use App\Service\Wechat;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Bean\BeanFactory;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Http\Server\Annotation\Ser;
use App\Model\Logic\ConnectLogic;
use App\Model\Logic\UserLogic;

/**
 * Class CompanyController
 * @Controller(prefix="/auth")
 * @package App\Http\Controller\Backend
 */
class AuthController {

    /**
     * @Inject()
     * @var ConnectLogic
     */
    private $connectLogic;

    /**
     * @Inject()
     * @var UserLogic
     */
    private $userLogic;

    /**
     * @RequestMapping(route="login",method={RequestMethod::POST})
     */
    public function login(Request $request) {
        //print_r($this->connectLogic->getByOpenId('23323'));exit;
        $params = $request->post();
        if(!$params['type']) {
            return Message::error('缺少参数!');
        }
        /* @var Wechat $res*/
        $res = BeanFactory::getBean(Wechat::class);
        if($loginKey = $res->login($params)) {
            return Message::success('授权成功',['loginKey'=>$loginKey]);
        }
        return Message::success('授权失败',['loginKey'=>$loginKey]);
    }
}

自定义服务类

<?php

namespace App\Service;

use App\Exception\ServiceException;
use App\Model\Logic\UserLogic;
use GuzzleHttp\Client;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use App\Model\Logic\ConnectLogic;

/**
 * 微信操作
 *
 * @Bean()
 */
class Wechat {

    //用户SESSION_KEY API
    const JSCODE_SESSION_API = 'https://api.weixin.qq.com/sns/jscode2session?';

    const ACCESSTOKEN_API = 'https://api.weixin.qq.com/cgi-bin/token?';

    const WXACODE_API = 'https://api.weixin.qq.com/wxa/getwxacode?';

    /**
     * @Inject()
     * @var ConnectLogic
     */
    private $connectLogic;

    /**
     * @Inject()
     * @var UserLogic
     */
    private $userLogic;

    /**
     * @param $params
     * @return bool|mixed
     * @throws ServiceException
     */
    public function login($params) {
        if(!$params['code']) {
            throw new ServiceException('code参数丢失');
        }
        $stdClass = $this->codeToSessionKey($params['code']);
        if(isset($stdClass->session_key) && $stdClass->session_key) {
            $openId = $stdClass->openid;

            if(!$infos = $this->connectLogic->getByOpenId($openId)) {
                //创建用户
                $infos = $this->connectLogic->save(['openid'=>$openId]);
            }
            //更新用户登陆时间
            $userInfo = $this->userLogic->getUser($infos->getUserId());
            if(!$userInfo) {
                throw new ServiceException('登陆数据异常');
            }
            $updata = [
                'login_at'=>time(),
                'login_ip'=> '192.168.100.1',
                'id'=>$infos->getUserId()
            ];
            if($userInfo->update($updata)) {
                $loginData = [
                    'session_key'=>$stdClass->session_key,
                    'openid'=>$openId,
                    'user_id'=>$userInfo->getId(),
                ];
                $loginKey = $this->userLogic->loginData($loginData);
                if($loginKey) {
                   return $loginKey;
                }
            }
            return false;
        }
        $msg = isset($stdClass->errmsg)?$stdClass->errmsg:'授权失败';
        throw new ServiceException($msg);
    }

在这个类$this->userLogic 和$this->connectLogic无法调用     
stelin commented 5 years ago

@lzmshow 发出你的注入代码,你加@Inject 注解了么

stelin commented 5 years ago

我已亲测没问题,你要查下你那里使用不对

stelin commented 5 years ago

或者提供最小复现demo

lzmshow commented 5 years ago

我已亲测没问题,你要查下你那里使用不对

可以贴下下你的服务类代码吗,看能不能发现我的问题在哪