SocialService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\enums\StatusEnum;
  4. use addons\AvoidTax\common\jdk\socialwork\SocialApi;
  5. use addons\AvoidTax\common\models\TaxOrder;
  6. use addons\AvoidTax\common\models\TaxUserAuth;
  7. use common\models\user\UserWithdrawLog;
  8. use phpDocumentor\Reflection\Types\This;
  9. use yii\base\BaseObject;
  10. use yii\helpers\Json;
  11. /**
  12. * 灵工免税-猎聘版
  13. * @package addons\AvoidTax\common\service
  14. */
  15. class SocialService extends BaseService
  16. {
  17. /**
  18. * 是否实名
  19. * @param int $user_id 用户ID
  20. * @return bool true 没有授权 false 已经授权
  21. */
  22. public function checkAuth(int $user_id):bool
  23. {
  24. $user = TaxUserAuth::findAuthUser($this->mall_id,$user_id,TaxUserAuth::STATUS_OK);
  25. return empty($user) ? true : false;
  26. }
  27. /**
  28. * 生成实名链接,成功会返回实名认证url,跳转进行实名
  29. * @param int $user_id 用户ID
  30. * @param int $name 姓名
  31. * @param int $mobile 手机号码
  32. * @return bool|string 授权url
  33. */
  34. public function goToAuth(int $user_id,string $name,string $mobile)
  35. {
  36. $api = new SocialApi();
  37. // 回调地址
  38. $url = \Yii::$app->services->setting->platform->get('system.api_url')."/avoid-tax/notify/user-auth";
  39. $param = [
  40. 'mall_user_id' => $this->mall_id .'_'.$user_id,
  41. ];
  42. $callback = $url .'?'. http_build_query($param);
  43. // 调用api接口
  44. list($res,$auth_url) = $api->userAuth($name,$mobile,$callback);
  45. if($res){
  46. // 添加授权用户
  47. TaxUserAuth::addAuthUser($this->mall_id,$user_id,$auth_url);
  48. return $auth_url;
  49. }
  50. \Yii::debug("avoidtax goToAuth err ".$auth_url);
  51. return $this->error($auth_url);
  52. }
  53. /**
  54. * 实名认证异步回调
  55. * @ApiResponse(code="500", template="error")
  56. * @ApiResponse(code="200", template="success")
  57. */
  58. public function authNotify(array $data)
  59. {
  60. // {"name":"杨金成","id_card_no":"450121199206190092","id_card_front":"","id_card_back":"","mobile":"13923698558","user_id":"751937135803633664"}
  61. $malluserid = $data['mall_user_id'] ?? '';
  62. $name = $data['name'] ?? ''; // 姓名
  63. $id_card_no = $data['id_card_no'] ?? ''; // 身份证
  64. $mobile = $data['mobile'] ?? ''; // 手机号码
  65. $workuser_id= $data['user_id'] ?? ''; //
  66. list($mall_id,$user_id) = explode('_',$malluserid);
  67. if(empty($user_id) || empty($name) || empty($id_card_no)) {
  68. \Yii::debug("avoidtax authNotify 参数错误 ");
  69. \Yii::debug(Json::encode($data));
  70. return $this->error(" 参数错误 ");
  71. }
  72. $user = TaxUserAuth::find()->where(['user_id'=>$user_id,'mall_id'=>$mall_id])->one();
  73. if(empty($user)) {
  74. \Yii::debug("avoidtax authNotify err " . "user_id 记录不存 ");
  75. \Yii::debug(Json::encode($data));
  76. $this->error(" user_id 记录不存 ");
  77. }
  78. return TaxUserAuth::authOk($user->id,$name,$id_card_no,$mobile,$workuser_id);
  79. }
  80. /**
  81. * 创建发放订单
  82. * 只支持单笔
  83. * @ApiResponse(code="500", template="error")
  84. * @ApiResponse(code="200", template="success")
  85. * @param array $data
  86. *
  87. * @return array|bool
  88. */
  89. public function createOrder($tax_batch_num)
  90. {
  91. $api = new SocialApi();
  92. $orders = TaxOrder::find()->with('userAuth')->where(['batch_num'=>$tax_batch_num])->all();
  93. // 创建订单
  94. list ( $res,$job_id ) = $api->createOrder($tax_batch_num);
  95. if ( !$res ) return $this->error($job_id);
  96. // var_dump($job_id);
  97. // $job_id = "753709199636041728";
  98. // 添加批次
  99. $batch_user = [];
  100. foreach ( $orders as $item ) {
  101. $one['job_name'] = "销售佣金"; // 任务名称
  102. $one['job_no'] = $item['batch_num']; // 任务名称
  103. $one['total_salary'] = $item['amount'];// 总金额
  104. $one['salary'] = $item['amount'];// 金额
  105. // 实名信息
  106. $one['user_name'] = $item->userAuth->name;
  107. $one['user_mobile'] = $item->userAuth->mobile;
  108. $one['id_card_no'] = $item->userAuth->id_card_no;
  109. // 支付宝信息
  110. // $one['bank_name'] = "支付宝";
  111. // $one['bank_card'] = $item['account_num'];
  112. $one['bank_name'] = "工商银行";
  113. $one['bank_card'] = "6212263602070285796";
  114. $batch_user[] = $one;
  115. }
  116. // 往批次号添加明细
  117. // echo json_encode($batch_user);
  118. $resp = $api->createOrderDetail($job_id,$batch_user);
  119. // echo json_encode($resp);
  120. // 失败情况
  121. // $resp = '{"items":[{"user_name":"","user_mobile":"","id_card_no":"","bank_card":"","total_salary":"","status":"f","id":"","err_msg":"totalSalary(任务金额)格式不正确","extra":"","real_total_salary":""}],"err_msg":null}';
  122. // // 成功请求
  123. // $resp = '{"items":[{"user_name":"123","user_mobile":"15820232740","id_card_no":"450121199206190092","bank_card":"15820232749","total_salary":"1.00","status":"s","id":"753709201569615872","err_msg":"","extra":"","real_total_salary":""}],"err_msg":null}';
  124. // $resp = Json::decode($resp);
  125. foreach ($resp['items'] as $item) {
  126. if($item['status'] != SocialApi::ORDER_SUCCESS){
  127. // 失败
  128. TaxOrder::changStatus($tax_batch_num,[
  129. 'status' => StatusEnum::STATUS_FAIL,
  130. 'error' => $item['err_msg'],
  131. ]);
  132. return $this->error($item['err_msg']);
  133. }
  134. // 成功
  135. TaxOrder::changStatus($tax_batch_num,[
  136. 'status' => StatusEnum::STATUS_IS_SUBMIT,
  137. 'job_id' => $job_id,
  138. 'job_detail_id' => $item['id'],
  139. ]);
  140. }
  141. return true;
  142. }
  143. /**
  144. * 订单添加详情
  145. * @ApiResponse(code="500", template="error")
  146. * @ApiResponse(code="200", template="success")
  147. */
  148. public function addOrderDetail(int $order_id,array $detail_date = []):array
  149. {
  150. return [];
  151. }
  152. }