User.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 app\common\library\Wechat;
  15. use TencentCloud\Common\Credential;
  16. use TencentCloud\Common\Profile\ClientProfile;
  17. use TencentCloud\Common\Profile\HttpProfile;
  18. use TencentCloud\Common\Exception\TencentCloudSDKException;
  19. use TencentCloud\Faceid\V20180301\FaceidClient;
  20. use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
  21. use TencentCloud\Iai\V20200303\IaiClient;
  22. use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;
  23. /**
  24. * 会员接口,登录,注册,修改资料等
  25. */
  26. class User extends Api
  27. {
  28. protected $noNeedLogin = ['emaillogin','emailregister','mobilelogin','resetpwd'];
  29. protected $noNeedRight = '*';
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. }
  34. /**
  35. * 邮箱登录
  36. *
  37. * @ApiMethod (POST)
  38. * @param string $account 账号
  39. * @param string $password 密码
  40. */
  41. public function emaillogin()
  42. {
  43. $account = input('account');
  44. $password = input('password');
  45. if (!$account || !$password) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. if (!Validate::is($account, 'email')) {
  49. $this->error(__('Email is incorrect'));
  50. }
  51. $ret = $this->auth->login($account, $password);
  52. if ($ret) {
  53. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. //邮箱注册
  59. public function emailregister()
  60. {
  61. $account = input('account');
  62. $captcha = input('captcha');
  63. $password = input('password');
  64. if (!$account || !$captcha || !$password) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::is($account, 'email')) {
  68. $this->error(__('Email is incorrect'));
  69. }
  70. $ret = Ems::check($account, $captcha, 'register');
  71. if (!$ret) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $extend = [
  75. 'register_from' => input('register_from',''),
  76. 'gender' => -1
  77. ];
  78. $ret = $this->auth->register('',$password,$account,'', $extend);
  79. if ($ret) {
  80. Ems::flush($account);
  81. $this->success('注册成功', $this->auth->getUserinfo_simple());
  82. } else {
  83. $this->error($this->auth->getError());
  84. }
  85. }
  86. /**
  87. * 手机验证码登录 + 注册
  88. *
  89. * @ApiMethod (POST)
  90. * @param string $mobile 手机号
  91. * @param string $captcha 验证码
  92. */
  93. public function mobilelogin()
  94. {
  95. $mobile = input('mobile');
  96. $captcha = input('captcha');
  97. if (!$mobile || !$captcha) {
  98. $this->error(__('Invalid parameters'));
  99. }
  100. if (!Validate::regex($mobile, "^1\d{10}$")) {
  101. $this->error(__('Mobile is incorrect'));
  102. }
  103. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  104. $this->error(__('Captcha is incorrect'));
  105. }
  106. $user = \app\common\model\User::getByMobile($mobile);
  107. if ($user) {
  108. if ($user->status == -1) {
  109. $this->error('账户已注销');
  110. }
  111. if (!in_array($user->status,[1,2])) {
  112. $this->error(__('Account is locked'));
  113. }
  114. if ($user->frozentime > time()) {
  115. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  116. }
  117. //如果已经有账号则直接登录
  118. $ret = $this->auth->direct($user->id);
  119. } else {
  120. $extend = [
  121. 'register_from' => input('register_from',''),
  122. 'gender' => -1
  123. ];
  124. $ret = $this->auth->register('', '', '', $mobile, $extend);
  125. }
  126. if ($ret) {
  127. Sms::flush($mobile, 'mobilelogin');
  128. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  129. } else {
  130. $this->error($this->auth->getError());
  131. }
  132. }
  133. //注册设置性别
  134. public function setgender() {
  135. $user_id = $this->auth->id;
  136. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  137. if (!in_array($gender, [1, 0])) {
  138. $this->error('性别错误');
  139. }
  140. $edit_data['gender'] = $gender;
  141. $edit_data['avatar'] = $gender == 1 ? config('avatar_boy') : config('avatar_girl'); //头像
  142. $rs = Db::name('user')->where(['id' => $user_id])->update($edit_data);
  143. if ($rs === false) {
  144. $this->error('您的网络开小差啦~');
  145. }
  146. //$data = $this->userInfo('return');
  147. $data['gender'] = $edit_data['gender'];
  148. $data['avatar'] = $edit_data['avatar'];
  149. $this->success('success', $data);
  150. }
  151. //刷新随机昵称
  152. public function get_rand_nick_name(){
  153. $nickname = $this->auth->get_rand_nick_name();
  154. $this->success('success', $nickname);
  155. }
  156. //注册完善资料
  157. public function perfect_info() {
  158. $avatar = input('avatar', '', 'trim'); //头像
  159. $nickname = input('nickname', '', 'trim'); //昵称
  160. $birthday = input('birthday', '', 'strtotime'); //生日
  161. $hometown_cityid = input('hometown_cityid', '', 'trim'); //城市id
  162. $bio = input('bio', '', 'trim'); //个性签名
  163. $hobby = input('hobby', '', 'trim'); //爱好
  164. $marital = input('marital', '', 'trim'); //婚姻
  165. $introcode = input('introcode', '', 'trim'); //邀请码
  166. $data = [];
  167. if ($avatar) {
  168. $data['avatar'] = $avatar;
  169. }
  170. if ($nickname !== '') {
  171. if (iconv_strlen($nickname, 'utf-8') > 10) {
  172. $this->error('昵称最多10个字~');
  173. }
  174. $data['nickname'] = $nickname;
  175. }
  176. if ($birthday) {
  177. $data['birthday'] = $birthday;
  178. }
  179. if ($hometown_cityid) {
  180. $count = Db::name('area')->where('id', $hometown_cityid)->count('id');
  181. if (!$count) {
  182. $this->error('城市不存在');
  183. }
  184. $data['hometown_cityid'] = $hometown_cityid;
  185. }
  186. if ($bio) {
  187. $data['bio'] = $bio;
  188. }
  189. if ($hobby) {
  190. $data['hobby'] = $hobby;
  191. }
  192. if ($marital) {
  193. $data['marital'] = $marital;
  194. }
  195. if ($introcode && !$this->auth->intro_uid) {
  196. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $introcode)->find();
  197. if ($intro_user && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id) {
  198. $data['intro_uid'] = $intro_user['id'];
  199. $data['invite_time'] = time();
  200. }
  201. }
  202. //开启事务
  203. Db::startTrans();
  204. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  205. if($update_rs === false){
  206. Db::rollback();
  207. $this->error('修改失败');
  208. }
  209. //给上级发放钻石
  210. if(isset($data['intro_uid'])){
  211. $intro_gold = config('site.new_user_intro_gold');
  212. if($intro_gold > 0){
  213. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'gold',$intro_gold,34,'邀请'.$this->auth->username.'注册奖励');
  214. if($rs_wallet['status'] === false){
  215. Db::rollback();
  216. $this->error('邀请新人奖励赠送失败');
  217. }
  218. }
  219. }
  220. //上传头像加5金币
  221. if(isset($data['avatar'])){
  222. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  223. if($task_rs === false){
  224. Db::rollback();
  225. $this->error('完成任务赠送奖励失败');
  226. }
  227. }
  228. if (isset($data['birthday'])) {
  229. //完成设置生日 +5金币
  230. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  231. if($task_rs === false){
  232. Db::rollback();
  233. $this->error('完成任务赠送奖励失败');
  234. }
  235. }
  236. Db::commit();
  237. $this->success('修改成功');
  238. }
  239. //用户详细资料
  240. public function userInfo($type = 1){
  241. $info = $this->auth->getUserinfo();
  242. if($type == 'return'){
  243. return $info;
  244. }
  245. $this->success(__('success'),$info);
  246. }
  247. /**
  248. * 修改会员个人信息
  249. *
  250. * @ApiMethod (POST)
  251. * @param string $avatar 头像地址
  252. * @param string $username 用户名
  253. * @param string $nickname 昵称
  254. * @param string $bio 个人简介
  255. */
  256. public function profile()
  257. {
  258. $field_array = ['nickname','introcode',/*'gender',*/'birthday','height','weight','bio','audio_bio','avatar','photo_images','education','hobby','job','marital','tag','wages','hometown_cityid','hide_is_finishinfo',/*'wechat_account',*/'character','constellation','stature','is_appointment', 'greet_voice', 'greet_chat', 'is_cohabit', 'live', 'is_house', 'car', 'chest', 'waist'];
  259. $data = [];
  260. foreach($field_array as $key => $field){
  261. if(!input('?'.$field)){
  262. continue;
  263. }
  264. $newone = input($field);
  265. if($field == 'avatar'){
  266. // if ($this->auth->real_status == 1) {
  267. //$this->error('您已经真人认证不能修改头像~');
  268. // die;
  269. // }
  270. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  271. }
  272. if($field == 'photo_images'){
  273. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  274. }
  275. $data[$field] = $newone;
  276. }
  277. if(isset($data['birthday'])){
  278. $data['birthday'] = strtotime($data['birthday']);
  279. }
  280. if(isset($data['avatar'])){
  281. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  282. }
  283. if(isset($data['introcode'])){
  284. if ($this->auth->intro_uid != 0) {
  285. $this->error('邀请人不可修改~');
  286. }
  287. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $data['introcode'])->find();
  288. if(!$intro_user){
  289. $this->error('不存在的邀请人');
  290. }
  291. if ($intro_user['id'] == $this->auth->id) {
  292. $this->error('不能填写自己邀请码');
  293. }
  294. if ($intro_user['intro_uid'] == $this->auth->id) {
  295. $this->error('不能填写下级邀请码');
  296. }
  297. unset($data['introcode']);//别人的邀请码,不能改了自己的
  298. $data['intro_uid'] = $intro_user['id'];
  299. $data['invite_time'] = time();
  300. }
  301. //dump($data);
  302. if(empty($data)){
  303. $this->error('没有任何改变');
  304. }
  305. Db::startTrans();
  306. $userData = Db::name('user')->field('avatar,gender,status')->where('id',$this->auth->id)->find();
  307. if (isset($data['avatar']) && !empty($data['avatar']) && $userData['status'] == 2) {//隐藏
  308. $boyAvatar = config('avatar_boy');
  309. $girlAvatar = config('avatar_girl');
  310. if ($userData['gender'] == 1 && $data['avatar'] != $boyAvatar) {
  311. $data['status'] = 1;//更新为正常
  312. } elseif ($userData['gender'] == 0 && $data['avatar'] != $girlAvatar) {
  313. $data['status'] = 1;//更新为正常
  314. }
  315. }
  316. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  317. if($update_rs === false){
  318. Db::rollback();
  319. $this->error('修改资料失败');
  320. }
  321. //给上级发放钻石
  322. if(isset($data['intro_uid'])){
  323. $intro_gold = config('site.new_user_intro_gold');
  324. if($intro_gold > 0){
  325. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'gold',$intro_gold,34,'邀请'.$this->auth->username.'注册奖励');
  326. if($rs_wallet['status'] === false){
  327. Db::rollback();
  328. $this->error('邀请新人奖励赠送失败');
  329. }
  330. }
  331. }
  332. //tag任务赠送金币
  333. //上传头像加5金币
  334. if(isset($data['avatar'])){
  335. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  336. if($task_rs === false){
  337. Db::rollback();
  338. $this->error('完成任务赠送奖励失败');
  339. }
  340. }
  341. //上传生日 +5金币
  342. if (isset($data['birthday'])) {
  343. //完成设置生日 +5金币
  344. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  345. if($task_rs === false){
  346. Db::rollback();
  347. $this->error('完成任务赠送奖励失败');
  348. }
  349. }
  350. //上传个性签名 5金币
  351. if(isset($data['bio'])){
  352. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,2);
  353. if($task_rs === false){
  354. Db::rollback();
  355. $this->error('完成任务赠送奖励失败');
  356. }
  357. }
  358. //上传本人语音介绍 10金币
  359. if(isset($data['audio_bio'])){
  360. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,21);
  361. if($task_rs === false){
  362. Db::rollback();
  363. $this->error('完成任务赠送奖励失败');
  364. }
  365. }
  366. //上传本人五张照片 10金币
  367. if(isset($data['photo_images'])){
  368. $photo_images_num = count(explode(',',$data['photo_images'])) + count(explode(',',$this->auth->photo_images));
  369. if ($photo_images_num >= 5) {
  370. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id, 5);
  371. if ($task_rs === false) {
  372. Db::rollback();
  373. $this->error('完成任务赠送奖励失败');
  374. }
  375. }
  376. }
  377. Db::commit();
  378. $this->success();
  379. }
  380. /**
  381. * 退出登录
  382. * @ApiMethod (POST)
  383. */
  384. public function logout()
  385. {
  386. if (!$this->request->isPost()) {
  387. $this->error(__('Invalid parameters'));
  388. }
  389. //退出im
  390. $tenIm = new Tenim();
  391. $tenIm->loginoutim($this->auth->id);
  392. //修改用户活跃0
  393. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  394. $this->auth->logout();
  395. $this->success(__('Logout successful'));
  396. }
  397. /**
  398. * 重置密码
  399. *
  400. * @ApiMethod (POST)
  401. * @param string $mobile 手机号
  402. * @param string $newpassword 新密码
  403. * @param string $captcha 验证码
  404. */
  405. public function resetpwd()
  406. {
  407. //$type = input("type");
  408. $type = 'email';
  409. $mobile = input("mobile");
  410. $email = input("email");
  411. $newpassword = input("newpassword");
  412. $captcha = input("captcha");
  413. if (!$newpassword || !$captcha) {
  414. $this->error(__('Invalid parameters'));
  415. }
  416. if ($type == 'mobile') {
  417. if (!Validate::regex($mobile, "^1\d{10}$")) {
  418. $this->error(__('Mobile is incorrect'));
  419. }
  420. $user = \app\common\model\User::getByMobile($mobile);
  421. if (!$user) {
  422. $this->error(__('User not found'));
  423. }
  424. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  425. if (!$ret) {
  426. $this->error(__('Captcha is incorrect'));
  427. }
  428. Sms::flush($mobile, 'resetpwd');
  429. } else {
  430. if (!Validate::is($email, "email")) {
  431. $this->error(__('Email is incorrect'));
  432. }
  433. $user = \app\common\model\User::getByEmail($email);
  434. if (!$user) {
  435. $this->error(__('User not found'));
  436. }
  437. $ret = Ems::check($email, $captcha, 'resetpwd');
  438. if (!$ret) {
  439. $this->error(__('Captcha is incorrect'));
  440. }
  441. Ems::flush($email, 'resetpwd');
  442. }
  443. //模拟一次登录
  444. $this->auth->direct($user->id);
  445. $ret = $this->auth->changepwd($newpassword, '', true);
  446. if ($ret) {
  447. $this->success(__('Reset password successful'));
  448. } else {
  449. $this->error($this->auth->getError());
  450. }
  451. }
  452. /**
  453. * 微信注册来的,绑定手机号
  454. *
  455. * @ApiMethod (POST)
  456. * @param string $mobile 手机号
  457. * @param string $captcha 验证码
  458. */
  459. public function bindmobile()
  460. {
  461. $user = $this->auth->getUser();
  462. $mobile = $this->request->request('mobile');
  463. $captcha = $this->request->request('captcha');
  464. if(!empty($this->auth->mobile)){
  465. $this->error('已经绑定了手机号');
  466. }
  467. if (!$mobile || !$captcha) {
  468. $this->error(__('Invalid parameters'));
  469. }
  470. if (!Validate::regex($mobile, "^1\d{10}$")) {
  471. $this->error(__('Mobile is incorrect'));
  472. }
  473. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  474. $this->error('该手机号已被其他用户绑定');
  475. }
  476. $result = Sms::check($mobile, $captcha, 'changemobile');
  477. if (!$result) {
  478. $this->error(__('Captcha is incorrect'));
  479. }
  480. $user->mobile = $mobile;
  481. $user->save();
  482. Sms::flush($mobile, 'changemobile');
  483. //手机号奖励
  484. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,18);
  485. if($task_rs === false){
  486. Db::rollback();
  487. return false;
  488. }
  489. $this->success('success',$this->userInfo('return'));
  490. }
  491. //注销配置
  492. public function cancleconfig(){
  493. $rs = [
  494. 'rule' => config('site.user_cancle_rules'),
  495. 'reason' => [
  496. '想换个新账号',
  497. '没有聊得来的',
  498. '不想玩了',
  499. '其他原因',
  500. ],
  501. ];
  502. $this->success(1,$rs);
  503. }
  504. //假注销
  505. public function cancleUser(){
  506. if (!$this->request->isPost()) {
  507. $this->error(__('Invalid parameters'));
  508. }
  509. //退出im
  510. $tenIm = new Tenim();
  511. $tenIm->loginoutim($this->auth->id);
  512. $data = [
  513. 'status' => -1,
  514. 'mobile' => 'close_'.$this->auth->mobile,
  515. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  516. ];
  517. Db::name('user')->where('id',$this->auth->id)->update($data);
  518. $this->auth->logout();
  519. $this->success('注销成功');
  520. }
  521. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  522. /**
  523. * 手机验证码验证
  524. *
  525. * @ApiMethod (POST)
  526. * @param string $mobile 手机号
  527. * @param string $captcha 验证码
  528. */
  529. /*public function mobilecheck()
  530. {
  531. $mobile = input('mobile');
  532. $captcha = input('captcha');
  533. if (!$mobile || !$captcha) {
  534. $this->error(__('Invalid parameters'));
  535. }
  536. if (!Validate::regex($mobile, "^1\d{10}$")) {
  537. $this->error(__('Mobile is incorrect'));
  538. }
  539. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  540. $this->error(__('Captcha is incorrect'));
  541. }
  542. $this->success('验证通过');
  543. }*/
  544. /**
  545. * 手机验证码注册
  546. *
  547. * @ApiMethod (POST)
  548. * @param string $mobile 手机号
  549. * @param string $captcha 验证码
  550. * @param string $gender 性别:1=男,0=女
  551. */
  552. /*public function mobileregister()
  553. {
  554. $mobile = input('mobile');
  555. $captcha = input('captcha');
  556. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  557. if (!$mobile || !$captcha) {
  558. $this->error(__('Invalid parameters'));
  559. }
  560. if (!Validate::regex($mobile, "^1\d{10}$")) {
  561. $this->error(__('Mobile is incorrect'));
  562. }
  563. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  564. $this->error(__('Captcha is incorrect'));
  565. }
  566. if (!in_array($gender, [1, 0])) {
  567. $this->error('性别错误');
  568. }
  569. $user = \app\common\model\User::getByMobile($mobile);
  570. if ($user) {
  571. $this->error('账号已经存在,请直接登录');
  572. if ($user->status == -1) {
  573. $this->error('账户已注销');
  574. }
  575. if (!in_array($user->status,[1,2])) {
  576. $this->error(__('Account is locked'));
  577. }
  578. //如果已经有账号则直接登录
  579. $ret = $this->auth->direct($user->id);
  580. } else {
  581. $extend = [
  582. 'register_from' => input('register_from',''),
  583. 'gender' => $gender
  584. ];
  585. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  586. }
  587. if ($ret) {
  588. Sms::flush($mobile, 'mobilelogin');
  589. $data = $this->userInfo('return');
  590. $this->success(__('Logged in successful'), $data);
  591. } else {
  592. $this->error($this->auth->getError());
  593. }
  594. }*/
  595. /**
  596. * 运营商一键登录
  597. */
  598. /*public function onLogin()
  599. {
  600. $accessToken = input('accessToken');// 运营商预取号获取到的token
  601. $token = input('tokenT');// 易盾返回的token
  602. if (!$accessToken || !$token) {
  603. $this->error("参数获取失败!");
  604. }
  605. $params = array(
  606. // 运营商预取号获取到的token
  607. "accessToken" => $accessToken,
  608. // 易盾返回的token
  609. "token" => $token
  610. );
  611. // 获取密钥配置
  612. $configInfo = config("onLogin");
  613. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  614. $onret = $onlogin->check($params);
  615. // $ret = [];
  616. // $ret["code"] = 200;
  617. // $ret["msg"] = "ok";
  618. // $ret["data"] = [
  619. // "phone" => "17574504021",
  620. // "resultCode" => 0
  621. // ];
  622. if ($onret["code"] == 200) {
  623. $mobile = $onret["data"]["phone"];
  624. if (empty($mobile)) {
  625. // 取号失败,建议进行二次验证,例如短信验证码
  626. $this->error("取号登录失败,请用验证码方式登录!");
  627. } else {
  628. // 取号成功, 执行登录等流程
  629. // 用户登录逻辑 === 开始
  630. $user = \app\common\model\User::getByMobile($mobile);
  631. if ($user) {
  632. if (!in_array($user->status,[1,2])) {
  633. $this->error(__('Account is locked'));
  634. }
  635. if ($user->frozentime > time()) {
  636. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  637. }
  638. //如果已经有账号则直接登录
  639. $ret = $this->auth->direct($user->id);
  640. $is_register = 0;
  641. } else {
  642. $extend = [
  643. 'register_from' => input('register_from',''),
  644. 'gender' => -1
  645. ];
  646. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  647. $is_register = 1;
  648. }
  649. if ($ret) {
  650. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  651. } else {
  652. $this->error($this->auth->getError());
  653. }
  654. // 用户登录逻辑 === 结束
  655. }
  656. } else {
  657. $this->error("登录失败,请用验证码方式登录!");
  658. }
  659. }*/
  660. /**
  661. * 运营商一键登录注册
  662. */
  663. /*public function onregister()
  664. {
  665. $accessToken = input('accessToken');// 运营商预取号获取到的token
  666. $token = input('tokenT');// 易盾返回的token
  667. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  668. if (!$accessToken || !$token) {
  669. $this->error("参数获取失败!");
  670. }
  671. if (!in_array($gender, [1, 0])) {
  672. $this->error('性别错误');
  673. }
  674. $params = array(
  675. // 运营商预取号获取到的token
  676. "accessToken" => $accessToken,
  677. // 易盾返回的token
  678. "token" => $token
  679. );
  680. // 获取密钥配置
  681. $configInfo = config("onLogin");
  682. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  683. $onret = $onlogin->check($params);
  684. // $ret = [];
  685. // $ret["code"] = 200;
  686. // $ret["msg"] = "ok";
  687. // $ret["data"] = [
  688. // "phone" => "17574504021",
  689. // "resultCode" => 0
  690. // ];
  691. if ($onret["code"] == 200) {
  692. $mobile = $onret["data"]["phone"];
  693. if (empty($mobile)) {
  694. // 取号失败,建议进行二次验证,例如短信验证码
  695. $this->error("取号登录失败,请用验证码方式登录!");
  696. } else {
  697. // 取号成功, 执行登录等流程
  698. // 用户登录逻辑 === 开始
  699. $user = \app\common\model\User::getByMobile($mobile);
  700. if ($user) {
  701. $this->error('账号已经存在,请直接登录');
  702. if (!in_array($user->status,[1,2])) {
  703. $this->error(__('Account is locked'));
  704. }
  705. //如果已经有账号则直接登录
  706. $ret = $this->auth->direct($user->id);
  707. $is_register = 0;
  708. } else {
  709. $extend = [
  710. 'register_from' => input('register_from',''),
  711. 'gender' => $gender
  712. ];
  713. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  714. $is_register = 1;
  715. }
  716. //结果
  717. if ($ret) {
  718. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  719. } else {
  720. $this->error($this->auth->getError());
  721. }
  722. // 用户登录逻辑 === 结束
  723. }
  724. } else {
  725. $this->error("登录失败,请用验证码方式登录!");
  726. }
  727. }*/
  728. //微信登录,预先假注册
  729. /*public function wechatlogin(){
  730. $code = input('code','');
  731. if(!$code){
  732. $this->error(__('Invalid parameters'));
  733. }
  734. //微信
  735. $wechat = new Wechat();
  736. $wxuserinfo = $wechat->getAccessToken($code);
  737. if(!$wxuserinfo){
  738. $this->error('openid获取失败');
  739. }
  740. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  741. $this->error('openid获取失败');
  742. }
  743. $openid = $wxuserinfo['openid'];
  744. //检查用户
  745. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  746. if ($user) {
  747. if ($user['status'] == -1) {
  748. $this->error('账户已注销');
  749. }
  750. if ($user['status'] != 1) {
  751. $this->error(__('Account is locked'));
  752. }
  753. //如果已经有账号则直接登录
  754. $ret = $this->auth->direct($user['id']);
  755. if ($ret) {
  756. $userInfo = $this->auth->getUserinfo();
  757. $userInfo['is_register'] = 0;
  758. $userInfo['code'] = $code;
  759. $this->success(__('Logged in successful'), $userInfo);
  760. } else {
  761. $this->error($this->auth->getError());
  762. }
  763. } else {
  764. //记录code和openid,绑定手机号的时候更新openid
  765. $wechatCodeData = [
  766. 'code' => $code,
  767. 'openid' => $openid,
  768. 'createtime' => time(),
  769. ];
  770. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  771. if (empty($wechatCode)) {
  772. Db::name('wechat_code')->insertGetId($wechatCodeData);
  773. } else {
  774. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  775. }
  776. //直接返回
  777. $userInfo = [];
  778. $userInfo['is_register'] = 1;
  779. $userInfo['code'] = $code;
  780. $this->success('获取信息成功', $userInfo);
  781. }
  782. }*/
  783. /**
  784. * 微信注册来的,绑定手机号
  785. *
  786. * @ApiMethod (POST)
  787. * @param string $mobile 手机号
  788. * @param string $captcha 验证码
  789. */
  790. /*public function wechatbindmobile()
  791. {
  792. $mobile = $this->request->param('mobile');
  793. $captcha = $this->request->param('captcha');
  794. $code = $this->request->param('code');
  795. if (!$mobile || !$captcha || !$code) {
  796. $this->error(__('Invalid parameters'));
  797. }
  798. if (!Validate::regex($mobile, "^1\d{10}$")) {
  799. $this->error(__('Mobile is incorrect'));
  800. }
  801. $result = Sms::check($mobile, $captcha, 'changemobile');
  802. if (!$result) {
  803. $this->error(__('Captcha is incorrect'));
  804. }
  805. $wechatCodeWhere['code'] = $code;
  806. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  807. if (empty($wechatCode)) {
  808. $this->error('请先微信登录');
  809. }
  810. //检查appid绑定的用户
  811. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  812. if ($user) {
  813. if ($user['status'] == -1) {
  814. $this->error('账户已注销');
  815. }
  816. if ($user['status'] != 1) {
  817. $this->error(__('Account is locked'));
  818. }
  819. //如果已经有账号则直接登录
  820. $ret = $this->auth->direct($user['id']);
  821. $this->success('success',$this->auth->getUserinfo());
  822. }
  823. //新的openid用户
  824. $where = [];
  825. $where['mobile'] = $mobile;
  826. $userData = Db::name('user')->where($where)->find();//老用户
  827. if (!empty($userData)) {
  828. if (empty($userData['wechat_openid'])) {
  829. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  830. } else {
  831. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  832. $this->error('该手机号已被其他用户绑定');
  833. }
  834. }
  835. $ret = $this->auth->direct($userData['id']);
  836. } else {
  837. $extend = [
  838. 'wechat_openid' => $wechatCode['openid'],
  839. ];
  840. $ret = $this->auth->register('', '','', $mobile, $extend);
  841. }
  842. if (!$ret) {
  843. $this->error($this->auth->getError());
  844. }
  845. $this->success('success',$this->auth->getUserinfo());
  846. }*/
  847. //实名认证信息
  848. public function idcard_confirm_info(){
  849. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  850. if (!$check) {
  851. $check = (object)[];
  852. }
  853. $this->success('success',$check);
  854. }
  855. /*
  856. * 修改用户的坐标
  857. * */
  858. public function change_longlat(){
  859. $longitude = input_post('longitude');
  860. $latitude = input_post('latitude');
  861. $cityname = input_post('cityname');
  862. if(empty($longitude) || empty($latitude) || empty($cityname)){
  863. $this->error();
  864. }
  865. $data = [
  866. 'longitude' => $longitude,
  867. 'latitude' => $latitude,
  868. 'cityname' => $cityname,
  869. ];
  870. Db::name('user')->where('id',$this->auth->id)->update($data);
  871. $this->success();
  872. }
  873. /**
  874. * 修改手机号
  875. *
  876. * @ApiMethod (POST)
  877. * @param string $mobile 手机号
  878. * @param string $captcha 验证码
  879. */
  880. public function changemobile()
  881. {
  882. $user = $this->auth->getUser();
  883. $oldcaptcha = $this->request->request('oldcaptcha');
  884. $mobile = $this->request->request('mobile');
  885. $captcha = $this->request->request('captcha');
  886. if (!$oldcaptcha || !$mobile || !$captcha) {
  887. $this->error(__('Invalid parameters'));
  888. }
  889. if (!Validate::regex($mobile, "^1\d{10}$")) {
  890. $this->error(__('Mobile is incorrect'));
  891. }
  892. if($user->mobile == $mobile){
  893. $this->error('新手机号不能与旧手机号相同');
  894. }
  895. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  896. $this->error(__('Mobile already exist'));
  897. }
  898. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  899. if (!$result) {
  900. $this->error(__('Captcha is incorrect'));
  901. }
  902. $result = Sms::check($mobile, $captcha, 'changemobile');
  903. if (!$result) {
  904. $this->error(__('Captcha is incorrect'));
  905. }
  906. /*$verification = $user->verification;
  907. $verification->mobile = 1;
  908. $user->verification = $verification;*/
  909. $user->mobile = $mobile;
  910. $user->save();
  911. Sms::flush($user->mobile, 'changemobile');
  912. Sms::flush($mobile, 'changemobile');
  913. $this->success();
  914. }
  915. /**
  916. * 手机号注册来的,绑定微信
  917. *
  918. * @ApiMethod (POST)
  919. * @param string wechat_openid 微信openid
  920. */
  921. /*public function bindopenid()
  922. {
  923. $user = $this->auth->getUser();
  924. $wechat_openid = $this->request->request('wechat_openid');
  925. if(!empty($this->auth->wechat_openid)){
  926. $this->error('已经绑定了微信号');
  927. }
  928. if (!$wechat_openid) {
  929. $this->error(__('Invalid parameters'));
  930. }
  931. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  932. $this->error('该微信号已被其他用户绑定');
  933. }
  934. $user->wechat_openid = $wechat_openid;
  935. $user->save();
  936. $this->success('success',$this->userInfo('return'));
  937. }*/
  938. /**
  939. * 修改密码
  940. *
  941. * @ApiMethod (POST)
  942. * @param string $newpassword 新密码
  943. * @param string $oldpassword 旧密码
  944. */
  945. public function changepwd(){
  946. $newpassword = input('newpassword');
  947. $oldpassword = input('oldpassword','');
  948. if (!$newpassword) {
  949. $this->error(__('Invalid parameters'));
  950. }
  951. if($this->auth->password && empty($oldpassword)){
  952. $this->error('原密码必填');
  953. }
  954. if(empty($this->auth->password)){
  955. $ret = $this->auth->changepwd($newpassword, '', true);
  956. }else{
  957. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  958. }
  959. if ($ret) {
  960. $this->success(__('Reset password successful'));
  961. } else {
  962. $this->error($this->auth->getError());
  963. }
  964. }
  965. /**
  966. * 记录当前登陆的设备ID,设备信息,IP等
  967. */
  968. /* public function changeDeviceIp()
  969. {
  970. // 接口防并发
  971. if (!$this->apiLimit(1, 5)) {
  972. return ;
  973. }
  974. $user = $this->auth->getUser();
  975. $ip = request()->ip();
  976. $deviceId = $this->request->request('device_id','');
  977. $phoneModel = $this->request->request('phone_model','');
  978. $brand = $this->request->request('brand','');
  979. $apiVersion = $this->request->request('api_version','');
  980. $deviceOs = $this->request->request('device_os','');
  981. if ($ip !== $user->loginip){
  982. $update = [];
  983. $update['id'] = $user->id;
  984. $update['loginip'] = $ip;
  985. \app\common\model\User::update($update);
  986. }
  987. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  988. if (empty($userDeviceInfo)){
  989. $userDeviceInfo = new UserDeviceInfo();
  990. $userDeviceInfo->user_id = $user->u_id;
  991. }
  992. $userDeviceInfo->device_os = $deviceOs;
  993. $userDeviceInfo->device_id = $deviceId;
  994. $userDeviceInfo->phone_model = $phoneModel;
  995. $userDeviceInfo->brand = $brand;
  996. $userDeviceInfo->api_version = $apiVersion;
  997. $userDeviceInfo->save();
  998. //首页接口调用,这里不反回信息
  999. // $this->success("更新成功!");
  1000. }*/
  1001. //修改用户活跃1
  1002. /*public function useractive(){
  1003. $this->success('success');
  1004. }*/
  1005. //公众号获取openid
  1006. /*public function getUserOpenid_gzh(){
  1007. $configValue = Service::getConfig('wechat');
  1008. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1009. $rs = $wechat->getOpenid();
  1010. $this->success('success',$rs);
  1011. }*/
  1012. /**
  1013. * 微信内H5-JSAPI支付
  1014. */
  1015. /*public function jssdkBuildConfig() {
  1016. $url = $this->request->request("url");
  1017. $configValue = Service::getConfig('wechat');
  1018. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1019. $sign = $wechat->getSignPackage(urldecode($url));
  1020. $this->success("获取成功!",$sign);
  1021. }*/
  1022. //苹果账号登录
  1023. /*public function ioslogin(){
  1024. $ios_openid = input('ios_openid','');
  1025. if (!$ios_openid) {
  1026. $this->error(__('Invalid parameters'));
  1027. }
  1028. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1029. // if (!$user) {
  1030. // $this->success('选择性别', ['code' => 5]);
  1031. // }
  1032. if ($user) {
  1033. if (!in_array($user->status,[1,2])) {
  1034. $this->error(__('Account is locked'));
  1035. }
  1036. if ($user->frozentime > time()) {
  1037. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  1038. }
  1039. //如果已经有账号则直接登录
  1040. $ret = $this->auth->direct($user->id);
  1041. } else {
  1042. $reg_data = [
  1043. 'register_from' => input('register_from',''),
  1044. 'gender' => -1
  1045. ];
  1046. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1047. }
  1048. if ($ret) {
  1049. $data = $this->userInfo('return');
  1050. $this->success(__('Logged in successful'), $data);
  1051. } else {
  1052. $this->error($this->auth->getError());
  1053. }
  1054. }*/
  1055. //苹果账号注册
  1056. /*public function iosregiter(){
  1057. $ios_openid = input('ios_openid', '', 'trim');
  1058. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  1059. if (!$ios_openid) {
  1060. $this->error(__('Invalid parameters'));
  1061. }
  1062. if (!in_array($gender, [1, 0])) {
  1063. $this->error('性别错误');
  1064. }
  1065. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1066. if ($user) {
  1067. $this->error('账号已经存在,请直接登录');
  1068. }
  1069. $reg_data = [
  1070. 'register_from' => input('register_from',''),
  1071. 'gender' => $gender
  1072. ];
  1073. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1074. if ($ret) {
  1075. $data = $this->userInfo('return');
  1076. $this->success(__('Logged in successful'), $data);
  1077. } else {
  1078. $this->error($this->auth->getError());
  1079. }
  1080. }*/
  1081. //实名认证
  1082. public function idcard_auth() {
  1083. $info = Db::name('user_idconfirm')->where(['user_id' => $this->auth->id])->find();
  1084. // if ($info && $info['status'] == 0) {
  1085. // $this->error('您已经提交信息了,请进行人脸认证!');
  1086. // }
  1087. if ($info && $info['status'] == 1) {
  1088. $this->error('您已通过审核!');
  1089. }
  1090. $nickname = input('nickname', '', 'trim'); // 姓名
  1091. $idcard = input('idcard', '', 'trim'); // 身份证号
  1092. if ($nickname === '') {
  1093. $this->error('请输入姓名');
  1094. }
  1095. if (iconv_strlen($nickname, 'utf-8') > 50) {
  1096. $this->error('请输入正确姓名');
  1097. }
  1098. if ($idcard === '') {
  1099. $this->error('请输入身份证号');
  1100. }
  1101. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1102. $this->error('请输入正确身份证号');
  1103. }
  1104. $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  1105. if ($count) {
  1106. $this->error('身份证号已存在');
  1107. }
  1108. $data = [];
  1109. $data['truename'] = $nickname;
  1110. $data['idcard'] = $idcard;
  1111. //腾讯云身份证二要素认证
  1112. $auth_restult = $this->userauth_tencent($idcard, $nickname);
  1113. if ($auth_restult) {
  1114. $data['status'] = 1; //通过
  1115. $msg = '认证通过';
  1116. } else {
  1117. $data['status'] = 2; //不通过
  1118. $msg = '认证不通过';
  1119. }
  1120. //开启事务
  1121. Db::startTrans();
  1122. if (!$info) { //未认证
  1123. $data["user_id"] = $this->auth->id;
  1124. $data["createtime"] = time();
  1125. $res = Db::name('user_idconfirm')->insertGetId($data);
  1126. } else { //认证被拒绝过
  1127. $data['updatetime'] = time();
  1128. $res = Db::name('user_idconfirm')->where(['id' => $info['id'], 'user_id' => $this->auth->id])->setField($data);
  1129. }
  1130. if (!$res) {
  1131. Db::rollback();
  1132. $this->error('认证失败');
  1133. }
  1134. $rt = Db::name('user')->where(['id' => $this->auth->id, 'idcard_status' => $this->auth->idcard_status])->setField('idcard_status', $data['status']);
  1135. if ($rt === false) {
  1136. Db::rollback();
  1137. $this->error('认证失败');
  1138. }
  1139. if ($data['status'] == 1) {
  1140. //完成实名认证 +20金币
  1141. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  1142. if($task_rs === false){
  1143. Db::rollback();
  1144. $this->error('完成任务赠送奖励失败');
  1145. }
  1146. //系统消息
  1147. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证已经审核通过');
  1148. } else {
  1149. //系统消息
  1150. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证审核不通过');
  1151. }
  1152. Db::commit();
  1153. $this->success($msg);
  1154. }
  1155. //腾讯云身份证二要素认证
  1156. public function userauth_tencent($idcard = '', $nickname = '') {
  1157. // require_once 'vendor/autoload.php';
  1158. try {
  1159. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1160. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1161. $config = config('tencent_im');
  1162. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1163. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1164. $httpProfile = new HttpProfile();
  1165. $httpProfile->setEndpoint("faceid.tencentcloudapi.com");
  1166. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1167. $clientProfile = new ClientProfile();
  1168. $clientProfile->setHttpProfile($httpProfile);
  1169. // 实例化要请求产品的client对象,clientProfile是可选的
  1170. $client = new FaceidClient($cred, "", $clientProfile);
  1171. // 实例化一个请求对象,每个接口都会对应一个request对象
  1172. $req = new IdCardVerificationRequest();
  1173. $params = array(
  1174. "IdCard" => $idcard,
  1175. "Name" => $nickname
  1176. );
  1177. $req->fromJsonString(json_encode($params));
  1178. // 返回的resp是一个IdCardVerificationResponse的实例,与请求对象对应
  1179. $resp = $client->IdCardVerification($req);
  1180. // 输出json格式的字符串回包
  1181. // print_r($resp->toJsonString());
  1182. $result = json_decode($resp->toJsonString(), true);
  1183. if (isset($result['Result']) && $result['Result'] == 0) {
  1184. return 1; //通过
  1185. } else {
  1186. return 0;
  1187. }
  1188. }
  1189. catch(TencentCloudSDKException $e) {
  1190. // echo $e;
  1191. return 0;
  1192. }
  1193. }
  1194. //申请真人认证
  1195. public function realauth() {
  1196. if ($this->auth->real_status == 1) {
  1197. $this->error('您已经真人认证过了~');
  1198. }
  1199. if ($this->auth->avatar == config('avatar_boy') || $this->auth->avatar == config('avatar_girl')) {
  1200. $this->error('请先上传真人头像~');
  1201. }
  1202. //获取token
  1203. $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';
  1204. $token_result = file_get_contents($token_url);
  1205. if (!$token_result) {
  1206. $this->error('您的网络开小差啦1~');
  1207. }
  1208. $token_result = json_decode($token_result, true);
  1209. if ($token_result['code'] != 0) {
  1210. $this->error('您的网络开小差啦2~');
  1211. }
  1212. $token = $token_result['access_token'];
  1213. //获取签名鉴权参数ticket
  1214. $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';
  1215. $ticket_result = file_get_contents($ticket_url);
  1216. if (!$ticket_result) {
  1217. $this->error('您的网络开小差啦3~');
  1218. }
  1219. $ticket_result = json_decode($ticket_result, true);
  1220. if ($ticket_result['code'] != 0) {
  1221. $this->error('您的网络开小差啦4~');
  1222. }
  1223. $ticket = $ticket_result['tickets'][0]['value'];
  1224. //获取签名
  1225. $sign_data = [
  1226. 'wbappid' => config('tencent_yun')['secret_id'],
  1227. 'userId' => (string)$this->auth->id,
  1228. 'version' => '1.0.0',
  1229. 'ticket' => $ticket,
  1230. 'nonce' => Random::alnum(32)
  1231. ];//p($sign_data);
  1232. asort($sign_data); //p($sign_data);//排序
  1233. $sign_string = join('', $sign_data);//p($sign_string);
  1234. $sign = sha1($sign_string);//p($sign);
  1235. //上传身份信息
  1236. $orderNo = getMillisecond() . $this->auth->id . mt_rand(1, 1000); //商户请求的唯一标识
  1237. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo=' . $orderNo;
  1238. $avatar = one_domain_image($this->auth->avatar);
  1239. $avatar = str_replace('https', 'http', $avatar);
  1240. $img = file_get_contents($avatar);
  1241. $img = str_replace('data:image/jpg;base64', '', $img);
  1242. $img = str_replace('\n', '', $img);
  1243. $sourcePhotoStr = base64_encode($img);
  1244. $data = [
  1245. 'webankAppId' => config('tencent_yun')['secret_id'],
  1246. 'orderNo' => $orderNo,
  1247. 'userId' => (string)$this->auth->id,
  1248. 'sourcePhotoStr' => $sourcePhotoStr,
  1249. 'sourcePhotoType' => 2,
  1250. 'version' => '1.0.0',
  1251. 'sign' => $sign,
  1252. 'nonce' => $sign_data['nonce']
  1253. ];
  1254. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1255. if (!$rs) {
  1256. $this->error('您的网络开小差啦5~');
  1257. }
  1258. $rs = json_decode($rs, true);
  1259. if (!$rs || $rs['code'] != 0) {
  1260. $this->error('您的网络开小差啦6~');
  1261. }
  1262. $user_auth = [
  1263. 'user_id' => $this->auth->id,
  1264. 'certify_id' => $rs['result']['faceId'],
  1265. 'out_trade_no' => $data['orderNo'],
  1266. 'status' => 0,
  1267. 'createtime' => time(),
  1268. 'updatetime' => time()
  1269. ];
  1270. //开启事务
  1271. Db::startTrans();
  1272. //查询是否认证过
  1273. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1274. if ($info) {
  1275. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  1276. } else {
  1277. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  1278. }
  1279. if (!$auth_rs) {
  1280. Db::rollback();
  1281. $this->error('您的网络开小差啦7~');
  1282. }
  1283. //修改用户表认证状态
  1284. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', 0);
  1285. if ($user_rs === false) {
  1286. Db::rollback();
  1287. $this->error('您的网络开小差啦8~');
  1288. }
  1289. Db::commit();
  1290. $return_data = [
  1291. 'face_id' => $user_auth['certify_id'],
  1292. 'order_no' => $user_auth['out_trade_no'],
  1293. 'user_id' => (string)$this->auth->id,
  1294. 'nonce' => $sign_data['nonce'],
  1295. 'sign' => $sign
  1296. ];
  1297. $this->success('success', $return_data);
  1298. }
  1299. //查询真人认证结果
  1300. public function getrealauthresult() {
  1301. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1302. if (!$user_auth) {
  1303. $this->success('尚未认证');
  1304. }
  1305. if ($user_auth['status'] == 1) {
  1306. $this->success('真人认证通过');
  1307. }
  1308. if (!$user_auth['certify_id']) {
  1309. $this->success('请先进行真人认证');
  1310. }
  1311. //获取token
  1312. $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';
  1313. $token_result = file_get_contents($token_url);
  1314. if (!$token_result) {
  1315. $this->error('您的网络开小差啦1~');
  1316. }
  1317. $token_result = json_decode($token_result, true);
  1318. if ($token_result['code'] != 0) {
  1319. $this->error('您的网络开小差啦2~');
  1320. }
  1321. $token = $token_result['access_token'];
  1322. //获取签名鉴权参数ticket
  1323. $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';
  1324. $ticket_result = file_get_contents($ticket_url);
  1325. if (!$ticket_result) {
  1326. $this->error('您的网络开小差啦3~');
  1327. }
  1328. $ticket_result = json_decode($ticket_result, true);
  1329. if ($ticket_result['code'] != 0) {
  1330. $this->error('您的网络开小差啦4~');
  1331. }
  1332. $ticket = $ticket_result['tickets'][0]['value'];
  1333. //获取签名
  1334. $sign_data = [
  1335. 'wbappid' => config('tencent_yun')['secret_id'],
  1336. 'orderNo' => $user_auth['out_trade_no'],
  1337. 'version' => '1.0.0',
  1338. 'ticket' => $ticket,
  1339. 'nonce' => Random::alnum(32)
  1340. ];//p($sign_data);
  1341. asort($sign_data); //p($sign_data);//排序
  1342. $sign_string = join('', $sign_data);//p($sign_string);
  1343. $sign = sha1($sign_string);//p($sign);
  1344. //人脸核身结果查询
  1345. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  1346. $data = [
  1347. 'appId' => config('tencent_yun')['secret_id'],
  1348. 'version' => '1.0.0',
  1349. 'nonce' => $sign_data['nonce'],
  1350. 'orderNo' => $user_auth['out_trade_no'],
  1351. 'sign' => $sign
  1352. ];
  1353. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1354. if (!$rs) {
  1355. $this->error('您的网络开小差啦5~');
  1356. }
  1357. $rs = json_decode($rs, true);
  1358. if (!$rs || $rs['code'] != 0) {
  1359. $this->error($rs['msg']);
  1360. }
  1361. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  1362. $edit_data['status'] = 1;
  1363. $msg = '真人认证成功';
  1364. } else {
  1365. $edit_data['status'] = 2;
  1366. $edit_data['certify_id'] = '';
  1367. $edit_data['out_trade_no'] = '';
  1368. $msg = '真人认证失败';
  1369. }
  1370. $edit_data['updatetime'] = time();
  1371. //开启事务
  1372. Db::startTrans();
  1373. //修改认证信息
  1374. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1375. if (!$result) {
  1376. Db::rollback();
  1377. $this->error('查询认证结果失败2');
  1378. }
  1379. //修改用户信息
  1380. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', $edit_data['status']);
  1381. if (!$rs) {
  1382. Db::rollback();
  1383. $this->error('查询认证结果失败3');
  1384. }
  1385. if ($edit_data['status'] == 1) { //通过
  1386. //tag任务赠送金币
  1387. //真人认证奖励
  1388. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1389. if($task_rs === false){
  1390. Db::rollback();
  1391. $this->error('完成任务赠送奖励失败');
  1392. }
  1393. //系统消息
  1394. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1395. } else {
  1396. //系统消息
  1397. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1398. }
  1399. Db::commit();
  1400. $this->success($msg);
  1401. }
  1402. //客服
  1403. public function kefu() {
  1404. $type = input('type', 0, 'intval'); //客服位置: 0客服中心 1充值客服
  1405. if (!in_array($type, [0, 1])) {
  1406. $this->error('您的网络开小差啦~');
  1407. }
  1408. if ($type == 0) {
  1409. $user_id = config('site.customer_service_id') ? : 0; //指定客服id
  1410. } else {
  1411. $user_id = config('site.pay_customer_service_id') ? : 0; //指定客服id
  1412. }
  1413. $list = Db::name('user')->field('id')->where(['status' => 1, 'is_kefu' => 1, 'id' => $user_id])->select();
  1414. if (!$list) {
  1415. $this->success('success', $list);
  1416. }
  1417. foreach ($list as $k => &$v) {
  1418. $v['nickname'] = '客服' . ($k + 1);
  1419. $v['avatar'] = config('avatar_girl');
  1420. }
  1421. $this->success('success', $list);
  1422. }
  1423. //真人认证后修改头像前比对
  1424. public function realavatar_auit() {
  1425. if ($this->auth->real_status != 1) {
  1426. $this->error('尚未通过真人认证');
  1427. }
  1428. $avatar = input('avatar', '', 'trim'); //头像地址
  1429. if ($avatar === '') {
  1430. $this->error('参数缺失');
  1431. }
  1432. $avatar = one_domain_image($avatar);
  1433. $now_avatar = one_domain_image($this->auth->avatar);
  1434. if ($avatar == $now_avatar) {
  1435. $this->error('头像未改变');
  1436. }
  1437. //腾讯云人脸识别
  1438. $result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1439. $this->success('结果', $result);
  1440. }
  1441. //真人认证后修改头像
  1442. public function editrealavatar() {
  1443. if ($this->auth->real_status != 1) {
  1444. $this->error('尚未通过真人认证');
  1445. }
  1446. $avatar = input('avatar', '', 'trim'); //头像地址
  1447. if ($avatar === '') {
  1448. $this->error('参数缺失');
  1449. }
  1450. $avatar = one_domain_image($avatar);
  1451. $now_avatar = one_domain_image($this->auth->avatar);
  1452. if ($avatar == $now_avatar) {
  1453. $this->error('头像未改变');
  1454. }
  1455. //腾讯云人脸识别
  1456. $auit_result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1457. if ($auit_result != 1) {
  1458. $this->success('提示', ['code' => 2]);
  1459. }
  1460. $data['avatar'] = $avatar;
  1461. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1462. if (!$user_result) {
  1463. $this->error('修改失败');
  1464. }
  1465. $this->success('修改成功');
  1466. }
  1467. //真人认证后修改头像并取消真人认证
  1468. public function editrealavatarcancelauit() {
  1469. if ($this->auth->real_status != 1) {
  1470. $this->error('尚未通过真人认证');
  1471. }
  1472. $avatar = input('avatar', '', 'trim'); //头像地址
  1473. if ($avatar === '') {
  1474. $this->error('参数缺失');
  1475. }
  1476. $avatar = one_domain_image($avatar);
  1477. $now_avatar = one_domain_image($this->auth->avatar);
  1478. if ($avatar == $now_avatar) {
  1479. $this->error('头像未改变');
  1480. }
  1481. $data['avatar'] = $avatar;
  1482. $data['real_status'] = -1;
  1483. //开启事务
  1484. Db::startTrans();
  1485. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1486. if (!$user_result) {
  1487. Db::rollback();
  1488. $this->error('修改失败');
  1489. }
  1490. $user_auth_result = Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  1491. if (!$user_auth_result) {
  1492. Db::rollback();
  1493. $this->error('修改失败');
  1494. }
  1495. Db::commit();
  1496. $this->success('修改成功');
  1497. }
  1498. //腾讯云人脸识别
  1499. public function face_tencent($urla = '', $urlb = '') {
  1500. // require_once 'vendor/autoload.php';
  1501. try {
  1502. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1503. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1504. $config = config('tencent_im');
  1505. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1506. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1507. $httpProfile = new HttpProfile();
  1508. $httpProfile->setEndpoint("iai.tencentcloudapi.com");
  1509. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1510. $clientProfile = new ClientProfile();
  1511. $clientProfile->setHttpProfile($httpProfile);
  1512. // 实例化要请求产品的client对象,clientProfile是可选的
  1513. $client = new IaiClient($cred, "ap-beijing", $clientProfile);
  1514. // 实例化一个请求对象,每个接口都会对应一个request对象
  1515. $req = new CompareFaceRequest();
  1516. $params = array(
  1517. "UrlA" => $urla,
  1518. "UrlB" => $urlb,
  1519. "FaceModelVersion" => "3.0"
  1520. );
  1521. $req->fromJsonString(json_encode($params));
  1522. // 返回的resp是一个CompareFaceResponse的实例,与请求对象对应
  1523. $resp = $client->CompareFace($req);
  1524. // 输出json格式的字符串回包
  1525. // print_r($resp->toJsonString());
  1526. $result = json_decode($resp->toJsonString(), true);
  1527. //3.0版本误识率千分之一对应分数为40分,误识率万分之一对应分数为50分,误识率十万分之一对应分数为60分。 一般超过50分则可认定为同一人。
  1528. if (isset($result['Score']) && $result['Score'] >= 60) {
  1529. return 1; //通过
  1530. } else {
  1531. return 0;
  1532. }
  1533. }
  1534. catch(TencentCloudSDKException $e) {
  1535. // echo $e;
  1536. return 0;
  1537. }
  1538. }
  1539. //修改城市
  1540. public function editcity() {
  1541. $name = input('name', '', 'trim'); //城市名
  1542. if ($name === '') {
  1543. $this->error('参数缺失');
  1544. }
  1545. $hometown_cityid = Db::name('area')->where(['name' => $name])->value('id');
  1546. if (!$hometown_cityid) {
  1547. $this->success('修改成功');
  1548. }
  1549. Db::name('user')->where(['id' => $this->auth->id])->setField('hometown_cityid', $hometown_cityid);
  1550. $this->success('修改成功');
  1551. }
  1552. //搜索用户
  1553. public function searchuser() {
  1554. $keyword = input('keyword', '', 'trim'); //昵称或ID
  1555. if ($keyword === '') {
  1556. $this->error('请输入关键字');
  1557. }
  1558. $id = Db::name('user')->where(['nickname|username' => $keyword])->value('id');
  1559. $id = $id ? : 0;
  1560. $this->success('用户', $id);
  1561. }
  1562. }