Userauth.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. //use addons\epay\library\Wechat;
  15. use app\common\library\Wechat;
  16. /**
  17. * 实名认证,真人认证相关
  18. */
  19. class Userauth extends Api
  20. {
  21. protected $noNeedLogin = [];
  22. protected $noNeedRight = '*';
  23. //申请真人认证
  24. public function apply_real_confirm(){
  25. Db::startTrans();
  26. //tag任务赠送金币
  27. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  28. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  29. if($task_rs === false){
  30. Db::rollback();
  31. $this->error('完成任务赠送奖励失败');
  32. }
  33. //完成真人头像 +5金币
  34. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  35. if($task_rs === false){
  36. Db::rollback();
  37. $this->error('完成任务赠送奖励失败');
  38. }
  39. //邀请人拿奖励,男性3元
  40. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  41. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  42. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  43. if($task_rs['status'] === false){
  44. Db::rollback();
  45. $this->error($task_rs['msg']);
  46. }
  47. }
  48. //系统消息
  49. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  50. Db::commit();
  51. $this->success();
  52. }
  53. //申请实名认证
  54. public function apply_idcard_confirm(){
  55. $truename = input('truename','');
  56. $idcard = input('idcard' ,'');
  57. if(empty($truename) || empty($idcard)){
  58. $this->error('实名认证信息必填');
  59. }
  60. if($this->auth->idcard_status == 1){
  61. $this->error('您已经完成实名认证');
  62. }
  63. if($this->auth->idcard_status == 0){
  64. $this->error('您已经提交实名认证,请等待审核');
  65. }
  66. Db::startTrans();
  67. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  68. if(!empty($check)){
  69. if($check['status'] == 0){
  70. Db::rollback();
  71. $this->error('您已经提交实名认证,请等待审核');
  72. }
  73. if($check['status'] == 1){
  74. Db::rollback();
  75. $this->error('您已经完成实名认证');
  76. }
  77. }
  78. $data = [
  79. 'user_id' => $this->auth->id,
  80. 'truename' => $truename,
  81. 'idcard' => $idcard,
  82. 'status' => 0,
  83. 'createtime' => time(),
  84. 'updatetime' => time(),
  85. ];
  86. //更新
  87. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  88. if(!empty($check)){
  89. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  90. }else{
  91. $rs = Db::name('user_idconfirm')->insertGetId($data);
  92. }
  93. if(!$rs || !$update_rs){
  94. Db::rollback();
  95. $this->error('提交失败');
  96. }
  97. Db::commit();
  98. $this->success('提交成功,请等待审核');
  99. }
  100. //申请真人认证
  101. public function realauth() {
  102. if ($this->auth->real_status == 1) {
  103. $this->error('您已经真人认证过了~');
  104. }
  105. if ($this->auth->avatar == config('avatar_boy') || $this->auth->avatar == config('avatar_girl')) {
  106. $this->error('请先上传真人头像~');
  107. }
  108. //获取token
  109. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.config('tencent_yun')['secret_id'].'&secret='.config('tencent_yun')['secret_key'].'&grant_type=client_credential&version=1.0.0';
  110. $token_result = file_get_contents($token_url);
  111. if (!$token_result) {
  112. $this->error('您的网络开小差啦1~');
  113. }
  114. $token_result = json_decode($token_result, true);
  115. if ($token_result['code'] != 0) {
  116. $this->error('您的网络开小差啦2~');
  117. }
  118. $token = $token_result['access_token'];
  119. //获取签名鉴权参数ticket
  120. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.config('tencent_yun')['secret_id'].'&access_token='.$token.'&type=SIGN&version=1.0.0';
  121. $ticket_result = file_get_contents($ticket_url);
  122. if (!$ticket_result) {
  123. $this->error('您的网络开小差啦3~');
  124. }
  125. $ticket_result = json_decode($ticket_result, true);
  126. if ($ticket_result['code'] != 0) {
  127. $this->error('您的网络开小差啦4~');
  128. }
  129. $ticket = $ticket_result['tickets'][0]['value'];
  130. //获取签名
  131. $sign_data = [
  132. 'wbappid' => config('tencent_yun')['secret_id'],
  133. 'userId' => (string)$this->auth->id,
  134. 'version' => '1.0.0',
  135. 'ticket' => $ticket,
  136. 'nonce' => Random::alnum(32)
  137. ];//p($sign_data);
  138. asort($sign_data); //p($sign_data);//排序
  139. $sign_string = join('', $sign_data);//p($sign_string);
  140. $sign = sha1($sign_string);//p($sign);
  141. //上传身份信息
  142. // $orderNo = getMillisecond() . $this->auth->id . mt_rand(1, 1000); //商户请求的唯一标识
  143. $orderNo = createUniqueNo('A',$this->auth->id);
  144. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo=' . $orderNo;
  145. $avatar = one_domain_image($this->auth->avatar);
  146. $avatar = str_replace('https', 'http', $avatar);
  147. $img = file_get_contents($avatar);
  148. $img = str_replace('data:image/jpg;base64', '', $img);
  149. $img = str_replace('\n', '', $img);
  150. $sourcePhotoStr = base64_encode($img);
  151. $data = [
  152. 'webankAppId' => config('tencent_yun')['secret_id'],
  153. 'orderNo' => $orderNo,
  154. 'userId' => (string)$this->auth->id,
  155. 'sourcePhotoStr' => $sourcePhotoStr,
  156. 'sourcePhotoType' => 2,
  157. 'version' => '1.0.0',
  158. 'sign' => $sign,
  159. 'nonce' => $sign_data['nonce']
  160. ];
  161. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  162. if (!$rs) {
  163. $this->error('您的网络开小差啦5~');
  164. }
  165. $rs = json_decode($rs, true);
  166. if (!$rs || $rs['code'] != 0) {
  167. $this->error('您的网络开小差啦6~');
  168. }
  169. $user_auth = [
  170. 'user_id' => $this->auth->id,
  171. 'certify_id' => $rs['result']['faceId'],
  172. 'out_trade_no' => $data['orderNo'],
  173. 'status' => 0,
  174. 'createtime' => time(),
  175. 'updatetime' => time()
  176. ];
  177. //开启事务
  178. Db::startTrans();
  179. //查询是否认证过
  180. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  181. if ($info) {
  182. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  183. } else {
  184. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  185. }
  186. if (!$auth_rs) {
  187. Db::rollback();
  188. $this->error('您的网络开小差啦7~');
  189. }
  190. //修改用户表认证状态
  191. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', 0);
  192. if ($user_rs === false) {
  193. Db::rollback();
  194. $this->error('您的网络开小差啦8~');
  195. }
  196. Db::commit();
  197. $return_data = [
  198. 'face_id' => $user_auth['certify_id'],
  199. 'order_no' => $user_auth['out_trade_no'],
  200. 'user_id' => (string)$this->auth->id,
  201. 'nonce' => $sign_data['nonce'],
  202. 'sign' => $sign
  203. ];
  204. $this->success('success', $return_data);
  205. }
  206. //查询真人认证结果
  207. public function getrealauthresult() {
  208. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  209. if (!$user_auth) {
  210. $this->success('尚未认证');
  211. }
  212. if ($user_auth['status'] == 1) {
  213. $this->success('真人认证通过');
  214. }
  215. if (!$user_auth['certify_id']) {
  216. $this->success('请先进行真人认证');
  217. }
  218. //获取token
  219. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.config('tencent_yun')['secret_id'].'&secret='.config('tencent_yun')['secret_key'].'&grant_type=client_credential&version=1.0.0';
  220. $token_result = file_get_contents($token_url);
  221. if (!$token_result) {
  222. $this->error('您的网络开小差啦1~');
  223. }
  224. $token_result = json_decode($token_result, true);
  225. if ($token_result['code'] != 0) {
  226. $this->error('您的网络开小差啦2~');
  227. }
  228. $token = $token_result['access_token'];
  229. //获取签名鉴权参数ticket
  230. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.config('tencent_yun')['secret_id'].'&access_token='.$token.'&type=SIGN&version=1.0.0';
  231. $ticket_result = file_get_contents($ticket_url);
  232. if (!$ticket_result) {
  233. $this->error('您的网络开小差啦3~');
  234. }
  235. $ticket_result = json_decode($ticket_result, true);
  236. if ($ticket_result['code'] != 0) {
  237. $this->error('您的网络开小差啦4~');
  238. }
  239. $ticket = $ticket_result['tickets'][0]['value'];
  240. //获取签名
  241. $sign_data = [
  242. 'wbappid' => config('tencent_yun')['secret_id'],
  243. 'orderNo' => $user_auth['out_trade_no'],
  244. 'version' => '1.0.0',
  245. 'ticket' => $ticket,
  246. 'nonce' => Random::alnum(32)
  247. ];//p($sign_data);
  248. asort($sign_data); //p($sign_data);//排序
  249. $sign_string = join('', $sign_data);//p($sign_string);
  250. $sign = sha1($sign_string);//p($sign);
  251. //人脸核身结果查询
  252. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  253. $data = [
  254. 'appId' => config('tencent_yun')['secret_id'],
  255. 'version' => '1.0.0',
  256. 'nonce' => $sign_data['nonce'],
  257. 'orderNo' => $user_auth['out_trade_no'],
  258. 'sign' => $sign
  259. ];
  260. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  261. if (!$rs) {
  262. $this->error('您的网络开小差啦5~');
  263. }
  264. $rs = json_decode($rs, true);
  265. if (!$rs || $rs['code'] != 0) {
  266. $this->error($rs['msg']);
  267. }
  268. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  269. $edit_data['status'] = 1;
  270. $msg = '真人认证成功';
  271. } else {
  272. $edit_data['status'] = 2;
  273. $edit_data['certify_id'] = '';
  274. $edit_data['out_trade_no'] = '';
  275. $msg = '真人认证失败';
  276. }
  277. $edit_data['updatetime'] = time();
  278. //开启事务
  279. Db::startTrans();
  280. //修改认证信息
  281. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  282. if (!$result) {
  283. Db::rollback();
  284. $this->error('查询认证结果失败2');
  285. }
  286. //修改用户信息
  287. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', $edit_data['status']);
  288. if (!$rs) {
  289. Db::rollback();
  290. $this->error('查询认证结果失败3');
  291. }
  292. if ($edit_data['status'] == 1) { //通过
  293. //tag任务赠送金币
  294. //真人认证奖励
  295. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  296. if($task_rs === false){
  297. Db::rollback();
  298. $this->error('完成任务赠送奖励失败');
  299. }
  300. //系统消息
  301. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  302. } else {
  303. //系统消息
  304. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  305. }
  306. Db::commit();
  307. $this->success($msg);
  308. }
  309. }