| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\enums\StatusEnum;
- use addons\AvoidTax\common\jdk\socialwork\SocialApi;
- use addons\AvoidTax\common\models\TaxOrder;
- use addons\AvoidTax\common\models\TaxUserAuth;
- use common\models\user\UserWithdrawLog;
- use phpDocumentor\Reflection\Types\This;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- /**
- * 灵工免税-猎聘版
- * @package addons\AvoidTax\common\service
- */
- class SocialService extends BaseService
- {
- /**
- * 是否实名
- * @param int $user_id 用户ID
- * @return bool true 没有授权 false 已经授权
- */
- public function checkAuth(int $user_id):bool
- {
- $user = TaxUserAuth::findAuthUser($this->mall_id,$user_id,TaxUserAuth::STATUS_OK);
- return empty($user) ? true : false;
- }
- /**
- * 生成实名链接,成功会返回实名认证url,跳转进行实名
- * @param int $user_id 用户ID
- * @param int $name 姓名
- * @param int $mobile 手机号码
- * @return bool|string 授权url
- */
- public function goToAuth(int $user_id,string $name,string $mobile)
- {
- $api = new SocialApi();
- // 回调地址
- $url = \Yii::$app->services->setting->platform->get('system.api_url')."/avoid-tax/notify/user-auth";
- $param = [
- 'mall_user_id' => $this->mall_id .'_'.$user_id,
- ];
- $callback = $url .'?'. http_build_query($param);
- // 调用api接口
- list($res,$auth_url) = $api->userAuth($name,$mobile,$callback);
- if($res){
- // 添加授权用户
- TaxUserAuth::addAuthUser($this->mall_id,$user_id,$auth_url);
- return $auth_url;
- }
- \Yii::debug("avoidtax goToAuth err ".$auth_url);
- return $this->error($auth_url);
- }
- /**
- * 实名认证异步回调
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- */
- public function authNotify(array $data)
- {
- // {"name":"杨金成","id_card_no":"450121199206190092","id_card_front":"","id_card_back":"","mobile":"13923698558","user_id":"751937135803633664"}
- $malluserid = $data['mall_user_id'] ?? '';
- $name = $data['name'] ?? ''; // 姓名
- $id_card_no = $data['id_card_no'] ?? ''; // 身份证
- $mobile = $data['mobile'] ?? ''; // 手机号码
- $workuser_id= $data['user_id'] ?? ''; //
- list($mall_id,$user_id) = explode('_',$malluserid);
- if(empty($user_id) || empty($name) || empty($id_card_no)) {
- \Yii::debug("avoidtax authNotify 参数错误 ");
- \Yii::debug(Json::encode($data));
- return $this->error(" 参数错误 ");
- }
- $user = TaxUserAuth::find()->where(['user_id'=>$user_id,'mall_id'=>$mall_id])->one();
- if(empty($user)) {
- \Yii::debug("avoidtax authNotify err " . "user_id 记录不存 ");
- \Yii::debug(Json::encode($data));
- $this->error(" user_id 记录不存 ");
- }
- return TaxUserAuth::authOk($user->id,$name,$id_card_no,$mobile,$workuser_id);
- }
- /**
- * 创建发放订单
- * 只支持单笔
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- * @param array $data
- *
- * @return array|bool
- */
- public function createOrder($tax_batch_num)
- {
- $api = new SocialApi();
- $orders = TaxOrder::find()->with('userAuth')->where(['batch_num'=>$tax_batch_num])->all();
- // 创建订单
- list ( $res,$job_id ) = $api->createOrder($tax_batch_num);
- if ( !$res ) return $this->error($job_id);
- // var_dump($job_id);
- // $job_id = "753709199636041728";
- // 添加批次
- $batch_user = [];
- foreach ( $orders as $item ) {
- $one['job_name'] = "销售佣金"; // 任务名称
- $one['job_no'] = $item['batch_num']; // 任务名称
- $one['total_salary'] = $item['amount'];// 总金额
- $one['salary'] = $item['amount'];// 金额
- // 实名信息
- $one['user_name'] = $item->userAuth->name;
- $one['user_mobile'] = $item->userAuth->mobile;
- $one['id_card_no'] = $item->userAuth->id_card_no;
- // 支付宝信息
- // $one['bank_name'] = "支付宝";
- // $one['bank_card'] = $item['account_num'];
- $one['bank_name'] = "工商银行";
- $one['bank_card'] = "6212263602070285796";
- $batch_user[] = $one;
- }
- // 往批次号添加明细
- // echo json_encode($batch_user);
- $resp = $api->createOrderDetail($job_id,$batch_user);
- // echo json_encode($resp);
- // 失败情况
- // $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}';
- // // 成功请求
- // $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}';
- // $resp = Json::decode($resp);
- foreach ($resp['items'] as $item) {
- if($item['status'] != SocialApi::ORDER_SUCCESS){
- // 失败
- TaxOrder::changStatus($tax_batch_num,[
- 'status' => StatusEnum::STATUS_FAIL,
- 'error' => $item['err_msg'],
- ]);
- return $this->error($item['err_msg']);
- }
- // 成功
- TaxOrder::changStatus($tax_batch_num,[
- 'status' => StatusEnum::STATUS_IS_SUBMIT,
- 'job_id' => $job_id,
- 'job_detail_id' => $item['id'],
- ]);
- }
- return true;
- }
- /**
- * 订单添加详情
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- */
- public function addOrderDetail(int $order_id,array $detail_date = []):array
- {
- return [];
- }
- }
|