User.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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. public function cancleUser(){
  454. if (!$this->request->isPost()) {
  455. $this->error(__('Invalid parameters'));
  456. }
  457. //退出im
  458. $tenIm = new Tenim();
  459. $tenIm->loginoutim($this->auth->id);
  460. $data = [
  461. 'status' => -1,
  462. 'mobile' => 'close_'.$this->auth->mobile,
  463. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  464. ];
  465. Db::name('user')->where('id',$this->auth->id)->update($data);
  466. $this->auth->logout();
  467. $this->success('注销成功');
  468. }
  469. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  470. /**
  471. * 手机验证码验证
  472. *
  473. * @ApiMethod (POST)
  474. * @param string $mobile 手机号
  475. * @param string $captcha 验证码
  476. */
  477. /*public function mobilecheck()
  478. {
  479. $mobile = input('mobile');
  480. $captcha = input('captcha');
  481. if (!$mobile || !$captcha) {
  482. $this->error(__('Invalid parameters'));
  483. }
  484. if (!Validate::regex($mobile, "^1\d{10}$")) {
  485. $this->error(__('Mobile is incorrect'));
  486. }
  487. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  488. $this->error(__('Captcha is incorrect'));
  489. }
  490. $this->success('验证通过');
  491. }*/
  492. /**
  493. * 手机验证码注册
  494. *
  495. * @ApiMethod (POST)
  496. * @param string $mobile 手机号
  497. * @param string $captcha 验证码
  498. * @param string $gender 性别:1=男,0=女
  499. */
  500. /*public function mobileregister()
  501. {
  502. $mobile = input('mobile');
  503. $captcha = input('captcha');
  504. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  505. if (!$mobile || !$captcha) {
  506. $this->error(__('Invalid parameters'));
  507. }
  508. if (!Validate::regex($mobile, "^1\d{10}$")) {
  509. $this->error(__('Mobile is incorrect'));
  510. }
  511. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  512. $this->error(__('Captcha is incorrect'));
  513. }
  514. if (!in_array($gender, [1, 0])) {
  515. $this->error('性别错误');
  516. }
  517. $user = \app\common\model\User::getByMobile($mobile);
  518. if ($user) {
  519. $this->error('账号已经存在,请直接登录');
  520. if ($user->status == -1) {
  521. $this->error('账户已注销');
  522. }
  523. if (!in_array($user->status,[1,2])) {
  524. $this->error(__('Account is locked'));
  525. }
  526. //如果已经有账号则直接登录
  527. $ret = $this->auth->direct($user->id);
  528. } else {
  529. $extend = [
  530. 'register_from' => input('register_from',''),
  531. 'gender' => $gender
  532. ];
  533. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  534. }
  535. if ($ret) {
  536. Sms::flush($mobile, 'mobilelogin');
  537. $data = $this->userInfo('return');
  538. $this->success(__('Logged in successful'), $data);
  539. } else {
  540. $this->error($this->auth->getError());
  541. }
  542. }*/
  543. /**
  544. * 运营商一键登录
  545. */
  546. /*public function onLogin()
  547. {
  548. $accessToken = input('accessToken');// 运营商预取号获取到的token
  549. $token = input('tokenT');// 易盾返回的token
  550. if (!$accessToken || !$token) {
  551. $this->error("参数获取失败!");
  552. }
  553. $params = array(
  554. // 运营商预取号获取到的token
  555. "accessToken" => $accessToken,
  556. // 易盾返回的token
  557. "token" => $token
  558. );
  559. // 获取密钥配置
  560. $configInfo = config("onLogin");
  561. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  562. $onret = $onlogin->check($params);
  563. // $ret = [];
  564. // $ret["code"] = 200;
  565. // $ret["msg"] = "ok";
  566. // $ret["data"] = [
  567. // "phone" => "17574504021",
  568. // "resultCode" => 0
  569. // ];
  570. if ($onret["code"] == 200) {
  571. $mobile = $onret["data"]["phone"];
  572. if (empty($mobile)) {
  573. // 取号失败,建议进行二次验证,例如短信验证码
  574. $this->error("取号登录失败,请用验证码方式登录!");
  575. } else {
  576. // 取号成功, 执行登录等流程
  577. // 用户登录逻辑 === 开始
  578. $user = \app\common\model\User::getByMobile($mobile);
  579. if ($user) {
  580. if (!in_array($user->status,[1,2])) {
  581. $this->error(__('Account is locked'));
  582. }
  583. if ($user->frozentime > time()) {
  584. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  585. }
  586. //如果已经有账号则直接登录
  587. $ret = $this->auth->direct($user->id);
  588. $is_register = 0;
  589. } else {
  590. $extend = [
  591. 'register_from' => input('register_from',''),
  592. 'gender' => -1
  593. ];
  594. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  595. $is_register = 1;
  596. }
  597. if ($ret) {
  598. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  599. } else {
  600. $this->error($this->auth->getError());
  601. }
  602. // 用户登录逻辑 === 结束
  603. }
  604. } else {
  605. $this->error("登录失败,请用验证码方式登录!");
  606. }
  607. }*/
  608. /**
  609. * 运营商一键登录注册
  610. */
  611. /*public function onregister()
  612. {
  613. $accessToken = input('accessToken');// 运营商预取号获取到的token
  614. $token = input('tokenT');// 易盾返回的token
  615. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  616. if (!$accessToken || !$token) {
  617. $this->error("参数获取失败!");
  618. }
  619. if (!in_array($gender, [1, 0])) {
  620. $this->error('性别错误');
  621. }
  622. $params = array(
  623. // 运营商预取号获取到的token
  624. "accessToken" => $accessToken,
  625. // 易盾返回的token
  626. "token" => $token
  627. );
  628. // 获取密钥配置
  629. $configInfo = config("onLogin");
  630. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  631. $onret = $onlogin->check($params);
  632. // $ret = [];
  633. // $ret["code"] = 200;
  634. // $ret["msg"] = "ok";
  635. // $ret["data"] = [
  636. // "phone" => "17574504021",
  637. // "resultCode" => 0
  638. // ];
  639. if ($onret["code"] == 200) {
  640. $mobile = $onret["data"]["phone"];
  641. if (empty($mobile)) {
  642. // 取号失败,建议进行二次验证,例如短信验证码
  643. $this->error("取号登录失败,请用验证码方式登录!");
  644. } else {
  645. // 取号成功, 执行登录等流程
  646. // 用户登录逻辑 === 开始
  647. $user = \app\common\model\User::getByMobile($mobile);
  648. if ($user) {
  649. $this->error('账号已经存在,请直接登录');
  650. if (!in_array($user->status,[1,2])) {
  651. $this->error(__('Account is locked'));
  652. }
  653. //如果已经有账号则直接登录
  654. $ret = $this->auth->direct($user->id);
  655. $is_register = 0;
  656. } else {
  657. $extend = [
  658. 'register_from' => input('register_from',''),
  659. 'gender' => $gender
  660. ];
  661. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  662. $is_register = 1;
  663. }
  664. //结果
  665. if ($ret) {
  666. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  667. } else {
  668. $this->error($this->auth->getError());
  669. }
  670. // 用户登录逻辑 === 结束
  671. }
  672. } else {
  673. $this->error("登录失败,请用验证码方式登录!");
  674. }
  675. }*/
  676. //微信登录,预先假注册
  677. /*public function wechatlogin(){
  678. $code = input('code','');
  679. if(!$code){
  680. $this->error(__('Invalid parameters'));
  681. }
  682. //微信
  683. $wechat = new Wechat();
  684. $wxuserinfo = $wechat->getAccessToken($code);
  685. if(!$wxuserinfo){
  686. $this->error('openid获取失败');
  687. }
  688. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  689. $this->error('openid获取失败');
  690. }
  691. $openid = $wxuserinfo['openid'];
  692. //检查用户
  693. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  694. if ($user) {
  695. if ($user['status'] == -1) {
  696. $this->error('账户已注销');
  697. }
  698. if ($user['status'] != 1) {
  699. $this->error(__('Account is locked'));
  700. }
  701. //如果已经有账号则直接登录
  702. $ret = $this->auth->direct($user['id']);
  703. if ($ret) {
  704. $userInfo = $this->auth->getUserinfo();
  705. $userInfo['is_register'] = 0;
  706. $userInfo['code'] = $code;
  707. $this->success(__('Logged in successful'), $userInfo);
  708. } else {
  709. $this->error($this->auth->getError());
  710. }
  711. } else {
  712. //记录code和openid,绑定手机号的时候更新openid
  713. $wechatCodeData = [
  714. 'code' => $code,
  715. 'openid' => $openid,
  716. 'createtime' => time(),
  717. ];
  718. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  719. if (empty($wechatCode)) {
  720. Db::name('wechat_code')->insertGetId($wechatCodeData);
  721. } else {
  722. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  723. }
  724. //直接返回
  725. $userInfo = [];
  726. $userInfo['is_register'] = 1;
  727. $userInfo['code'] = $code;
  728. $this->success('获取信息成功', $userInfo);
  729. }
  730. }*/
  731. /**
  732. * 微信注册来的,绑定手机号
  733. *
  734. * @ApiMethod (POST)
  735. * @param string $mobile 手机号
  736. * @param string $captcha 验证码
  737. */
  738. /*public function wechatbindmobile()
  739. {
  740. $mobile = $this->request->param('mobile');
  741. $captcha = $this->request->param('captcha');
  742. $code = $this->request->param('code');
  743. if (!$mobile || !$captcha || !$code) {
  744. $this->error(__('Invalid parameters'));
  745. }
  746. if (!Validate::regex($mobile, "^1\d{10}$")) {
  747. $this->error(__('Mobile is incorrect'));
  748. }
  749. $result = Sms::check($mobile, $captcha, 'changemobile');
  750. if (!$result) {
  751. $this->error(__('Captcha is incorrect'));
  752. }
  753. $wechatCodeWhere['code'] = $code;
  754. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  755. if (empty($wechatCode)) {
  756. $this->error('请先微信登录');
  757. }
  758. //检查appid绑定的用户
  759. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  760. if ($user) {
  761. if ($user['status'] == -1) {
  762. $this->error('账户已注销');
  763. }
  764. if ($user['status'] != 1) {
  765. $this->error(__('Account is locked'));
  766. }
  767. //如果已经有账号则直接登录
  768. $ret = $this->auth->direct($user['id']);
  769. $this->success('success',$this->auth->getUserinfo());
  770. }
  771. //新的openid用户
  772. $where = [];
  773. $where['mobile'] = $mobile;
  774. $userData = Db::name('user')->where($where)->find();//老用户
  775. if (!empty($userData)) {
  776. if (empty($userData['wechat_openid'])) {
  777. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  778. } else {
  779. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  780. $this->error('该手机号已被其他用户绑定');
  781. }
  782. }
  783. $ret = $this->auth->direct($userData['id']);
  784. } else {
  785. $extend = [
  786. 'wechat_openid' => $wechatCode['openid'],
  787. ];
  788. $ret = $this->auth->register('', '','', $mobile, $extend);
  789. }
  790. if (!$ret) {
  791. $this->error($this->auth->getError());
  792. }
  793. $this->success('success',$this->auth->getUserinfo());
  794. }*/
  795. public function testhaibao(){
  796. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
  797. // dump($haibao);
  798. $this->success('success', $haibao);
  799. }
  800. //海报
  801. public function haibao($player_id,$data){
  802. //下载页二维码,没必要保留
  803. $params = [
  804. 'text' => config('site.domain_name').'?code=' . $data['introcode'],
  805. 'size' => 90,
  806. 'logo' => false,
  807. 'label' => false,
  808. 'padding' => 0,
  809. ];
  810. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  811. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  812. mk_dir($qrcode_path);
  813. $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
  814. $qrCode->writeFile($download_qrcode);
  815. //海报
  816. $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
  817. return $haibao;
  818. }
  819. /*public function createhaibao($download_qrcode,$player_id,$sub_data){
  820. //背景图
  821. $background = $sub_data['background'] ? $sub_data['background'] : config('site.domain_name').'/assets/img/haibao.png';
  822. //海报图片路径
  823. $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
  824. mk_dir($new_path);
  825. $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
  826. //二维码
  827. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  828. //合成wap图片
  829. $image = new \addons\poster\library\Image2();
  830. $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
  831. return '/'.$wap_file_name;
  832. }*/
  833. public function createhaibao($download_qrcode,$player_id,$sub_data){
  834. //二维码
  835. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  836. $data = [
  837. [
  838. "left" => "15px",
  839. "top" => "296px",
  840. "type" => "img",
  841. "width" => "58px",
  842. "height" => "58px",
  843. "src" => one_domain_image($this->auth->avatar)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  844. ],
  845. [
  846. "left" => "81px",
  847. "top" => "300px",
  848. "type" => "nickname",
  849. "width" => "80px",
  850. "height" => "24px",
  851. "size" => "12px",
  852. "color" => "#000",
  853. "content" => $this->auth->nickname
  854. ],
  855. [
  856. "left" => "81px",
  857. "top" => "327px",
  858. "type" => "nickname",
  859. "width" => "80px",
  860. "height" => "24px",
  861. "size" => "12px",
  862. "color" => "#000",
  863. "content" => $this->auth->introcode
  864. ],
  865. [
  866. "left" => "227px",
  867. "top" => "283px",
  868. "type" => "img",
  869. "width" => "80px",
  870. "height" => "80px",
  871. // "src" => httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  872. "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  873. ]
  874. ];
  875. $data = json_encode($data, 320);
  876. $poster = [
  877. 'id' => 1,
  878. 'title' => '测试2',
  879. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  880. 'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
  881. 'data' => $data,
  882. 'status' => 'normal',
  883. 'weigh' => 0,
  884. 'createtime' => 1653993709,
  885. 'updatetime' => 1653994259,
  886. ];
  887. $image = new \addons\poster\library\Image();
  888. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  889. if (!$imgurl) {
  890. $this->error('生成海报出错');
  891. }
  892. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  893. return '/' . $imgurl;
  894. }
  895. //实名认证信息
  896. public function idcard_confirm_info(){
  897. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  898. if (!$check) {
  899. $check = (object)[];
  900. }
  901. $this->success('success',$check);
  902. }
  903. /*
  904. * 修改用户的坐标
  905. * */
  906. public function change_longlat(){
  907. $longitude = input_post('longitude');
  908. $latitude = input_post('latitude');
  909. $cityname = input_post('cityname');
  910. if(empty($longitude) || empty($latitude) || empty($cityname)){
  911. $this->error();
  912. }
  913. $data = [
  914. 'longitude' => $longitude,
  915. 'latitude' => $latitude,
  916. 'cityname' => $cityname,
  917. ];
  918. Db::name('user')->where('id',$this->auth->id)->update($data);
  919. $this->success();
  920. }
  921. /**
  922. * 修改手机号
  923. *
  924. * @ApiMethod (POST)
  925. * @param string $mobile 手机号
  926. * @param string $captcha 验证码
  927. */
  928. public function changemobile()
  929. {
  930. $user = $this->auth->getUser();
  931. $oldcaptcha = $this->request->request('oldcaptcha');
  932. $mobile = $this->request->request('mobile');
  933. $captcha = $this->request->request('captcha');
  934. if (!$oldcaptcha || !$mobile || !$captcha) {
  935. $this->error(__('Invalid parameters'));
  936. }
  937. if (!Validate::regex($mobile, "^1\d{10}$")) {
  938. $this->error(__('Mobile is incorrect'));
  939. }
  940. if($user->mobile == $mobile){
  941. $this->error('新手机号不能与旧手机号相同');
  942. }
  943. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  944. $this->error(__('Mobile already exist'));
  945. }
  946. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  947. if (!$result) {
  948. $this->error(__('Captcha is incorrect'));
  949. }
  950. $result = Sms::check($mobile, $captcha, 'changemobile');
  951. if (!$result) {
  952. $this->error(__('Captcha is incorrect'));
  953. }
  954. /*$verification = $user->verification;
  955. $verification->mobile = 1;
  956. $user->verification = $verification;*/
  957. $user->mobile = $mobile;
  958. $user->save();
  959. Sms::flush($user->mobile, 'changemobile');
  960. Sms::flush($mobile, 'changemobile');
  961. $this->success();
  962. }
  963. /**
  964. * 微信注册来的,绑定手机号
  965. *
  966. * @ApiMethod (POST)
  967. * @param string $mobile 手机号
  968. * @param string $captcha 验证码
  969. */
  970. /*public function bindmobile()
  971. {
  972. $user = $this->auth->getUser();
  973. $mobile = $this->request->request('mobile');
  974. $captcha = $this->request->request('captcha');
  975. if(!empty($this->auth->mobile)){
  976. $this->error('已经绑定了手机号');
  977. }
  978. if (!$mobile || !$captcha) {
  979. $this->error(__('Invalid parameters'));
  980. }
  981. if (!Validate::regex($mobile, "^1\d{10}$")) {
  982. $this->error(__('Mobile is incorrect'));
  983. }
  984. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  985. $this->error('该手机号已被其他用户绑定');
  986. }
  987. $result = Sms::check($mobile, $captcha, 'changemobile');
  988. if (!$result) {
  989. $this->error(__('Captcha is incorrect'));
  990. }
  991. $verification = $user->verification;
  992. $verification->mobile = 1;
  993. $user->verification = $verification;
  994. $user->mobile = $mobile;
  995. $user->save();
  996. Sms::flush($mobile, 'changemobile');
  997. //手机号奖励
  998. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,18);
  999. if($task_rs === false){
  1000. Db::rollback();
  1001. return false;
  1002. }
  1003. $this->success('success',$this->userInfo('return'));
  1004. }*/
  1005. /**
  1006. * 手机号注册来的,绑定微信
  1007. *
  1008. * @ApiMethod (POST)
  1009. * @param string wechat_openid 微信openid
  1010. */
  1011. /*public function bindopenid()
  1012. {
  1013. $user = $this->auth->getUser();
  1014. $wechat_openid = $this->request->request('wechat_openid');
  1015. if(!empty($this->auth->wechat_openid)){
  1016. $this->error('已经绑定了微信号');
  1017. }
  1018. if (!$wechat_openid) {
  1019. $this->error(__('Invalid parameters'));
  1020. }
  1021. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  1022. $this->error('该微信号已被其他用户绑定');
  1023. }
  1024. $user->wechat_openid = $wechat_openid;
  1025. $user->save();
  1026. $this->success('success',$this->userInfo('return'));
  1027. }*/
  1028. /**
  1029. * 修改密码
  1030. *
  1031. * @ApiMethod (POST)
  1032. * @param string $newpassword 新密码
  1033. * @param string $oldpassword 旧密码
  1034. */
  1035. public function changepwd(){
  1036. $newpassword = input('newpassword');
  1037. $oldpassword = input('oldpassword','');
  1038. if (!$newpassword) {
  1039. $this->error(__('Invalid parameters'));
  1040. }
  1041. if($this->auth->password && empty($oldpassword)){
  1042. $this->error('原密码必填');
  1043. }
  1044. if(empty($this->auth->password)){
  1045. $ret = $this->auth->changepwd($newpassword, '', true);
  1046. }else{
  1047. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  1048. }
  1049. if ($ret) {
  1050. $this->success(__('Reset password successful'));
  1051. } else {
  1052. $this->error($this->auth->getError());
  1053. }
  1054. }
  1055. /**
  1056. * 记录当前登陆的设备ID,设备信息,IP等
  1057. */
  1058. /* public function changeDeviceIp()
  1059. {
  1060. // 接口防并发
  1061. if (!$this->apiLimit(1, 5)) {
  1062. return ;
  1063. }
  1064. $user = $this->auth->getUser();
  1065. $ip = request()->ip();
  1066. $deviceId = $this->request->request('device_id','');
  1067. $phoneModel = $this->request->request('phone_model','');
  1068. $brand = $this->request->request('brand','');
  1069. $apiVersion = $this->request->request('api_version','');
  1070. $deviceOs = $this->request->request('device_os','');
  1071. if ($ip !== $user->loginip){
  1072. $update = [];
  1073. $update['id'] = $user->id;
  1074. $update['loginip'] = $ip;
  1075. \app\common\model\User::update($update);
  1076. }
  1077. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  1078. if (empty($userDeviceInfo)){
  1079. $userDeviceInfo = new UserDeviceInfo();
  1080. $userDeviceInfo->user_id = $user->u_id;
  1081. }
  1082. $userDeviceInfo->device_os = $deviceOs;
  1083. $userDeviceInfo->device_id = $deviceId;
  1084. $userDeviceInfo->phone_model = $phoneModel;
  1085. $userDeviceInfo->brand = $brand;
  1086. $userDeviceInfo->api_version = $apiVersion;
  1087. $userDeviceInfo->save();
  1088. //首页接口调用,这里不反回信息
  1089. // $this->success("更新成功!");
  1090. }*/
  1091. //修改用户活跃1
  1092. /*public function useractive(){
  1093. $this->success('success');
  1094. }*/
  1095. //公众号获取openid
  1096. /*public function getUserOpenid_gzh(){
  1097. $configValue = Service::getConfig('wechat');
  1098. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1099. $rs = $wechat->getOpenid();
  1100. $this->success('success',$rs);
  1101. }*/
  1102. /**
  1103. * 微信内H5-JSAPI支付
  1104. */
  1105. /*public function jssdkBuildConfig() {
  1106. $url = $this->request->request("url");
  1107. $configValue = Service::getConfig('wechat');
  1108. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1109. $sign = $wechat->getSignPackage(urldecode($url));
  1110. $this->success("获取成功!",$sign);
  1111. }*/
  1112. //苹果账号登录
  1113. /*public function ioslogin(){
  1114. $ios_openid = input('ios_openid','');
  1115. if (!$ios_openid) {
  1116. $this->error(__('Invalid parameters'));
  1117. }
  1118. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1119. // if (!$user) {
  1120. // $this->success('选择性别', ['code' => 5]);
  1121. // }
  1122. if ($user) {
  1123. if (!in_array($user->status,[1,2])) {
  1124. $this->error(__('Account is locked'));
  1125. }
  1126. if ($user->frozentime > time()) {
  1127. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  1128. }
  1129. //如果已经有账号则直接登录
  1130. $ret = $this->auth->direct($user->id);
  1131. } else {
  1132. $reg_data = [
  1133. 'register_from' => input('register_from',''),
  1134. 'gender' => -1
  1135. ];
  1136. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1137. }
  1138. if ($ret) {
  1139. $data = $this->userInfo('return');
  1140. $this->success(__('Logged in successful'), $data);
  1141. } else {
  1142. $this->error($this->auth->getError());
  1143. }
  1144. }*/
  1145. //苹果账号注册
  1146. /*public function iosregiter(){
  1147. $ios_openid = input('ios_openid', '', 'trim');
  1148. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  1149. if (!$ios_openid) {
  1150. $this->error(__('Invalid parameters'));
  1151. }
  1152. if (!in_array($gender, [1, 0])) {
  1153. $this->error('性别错误');
  1154. }
  1155. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1156. if ($user) {
  1157. $this->error('账号已经存在,请直接登录');
  1158. }
  1159. $reg_data = [
  1160. 'register_from' => input('register_from',''),
  1161. 'gender' => $gender
  1162. ];
  1163. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1164. if ($ret) {
  1165. $data = $this->userInfo('return');
  1166. $this->success(__('Logged in successful'), $data);
  1167. } else {
  1168. $this->error($this->auth->getError());
  1169. }
  1170. }*/
  1171. //实名认证
  1172. public function idcard_auth() {
  1173. $info = Db::name('user_idconfirm')->where(['user_id' => $this->auth->id])->find();
  1174. // if ($info && $info['status'] == 0) {
  1175. // $this->error('您已经提交信息了,请进行人脸认证!');
  1176. // }
  1177. if ($info && $info['status'] == 1) {
  1178. $this->error('您已通过审核!');
  1179. }
  1180. $nickname = input('nickname', '', 'trim'); // 姓名
  1181. $idcard = input('idcard', '', 'trim'); // 身份证号
  1182. if ($nickname === '') {
  1183. $this->error('请输入姓名');
  1184. }
  1185. if (iconv_strlen($nickname, 'utf-8') > 50) {
  1186. $this->error('请输入正确姓名');
  1187. }
  1188. if ($idcard === '') {
  1189. $this->error('请输入身份证号');
  1190. }
  1191. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1192. $this->error('请输入正确身份证号');
  1193. }
  1194. $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  1195. if ($count) {
  1196. $this->error('身份证号已存在');
  1197. }
  1198. $data = [];
  1199. $data['truename'] = $nickname;
  1200. $data['idcard'] = $idcard;
  1201. //腾讯云身份证二要素认证
  1202. $auth_restult = $this->userauth_tencent($idcard, $nickname);
  1203. if ($auth_restult) {
  1204. $data['status'] = 1; //通过
  1205. $msg = '认证通过';
  1206. } else {
  1207. $data['status'] = 2; //不通过
  1208. $msg = '认证不通过';
  1209. }
  1210. //开启事务
  1211. Db::startTrans();
  1212. if (!$info) { //未认证
  1213. $data["user_id"] = $this->auth->id;
  1214. $data["createtime"] = time();
  1215. $res = Db::name('user_idconfirm')->insertGetId($data);
  1216. } else { //认证被拒绝过
  1217. $data['updatetime'] = time();
  1218. $res = Db::name('user_idconfirm')->where(['id' => $info['id'], 'user_id' => $this->auth->id])->setField($data);
  1219. }
  1220. if (!$res) {
  1221. Db::rollback();
  1222. $this->error('认证失败');
  1223. }
  1224. $rt = Db::name('user')->where(['id' => $this->auth->id, 'idcard_status' => $this->auth->idcard_status])->setField('idcard_status', $data['status']);
  1225. if ($rt === false) {
  1226. Db::rollback();
  1227. $this->error('认证失败');
  1228. }
  1229. if ($data['status'] == 1) {
  1230. //完成实名认证 +20金币
  1231. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  1232. if($task_rs === false){
  1233. Db::rollback();
  1234. $this->error('完成任务赠送奖励失败');
  1235. }
  1236. //系统消息
  1237. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证已经审核通过');
  1238. } else {
  1239. //系统消息
  1240. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'实名认证','实名认证审核不通过');
  1241. }
  1242. Db::commit();
  1243. $this->success($msg);
  1244. }
  1245. //腾讯云身份证二要素认证
  1246. public function userauth_tencent($idcard = '', $nickname = '') {
  1247. // require_once 'vendor/autoload.php';
  1248. try {
  1249. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1250. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1251. $config = config('tencent_im');
  1252. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1253. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1254. $httpProfile = new HttpProfile();
  1255. $httpProfile->setEndpoint("faceid.tencentcloudapi.com");
  1256. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1257. $clientProfile = new ClientProfile();
  1258. $clientProfile->setHttpProfile($httpProfile);
  1259. // 实例化要请求产品的client对象,clientProfile是可选的
  1260. $client = new FaceidClient($cred, "", $clientProfile);
  1261. // 实例化一个请求对象,每个接口都会对应一个request对象
  1262. $req = new IdCardVerificationRequest();
  1263. $params = array(
  1264. "IdCard" => $idcard,
  1265. "Name" => $nickname
  1266. );
  1267. $req->fromJsonString(json_encode($params));
  1268. // 返回的resp是一个IdCardVerificationResponse的实例,与请求对象对应
  1269. $resp = $client->IdCardVerification($req);
  1270. // 输出json格式的字符串回包
  1271. // print_r($resp->toJsonString());
  1272. $result = json_decode($resp->toJsonString(), true);
  1273. if (isset($result['Result']) && $result['Result'] == 0) {
  1274. return 1; //通过
  1275. } else {
  1276. return 0;
  1277. }
  1278. }
  1279. catch(TencentCloudSDKException $e) {
  1280. // echo $e;
  1281. return 0;
  1282. }
  1283. }
  1284. //申请真人认证
  1285. public function realauth() {
  1286. if ($this->auth->real_status == 1) {
  1287. $this->error('您已经真人认证过了~');
  1288. }
  1289. if ($this->auth->avatar == config('avatar_boy') || $this->auth->avatar == config('avatar_girl')) {
  1290. $this->error('请先上传真人头像~');
  1291. }
  1292. //获取token
  1293. $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';
  1294. $token_result = file_get_contents($token_url);
  1295. if (!$token_result) {
  1296. $this->error('您的网络开小差啦1~');
  1297. }
  1298. $token_result = json_decode($token_result, true);
  1299. if ($token_result['code'] != 0) {
  1300. $this->error('您的网络开小差啦2~');
  1301. }
  1302. $token = $token_result['access_token'];
  1303. //获取签名鉴权参数ticket
  1304. $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';
  1305. $ticket_result = file_get_contents($ticket_url);
  1306. if (!$ticket_result) {
  1307. $this->error('您的网络开小差啦3~');
  1308. }
  1309. $ticket_result = json_decode($ticket_result, true);
  1310. if ($ticket_result['code'] != 0) {
  1311. $this->error('您的网络开小差啦4~');
  1312. }
  1313. $ticket = $ticket_result['tickets'][0]['value'];
  1314. //获取签名
  1315. $sign_data = [
  1316. 'wbappid' => config('tencent_yun')['secret_id'],
  1317. 'userId' => (string)$this->auth->id,
  1318. 'version' => '1.0.0',
  1319. 'ticket' => $ticket,
  1320. 'nonce' => Random::alnum(32)
  1321. ];//p($sign_data);
  1322. asort($sign_data); //p($sign_data);//排序
  1323. $sign_string = join('', $sign_data);//p($sign_string);
  1324. $sign = sha1($sign_string);//p($sign);
  1325. //上传身份信息
  1326. $orderNo = getMillisecond() . $this->auth->id . mt_rand(1, 1000); //商户请求的唯一标识
  1327. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo=' . $orderNo;
  1328. $avatar = one_domain_image($this->auth->avatar);
  1329. $avatar = str_replace('https', 'http', $avatar);
  1330. $img = file_get_contents($avatar);
  1331. $img = str_replace('data:image/jpg;base64', '', $img);
  1332. $img = str_replace('\n', '', $img);
  1333. $sourcePhotoStr = base64_encode($img);
  1334. $data = [
  1335. 'webankAppId' => config('tencent_yun')['secret_id'],
  1336. 'orderNo' => $orderNo,
  1337. 'userId' => (string)$this->auth->id,
  1338. 'sourcePhotoStr' => $sourcePhotoStr,
  1339. 'sourcePhotoType' => 2,
  1340. 'version' => '1.0.0',
  1341. 'sign' => $sign,
  1342. 'nonce' => $sign_data['nonce']
  1343. ];
  1344. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1345. if (!$rs) {
  1346. $this->error('您的网络开小差啦5~');
  1347. }
  1348. $rs = json_decode($rs, true);
  1349. if (!$rs || $rs['code'] != 0) {
  1350. $this->error('您的网络开小差啦6~');
  1351. }
  1352. $user_auth = [
  1353. 'user_id' => $this->auth->id,
  1354. 'certify_id' => $rs['result']['faceId'],
  1355. 'out_trade_no' => $data['orderNo'],
  1356. 'status' => 0,
  1357. 'createtime' => time(),
  1358. 'updatetime' => time()
  1359. ];
  1360. //开启事务
  1361. Db::startTrans();
  1362. //查询是否认证过
  1363. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1364. if ($info) {
  1365. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  1366. } else {
  1367. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  1368. }
  1369. if (!$auth_rs) {
  1370. Db::rollback();
  1371. $this->error('您的网络开小差啦7~');
  1372. }
  1373. //修改用户表认证状态
  1374. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', 0);
  1375. if ($user_rs === false) {
  1376. Db::rollback();
  1377. $this->error('您的网络开小差啦8~');
  1378. }
  1379. Db::commit();
  1380. $return_data = [
  1381. 'face_id' => $user_auth['certify_id'],
  1382. 'order_no' => $user_auth['out_trade_no'],
  1383. 'user_id' => (string)$this->auth->id,
  1384. 'nonce' => $sign_data['nonce'],
  1385. 'sign' => $sign
  1386. ];
  1387. $this->success('success', $return_data);
  1388. }
  1389. //查询真人认证结果
  1390. public function getrealauthresult() {
  1391. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  1392. if (!$user_auth) {
  1393. $this->success('尚未认证');
  1394. }
  1395. if ($user_auth['status'] == 1) {
  1396. $this->success('真人认证通过');
  1397. }
  1398. if (!$user_auth['certify_id']) {
  1399. $this->success('请先进行真人认证');
  1400. }
  1401. //获取token
  1402. $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';
  1403. $token_result = file_get_contents($token_url);
  1404. if (!$token_result) {
  1405. $this->error('您的网络开小差啦1~');
  1406. }
  1407. $token_result = json_decode($token_result, true);
  1408. if ($token_result['code'] != 0) {
  1409. $this->error('您的网络开小差啦2~');
  1410. }
  1411. $token = $token_result['access_token'];
  1412. //获取签名鉴权参数ticket
  1413. $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';
  1414. $ticket_result = file_get_contents($ticket_url);
  1415. if (!$ticket_result) {
  1416. $this->error('您的网络开小差啦3~');
  1417. }
  1418. $ticket_result = json_decode($ticket_result, true);
  1419. if ($ticket_result['code'] != 0) {
  1420. $this->error('您的网络开小差啦4~');
  1421. }
  1422. $ticket = $ticket_result['tickets'][0]['value'];
  1423. //获取签名
  1424. $sign_data = [
  1425. 'wbappid' => config('tencent_yun')['secret_id'],
  1426. 'orderNo' => $user_auth['out_trade_no'],
  1427. 'version' => '1.0.0',
  1428. 'ticket' => $ticket,
  1429. 'nonce' => Random::alnum(32)
  1430. ];//p($sign_data);
  1431. asort($sign_data); //p($sign_data);//排序
  1432. $sign_string = join('', $sign_data);//p($sign_string);
  1433. $sign = sha1($sign_string);//p($sign);
  1434. //人脸核身结果查询
  1435. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  1436. $data = [
  1437. 'appId' => config('tencent_yun')['secret_id'],
  1438. 'version' => '1.0.0',
  1439. 'nonce' => $sign_data['nonce'],
  1440. 'orderNo' => $user_auth['out_trade_no'],
  1441. 'sign' => $sign
  1442. ];
  1443. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1444. if (!$rs) {
  1445. $this->error('您的网络开小差啦5~');
  1446. }
  1447. $rs = json_decode($rs, true);
  1448. if (!$rs || $rs['code'] != 0) {
  1449. $this->error($rs['msg']);
  1450. }
  1451. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  1452. $edit_data['status'] = 1;
  1453. $msg = '真人认证成功';
  1454. } else {
  1455. $edit_data['status'] = 2;
  1456. $edit_data['certify_id'] = '';
  1457. $edit_data['out_trade_no'] = '';
  1458. $msg = '真人认证失败';
  1459. }
  1460. $edit_data['updatetime'] = time();
  1461. //开启事务
  1462. Db::startTrans();
  1463. //修改认证信息
  1464. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1465. if (!$result) {
  1466. Db::rollback();
  1467. $this->error('查询认证结果失败2');
  1468. }
  1469. //修改用户信息
  1470. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('real_status', $edit_data['status']);
  1471. if (!$rs) {
  1472. Db::rollback();
  1473. $this->error('查询认证结果失败3');
  1474. }
  1475. if ($edit_data['status'] == 1) { //通过
  1476. //tag任务赠送金币
  1477. //真人认证奖励
  1478. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1479. if($task_rs === false){
  1480. Db::rollback();
  1481. $this->error('完成任务赠送奖励失败');
  1482. }
  1483. //系统消息
  1484. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1485. } else {
  1486. //系统消息
  1487. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1488. }
  1489. Db::commit();
  1490. $this->success($msg);
  1491. }
  1492. //客服
  1493. public function kefu() {
  1494. $type = input('type', 0, 'intval'); //客服位置: 0客服中心 1充值客服
  1495. if (!in_array($type, [0, 1])) {
  1496. $this->error('您的网络开小差啦~');
  1497. }
  1498. if ($type == 0) {
  1499. $user_id = config('site.customer_service_id') ? : 0; //指定客服id
  1500. } else {
  1501. $user_id = config('site.pay_customer_service_id') ? : 0; //指定客服id
  1502. }
  1503. $list = Db::name('user')->field('id')->where(['status' => 1, 'is_kefu' => 1, 'id' => $user_id])->select();
  1504. if (!$list) {
  1505. $this->success('success', $list);
  1506. }
  1507. foreach ($list as $k => &$v) {
  1508. $v['nickname'] = '客服' . ($k + 1);
  1509. $v['avatar'] = config('avatar_girl');
  1510. }
  1511. $this->success('success', $list);
  1512. }
  1513. //海报背景图
  1514. public function posterlist() {
  1515. $list = config('site.intro_images');
  1516. if (!$list) {
  1517. $this->success('success', (object)[]);
  1518. }
  1519. foreach ($list as &$v) {
  1520. $v = config('img_url') . $this->createposter(config('site.domain_cdnurl') . $v);
  1521. }
  1522. $this->success('success', $list);
  1523. }
  1524. //生成海报
  1525. public function createposter($image = '') {
  1526. // $image = input('image', '', 'trim');
  1527. // if (!$image) {
  1528. // $this->error('您的网络开小差啦~');
  1529. // }
  1530. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode, 'background' => $image]);
  1531. return $haibao;
  1532. // $this->success('success', $haibao);
  1533. }
  1534. //真人认证后修改头像前比对
  1535. public function realavatar_auit() {
  1536. if ($this->auth->real_status != 1) {
  1537. $this->error('尚未通过真人认证');
  1538. }
  1539. $avatar = input('avatar', '', 'trim'); //头像地址
  1540. if ($avatar === '') {
  1541. $this->error('参数缺失');
  1542. }
  1543. $avatar = one_domain_image($avatar);
  1544. $now_avatar = one_domain_image($this->auth->avatar);
  1545. if ($avatar == $now_avatar) {
  1546. $this->error('头像未改变');
  1547. }
  1548. //腾讯云人脸识别
  1549. $result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1550. $this->success('结果', $result);
  1551. }
  1552. //真人认证后修改头像
  1553. public function editrealavatar() {
  1554. if ($this->auth->real_status != 1) {
  1555. $this->error('尚未通过真人认证');
  1556. }
  1557. $avatar = input('avatar', '', 'trim'); //头像地址
  1558. if ($avatar === '') {
  1559. $this->error('参数缺失');
  1560. }
  1561. $avatar = one_domain_image($avatar);
  1562. $now_avatar = one_domain_image($this->auth->avatar);
  1563. if ($avatar == $now_avatar) {
  1564. $this->error('头像未改变');
  1565. }
  1566. //腾讯云人脸识别
  1567. $auit_result = $this->face_tencent($now_avatar, $avatar); //1通过 0拒绝
  1568. if ($auit_result != 1) {
  1569. $this->success('提示', ['code' => 2]);
  1570. }
  1571. $data['avatar'] = $avatar;
  1572. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1573. if (!$user_result) {
  1574. $this->error('修改失败');
  1575. }
  1576. $this->success('修改成功');
  1577. }
  1578. //真人认证后修改头像并取消真人认证
  1579. public function editrealavatarcancelauit() {
  1580. if ($this->auth->real_status != 1) {
  1581. $this->error('尚未通过真人认证');
  1582. }
  1583. $avatar = input('avatar', '', 'trim'); //头像地址
  1584. if ($avatar === '') {
  1585. $this->error('参数缺失');
  1586. }
  1587. $avatar = one_domain_image($avatar);
  1588. $now_avatar = one_domain_image($this->auth->avatar);
  1589. if ($avatar == $now_avatar) {
  1590. $this->error('头像未改变');
  1591. }
  1592. $data['avatar'] = $avatar;
  1593. $data['real_status'] = -1;
  1594. //开启事务
  1595. Db::startTrans();
  1596. $user_result = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  1597. if (!$user_result) {
  1598. Db::rollback();
  1599. $this->error('修改失败');
  1600. }
  1601. $user_auth_result = Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  1602. if (!$user_auth_result) {
  1603. Db::rollback();
  1604. $this->error('修改失败');
  1605. }
  1606. Db::commit();
  1607. $this->success('修改成功');
  1608. }
  1609. //腾讯云人脸识别
  1610. public function face_tencent($urla = '', $urlb = '') {
  1611. // require_once 'vendor/autoload.php';
  1612. try {
  1613. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  1614. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  1615. $config = config('tencent_im');
  1616. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  1617. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  1618. $httpProfile = new HttpProfile();
  1619. $httpProfile->setEndpoint("iai.tencentcloudapi.com");
  1620. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  1621. $clientProfile = new ClientProfile();
  1622. $clientProfile->setHttpProfile($httpProfile);
  1623. // 实例化要请求产品的client对象,clientProfile是可选的
  1624. $client = new IaiClient($cred, "ap-beijing", $clientProfile);
  1625. // 实例化一个请求对象,每个接口都会对应一个request对象
  1626. $req = new CompareFaceRequest();
  1627. $params = array(
  1628. "UrlA" => $urla,
  1629. "UrlB" => $urlb,
  1630. "FaceModelVersion" => "3.0"
  1631. );
  1632. $req->fromJsonString(json_encode($params));
  1633. // 返回的resp是一个CompareFaceResponse的实例,与请求对象对应
  1634. $resp = $client->CompareFace($req);
  1635. // 输出json格式的字符串回包
  1636. // print_r($resp->toJsonString());
  1637. $result = json_decode($resp->toJsonString(), true);
  1638. //3.0版本误识率千分之一对应分数为40分,误识率万分之一对应分数为50分,误识率十万分之一对应分数为60分。 一般超过50分则可认定为同一人。
  1639. if (isset($result['Score']) && $result['Score'] >= 60) {
  1640. return 1; //通过
  1641. } else {
  1642. return 0;
  1643. }
  1644. }
  1645. catch(TencentCloudSDKException $e) {
  1646. // echo $e;
  1647. return 0;
  1648. }
  1649. }
  1650. //修改城市
  1651. public function editcity() {
  1652. $name = input('name', '', 'trim'); //城市名
  1653. if ($name === '') {
  1654. $this->error('参数缺失');
  1655. }
  1656. $hometown_cityid = Db::name('area')->where(['name' => $name])->value('id');
  1657. if (!$hometown_cityid) {
  1658. $this->success('修改成功');
  1659. }
  1660. Db::name('user')->where(['id' => $this->auth->id])->setField('hometown_cityid', $hometown_cityid);
  1661. $this->success('修改成功');
  1662. }
  1663. //搜索用户
  1664. public function searchuser() {
  1665. $keyword = input('keyword', '', 'trim'); //昵称或ID
  1666. if ($keyword === '') {
  1667. $this->error('请输入关键字');
  1668. }
  1669. $id = Db::name('user')->where(['nickname|username' => $keyword])->value('id');
  1670. $id = $id ? : 0;
  1671. $this->success('用户', $id);
  1672. }
  1673. //绑定支付宝账号信息
  1674. public function bindalipayaccount() {
  1675. $mt_user_bank = Db::name('user_bank');
  1676. $count = $mt_user_bank->where(['user_id' => $this->auth->id, 'type' => 1])->count();
  1677. if ($count) {
  1678. $this->error('您已绑定支付宝啦');
  1679. }
  1680. $auth_code = input('auth_code', '', 'trim');
  1681. if (!$auth_code) {
  1682. $this->error('授权码缺失');
  1683. }
  1684. //获取支付宝用户信息
  1685. $rs = getAlipayInfo($auth_code);
  1686. if ($rs['status'] == 0) {
  1687. $this->error($rs['info']);
  1688. }
  1689. $rs = $rs['data'];
  1690. /*
  1691. * Array(
  1692. [alipay_user_info_share_response] => Array
  1693. (
  1694. [code] => 10000
  1695. [msg] => Success
  1696. [avatar] => https://tfs.alipayobjects.com/images/partner/T1BQJsXhhbXXXXXXXX 头像
  1697. [city] => 临沂市 市名称。
  1698. [gender] => m 【注意】只有is_certified为T的时候才有意义,否则不保证准确性. 性别(F:女性;M:男性)。
  1699. [is_certified] => T 是否通过实名认证。T是通过 F是没有实名认证。
  1700. [is_student_certified] => F 是否是学生 T是 F否
  1701. [nick_name] => 风的追求 用户昵称
  1702. [province] => 山东省 省份名称
  1703. [user_id] => 2088902918001020 支付宝用户的userId
  1704. [user_status] => T 用户状态(Q/T/B/W)。 Q代表快速注册用户 T代表已认证用户 B代表被冻结账户 W代表已注册,未激活的账户
  1705. [user_type] => 2 用户类型(1/2) 1代表公司账户2代表个人账户
  1706. )
  1707. )
  1708. */
  1709. $data['banknumber'] = $rs['user_id']; //支付宝用户id
  1710. if (!$data['banknumber']) {
  1711. $this->error('获取用户信息失败');
  1712. }
  1713. $bank_number_count = $mt_user_bank->where(['banknumber' => $data['banknumber'], 'type' => 1])->count();
  1714. if ($bank_number_count) {
  1715. $this->error('该支付宝账号已经绑定用户, 请先解除绑定');
  1716. }
  1717. if (isset($rs['nick_name'])) {
  1718. $data['realname'] = $rs['nick_name']; //支付宝用户昵称
  1719. }
  1720. $data['user_id'] = $this->auth->id;
  1721. $data['createtime'] = time();
  1722. $data['type'] = 1;
  1723. $rt = $mt_user_bank->insertGetId($data);
  1724. if (!$rt) {
  1725. $this->error('绑定失败');
  1726. }
  1727. $this->success('绑定成功');
  1728. }
  1729. }