| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\models\LySilentSign;
- use addons\AvoidTax\common\models\TaxUserAuth;
- use addons\AvoidTax\common\models\User;
- use common\enums\RabbitMqEnum;
- use Yii;
- use yii\data\Pagination;
- /**
- * AuthService class
- * @package addons\AvoidTax\common\service
- */
- class AuthService extends BaseService
- {
- /**
- * 列表
- *
- * @param array $params
- * @return array
- */
- public function getList($params = [])
- {
- $page = $this->getPage($params);
- $limit = $this->getLimit($params);
- if ($this->getConfigService()->isXLApiType()) {
- return TaxUserAuth::getPageList($page, $limit, function ($model) use ($params) {
- // 查询用户表
- $model->with(['user' => function ($query) {
- $query->select('id, avatar_url, nickname, username, mobile');
- }]);
- $model->where(['mall_id' => $this->mall_id, 'status' => TaxUserAuth::STATUS_OK]);
-
- if (!empty($params['keyword'])) {
- $ids = User::getIdsByKeywordAndMallId($this->mall_id, $params['keyword']);
- $model->andWhere(['in', 'user_id', $ids]);
- }
- });
- }
- if ($this->getConfigService()->isLYApiType()) {
- return LySilentSign::getPageList($page, $limit, function ($model) use ($params) {
- // 查询用户表
- $model->with(['user' => function ($query) {
- $query->select('id, avatar_url, nickname, username, mobile');
- }]);
- $model->where(['mall_id' => $this->mall_id, 'status' => 1]);
- $model->select('*, id_card as id_card_no, phone as mobile');
-
- if (!empty($params['keyword'])) {
- $ids = User::getIdsByKeywordAndMallId($this->mall_id, $params['keyword']);
- $model->andWhere(['in', 'user_id', $ids]);
- }
- });
- }
- }
- /**
- * 获取当前用户的认证列表
- *
- * @param integer $user_id
- * @return array
- */
- public function getUserAuthList(int $user_id)
- {
- if ($this->getConfigService()->isXLApiType()) {
- // 获取前先验证
- $list = TaxUserAuth::getUserNotAuthListByOneDay($this->mall_id, $user_id);
- foreach ($list as $v) {
- $this->getXlService()->signQuery($v);
- }
- return TaxUserAuth::getUserAuthList($this->mall_id, $user_id);
- }
- if ($this->getConfigService()->isLYApiType()) {
- return LySilentSign::getUserAuthList($this->mall_id, $user_id);
- }
- }
- /**
- * 设置默认
- *
- * @param integer $id
- * @return boolean
- */
- public function setAuthDefault(int $id)
- {
- $db = Yii::$app->db->beginTransaction();
- try {
- if ($this->getConfigService()->isXLApiType()) {
- /**
- * @var TaxUserAuth
- */
- $static = TaxUserAuth::class;
- }
- if ($this->getConfigService()->isLYApiType()) {
- /**
- * @var LySilentSign
- */
- $static = LySilentSign::class;
- }
- $auth = $static::getAuthById($this->mall_id, $id);
- if (empty($auth)) return $this->error('记录不存在');
- if ($auth->default == 1) return true;
- // 设置0
- $static::setNotDefault($this->mall_id, $auth->user_id);
- // 设置默认
- $auth->setDefault();
- $db->commit();
- return true;
- } catch (\Exception $e) {
- $db->callback();
- return $this->error($e->getMessage());
- }
- }
- /**
- * 修改支付宝账号
- *
- * @param integer $id
- * @param string $mobile
- * @param string|null $mobile
- * @return boolean
- */
- public function changeMobile(int $id, string $mobile, $account)
- {
- if ($this->getConfigService()->isXLApiType()) {
- $auth = TaxUserAuth::getUserByIdAndMallId($this->mall_id, $id);
- if (empty($auth)) {
- return $this->error('用户账号不存在');
- }
- }
- if ($this->getConfigService()->isLYApiType()) {
- $auth = LySilentSign::getUserByIdAndMallId($this->mall_id, $id);
- if (empty($auth)) {
- return $this->error('用户账号不存在');
- }
- }
- return $auth->changeMobile($mobile, $account);
- }
- /**
- * 转换
- *
- * @return boolean
- */
- public function convert()
- {
- $users = TaxUserAuth::getAllAuthUsers($this->mall_id);
- foreach ($users as $user) {
- $data = [
- 'mall_id' => $this->mall_id,
- 'sign_user' => [
- 'user_id' => $user->user_id,
- 'name' => $user->name,
- 'id_card' => $user->id_card_no,
- 'mobile' => $user->mobile,
- ],
- ];
- // 添加异步导入任务
- Yii::$app->services->rabbitMq->push(
- RabbitMqEnum::EXCHANGE_TASK,
- RabbitMqEnum::TASK_QUEUE,
- $data,
- static::class,
- 'asyncImportSignUser'
- );
- }
- }
- /**
- * 异步导入
- *
- * @param array $data
- * [
- * 'mall_id' => int,
- * 'user_id' => int,
- * 'sign_user' => array
- * ]
- * @return void
- */
- public static function asyncImportSignUser(array $data)
- {
- $mall_id = $data['mall_id'];
- $sign_user = $data['sign_user'];
- $service = new UserService(['mall_id' => $mall_id]);
-
- // 查询用户是否签约
- $is_sgin = LySilentSign::userIsSignByNMI(
- $sign_user['user_id'],
- $sign_user['name'],
- $sign_user['mobile'],
- $sign_user['id_card']
- );
- if (!$is_sgin) {
- $service->silentSign(
- $sign_user['user_id'],
- $sign_user['name'],
- $sign_user['mobile'],
- $sign_user['id_card']
- );
- }
- }
- /**
- * 解除绑定
- *
- * @param integer $id
- * @return boolean
- */
- public function unbind(int $id)
- {
- if ($this->getConfigService()->isXLApiType()) {
- $auth = TaxUserAuth::getOkAuthById($this->mall_id, $id);
- if (empty($auth)) {
- return $this->error('记录不存在');
- }
-
- return $auth->unbind() ? true : $this->error('解绑失败');
- }
- if ($this->getConfigService()->isLYApiType()) {
- $auth = LySilentSign::getOkAuthById($this->mall_id, $id);
- if (empty($auth)) {
- return $this->error('记录不存在');
- }
-
- return $auth->setLift() ? true : $this->error('解绑失败');
- }
- }
- /**
- * 解除绑定
- *
- * @param integer $user_id
- * @param integer $id
- * @return boolean
- */
- public function lift(int $user_id, int $id)
- {
- if ($this->getConfigService()->isXLApiType()) {
- return $this->error('该功能暂未开放');
- }
- if ($this->getConfigService()->isLYApiType()) {
- $auth = LySilentSign::getSignById($user_id, $id);
- if (empty($auth)) {
- return $this->error('记录不存在');
- }
- if ($auth->default) return $this->error('默认签约不能解绑');
- return $auth->lift() ? true : $this->error('解绑失败');
- }
- }
- }
|