UserService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\models\LySilentSign;
  4. use addons\AvoidTax\common\models\TaxUserAuth;
  5. use Yii;
  6. /**
  7. * 签约相关
  8. * @package addons\AvoidTax\common\service
  9. */
  10. class UserService extends BaseService
  11. {
  12. /**
  13. * 是否签约
  14. *
  15. * @param integer $user_id
  16. * @return array|boolean
  17. */
  18. public function checkSign($user_id)
  19. {
  20. if ($this->getConfigService()->isXLApiType()) {
  21. $auth_user = TaxUserAuth::getAccountUser($this->mall_id, $user_id);
  22. if (empty($auth_user)) {
  23. return false;
  24. }
  25. if ($auth_user['status'] == TaxUserAuth::STATUS_WAIT) {
  26. // 签约中,查询签约结果
  27. $service = new XinlongService(['mall_id' => $this->mall_id]);
  28. $res = $service->signQuery($auth_user);
  29. if ($res == TaxUserAuth::STATUS_OK) {
  30. // 已签约成功
  31. return [
  32. 'name' => $auth_user['name'],
  33. 'id_card' => $auth_user['id_card_no'],
  34. 'mobile' => $auth_user['mobile']
  35. ];
  36. }
  37. }
  38. if ($auth_user['status'] == TaxUserAuth::STATUS_OK) {
  39. // 签约成功
  40. return [
  41. 'name' => $auth_user['name'],
  42. 'id_card' => $auth_user['id_card_no'],
  43. 'mobile' => $auth_user['mobile']
  44. ];
  45. }
  46. return false;
  47. }
  48. if ($this->getConfigService()->isLYApiType()) {
  49. $auth_user = LySilentSign::getSignUser($user_id);
  50. if (empty($auth_user)) {
  51. return false;
  52. }
  53. return [
  54. 'name' => $auth_user->name,
  55. 'id_card' => $auth_user->id_card,
  56. 'mobile' => $auth_user->phone
  57. ];
  58. }
  59. }
  60. /**
  61. * 获取默认详情
  62. *
  63. * @param integer $user_id
  64. * @return array|false
  65. */
  66. public function getDefaultSignInfo(int $user_id)
  67. {
  68. if ($this->getConfigService()->isXLApiType()) {
  69. /**
  70. * @var TaxUserAuth
  71. */
  72. $static = TaxUserAuth::class;
  73. }
  74. if ($this->getConfigService()->isLYApiType()) {
  75. /**
  76. * @var LySilentSign
  77. */
  78. $static = LySilentSign::class;
  79. }
  80. $auth = $static::getDefaultSignInfo($this->mall_id, $user_id);
  81. if (empty($auth)) {
  82. return $this->error('还没有默认提现信息');
  83. }
  84. // 如果设置了支付宝账号则替换
  85. $auth['account'] && $auth['mobile'] = $auth['account'];
  86. return $auth;
  87. }
  88. /**
  89. * 去签约
  90. *
  91. * @FormData(key="参数名|中文名", rule="")
  92. * @FormData(key="参数名|中文名", rule="")
  93. * @ApiResponse(code="500", template="error")
  94. * @ApiResponse(code="200", template="success")
  95. * @param $user_id
  96. * @param $mobile
  97. * @param $id_card
  98. * @param $name
  99. *
  100. * @return bool|array true 已经签约 false 有错误信息 array {签约的url, }
  101. */
  102. public function goToSing($user_id, $mobile, $id_card, $name, $redirect_url = null)
  103. {
  104. // 签约前校验
  105. // if (!$this->beforeSignCheckAccount($mobile, $id_card, $name)) {
  106. // return $this->error('实名信息不一致');
  107. // }
  108. try {
  109. $service = new XinlongService(['mall_id' => $this->mall_id]);
  110. // 开账户
  111. $auth_user_model = $service->createAccount($user_id, $mobile, $id_card, $name);
  112. if ($auth_user_model == false) {
  113. return $this->error($service->error);
  114. }
  115. // 生成签约链接
  116. $url = $service->goToSign($auth_user_model, $redirect_url);
  117. return $url === false
  118. ? $this->error($service->error)
  119. : [
  120. 'url' => $url,
  121. 'accountId' => $auth_user_model->accountId,
  122. 'flowId' => $auth_user_model->flowId,
  123. ];
  124. } catch (\Exception$e) {
  125. return $this->error($e->getMessage());
  126. }
  127. }
  128. /**
  129. * 身份信息是否签约, 是否当前签约, 如果不是生成一条已签约记录
  130. *
  131. * @param string $idcard
  132. * @param string $name
  133. * @param integer $user_id
  134. * @return boolean
  135. */
  136. public function idIsSignAndAddAuthOfUid(string $idcard, string $name, int $user_id, string $mobile = '')
  137. {
  138. // xl
  139. if ($this->getConfigService()->isXLApiType()) {
  140. $auth = TaxUserAuth::getAutnByIdcardAndName($this->mall_id, $idcard, $name);
  141. if (empty($auth)) {
  142. return false;
  143. }
  144. // 当前用户已经签约
  145. if (TaxUserAuth::uidIsSign($this->mall_id, $user_id)) {
  146. return true;
  147. }
  148. // 生成一条新的记录
  149. if ($auth->createNewAuth($user_id, $mobile) === false) {
  150. throw new \Exception('生成签约记录失败');
  151. }
  152. return true;
  153. }
  154. // ly
  155. if ($this->getConfigService()->isLYApiType()) {
  156. $auth = LySilentSign::getAutnByIdcardAndName($this->mall_id, $idcard, $name);
  157. if (empty($auth)) {
  158. return false;
  159. }
  160. // 当前用户已经签约
  161. if (LySilentSign::uidIsSign($this->mall_id, $user_id)) {
  162. return true;
  163. }
  164. // 生成一条新的记录
  165. if ($auth->createNewAuth($user_id, $mobile) === false) {
  166. throw new \Exception('生成签约记录失败');
  167. }
  168. return true;
  169. }
  170. return false;
  171. }
  172. /**
  173. * 签约
  174. *
  175. * @param integer $uid
  176. * @param string $name
  177. * @param string $phone
  178. * @param string $id_card
  179. * @return boolean
  180. */
  181. public function silentSign(int $uid, string $name, string $phone, string $id_card)
  182. {
  183. $service = new LuYongService(['mall_id' => $this->mall_id]);
  184. // 1、查询用户是否签约
  185. if ($service->querySignUserByIdCard($id_card)) {
  186. // 1.1、使用签约返回用户信息
  187. $response = $service->getResponse();
  188. if (empty($user_data = $response['data'])) return $this->error('未查询到用户签约信息');
  189. // 1.2、生成签约记录
  190. if (LySilentSign::addSilentSign(
  191. $this->mall_id,
  192. $uid,
  193. $user_data['name'],
  194. $phone,
  195. $user_data['id_card'],
  196. $this->getConfigService()->getAccountId(),
  197. $response
  198. )) {
  199. return true;
  200. }
  201. return $this->error('生成签约记录失败');
  202. }
  203. // 2、查询用户是否注册
  204. if (!$service->queryRegisterUser($id_card)) {
  205. // 2.1、导入用户信息
  206. if (!$service->importSignUser($uid, $name, $phone, $id_card)) {
  207. return $this->error($service->getError());
  208. }
  209. }
  210. // 3、电签
  211. if ($service->silentSign($uid, $name, $phone, $id_card)) {
  212. return true;
  213. }
  214. return $this->error($service->getError());
  215. }
  216. /**
  217. * 新龙签约
  218. *
  219. * @param integer $uid
  220. * @param string $name
  221. * @param string $mobile
  222. * @param string $id_card
  223. * @return boolean|array
  224. */
  225. public function xlSign(int $uid, string $name, string $mobile, string $id_card, $url = '')
  226. {
  227. $auth = TaxUserAuth::getAutnByIdcardAndName($this->mall_id, $id_card, $name);
  228. if (!empty($auth)) {
  229. if ($auth->createNewAuth($uid, $mobile) === false) {
  230. return $this->error('添加失败');
  231. }
  232. return true;
  233. }
  234. // if (TaxUserAuth::isSignWait($uid, $name, $mobile, $id_card)) {
  235. // return true;
  236. // }
  237. // $service = $this->getXlService();
  238. $res = $this->goToSing(
  239. $uid,
  240. $mobile,
  241. $id_card,
  242. $name,
  243. $url
  244. );
  245. if ($res === false) {
  246. return $this->error($this->getError());
  247. }
  248. return $res;
  249. }
  250. /**
  251. * 签约前校验账号是否正确(需要跳转到支付宝 无法完成)
  252. *
  253. * @param string $mobile
  254. * @param string $id_card
  255. * @param string $name
  256. * @return boolean
  257. */
  258. public function beforeSignCheckAccount($mobile, $id_card, $name)
  259. {
  260. $service = new AlipayService($this->mall_id);
  261. $res = $service->certdocCertverifyPreconsult($mobile, $id_card, $name);
  262. if (!($res && $res->code == '10000')) {
  263. return $this->error('支付宝配置有误');
  264. }
  265. // 验证id
  266. $verify_id = $res->verify_id;
  267. $url = $service->getAppAuthorizeUrl($verify_id);
  268. header('Location:' . $url);
  269. die;
  270. $res = $service->certdocCertverifyConsult($verify_id);
  271. dd($res);
  272. }
  273. }