AuthService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\models\LySilentSign;
  4. use addons\AvoidTax\common\models\TaxUserAuth;
  5. use addons\AvoidTax\common\models\User;
  6. use common\enums\RabbitMqEnum;
  7. use Yii;
  8. use yii\data\Pagination;
  9. /**
  10. * AuthService class
  11. * @package addons\AvoidTax\common\service
  12. */
  13. class AuthService extends BaseService
  14. {
  15. /**
  16. * 列表
  17. *
  18. * @param array $params
  19. * @return array
  20. */
  21. public function getList($params = [])
  22. {
  23. $page = $this->getPage($params);
  24. $limit = $this->getLimit($params);
  25. if ($this->getConfigService()->isXLApiType()) {
  26. return TaxUserAuth::getPageList($page, $limit, function ($model) use ($params) {
  27. // 查询用户表
  28. $model->with(['user' => function ($query) {
  29. $query->select('id, avatar_url, nickname, username, mobile');
  30. }]);
  31. $model->where(['mall_id' => $this->mall_id, 'status' => TaxUserAuth::STATUS_OK]);
  32. if (!empty($params['keyword'])) {
  33. $ids = User::getIdsByKeywordAndMallId($this->mall_id, $params['keyword']);
  34. $model->andWhere(['in', 'user_id', $ids]);
  35. }
  36. });
  37. }
  38. if ($this->getConfigService()->isLYApiType()) {
  39. return LySilentSign::getPageList($page, $limit, function ($model) use ($params) {
  40. // 查询用户表
  41. $model->with(['user' => function ($query) {
  42. $query->select('id, avatar_url, nickname, username, mobile');
  43. }]);
  44. $model->where(['mall_id' => $this->mall_id, 'status' => 1]);
  45. $model->select('*, id_card as id_card_no, phone as mobile');
  46. if (!empty($params['keyword'])) {
  47. $ids = User::getIdsByKeywordAndMallId($this->mall_id, $params['keyword']);
  48. $model->andWhere(['in', 'user_id', $ids]);
  49. }
  50. });
  51. }
  52. }
  53. /**
  54. * 获取当前用户的认证列表
  55. *
  56. * @param integer $user_id
  57. * @return array
  58. */
  59. public function getUserAuthList(int $user_id)
  60. {
  61. if ($this->getConfigService()->isXLApiType()) {
  62. // 获取前先验证
  63. $list = TaxUserAuth::getUserNotAuthListByOneDay($this->mall_id, $user_id);
  64. foreach ($list as $v) {
  65. $this->getXlService()->signQuery($v);
  66. }
  67. return TaxUserAuth::getUserAuthList($this->mall_id, $user_id);
  68. }
  69. if ($this->getConfigService()->isLYApiType()) {
  70. return LySilentSign::getUserAuthList($this->mall_id, $user_id);
  71. }
  72. }
  73. /**
  74. * 设置默认
  75. *
  76. * @param integer $id
  77. * @return boolean
  78. */
  79. public function setAuthDefault(int $id)
  80. {
  81. $db = Yii::$app->db->beginTransaction();
  82. try {
  83. if ($this->getConfigService()->isXLApiType()) {
  84. /**
  85. * @var TaxUserAuth
  86. */
  87. $static = TaxUserAuth::class;
  88. }
  89. if ($this->getConfigService()->isLYApiType()) {
  90. /**
  91. * @var LySilentSign
  92. */
  93. $static = LySilentSign::class;
  94. }
  95. $auth = $static::getAuthById($this->mall_id, $id);
  96. if (empty($auth)) return $this->error('记录不存在');
  97. if ($auth->default == 1) return true;
  98. // 设置0
  99. $static::setNotDefault($this->mall_id, $auth->user_id);
  100. // 设置默认
  101. $auth->setDefault();
  102. $db->commit();
  103. return true;
  104. } catch (\Exception $e) {
  105. $db->callback();
  106. return $this->error($e->getMessage());
  107. }
  108. }
  109. /**
  110. * 修改支付宝账号
  111. *
  112. * @param integer $id
  113. * @param string $mobile
  114. * @param string|null $mobile
  115. * @return boolean
  116. */
  117. public function changeMobile(int $id, string $mobile, $account)
  118. {
  119. if ($this->getConfigService()->isXLApiType()) {
  120. $auth = TaxUserAuth::getUserByIdAndMallId($this->mall_id, $id);
  121. if (empty($auth)) {
  122. return $this->error('用户账号不存在');
  123. }
  124. }
  125. if ($this->getConfigService()->isLYApiType()) {
  126. $auth = LySilentSign::getUserByIdAndMallId($this->mall_id, $id);
  127. if (empty($auth)) {
  128. return $this->error('用户账号不存在');
  129. }
  130. }
  131. return $auth->changeMobile($mobile, $account);
  132. }
  133. /**
  134. * 转换
  135. *
  136. * @return boolean
  137. */
  138. public function convert()
  139. {
  140. $users = TaxUserAuth::getAllAuthUsers($this->mall_id);
  141. foreach ($users as $user) {
  142. $data = [
  143. 'mall_id' => $this->mall_id,
  144. 'sign_user' => [
  145. 'user_id' => $user->user_id,
  146. 'name' => $user->name,
  147. 'id_card' => $user->id_card_no,
  148. 'mobile' => $user->mobile,
  149. ],
  150. ];
  151. // 添加异步导入任务
  152. Yii::$app->services->rabbitMq->push(
  153. RabbitMqEnum::EXCHANGE_TASK,
  154. RabbitMqEnum::TASK_QUEUE,
  155. $data,
  156. static::class,
  157. 'asyncImportSignUser'
  158. );
  159. }
  160. }
  161. /**
  162. * 异步导入
  163. *
  164. * @param array $data
  165. * [
  166. * 'mall_id' => int,
  167. * 'user_id' => int,
  168. * 'sign_user' => array
  169. * ]
  170. * @return void
  171. */
  172. public static function asyncImportSignUser(array $data)
  173. {
  174. $mall_id = $data['mall_id'];
  175. $sign_user = $data['sign_user'];
  176. $service = new UserService(['mall_id' => $mall_id]);
  177. // 查询用户是否签约
  178. $is_sgin = LySilentSign::userIsSignByNMI(
  179. $sign_user['user_id'],
  180. $sign_user['name'],
  181. $sign_user['mobile'],
  182. $sign_user['id_card']
  183. );
  184. if (!$is_sgin) {
  185. $service->silentSign(
  186. $sign_user['user_id'],
  187. $sign_user['name'],
  188. $sign_user['mobile'],
  189. $sign_user['id_card']
  190. );
  191. }
  192. }
  193. /**
  194. * 解除绑定
  195. *
  196. * @param integer $id
  197. * @return boolean
  198. */
  199. public function unbind(int $id)
  200. {
  201. if ($this->getConfigService()->isXLApiType()) {
  202. $auth = TaxUserAuth::getOkAuthById($this->mall_id, $id);
  203. if (empty($auth)) {
  204. return $this->error('记录不存在');
  205. }
  206. return $auth->unbind() ? true : $this->error('解绑失败');
  207. }
  208. if ($this->getConfigService()->isLYApiType()) {
  209. $auth = LySilentSign::getOkAuthById($this->mall_id, $id);
  210. if (empty($auth)) {
  211. return $this->error('记录不存在');
  212. }
  213. return $auth->setLift() ? true : $this->error('解绑失败');
  214. }
  215. }
  216. /**
  217. * 解除绑定
  218. *
  219. * @param integer $user_id
  220. * @param integer $id
  221. * @return boolean
  222. */
  223. public function lift(int $user_id, int $id)
  224. {
  225. if ($this->getConfigService()->isXLApiType()) {
  226. return $this->error('该功能暂未开放');
  227. }
  228. if ($this->getConfigService()->isLYApiType()) {
  229. $auth = LySilentSign::getSignById($user_id, $id);
  230. if (empty($auth)) {
  231. return $this->error('记录不存在');
  232. }
  233. if ($auth->default) return $this->error('默认签约不能解绑');
  234. return $auth->lift() ? true : $this->error('解绑失败');
  235. }
  236. }
  237. }