UserController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace addons\AvoidTax\api\controllers;
  3. use addons\AvoidTax\common\models\TaxUserAuth;
  4. use addons\AvoidTax\common\service\AuthService;
  5. use addons\AvoidTax\common\service\UserService;
  6. use addons\AvoidTax\common\service\XinlongService;
  7. use api\controllers\OnAuthController;
  8. use common\enums\ApiStatusEnum;
  9. use common\helpers\ArrayHelper;
  10. use Yii;
  11. class UserController extends OnAuthController
  12. {
  13. public $modelClass = '';
  14. /**
  15. * @var UserService $service
  16. */
  17. public $service;
  18. public function __construct ($id, $module, $config = [])
  19. {
  20. parent::__construct($id, $module, $config);
  21. }
  22. public function beforeAction ($action)
  23. {
  24. if (!parent::beforeAction($action)) {
  25. return false;
  26. }
  27. $this->service = new UserService(['mall_id'=>$this->mall_id]);
  28. return true;
  29. }
  30. /**
  31. * 检查是否签约
  32. * @GetApi(path="actionCheckSign",summary="actionCheckSign")
  33. * @FormData(key="参数名|中文名", rule="")
  34. * @FormData(key="参数名|中文名", rule="")
  35. * @ApiResponse(code="500", template="error")
  36. * @ApiResponse(code="200", template="success")
  37. */
  38. public function actionCheckSign()
  39. {
  40. // 检测
  41. $this->service->checkSign($this->user_id);
  42. $service = new UserService(['mall_id'=>$this->mall_id]);
  43. $data = $service->getDefaultSignInfo($this->user_id);
  44. $data['is_sign'] = !empty($data) ? true : false;
  45. return $this->success_str('ok', !empty($data) ? $data : []);
  46. }
  47. /**
  48. * 去签约
  49. * @GetApi(path="actionGoToSign",summary="actionGoToSign")
  50. * @FormData(key="参数名|中文名", rule="")
  51. * @FormData(key="参数名|中文名", rule="")
  52. * @ApiResponse(code="500", template="error")
  53. * @ApiResponse(code="200", template="success")
  54. */
  55. public function actionGoToSign()
  56. {
  57. $data = $this->request->post();
  58. $res = Yii::$app->services->validate->validate($data, [
  59. [
  60. ['mobile', 'id_card', 'name', 'ali_account'], 'required'],
  61. ],
  62. [
  63. 'mobile' => '手机号码',
  64. 'id_card' => '身份证',
  65. 'name' => '姓名',
  66. 'ali_account'=> '支付宝账号',
  67. ]
  68. );
  69. if( $res === false ) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  70. $url = $this->service->goToSing($this->user_id,...array_values($data));
  71. // 已经签约
  72. if ( $url === true ) return $this->success('已签约,不需要重复签约',['is_sign'=>true,'url'=>'']);
  73. // 有错误
  74. if ( $url === false ) return $this->error('签约失败:'.$this->service->error);
  75. // 跳转url
  76. return $this->success('跳转url进行签约',['is_sign'=>false,'url'=>$url]);
  77. }
  78. /**
  79. * 获取用户签约列表
  80. *
  81. * @return void
  82. */
  83. public function actionAuthList()
  84. {
  85. $service = new AuthService(['mall_id' => $this->mall_id]);
  86. return $this->success_str('ok', $service->getUserAuthList($this->user_id));
  87. }
  88. /**
  89. * 创建签约
  90. *
  91. * @return void
  92. */
  93. public function actionCreateAccount()
  94. {
  95. $params = \Yii::$app->request->post();
  96. $res = \Yii::$app->services->validate->validate($params, [
  97. [['mobile'], 'required', 'message' => '请填写手机号码'],
  98. [['id_card'], 'required', 'message' => '请填写身份证号码'],
  99. [['name'], 'required', 'message' => '请填写姓名'],
  100. ['mobile', 'match', 'pattern' => '/^1\d{10}/', 'message' => '请输入正确的手机号码'],
  101. ]);
  102. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  103. try {
  104. $service = new UserService(['mall_id' => $this->mall_id]);
  105. // 新龙
  106. if ($service->getConfigService()->isXLApiType()) {
  107. $res = $service->xlSign(
  108. $this->user_id, $params['name'], $params['mobile'], $params['id_card']
  109. );
  110. if ($res === true) {
  111. return $this->success('签约成功');
  112. }
  113. if ($res === false) {
  114. return $this->error($service->getError());
  115. }
  116. return $this->success('ok', $res);
  117. }
  118. // 鹿用
  119. if ($service->getConfigService()->isLYApiType()) {
  120. $res = $service->silentSign($this->user_id, $params['name'], $params['mobile'], $params['id_card']);
  121. if ($res === false) {
  122. return $this->error($service->getError());
  123. }
  124. return $this->success('签约成功');
  125. }
  126. } catch (\Exception $e) {
  127. return $this->error('申请失败:' . $e->getMessage());
  128. }
  129. }
  130. /**
  131. * 设置默认
  132. *
  133. * @return void
  134. **/
  135. public function actionSetDefault()
  136. {
  137. $id = $this->request->get('id');
  138. $service = new AuthService(['mall_id' => $this->mall_id]);
  139. return $service->setAuthDefault($id)
  140. ? $this->success('ok')
  141. : $this->error($service->getError());
  142. }
  143. /**
  144. * 解除签约
  145. *
  146. * @param integer $id
  147. * @return string|array
  148. */
  149. public function actionLift(int $id)
  150. {
  151. $service = new AuthService(['mall_id' => $this->mall_id]);
  152. return $service->lift($this->user_id, $id)
  153. ? $this->success('ok')
  154. : $this->error($service->getError());
  155. }
  156. /**
  157. * 请求成功统消息格式化处理
  158. * @Author bing
  159. * @DateTime 2020-10-27 15:43:16
  160. * @copyright: Copyright (c) 2020 广东七件事集团
  161. * @param string $msg
  162. * @param array $data
  163. * @param [type] $code
  164. * @return void|mixed
  165. */
  166. private function success_str($msg = '操作成功', $data = [], $code = ApiStatusEnum::CODE_SUCCESS)
  167. {
  168. if (is_object($data)) $data = ArrayHelper::toArray($data);
  169. Yii::$app->controller->asJson(json_decode(json_encode(array(
  170. 'code' => $code,
  171. 'msg' => $msg,
  172. 'data' => $data
  173. )), true))->send();
  174. }
  175. }