User.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 app\common\library\Wechat;
  7. use app\common\service\UserService;
  8. use fast\Random;
  9. use think\Exception;
  10. use think\Log;
  11. use think\Validate;
  12. use miniprogram\wxBizDataCrypt;
  13. use onlogin\onlogin;
  14. use think\Db;
  15. /**
  16. * 会员接口
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin','bindmobile'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 会员登录
  35. *
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->request('account');
  42. $password = $this->request->request('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $this->auth->getUserinfo()];
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. */
  60. public function mobilelogin()
  61. {
  62. $mobile = $this->request->request('mobile');
  63. $captcha = $this->request->request('captcha');
  64. if (!$mobile || !$captcha) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::regex($mobile, "^1\d{10}$")) {
  68. $this->error(__('Mobile is incorrect'));
  69. }
  70. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user) {
  75. if ($user->status == -1) {
  76. $this->error('账户已注销');
  77. }
  78. if ($user->status != 1) {
  79. $this->error(__('Account is locked'));
  80. }
  81. //如果已经有账号则直接登录
  82. $is_register = 0;
  83. $ret = $this->auth->direct($user->id);
  84. } else {
  85. $is_register = 1;
  86. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  87. }
  88. if ($ret) {
  89. Sms::flush($mobile, 'mobilelogin');
  90. $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
  91. $this->success(__('Logged in successful'), $data);
  92. } else {
  93. $this->error($this->auth->getError());
  94. }
  95. }
  96. //苹果登录+注册
  97. public function applelogin(){
  98. $iosUserId = input('ios_user_id','');
  99. if(!$iosUserId){
  100. $this->error(__('Invalid parameters'));
  101. }
  102. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  103. if ($user) {
  104. if ($user['status'] == -1) {
  105. $this->error('账户已经注销');
  106. }
  107. if ($user['status'] != 1) {
  108. $this->error(__('Account is locked'));
  109. }
  110. //如果已经有账号则直接登录
  111. $ret = $this->auth->direct($user['id']);
  112. } else {
  113. $ret = $this->auth->ios_register($iosUserId);
  114. }
  115. if ($ret) {
  116. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  117. } else {
  118. $this->error($this->auth->getError());
  119. }
  120. }
  121. /*
  122. * 修改用户的坐标
  123. * */
  124. public function change_longlat(){
  125. $longitude = input('longitude',0);
  126. $latitude = input('latitude',0);
  127. if(empty($longitude) || empty($latitude)){
  128. $this->error();
  129. }
  130. $data = [
  131. 'longitude' => $longitude,
  132. 'latitude' => $latitude,
  133. ];
  134. Db::name('user')->where('id',$this->auth->id)->update($data);
  135. $this->success();
  136. }
  137. /**
  138. * 绑定用户
  139. */
  140. public function bindUser()
  141. {
  142. $invite_no = $this->request->request('invite_no'); // 邀请码
  143. if (!$invite_no) {
  144. $this->error("请输入邀请码!");
  145. }
  146. $user_id = $this->auth->id;
  147. // 查询邀请码用户信息
  148. $inviteUserInfo = \app\common\model\User::where(["invite_no" => $invite_no])->find();
  149. if (!$inviteUserInfo) $this->error("查询不到该邀请码用户信息!");
  150. if ($inviteUserInfo->id == $user_id) $this->error("不能邀请自己哦!");
  151. if ($inviteUserInfo->is_auth != 2) $this->error("该邀请码用户尚未完成实名认证");
  152. $res = \app\common\model\User::update(["pre_userid" => $inviteUserInfo->id,"bindtime" => time()], ["id" => $user_id]);
  153. if ($res) {
  154. $this->success("恭喜,绑定成功!");
  155. } else {
  156. $this->success("网络繁忙,请稍后重试!");
  157. }
  158. }
  159. /**
  160. * 注册会员
  161. *
  162. * @param string $username 用户名
  163. * @param string $password 密码
  164. * @param string $email 邮箱
  165. * @param string $mobile 手机号
  166. * @param string $code 验证码
  167. */
  168. public function register()
  169. {
  170. $username = $this->request->request('username');
  171. $password = $this->request->request('password');
  172. $mobile = $this->request->request('mobile');
  173. $code = $this->request->request('code');
  174. if (!$username || !$password) {
  175. $this->error(__('Invalid parameters'));
  176. }
  177. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  178. $this->error(__('Mobile is incorrect'));
  179. }
  180. // $ret = Sms::check($mobile, $code, 'register');
  181. // if (!$ret) {
  182. // $this->error(__('Captcha is incorrect'));
  183. // }
  184. $ret = $this->auth->register($username, $password, $mobile, []);
  185. if ($ret) {
  186. $data = ['userinfo' => $this->auth->getUserinfo()];
  187. $this->success(__('Sign up successful'), $data);
  188. } else {
  189. $this->error($this->auth->getError());
  190. }
  191. }
  192. /**
  193. * 退出登录
  194. */
  195. public function logout()
  196. {
  197. $this->auth->logout();
  198. $this->success(__('Logout successful'));
  199. }
  200. /**
  201. * 修改会员个人信息
  202. *
  203. * @ApiMethod (POST)
  204. * @param string $avatar 头像地址
  205. * @param string $username 用户名
  206. * @param string $nickname 昵称
  207. * @param string $bio 个人简介
  208. */
  209. public function profile()
  210. {
  211. $field_array = ['avatar','nickname','desc','age_id','gender','job_id','province_id','city_id','has_info'];
  212. $data = [];
  213. foreach($field_array as $key => $field){
  214. //前端传不了post,改了
  215. if(!request()->has($field,'post')){
  216. continue;
  217. }
  218. /*if(!input('?'.$field)){
  219. continue;
  220. }*/
  221. $newone = input($field);
  222. if($field == 'avatar'){
  223. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  224. }
  225. $data[$field] = $newone;
  226. }
  227. if(empty($data)){
  228. $this->error('没有任何改变');
  229. }
  230. /*if (!empty($data['nickname']) && !empty($data['avatar']) && !empty($data['age_id']) && $data['has_info']==0) {
  231. $data['has_info'] = 1;
  232. }*/
  233. Db::startTrans();
  234. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  235. if($update_rs === false){
  236. Db::rollback();
  237. $this->error('修改资料失败');
  238. }
  239. Db::commit();
  240. $this->success();
  241. }
  242. /**
  243. * 修改手机号
  244. *
  245. * @param string $mobile 手机号
  246. * @param string $captcha 验证码
  247. */
  248. public function changemobile()
  249. {
  250. $user = $this->auth->getUser();
  251. $mobile = $this->request->request('mobile');
  252. $captcha = $this->request->request('captcha');
  253. if (!$mobile || !$captcha) {
  254. $this->error(__('Invalid parameters'));
  255. }
  256. if (!Validate::regex($mobile, "^1\d{10}$")) {
  257. $this->error(__('Mobile is incorrect'));
  258. }
  259. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  260. $this->error(__('Mobile already exists'));
  261. }
  262. $result = Sms::check($mobile, $captcha, 'changeMobile');
  263. if (!$result) {
  264. $this->error(__('Captcha is incorrect'));
  265. }
  266. $verification = $user->verification;
  267. $verification->mobile = 1;
  268. $user->verification = $verification;
  269. $user->mobile = $mobile;
  270. $user->save();
  271. Sms::flush($mobile, 'changeMobile');
  272. $this->success("手机号更换成功!");
  273. }
  274. /**
  275. * 第三方登录
  276. *
  277. * @param string $platform 平台名称
  278. * @param string $code Code码
  279. */
  280. public function third()
  281. {
  282. $url = url('user/index');
  283. $platform = $this->request->request("platform");
  284. $code = $this->request->request("code");
  285. $config = get_addon_config('third');
  286. if (!$config || !isset($config[$platform])) {
  287. $this->error(__('Invalid parameters'));
  288. }
  289. $app = new \addons\third\library\Application($config);
  290. //通过code换access_token和绑定会员
  291. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  292. if ($result) {
  293. $loginret = \addons\third\library\Service::connect($platform, $result);
  294. if ($loginret) {
  295. $data = [
  296. 'userinfo' => $this->auth->getUserinfo(),
  297. 'thirdinfo' => $result
  298. ];
  299. $this->success(__('Logged in successful'), $data);
  300. }
  301. }
  302. $this->error(__('Operation failed'), $url);
  303. }
  304. /**
  305. * 重置密码
  306. *
  307. * @param string $mobile 手机号
  308. * @param string $newpassword 新密码
  309. * @param string $captcha 验证码
  310. */
  311. public function resetpwd()
  312. {
  313. $type = $this->request->request("type");
  314. $mobile = $this->request->request("mobile");
  315. $email = $this->request->request("email");
  316. $newpassword = $this->request->request("newpassword");
  317. $captcha = $this->request->request("captcha");
  318. if (!$newpassword || !$captcha) {
  319. $this->error(__('Invalid parameters'));
  320. }
  321. if ($type == 'mobile') {
  322. if (!Validate::regex($mobile, "^1\d{10}$")) {
  323. $this->error(__('Mobile is incorrect'));
  324. }
  325. $user = \app\common\model\User::getByMobile($mobile);
  326. if (!$user) {
  327. $this->error(__('User not found'));
  328. }
  329. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  330. if (!$ret) {
  331. $this->error(__('Captcha is incorrect'));
  332. }
  333. Sms::flush($mobile, 'resetpwd');
  334. } else {
  335. if (!Validate::is($email, "email")) {
  336. $this->error(__('Email is incorrect'));
  337. }
  338. $user = \app\common\model\User::getByEmail($email);
  339. if (!$user) {
  340. $this->error(__('User not found'));
  341. }
  342. $ret = Ems::check($email, $captcha, 'resetpwd');
  343. if (!$ret) {
  344. $this->error(__('Captcha is incorrect'));
  345. }
  346. Ems::flush($email, 'resetpwd');
  347. }
  348. //模拟一次登录
  349. $this->auth->direct($user->id);
  350. $ret = $this->auth->changepwd($newpassword, '', true);
  351. if ($ret) {
  352. $this->success(__('Reset password successful'));
  353. } else {
  354. $this->error($this->auth->getError());
  355. }
  356. }
  357. /**
  358. * 设置密码
  359. * @param string $newpassword 新密码
  360. * @param string $newpassword 新密码
  361. */
  362. public function setpwd()
  363. {
  364. $params = $this->request->param();
  365. $validate = new \app\api\validate\User();
  366. $result = $validate->scene('setPwd')->check($params);
  367. if (!$result) {
  368. $this->error($validate->getError());
  369. }
  370. $ret = $this->auth->changepwd($params['password'], '', true);
  371. if ($ret) {
  372. $this->success(__('Set password successful'));
  373. } else {
  374. $this->error($this->auth->getError());
  375. }
  376. }
  377. /**
  378. * 修改密码
  379. *
  380. * @param string $mobile 手机号
  381. * @param string $newpassword 新密码
  382. * @param string $captcha 验证码
  383. */
  384. public function changepwd()
  385. {
  386. $params = $this->request->param();
  387. $validate = new \app\api\validate\User();
  388. $result = $validate->scene('changePwd')->check($params);
  389. if (!$result) {
  390. $this->error($validate->getError());
  391. }
  392. $mobile = $this->request->request("mobile");
  393. $newpassword = $this->request->request("password");
  394. $captcha = $this->request->request("captcha");
  395. $user = \app\common\model\User::getByMobile($mobile);
  396. if (!$user) {
  397. $this->error(__('User not found'));
  398. }
  399. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  400. if (!$ret) {
  401. $this->error(__('Captcha is incorrect'));
  402. }
  403. Sms::flush($mobile, 'resetpwd');
  404. $ret = $this->auth->changepwd($newpassword, '', true);
  405. if ($ret) {
  406. $this->success(__('Change password successful'));
  407. } else {
  408. $this->error($this->auth->getError());
  409. }
  410. }
  411. /**
  412. * 获取用户openid
  413. */
  414. public function getUserOpenid()
  415. {
  416. $code = $this->request->param('code');// code值
  417. if (!$code) {
  418. $this->error(__('Invalid parameters'));
  419. }
  420. $config = config("wxMiniProgram");
  421. $getopenid = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config["appid"] . "&secret=" . $config["secret"] . "&js_code=" . $code . "&grant_type=authorization_code";
  422. $openidInfo = $this->getJson($getopenid);
  423. if (!isset($openidInfo["openid"])) {
  424. $this->error("用户openid获取失败", $openidInfo);
  425. }
  426. // 获取的结果存入数据库
  427. $sessionkeyModel = new \app\common\model\UserSessionkey();
  428. if ($sessionkeyModel->where(["openid" => $openidInfo["openid"]])->find()) {
  429. $update = [];
  430. $update["sessionkey"] = $openidInfo["session_key"];
  431. $res = $sessionkeyModel->update($update, ["openid" => $openidInfo["openid"]]);
  432. } else {
  433. $insert = [];
  434. $insert["sessionkey"] = $openidInfo["session_key"];
  435. $insert["openid"] = $openidInfo["openid"];
  436. $insert["createtime"] = time();
  437. $res = $sessionkeyModel->insert($insert);
  438. }
  439. if ($res) {
  440. $this->success("获取成功!", $openidInfo);
  441. } else {
  442. $this->error("获取失败!");
  443. }
  444. }
  445. /**
  446. * 微信小程序登录
  447. */
  448. public function wxMiniProgramLogin()
  449. {
  450. $openid = $this->request->param('openid');// openid值
  451. $encryptedData = $this->request->param('encryptedData');// 加密数据
  452. $iv = $this->request->param('iv');// 加密算法
  453. $signature = $this->request->param('signature');// 签名验证
  454. $rawData = $this->request->param('rawData');// 签名验证
  455. $logintype = $this->request->param('loginType', 1);// 登录方式:1=手机号,2=微信授权openid
  456. if (!$openid || !$encryptedData || !$iv) {
  457. $this->error(__('Invalid parameters'));
  458. }
  459. $encryptedData = urldecode($encryptedData);
  460. $config = config("wxMiniProgram");
  461. // 获取openid和sessionkey
  462. $sessionkeyModel = new \app\common\model\UserSessionkey();
  463. $openidInfo = $sessionkeyModel->where(["openid" => $openid])->find();
  464. $openid = $openidInfo['openid'];
  465. $session_key = $openidInfo['sessionkey'];
  466. // // 数据签名校验
  467. // $signature2 = sha1($rawData . $session_key);
  468. // if ($signature != $signature2) {
  469. // $this->error(__('数据签名验证失败'));
  470. // }
  471. // 根据加密数据和加密算法获取用户信息
  472. $pc = new WXBizDataCrypt($config["appid"], $session_key);
  473. $data = "";
  474. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  475. if ($errCode == 0) {
  476. $data = json_decode($data, true);
  477. // 用户登录逻辑 === 开始
  478. $userModel = new \app\common\model\User();
  479. $auth = \app\common\library\Auth::instance();
  480. if ($logintype == 1) { // 手机号登录
  481. $userInfo = $userModel->where(["mobile" => $data["purePhoneNumber"]])->find();
  482. // 用户信息不存在时使用
  483. $extend = ["mobile" => $data["purePhoneNumber"]];
  484. } else { // 微信授权openid登录
  485. $userInfo = $userModel->where(["openid" => $openid])->find();
  486. // 用户信息不存在时使用
  487. $extend = [
  488. 'openid' => $data['openId'],
  489. 'nickname' => $data['nickName'],
  490. 'avatar' => $data['avatarUrl'],
  491. 'gender' => $data['gender'],
  492. ];
  493. }
  494. // 判断用户是否已经存在
  495. if ($userInfo) { // 登录
  496. $user = \app\common\model\User::get($userInfo["id"]);
  497. if (!$user) {
  498. $this->error("网络错误!请稍后重试");
  499. }
  500. $user->save(["logintime" => time()]);
  501. $res = $auth->direct($user->id);
  502. $is_register = 0;
  503. } else { // 注册
  504. // 先随机一个用户名,随后再变更为u+数字id
  505. $username = Random::alnum(20);
  506. $password = Random::alnum(6);
  507. Db::startTrans();
  508. try {
  509. // 默认注册一个会员
  510. $result = $auth->register($username, $password, "", $extend);
  511. if (!$result) {
  512. return false;
  513. }
  514. $user = $auth->getUser();
  515. $fields = ['username' => 'u' . $user->id];
  516. // 更新会员资料
  517. $user = \app\common\model\User::get($user->id);
  518. $user->save($fields);
  519. Db::commit();
  520. } catch (PDOException $e) {
  521. Db::rollback();
  522. $auth->logout();
  523. return false;
  524. }
  525. // 写入登录Cookies和Token
  526. $res = $auth->direct($user->id);
  527. $is_register = 1;
  528. }
  529. $userInfo = $auth->getUserinfo();
  530. $userInfo["is_register"] = $is_register;
  531. if ($res) {
  532. $this->success("登录成功!", $userInfo);
  533. } else {
  534. $this->error("登录失败!");
  535. }
  536. // 用户登录逻辑 === 结束
  537. } else {
  538. $this->error("解密失败!", ["code" => $errCode]);
  539. }
  540. }
  541. /**
  542. * json 请求
  543. * @param $url
  544. * @return mixed
  545. */
  546. private function getJson($url)
  547. {
  548. $ch = curl_init();
  549. curl_setopt($ch, CURLOPT_URL, $url);
  550. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  551. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  552. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  553. $output = curl_exec($ch);
  554. curl_close($ch);
  555. return json_decode($output, true);
  556. }
  557. /**
  558. * 运营商一键登录
  559. */
  560. public function onLogin()
  561. {
  562. $accessToken = $this->request->param('accessToken');// 运营商预取号获取到的token
  563. $token = $this->request->param('tokenT');// 易盾返回的token
  564. if (!$accessToken || !$token) {
  565. $this->error("参数获取失败!");
  566. }
  567. $params = array(
  568. // 运营商预取号获取到的token
  569. "accessToken" => $accessToken,
  570. // 易盾返回的token
  571. "token" => $token
  572. );
  573. // 获取密钥配置
  574. $configInfo = config("onLogin");
  575. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  576. $ret = $onlogin->check($params);
  577. // $ret = [];
  578. // $ret["code"] = 200;
  579. // $ret["msg"] = "ok";
  580. // $ret["data"] = [
  581. // "phone" => "17574504021",
  582. // "resultCode" => 0
  583. // ];
  584. if ($ret["code"] == 200) {
  585. $data = $ret["data"];
  586. $phone = $data["phone"];
  587. if (empty($phone)) {
  588. // 取号失败,建议进行二次验证,例如短信验证码
  589. $this->error("取号登录失败,请用验证码方式登录!");
  590. } else {
  591. // 取号成功, 执行登录等流程
  592. // 用户登录逻辑 === 开始
  593. $userModel = new \app\common\model\User();
  594. $auth = \app\common\library\Auth::instance();
  595. $userInfo = $userModel->where(["mobile" => $phone])->find();
  596. // 用户信息不存在时使用
  597. $extend = ["mobile" => $phone];
  598. // 判断用户是否已经存在
  599. if ($userInfo) { // 登录
  600. $user = \app\common\model\User::get($userInfo["id"]);
  601. if (!$user) {
  602. $this->error("网络错误!请稍后重试");
  603. }
  604. if ($user->status != 'normal') {
  605. $this->error(__('Account is locked'));
  606. }
  607. $user->save(["logintime" => time()]);
  608. $res = $auth->direct($user->id);
  609. $is_register = 0;
  610. } else { // 注册
  611. // 先随机一个用户名,随后再变更为u+数字id
  612. $username = Random::alnum(20);
  613. $password = Random::alnum(6);
  614. Db::startTrans();
  615. try {
  616. // 默认注册一个会员
  617. $result = $auth->register($username, $password, "", $extend);
  618. if (!$result) {
  619. return false;
  620. }
  621. $user = $auth->getUser();
  622. $fields = ['username' => 'u' . $user->id];
  623. // 更新会员资料
  624. $user = \app\common\model\User::get($user->id);
  625. $user->save($fields);
  626. Db::commit();
  627. } catch (PDOException $e) {
  628. Db::rollback();
  629. $auth->logout();
  630. return false;
  631. }
  632. // 写入登录Cookies和Token
  633. $res = $auth->direct($user->id);
  634. $is_register = 1;
  635. }
  636. $userInfo["userinfo"] = $auth->getUserinfo();
  637. $userInfo["is_register"] = $is_register;
  638. if ($res) {
  639. $this->success("登录成功!", $userInfo);
  640. } else {
  641. $this->error("登录失败!");
  642. }
  643. // 用户登录逻辑 === 结束
  644. }
  645. } else {
  646. $this->error("登录失败,请用验证码方式登录!");
  647. }
  648. }
  649. /**
  650. * 注销账号
  651. *
  652. * @param string $mobile 手机号
  653. * @param string $captcha 验证码
  654. */
  655. public function cancleUser()
  656. {
  657. $user = $this->auth->getUser();
  658. $user->status = "cancel";
  659. unset($user->power);
  660. $user->save();
  661. $this->success("账号注销成功!");
  662. }
  663. /**
  664. * 用户举报
  665. *
  666. * @param string $mobile 手机号
  667. * @param string $captcha 验证码
  668. */
  669. public function report()
  670. {
  671. $type = $this->request->param('type');// 类型描述
  672. $content = $this->request->param('content');// 内容
  673. $images = $this->request->param('images');// 图片
  674. $ruser_id = $this->request->param('ruser_id');// 被举报用户ID
  675. if (!$type || !$content || !$images || !$ruser_id) {
  676. $this->error("请完成举报内容!");
  677. }
  678. $user_id = $this->auth->id;
  679. $data = [];
  680. $data["user_id"] = $user_id;
  681. $data["ruser_id"] = $ruser_id;
  682. $data["type"] = $type;
  683. $data["content"] = $content;
  684. $data["images"] = $images;
  685. $data["createtime"] = time();
  686. $res = \app\common\model\UserReport::insert($data);
  687. if ($res) {
  688. $this->success("举报内容提交成功!");
  689. } else {
  690. $this->error("网络错误,请稍后重试!");
  691. }
  692. }
  693. /**
  694. * 获取昵称
  695. * @return string
  696. */
  697. public function getNickName()
  698. {
  699. $result['nickname'] = get_rand_nick_name();
  700. $this->success('获取成功',$result);
  701. }
  702. //微信登录
  703. public function wechatlogin(){
  704. $code = $this->request->param('code','');
  705. if(!$code){
  706. $this->error(__('Invalid parameters'));
  707. }
  708. //微信
  709. $wechat = new Wechat();
  710. $wxuserinfo = $wechat->getwxuserinfo($code);
  711. Log::error('code:'.$code.',wxuserinfo:'.json_encode($wxuserinfo));
  712. if(!$wxuserinfo){
  713. $this->error('openid获取失败');
  714. }
  715. $openid = $wxuserinfo['openid'];
  716. $user = Db::name('user')->where('openid',$openid)->find();
  717. if ($user) {
  718. if ($user['status'] == -1) {
  719. $this->error('账户已注销');
  720. }
  721. if ($user['status'] != 1) {
  722. $this->error(__('Account is locked'));
  723. }
  724. //如果已经有账号则直接登录
  725. $ret = $this->auth->direct($user['id']);
  726. $is_register = 0;
  727. $userInfo = $this->auth->getUserinfo();
  728. } else {
  729. //记录code和openid,绑定手机号的时候更新openid
  730. $wechatCodeData = [
  731. 'code' => $code,
  732. 'openid' => $openid,
  733. 'createtime' => time(),
  734. ];
  735. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  736. if (empty($wechatCode)) {
  737. Db::name('wechat_code')->insertGetId($wechatCodeData);
  738. } else {
  739. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  740. }
  741. $ret = true;
  742. $is_register = 1;
  743. $userInfo = [];
  744. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  745. $this->success('获取信息成功', $data, 2);
  746. }
  747. if ($ret) {
  748. $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
  749. $this->success(__('Logged in successful'), $data);
  750. } else {
  751. $this->error($this->auth->getError());
  752. }
  753. }
  754. //获取openid
  755. public function getopenid() {
  756. //code
  757. $code = $this->request->post('code', '', 'trim');// code值
  758. if (!$code) {
  759. $this->error(__('Invalid parameters'));
  760. }
  761. $config = config('wxMiniProgram');
  762. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  763. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  764. $openidInfo = json_decode($openidInfo,true);
  765. if(!isset($openidInfo['openid'])) {
  766. $this->error('用户openid获取失败', $openidInfo);
  767. }
  768. $user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->find();
  769. //$openidInfo['mobile'] = isset($user['mobile']) ? $user['mobile'] : '';
  770. $this->success('获取成功', $openidInfo);
  771. }
  772. //生成海报
  773. public function createposter() {
  774. // $image = input('image', '', 'trim');
  775. // if (!$image) {
  776. // $this->error('您的网络开小差啦~');
  777. // }
  778. $image = config('site.intro_imges');
  779. $haibao = $this->haibao($this->auth->id,['invite_no'=>$this->auth->invite_no, 'background' => $image]);
  780. return $haibao;
  781. // $this->success('success', $haibao);
  782. }
  783. //海报
  784. public function haibao($player_id,$data){
  785. //下载页二维码,没必要保留
  786. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  787. $params = [
  788. 'text' => $httpStr.'/index/index/download?code=' . $data['invite_no'],
  789. 'size' => 90,
  790. 'logo' => false,
  791. 'label' => false,
  792. 'padding' => 0,
  793. ];
  794. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  795. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  796. mk_dir($qrcode_path);
  797. $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
  798. $qrCode->writeFile($download_qrcode);
  799. //海报
  800. $result['url'] = $this->createhaibao($download_qrcode,$player_id,$data);
  801. $this->success('获取成功',$result);
  802. }
  803. public function createhaibao($download_qrcode,$player_id,$sub_data){
  804. //二维码
  805. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  806. $download_qrcode= $httpStr.'/'.$download_qrcode;
  807. $data = [
  808. /*[
  809. "left" => "15px",
  810. "top" => "400px",
  811. "type" => "img",
  812. "width" => "58px",
  813. "height" => "58px",
  814. "src" => one_domain_image($this->auth->avatar),
  815. ],
  816. [
  817. "left" => "81px",
  818. "top" => "400px",
  819. "type" => "nickname",
  820. "width" => "80px",
  821. "height" => "24px",
  822. "size" => "12px",
  823. "color" => "#000",
  824. "content" => $this->auth->nickname
  825. ],
  826. [
  827. "left" => "81px",
  828. "top" => "400px",
  829. "type" => "nickname",
  830. "width" => "80px",
  831. "height" => "24px",
  832. "size" => "12px",
  833. "color" => "#000",
  834. "content" => $this->auth->invite_no
  835. ],*/
  836. [
  837. "left" => "18px",
  838. "top" => "410px",
  839. "type" => "text",
  840. "width" => "80px",
  841. "height" => "24px",
  842. "size" => "16px",
  843. "color" => "#123354",
  844. "content" => 'GG语音'
  845. ],
  846. [
  847. "left" => "18px",
  848. "top" => "445px",
  849. "type" => "text",
  850. "width" => "80px",
  851. "height" => "24px",
  852. "size" => "10px",
  853. "color" => "#123354",
  854. "content" => '更多现金奖励等你来拿'
  855. ],
  856. [
  857. "left" => "210px",
  858. "top" => "385px",
  859. "type" => "img",
  860. "width" => "95px",
  861. "height" => "95px",
  862. "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  863. ]
  864. ];
  865. $data = json_encode($data, 320);
  866. $poster = [
  867. 'id' => $player_id,
  868. 'title' => 'GG语音',
  869. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  870. 'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
  871. 'data' => $data,
  872. 'status' => 'normal',
  873. 'weigh' => 0,
  874. 'createtime' => 1653993709,
  875. 'updatetime' => 1653994259,
  876. ];
  877. $image = new \addons\poster\library\Image();
  878. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  879. if (!$imgurl) {
  880. $this->error('生成海报出错');
  881. }
  882. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  883. return $httpStr.'/' . $imgurl;
  884. }
  885. //申请真人认证
  886. public function realauth()
  887. {
  888. try {
  889. $realName = $this->request->param('real_name','');
  890. $idCard = $this->request->param('id_card','');
  891. if ($this->auth->is_auth == 2) {
  892. $this->error('您已经真人认证过了~');
  893. }
  894. if (empty($realName)) {
  895. throw new Exception('请输入姓名');
  896. }
  897. if (empty($idCard)) {
  898. throw new Exception('请输入身份证号');
  899. }
  900. $userAuthWhere['user_id'] = ['neq',$this->auth->id];
  901. $userAuthWhere['idcard'] = $idCard;
  902. // 获取用户已经实名认证的数量
  903. $authenticatedCount = model('UserAuth')->where(['idcard' => $idCard])->count();
  904. // 如果已经实名认证的数量不超过3个
  905. if ($authenticatedCount > 3) {
  906. throw new Exception('您当前身份证号已经认证过3次');
  907. }
  908. /*$userAuth = model('UserAuth')->where($userAuthWhere)->find();
  909. if (!empty($userAuth)) {
  910. throw new Exception('该身份证号已被其他用户实名验证过');
  911. }*/
  912. /*$userService = new UserService();
  913. $faceParams = [
  914. 'real_name' => $realName,
  915. 'id_card' => $idCard,
  916. 'user_id' => $this->auth->id,
  917. ];
  918. $res = $userService->faceAuth($faceParams);
  919. if (!$res['status']) {
  920. $this->error('您的网络开小差啦5~');
  921. }
  922. $rs = $res['data'];
  923. if (!$rs || $rs['code'] != 0) {
  924. $this->error('您的网络开小差啦6~');
  925. }
  926. $user_auth = [
  927. 'user_id' => $this->auth->id,
  928. 'realname' => $realName,
  929. 'idcard' => $idCard,
  930. 'certify_id' => isset($rs['result']['faceId']) ? $rs['result']['faceId'] : '',
  931. 'out_trade_no' => isset($rs['result']['orderNo']) ? $rs['result']['orderNo'] : '',
  932. 'status' => 0,
  933. 'createtime' => time(),
  934. 'updatetime' => time()
  935. ];*/
  936. $userService = new UserService();
  937. $aliParams = [
  938. 'id_card' => $idCard,
  939. 'real_name' => $realName,
  940. ];
  941. $aliCheckRes = $userService->aliCheck($aliParams);
  942. if (!$aliCheckRes['status']) {
  943. throw new Exception($aliCheckRes['msg']);
  944. }
  945. $user_auth = [
  946. 'user_id' => $this->auth->id,
  947. 'realname' => $realName,
  948. 'idcard' => $idCard,
  949. 'certify_id' => '',
  950. 'out_trade_no' => '',
  951. 'status' => 1,
  952. 'createtime' => time(),
  953. ];
  954. //开启事务
  955. Db::startTrans();
  956. //查询是否认证过
  957. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  958. if ($info) {
  959. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  960. } else {
  961. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  962. }
  963. if (!$auth_rs) {
  964. Db::rollback();
  965. $this->error('您的网络开小差啦7~');
  966. }
  967. //修改用户表认证状态
  968. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', 2);
  969. if ($user_rs === false) {
  970. Db::rollback();
  971. $this->error('您的网络开小差啦8~');
  972. }
  973. Db::commit();
  974. /*$return_data = [
  975. 'face_id' => $user_auth['certify_id'],
  976. 'order_no' => $user_auth['out_trade_no'],
  977. 'user_id' => (string)$this->auth->id,
  978. 'nonce' => $rs['nonce'],
  979. 'sign' => $rs['sign'],
  980. ];*/
  981. $this->success('验证成功');
  982. } catch (Exception $e) {
  983. $this->error($e->getMessage());
  984. }
  985. }
  986. //查询真人认证结果
  987. public function getrealauthresult() {
  988. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  989. if (!$user_auth) {
  990. $this->success('尚未认证');
  991. }
  992. if ($user_auth['status'] == 1) {
  993. $this->success('真人认证通过');
  994. }
  995. if (!$user_auth['certify_id']) {
  996. $this->success('请先进行真人认证');
  997. }
  998. $tencentConfig = config('tencent_yun');
  999. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  1000. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  1001. //获取token
  1002. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  1003. $token_result = file_get_contents($token_url);
  1004. if (!$token_result) {
  1005. $this->error('您的网络开小差啦1~');
  1006. }
  1007. $token_result = json_decode($token_result, true);
  1008. if ($token_result['code'] != 0) {
  1009. $this->error('您的网络开小差啦2~');
  1010. }
  1011. $token = $token_result['access_token'];
  1012. //获取签名鉴权参数ticket
  1013. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  1014. $ticket_result = file_get_contents($ticket_url);
  1015. if (!$ticket_result) {
  1016. $this->error('您的网络开小差啦3~');
  1017. }
  1018. $ticket_result = json_decode($ticket_result, true);
  1019. if ($ticket_result['code'] != 0) {
  1020. $this->error('您的网络开小差啦4~');
  1021. }
  1022. $ticket = $ticket_result['tickets'][0]['value'];
  1023. //获取签名
  1024. $sign_data = [
  1025. 'wbappid' => $sercrtId,
  1026. 'orderNo' => $user_auth['out_trade_no'],
  1027. 'version' => '1.0.0',
  1028. 'ticket' => $ticket,
  1029. 'nonce' => Random::alnum(32)
  1030. ];//p($sign_data);
  1031. asort($sign_data); //p($sign_data);//排序
  1032. $sign_string = join('', $sign_data);//p($sign_string);
  1033. $sign = sha1($sign_string);//p($sign);
  1034. //人脸核身结果查询
  1035. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  1036. $data = [
  1037. 'appId' => $sercrtId,
  1038. 'version' => '1.0.0',
  1039. 'nonce' => $sign_data['nonce'],
  1040. 'orderNo' => $user_auth['out_trade_no'],
  1041. 'sign' => $sign
  1042. ];
  1043. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  1044. if (!$rs) {
  1045. $this->error('您的网络开小差啦5~');
  1046. }
  1047. $rs = json_decode($rs, true);
  1048. if (!$rs || $rs['code'] != 0) {
  1049. $this->error($rs['msg']);
  1050. }
  1051. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  1052. $edit_data['status'] = 2;
  1053. $msg = '真人认证成功';
  1054. } else {
  1055. $edit_data['status'] = -1;
  1056. $edit_data['certify_id'] = '';
  1057. $edit_data['out_trade_no'] = '';
  1058. $msg = '真人认证失败';
  1059. }
  1060. $edit_data['updatetime'] = time();
  1061. //开启事务
  1062. Db::startTrans();
  1063. //修改认证信息
  1064. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1065. if (!$result) {
  1066. Db::rollback();
  1067. $this->error('查询认证结果失败2');
  1068. }
  1069. //修改用户信息
  1070. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', $edit_data['status']);
  1071. if (!$rs) {
  1072. Db::rollback();
  1073. $this->error('查询认证结果失败3');
  1074. }
  1075. if ($edit_data['status'] == 2) { //通过
  1076. //tag任务赠送金币
  1077. //真人认证奖励
  1078. /*$task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1079. if($task_rs === false){
  1080. Db::rollback();
  1081. $this->error('完成任务赠送奖励失败');
  1082. }*/
  1083. //系统消息
  1084. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1085. } else {
  1086. //系统消息
  1087. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1088. }
  1089. Db::commit();
  1090. $this->success($msg);
  1091. }
  1092. /**
  1093. * 微信注册来的,绑定手机号
  1094. *
  1095. * @ApiMethod (POST)
  1096. * @param string $mobile 手机号
  1097. * @param string $captcha 验证码
  1098. */
  1099. public function bindmobile()
  1100. {
  1101. Db::startTrans();
  1102. try {
  1103. $code = $this->request->param('code');
  1104. $mobile = $this->request->param('mobile');
  1105. $captcha = $this->request->param('captcha');
  1106. if (!$mobile || !$captcha || !$code) {
  1107. throw new Exception(__('Invalid parameters'));
  1108. }
  1109. if (!Validate::regex($mobile, "^1\d{10}$")) {
  1110. throw new Exception(__('Mobile is incorrect'));
  1111. }
  1112. $result = Sms::check($mobile, $captcha, 'changemobile');
  1113. if (!$result) {
  1114. throw new Exception(__('Captcha is incorrect'));
  1115. }
  1116. $where['mobile'] = $mobile;
  1117. $userData = model('User')->where($where)->find();//老用户
  1118. $wechatCodeWhere['code'] = $code;
  1119. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  1120. if (empty($wechatCode)) {
  1121. throw new Exception('请先微信登录');
  1122. }
  1123. if (!empty($userData)) {
  1124. if (empty($userData['openid'])) {
  1125. model('User')->update(['openid' => $wechatCode['openid']],$where);//老用户更新openid
  1126. } else {
  1127. if ($userData['openid'] != $wechatCode['openid']) {
  1128. throw new Exception('该手机号已被其他用户绑定');
  1129. }
  1130. }
  1131. $ret = $this->auth->direct($userData['id']);
  1132. } else {
  1133. $extend = [
  1134. 'openid' => $wechatCode['openid'],
  1135. ];
  1136. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
  1137. }
  1138. if (!$ret) {
  1139. throw new Exception($this->auth->getError());
  1140. }
  1141. Sms::flush($mobile, 'changemobile');
  1142. Db::commit();
  1143. $this->success('success',$this->userInfo('return'));
  1144. } catch (Exception $e) {
  1145. Db::rollback();
  1146. $this->error($e->getMessage());
  1147. }
  1148. }
  1149. /**
  1150. * 手机注册来的,绑定微信
  1151. *
  1152. * @ApiMethod (POST)
  1153. * @param string $wechat_openid
  1154. */
  1155. public function bindopenid()
  1156. {
  1157. Db::startTrans();
  1158. try {
  1159. $code = $this->request->param('code','');
  1160. if(!$code){
  1161. throw new Exception(__('Invalid parameters'));
  1162. }
  1163. //微信
  1164. $wechat = new Wechat();
  1165. $openid = $wechat->getOpenid($code);
  1166. if(!$openid){
  1167. throw new Exception('openid获取失败');
  1168. }
  1169. $user = model('User')->find($this->auth->id);
  1170. if(!empty($user['openid']) && $openid != $user['openid']){
  1171. throw new Exception('已经绑定了微信号');
  1172. }
  1173. $otherUserWhere['openid'] = $openid;
  1174. $otherUserWhere['id'] = ['neq',$this->auth->id];
  1175. $otherUser = model('User')->where($otherUserWhere)->find();
  1176. if (!empty($otherUser)) {
  1177. throw new Exception('该微信已被其他用户绑定过');
  1178. }
  1179. $user->openid = $openid;
  1180. $userRes = $user->save();
  1181. if (!$userRes) {
  1182. throw new Exception('绑定微信失败');
  1183. }
  1184. Db::commit();
  1185. $this->success('success',$this->userInfo('return'));
  1186. } catch (Exception $e) {
  1187. Db::rollback();
  1188. $this->error($e->getMessage());
  1189. }
  1190. }
  1191. //用户详细资料
  1192. public function userInfo($type = 1){
  1193. $info = $this->auth->getUserinfo();
  1194. if($type == 'return'){
  1195. return $info;
  1196. }
  1197. $this->success(__('success'),$info);
  1198. }
  1199. /**
  1200. * 微信解绑
  1201. * @return void
  1202. */
  1203. public function clearopenid()
  1204. {
  1205. try {
  1206. $userId = $this->auth->id;
  1207. $where['id'] = $userId;
  1208. $data['openid'] = '';
  1209. $user = model('User')->update($data,$where);
  1210. $this->success('解绑成功');
  1211. } catch (Exception $e) {
  1212. $this->error($e->getMessage());
  1213. }
  1214. }
  1215. }