User.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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\service\UserService;
  7. use fast\Random;
  8. use think\Exception;
  9. use think\Validate;
  10. use miniprogram\wxBizDataCrypt;
  11. use onlogin\onlogin;
  12. use think\Db;
  13. /**
  14. * 会员接口
  15. */
  16. class User extends Api
  17. {
  18. protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin'];
  19. protected $noNeedRight = '*';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. }
  24. /**
  25. * 会员中心
  26. */
  27. public function index()
  28. {
  29. $this->success('', ['welcome' => $this->auth->nickname]);
  30. }
  31. /**
  32. * 会员登录
  33. *
  34. * @param string $account 账号
  35. * @param string $password 密码
  36. */
  37. public function login()
  38. {
  39. $account = $this->request->request('account');
  40. $password = $this->request->request('password');
  41. if (!$account || !$password) {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. $ret = $this->auth->login($account, $password);
  45. if ($ret) {
  46. $data = ['userinfo' => $this->auth->getUserinfo()];
  47. $this->success(__('Logged in successful'), $data);
  48. } else {
  49. $this->error($this->auth->getError());
  50. }
  51. }
  52. /**
  53. * 手机验证码登录
  54. *
  55. * @param string $mobile 手机号
  56. * @param string $captcha 验证码
  57. */
  58. public function mobilelogin()
  59. {
  60. $mobile = $this->request->request('mobile');
  61. $captcha = $this->request->request('captcha');
  62. if (!$mobile || !$captcha) {
  63. $this->error(__('Invalid parameters'));
  64. }
  65. if (!Validate::regex($mobile, "^1\d{10}$")) {
  66. $this->error(__('Mobile is incorrect'));
  67. }
  68. if (!Sms::check($mobile, $captcha, 'mobilelogin') && $captcha != '1212') {
  69. $this->error(__('Captcha is incorrect'));
  70. }
  71. $user = \app\common\model\User::getByMobile($mobile);
  72. if ($user) {
  73. if ($user->status != 'normal') {
  74. $this->error(__('Account is locked'));
  75. }
  76. //如果已经有账号则直接登录
  77. $is_register = 0;
  78. $ret = $this->auth->direct($user->id);
  79. } else {
  80. $is_register = 1;
  81. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  82. }
  83. if ($ret) {
  84. Sms::flush($mobile, 'mobilelogin');
  85. $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  90. }
  91. /**
  92. * 绑定用户
  93. */
  94. public function bindUser()
  95. {
  96. $invite_no = $this->request->request('invite_no'); // 邀请码
  97. if (!$invite_no) {
  98. $this->error("请输入邀请码!");
  99. }
  100. $user_id = $this->auth->id;
  101. // 查询邀请码用户信息
  102. $inviteUserInfo = \app\common\model\User::where(["invite_no" => $invite_no])->find();
  103. if (!$inviteUserInfo) $this->error("查询不到该邀请码用户信息!");
  104. if ($inviteUserInfo->id == $user_id) $this->error("不能邀请自己哦!");
  105. if ($inviteUserInfo->is_auth != 2) $this->error("该邀请码用户尚未完成实名认证");
  106. $res = \app\common\model\User::update(["pre_userid" => $inviteUserInfo->id,"bindtime" => time()], ["id" => $user_id]);
  107. if ($res) {
  108. $this->success("恭喜,绑定成功!");
  109. } else {
  110. $this->success("网络繁忙,请稍后重试!");
  111. }
  112. }
  113. /**
  114. * 注册会员
  115. *
  116. * @param string $username 用户名
  117. * @param string $password 密码
  118. * @param string $email 邮箱
  119. * @param string $mobile 手机号
  120. * @param string $code 验证码
  121. */
  122. public function register()
  123. {
  124. $username = $this->request->request('username');
  125. $password = $this->request->request('password');
  126. $mobile = $this->request->request('mobile');
  127. $code = $this->request->request('code');
  128. if (!$username || !$password) {
  129. $this->error(__('Invalid parameters'));
  130. }
  131. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  132. $this->error(__('Mobile is incorrect'));
  133. }
  134. // $ret = Sms::check($mobile, $code, 'register');
  135. // if (!$ret) {
  136. // $this->error(__('Captcha is incorrect'));
  137. // }
  138. $ret = $this->auth->register($username, $password, $mobile, []);
  139. if ($ret) {
  140. $data = ['userinfo' => $this->auth->getUserinfo()];
  141. $this->success(__('Sign up successful'), $data);
  142. } else {
  143. $this->error($this->auth->getError());
  144. }
  145. }
  146. /**
  147. * 退出登录
  148. */
  149. public function logout()
  150. {
  151. $this->auth->logout();
  152. $this->success(__('Logout successful'));
  153. }
  154. /**
  155. * 修改会员个人信息
  156. *
  157. * @param string $avatar 头像地址
  158. * @param string $username 用户名
  159. * @param string $nickname 昵称
  160. * @param string $bio 个人简介
  161. */
  162. public function profile()
  163. {
  164. $user = $this->auth->getUser();
  165. $username = $this->request->request('username');
  166. $nickname = $this->request->request('nickname');
  167. $bio = $this->request->request('bio');
  168. $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
  169. if ($username) {
  170. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  171. if ($exists) {
  172. $this->error(__('Username already exists'));
  173. }
  174. $user->username = $username;
  175. }
  176. if ($nickname) {
  177. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  178. if ($exists) {
  179. $this->error(__('Nickname already exists'));
  180. }
  181. $user->nickname = $nickname;
  182. }
  183. $user->bio = $bio;
  184. $user->avatar = $avatar;
  185. $user->save();
  186. $this->success();
  187. }
  188. /**
  189. * 修改手机号
  190. *
  191. * @param string $mobile 手机号
  192. * @param string $captcha 验证码
  193. */
  194. public function changemobile()
  195. {
  196. $user = $this->auth->getUser();
  197. $mobile = $this->request->request('mobile');
  198. $captcha = $this->request->request('captcha');
  199. if (!$mobile || !$captcha) {
  200. $this->error(__('Invalid parameters'));
  201. }
  202. if (!Validate::regex($mobile, "^1\d{10}$")) {
  203. $this->error(__('Mobile is incorrect'));
  204. }
  205. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  206. $this->error(__('Mobile already exists'));
  207. }
  208. $result = Sms::check($mobile, $captcha, 'changeMobile');
  209. if (!$result) {
  210. $this->error(__('Captcha is incorrect'));
  211. }
  212. $verification = $user->verification;
  213. $verification->mobile = 1;
  214. $user->verification = $verification;
  215. $user->mobile = $mobile;
  216. $user->save();
  217. Sms::flush($mobile, 'changeMobile');
  218. $this->success("手机号更换成功!");
  219. }
  220. /**
  221. * 第三方登录
  222. *
  223. * @param string $platform 平台名称
  224. * @param string $code Code码
  225. */
  226. public function third()
  227. {
  228. $url = url('user/index');
  229. $platform = $this->request->request("platform");
  230. $code = $this->request->request("code");
  231. $config = get_addon_config('third');
  232. if (!$config || !isset($config[$platform])) {
  233. $this->error(__('Invalid parameters'));
  234. }
  235. $app = new \addons\third\library\Application($config);
  236. //通过code换access_token和绑定会员
  237. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  238. if ($result) {
  239. $loginret = \addons\third\library\Service::connect($platform, $result);
  240. if ($loginret) {
  241. $data = [
  242. 'userinfo' => $this->auth->getUserinfo(),
  243. 'thirdinfo' => $result
  244. ];
  245. $this->success(__('Logged in successful'), $data);
  246. }
  247. }
  248. $this->error(__('Operation failed'), $url);
  249. }
  250. /**
  251. * 重置密码
  252. *
  253. * @param string $mobile 手机号
  254. * @param string $newpassword 新密码
  255. * @param string $captcha 验证码
  256. */
  257. public function resetpwd()
  258. {
  259. $type = $this->request->request("type");
  260. $mobile = $this->request->request("mobile");
  261. $email = $this->request->request("email");
  262. $newpassword = $this->request->request("newpassword");
  263. $captcha = $this->request->request("captcha");
  264. if (!$newpassword || !$captcha) {
  265. $this->error(__('Invalid parameters'));
  266. }
  267. if ($type == 'mobile') {
  268. if (!Validate::regex($mobile, "^1\d{10}$")) {
  269. $this->error(__('Mobile is incorrect'));
  270. }
  271. $user = \app\common\model\User::getByMobile($mobile);
  272. if (!$user) {
  273. $this->error(__('User not found'));
  274. }
  275. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  276. if (!$ret) {
  277. $this->error(__('Captcha is incorrect'));
  278. }
  279. Sms::flush($mobile, 'resetpwd');
  280. } else {
  281. if (!Validate::is($email, "email")) {
  282. $this->error(__('Email is incorrect'));
  283. }
  284. $user = \app\common\model\User::getByEmail($email);
  285. if (!$user) {
  286. $this->error(__('User not found'));
  287. }
  288. $ret = Ems::check($email, $captcha, 'resetpwd');
  289. if (!$ret) {
  290. $this->error(__('Captcha is incorrect'));
  291. }
  292. Ems::flush($email, 'resetpwd');
  293. }
  294. //模拟一次登录
  295. $this->auth->direct($user->id);
  296. $ret = $this->auth->changepwd($newpassword, '', true);
  297. if ($ret) {
  298. $this->success(__('Reset password successful'));
  299. } else {
  300. $this->error($this->auth->getError());
  301. }
  302. }
  303. /**
  304. * 设置密码
  305. * @param string $newpassword 新密码
  306. * @param string $newpassword 新密码
  307. */
  308. public function setpwd()
  309. {
  310. $params = $this->request->param();
  311. $validate = new \app\api\validate\User();
  312. $result = $validate->scene('setPwd')->check($params);
  313. if (!$result) {
  314. $this->error($validate->getError());
  315. }
  316. $ret = $this->auth->changepwd($params['password'], '', true);
  317. if ($ret) {
  318. $this->success(__('Set password successful'));
  319. } else {
  320. $this->error($this->auth->getError());
  321. }
  322. }
  323. /**
  324. * 修改密码
  325. *
  326. * @param string $mobile 手机号
  327. * @param string $newpassword 新密码
  328. * @param string $captcha 验证码
  329. */
  330. public function changepwd()
  331. {
  332. $params = $this->request->param();
  333. $validate = new \app\api\validate\User();
  334. $result = $validate->scene('changePwd')->check($params);
  335. if (!$result) {
  336. $this->error($validate->getError());
  337. }
  338. $mobile = $this->request->request("mobile");
  339. $newpassword = $this->request->request("password");
  340. $captcha = $this->request->request("captcha");
  341. $user = \app\common\model\User::getByMobile($mobile);
  342. if (!$user) {
  343. $this->error(__('User not found'));
  344. }
  345. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  346. if (!$ret) {
  347. $this->error(__('Captcha is incorrect'));
  348. }
  349. Sms::flush($mobile, 'resetpwd');
  350. $ret = $this->auth->changepwd($newpassword, '', true);
  351. if ($ret) {
  352. $this->success(__('Change password successful'));
  353. } else {
  354. $this->error($this->auth->getError());
  355. }
  356. }
  357. /**
  358. * 获取用户openid
  359. */
  360. public function getUserOpenid()
  361. {
  362. $code = $this->request->param('code');// code值
  363. if (!$code) {
  364. $this->error(__('Invalid parameters'));
  365. }
  366. $config = config("wxMiniProgram");
  367. $getopenid = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config["appid"] . "&secret=" . $config["secret"] . "&js_code=" . $code . "&grant_type=authorization_code";
  368. $openidInfo = $this->getJson($getopenid);
  369. if (!isset($openidInfo["openid"])) {
  370. $this->error("用户openid获取失败", $openidInfo);
  371. }
  372. // 获取的结果存入数据库
  373. $sessionkeyModel = new \app\common\model\UserSessionkey();
  374. if ($sessionkeyModel->where(["openid" => $openidInfo["openid"]])->find()) {
  375. $update = [];
  376. $update["sessionkey"] = $openidInfo["session_key"];
  377. $res = $sessionkeyModel->update($update, ["openid" => $openidInfo["openid"]]);
  378. } else {
  379. $insert = [];
  380. $insert["sessionkey"] = $openidInfo["session_key"];
  381. $insert["openid"] = $openidInfo["openid"];
  382. $insert["createtime"] = time();
  383. $res = $sessionkeyModel->insert($insert);
  384. }
  385. if ($res) {
  386. $this->success("获取成功!", $openidInfo);
  387. } else {
  388. $this->error("获取失败!");
  389. }
  390. }
  391. /**
  392. * 微信小程序登录
  393. */
  394. public function wxMiniProgramLogin()
  395. {
  396. $openid = $this->request->param('openid');// openid值
  397. $encryptedData = $this->request->param('encryptedData');// 加密数据
  398. $iv = $this->request->param('iv');// 加密算法
  399. $signature = $this->request->param('signature');// 签名验证
  400. $rawData = $this->request->param('rawData');// 签名验证
  401. $logintype = $this->request->param('loginType', 1);// 登录方式:1=手机号,2=微信授权openid
  402. if (!$openid || !$encryptedData || !$iv) {
  403. $this->error(__('Invalid parameters'));
  404. }
  405. $encryptedData = urldecode($encryptedData);
  406. $config = config("wxMiniProgram");
  407. // 获取openid和sessionkey
  408. $sessionkeyModel = new \app\common\model\UserSessionkey();
  409. $openidInfo = $sessionkeyModel->where(["openid" => $openid])->find();
  410. $openid = $openidInfo['openid'];
  411. $session_key = $openidInfo['sessionkey'];
  412. // // 数据签名校验
  413. // $signature2 = sha1($rawData . $session_key);
  414. // if ($signature != $signature2) {
  415. // $this->error(__('数据签名验证失败'));
  416. // }
  417. // 根据加密数据和加密算法获取用户信息
  418. $pc = new WXBizDataCrypt($config["appid"], $session_key);
  419. $data = "";
  420. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  421. if ($errCode == 0) {
  422. $data = json_decode($data, true);
  423. // 用户登录逻辑 === 开始
  424. $userModel = new \app\common\model\User();
  425. $auth = \app\common\library\Auth::instance();
  426. if ($logintype == 1) { // 手机号登录
  427. $userInfo = $userModel->where(["mobile" => $data["purePhoneNumber"]])->find();
  428. // 用户信息不存在时使用
  429. $extend = ["mobile" => $data["purePhoneNumber"]];
  430. } else { // 微信授权openid登录
  431. $userInfo = $userModel->where(["openid" => $openid])->find();
  432. // 用户信息不存在时使用
  433. $extend = [
  434. 'openid' => $data['openId'],
  435. 'nickname' => $data['nickName'],
  436. 'avatar' => $data['avatarUrl'],
  437. 'gender' => $data['gender'],
  438. ];
  439. }
  440. // 判断用户是否已经存在
  441. if ($userInfo) { // 登录
  442. $user = \app\common\model\User::get($userInfo["id"]);
  443. if (!$user) {
  444. $this->error("网络错误!请稍后重试");
  445. }
  446. $user->save(["logintime" => time()]);
  447. $res = $auth->direct($user->id);
  448. $is_register = 0;
  449. } else { // 注册
  450. // 先随机一个用户名,随后再变更为u+数字id
  451. $username = Random::alnum(20);
  452. $password = Random::alnum(6);
  453. Db::startTrans();
  454. try {
  455. // 默认注册一个会员
  456. $result = $auth->register($username, $password, "", $extend);
  457. if (!$result) {
  458. return false;
  459. }
  460. $user = $auth->getUser();
  461. $fields = ['username' => 'u' . $user->id];
  462. // 更新会员资料
  463. $user = \app\common\model\User::get($user->id);
  464. $user->save($fields);
  465. Db::commit();
  466. } catch (PDOException $e) {
  467. Db::rollback();
  468. $auth->logout();
  469. return false;
  470. }
  471. // 写入登录Cookies和Token
  472. $res = $auth->direct($user->id);
  473. $is_register = 1;
  474. }
  475. $userInfo = $auth->getUserinfo();
  476. $userInfo["is_register"] = $is_register;
  477. if ($res) {
  478. $this->success("登录成功!", $userInfo);
  479. } else {
  480. $this->error("登录失败!");
  481. }
  482. // 用户登录逻辑 === 结束
  483. } else {
  484. $this->error("解密失败!", ["code" => $errCode]);
  485. }
  486. }
  487. /**
  488. * json 请求
  489. * @param $url
  490. * @return mixed
  491. */
  492. private function getJson($url)
  493. {
  494. $ch = curl_init();
  495. curl_setopt($ch, CURLOPT_URL, $url);
  496. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  497. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  498. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  499. $output = curl_exec($ch);
  500. curl_close($ch);
  501. return json_decode($output, true);
  502. }
  503. /**
  504. * 运营商一键登录
  505. */
  506. public function onLogin()
  507. {
  508. $accessToken = $this->request->param('accessToken');// 运营商预取号获取到的token
  509. $token = $this->request->param('tokenT');// 易盾返回的token
  510. if (!$accessToken || !$token) {
  511. $this->error("参数获取失败!");
  512. }
  513. $params = array(
  514. // 运营商预取号获取到的token
  515. "accessToken" => $accessToken,
  516. // 易盾返回的token
  517. "token" => $token
  518. );
  519. // 获取密钥配置
  520. $configInfo = config("onLogin");
  521. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  522. $ret = $onlogin->check($params);
  523. // $ret = [];
  524. // $ret["code"] = 200;
  525. // $ret["msg"] = "ok";
  526. // $ret["data"] = [
  527. // "phone" => "17574504021",
  528. // "resultCode" => 0
  529. // ];
  530. if ($ret["code"] == 200) {
  531. $data = $ret["data"];
  532. $phone = $data["phone"];
  533. if (empty($phone)) {
  534. // 取号失败,建议进行二次验证,例如短信验证码
  535. $this->error("取号登录失败,请用验证码方式登录!");
  536. } else {
  537. // 取号成功, 执行登录等流程
  538. // 用户登录逻辑 === 开始
  539. $userModel = new \app\common\model\User();
  540. $auth = \app\common\library\Auth::instance();
  541. $userInfo = $userModel->where(["mobile" => $phone])->find();
  542. // 用户信息不存在时使用
  543. $extend = ["mobile" => $phone];
  544. // 判断用户是否已经存在
  545. if ($userInfo) { // 登录
  546. $user = \app\common\model\User::get($userInfo["id"]);
  547. if (!$user) {
  548. $this->error("网络错误!请稍后重试");
  549. }
  550. if ($user->status != 'normal') {
  551. $this->error(__('Account is locked'));
  552. }
  553. $user->save(["logintime" => time()]);
  554. $res = $auth->direct($user->id);
  555. $is_register = 0;
  556. } else { // 注册
  557. // 先随机一个用户名,随后再变更为u+数字id
  558. $username = Random::alnum(20);
  559. $password = Random::alnum(6);
  560. Db::startTrans();
  561. try {
  562. // 默认注册一个会员
  563. $result = $auth->register($username, $password, "", $extend);
  564. if (!$result) {
  565. return false;
  566. }
  567. $user = $auth->getUser();
  568. $fields = ['username' => 'u' . $user->id];
  569. // 更新会员资料
  570. $user = \app\common\model\User::get($user->id);
  571. $user->save($fields);
  572. Db::commit();
  573. } catch (PDOException $e) {
  574. Db::rollback();
  575. $auth->logout();
  576. return false;
  577. }
  578. // 写入登录Cookies和Token
  579. $res = $auth->direct($user->id);
  580. $is_register = 1;
  581. }
  582. $userInfo["userinfo"] = $auth->getUserinfo();
  583. $userInfo["is_register"] = $is_register;
  584. if ($res) {
  585. $this->success("登录成功!", $userInfo);
  586. } else {
  587. $this->error("登录失败!");
  588. }
  589. // 用户登录逻辑 === 结束
  590. }
  591. } else {
  592. $this->error("登录失败,请用验证码方式登录!");
  593. }
  594. }
  595. /**
  596. * 注销账号
  597. *
  598. * @param string $mobile 手机号
  599. * @param string $captcha 验证码
  600. */
  601. public function cancleUser()
  602. {
  603. $user = $this->auth->getUser();
  604. $user->status = "cancel";
  605. unset($user->power);
  606. $user->save();
  607. $this->success("账号注销成功!");
  608. }
  609. /**
  610. * 用户举报
  611. *
  612. * @param string $mobile 手机号
  613. * @param string $captcha 验证码
  614. */
  615. public function report()
  616. {
  617. $type = $this->request->param('type');// 类型描述
  618. $content = $this->request->param('content');// 内容
  619. $images = $this->request->param('images');// 图片
  620. $ruser_id = $this->request->param('ruser_id');// 被举报用户ID
  621. if (!$type || !$content || !$images || !$ruser_id) {
  622. $this->error("请完成举报内容!");
  623. }
  624. $user_id = $this->auth->id;
  625. $data = [];
  626. $data["user_id"] = $user_id;
  627. $data["ruser_id"] = $ruser_id;
  628. $data["type"] = $type;
  629. $data["content"] = $content;
  630. $data["images"] = $images;
  631. $data["createtime"] = time();
  632. $res = \app\common\model\UserReport::insert($data);
  633. if ($res) {
  634. $this->success("举报内容提交成功!");
  635. } else {
  636. $this->error("网络错误,请稍后重试!");
  637. }
  638. }
  639. /**
  640. * 获取昵称
  641. * @return string
  642. */
  643. public function getNickName()
  644. {
  645. $nicheng_tou = array('快乐的', '冷静的', '醉熏的', '潇洒的', '糊涂的', '积极的', '冷酷的', '深情的', '粗暴的', '温柔的', '可爱的', '愉快的', '义气的', '认真的', '威武的', '帅气的', '传统的', '潇洒的', '漂亮的', '自然的', '专一的', '听话的', '昏睡的', '狂野的', '等待的', '搞怪的', '幽默的', '魁梧的', '活泼的', '开心的', '高兴的', '超帅的', '留胡子的', '坦率的', '直率的', '轻松的', '痴情的', '完美的', '精明的', '无聊的', '有魅力的', '丰富的', '繁荣的', '饱满的', '炙热的', '暴躁的', '碧蓝的', '俊逸的', '英勇的', '健忘的', '故意的', '无心的', '土豪的', '朴实的', '兴奋的', '幸福的', '淡定的', '不安的', '阔达的', '孤独的', '独特的', '疯狂的', '时尚的', '落后的', '风趣的', '忧伤的', '大胆的', '爱笑的', '矮小的', '健康的', '合适的', '玩命的', '沉默的', '斯文的', '香蕉', '苹果', '鲤鱼', '鳗鱼', '任性的', '细心的', '粗心的', '大意的', '甜甜的', '酷酷的', '健壮的', '英俊的', '霸气的', '阳光的', '默默的', '大力的', '孝顺的', '忧虑的', '着急的', '紧张的', '善良的', '凶狠的', '害怕的', '重要的', '危机的', '欢喜的', '欣慰的', '满意的', '跳跃的', '诚心的', '称心的', '如意的', '怡然的', '娇气的', '无奈的', '无语的', '激动的', '愤怒的', '美好的', '感动的', '激情的', '激昂的', '震动的', '虚拟的', '超级的', '寒冷的', '精明的', '明理的', '犹豫的', '忧郁的', '寂寞的', '奋斗的', '勤奋的', '现代的', '过时的', '稳重的', '热情的', '含蓄的', '开放的', '无辜的', '多情的', '纯真的', '拉长的', '热心的', '从容的', '体贴的', '风中的', '曾经的', '追寻的', '儒雅的', '优雅的', '开朗的', '外向的', '内向的', '清爽的', '文艺的', '长情的', '平常的', '单身的', '伶俐的', '高大的', '懦弱的', '柔弱的', '爱笑的', '乐观的', '耍酷的', '酷炫的', '神勇的', '年轻的', '唠叨的', '瘦瘦的', '无情的', '包容的', '顺心的', '畅快的', '舒适的', '靓丽的', '负责的', '背后的', '简单的', '谦让的', '彩色的', '缥缈的', '欢呼的', '生动的', '复杂的', '慈祥的', '仁爱的', '魔幻的', '虚幻的', '淡然的', '受伤的', '雪白的', '高高的', '糟糕的', '顺利的', '闪闪的', '羞涩的', '缓慢的', '迅速的', '优秀的', '聪明的', '含糊的', '俏皮的', '淡淡的', '坚强的', '平淡的', '欣喜的', '能干的', '灵巧的', '友好的', '机智的', '机灵的', '正直的', '谨慎的', '俭朴的', '殷勤的', '虚心的', '辛勤的', '自觉的', '无私的', '无限的', '踏实的', '老实的', '现实的', '可靠的', '务实的', '拼搏的', '个性的', '粗犷的', '活力的', '成就的', '勤劳的', '单纯的', '落寞的', '朴素的', '悲凉的', '忧心的', '洁净的', '清秀的', '自由的', '小巧的', '单薄的', '贪玩的', '刻苦的', '干净的', '壮观的', '和谐的', '文静的', '调皮的', '害羞的', '安详的', '自信的', '端庄的', '坚定的', '美满的', '舒心的', '温暖的', '专注的', '勤恳的', '美丽的', '腼腆的', '优美的', '甜美的', '甜蜜的', '整齐的', '动人的', '典雅的', '尊敬的', '舒服的', '妩媚的', '秀丽的', '喜悦的', '甜美的', '彪壮的', '强健的', '大方的', '俊秀的', '聪慧的', '迷人的', '陶醉的', '悦耳的', '动听的', '明亮的', '结实的', '魁梧的', '标致的', '清脆的', '敏感的', '光亮的', '大气的', '老迟到的', '知性的', '冷傲的', '呆萌的', '野性的', '隐形的', '笑点低的', '微笑的', '笨笨的', '难过的', '沉静的', '火星上的', '失眠的', '安静的', '纯情的', '要减肥的', '迷路的', '烂漫的', '哭泣的', '贤惠的', '苗条的', '温婉的', '发嗲的', '会撒娇的', '贪玩的', '执着的', '眯眯眼的', '花痴的', '想人陪的', '眼睛大的', '高贵的', '傲娇的', '心灵美的', '爱撒娇的', '细腻的', '天真的', '怕黑的', '感性的', '飘逸的', '怕孤独的', '忐忑的', '高挑的', '傻傻的', '冷艳的', '爱听歌的', '还单身的', '怕孤单的', '懵懂的');
  646. $nicheng_wei = array('嚓茶', '凉面', '便当', '毛豆', '花生', '可乐', '灯泡', '哈密瓜', '野狼', '背包', '眼神', '缘分', '雪碧', '人生', '牛排', '蚂蚁', '飞鸟', '灰狼', '斑马', '汉堡', '悟空', '巨人', '绿茶', '自行车', '保温杯', '大碗', '墨镜', '魔镜', '煎饼', '月饼', '月亮', '星星', '芝麻', '啤酒', '玫瑰', '大叔', '小伙', '哈密瓜,数据线', '太阳', '树叶', '芹菜', '黄蜂', '蜜粉', '蜜蜂', '信封', '西装', '外套', '裙子', '大象', '猫咪', '母鸡', '路灯', '蓝天', '白云', '星月', '彩虹', '微笑', '摩托', '板栗', '高山', '大地', '大树', '电灯胆', '砖头', '楼房', '水池', '鸡翅', '蜻蜓', '红牛', '咖啡', '机器猫', '枕头', '大船', '诺言', '钢笔', '刺猬', '天空', '飞机', '大炮', '冬天', '洋葱', '春天', '夏天', '秋天', '冬日', '航空', '毛衣', '豌豆', '黑米', '玉米', '眼睛', '老鼠', '白羊', '帅哥', '美女', '季节', '鲜花', '服饰', '裙子', '白开水', '秀发', '大山', '火车', '汽车', '歌曲', '舞蹈', '老师', '导师', '方盒', '大米', '麦片', '水杯', '水壶', '手套', '鞋子', '自行车', '鼠标', '手机', '电脑', '书本', '奇迹', '身影', '香烟', '夕阳', '台灯', '宝贝', '未来', '皮带', '钥匙', '心锁', '故事', '花瓣', '滑板', '画笔', '画板', '学姐', '店员', '电源', '饼干', '宝马', '过客', '大白', '时光', '石头', '钻石', '河马', '犀牛', '西牛', '绿草', '抽屉', '柜子', '往事', '寒风', '路人', '橘子', '耳机', '鸵鸟', '朋友', '苗条', '铅笔', '钢笔', '硬币', '热狗', '大侠', '御姐', '萝莉', '毛巾', '期待', '盼望', '白昼', '黑夜', '大门', '黑裤', '钢铁侠', '哑铃', '板凳', '枫叶', '荷花', '乌龟', '仙人掌', '衬衫', '大神', '草丛', '早晨', '心情', '茉莉', '流沙', '蜗牛', '战斗机', '冥王星', '猎豹', '棒球', '篮球', '乐曲', '电话', '网络', '世界', '中心', '鱼', '鸡', '狗', '老虎', '鸭子', '雨', '羽毛', '翅膀', '外套', '火', '丝袜', '书包', '钢笔', '冷风', '八宝粥', '烤鸡', '大雁', '音响', '招牌', '胡萝卜', '冰棍', '帽子', '菠萝', '蛋挞', '香水', '泥猴桃', '吐司', '溪流', '黄豆', '樱桃', '小鸽子', '小蝴蝶', '爆米花', '花卷', '小鸭子', '小海豚', '日记本', '小熊猫', '小懒猪', '小懒虫', '荔枝', '镜子', '曲奇', '金针菇', '小松鼠', '小虾米', '酒窝', '紫菜', '金鱼', '柚子', '果汁', '百褶裙', '项链', '帆布鞋', '火龙果', '奇异果', '煎蛋', '唇彩', '小土豆', '高跟鞋', '戒指', '雪糕', '睫毛', '铃铛', '手链', '香氛', '红酒', '月光', '酸奶', '银耳汤', '咖啡豆', '小蜜蜂', '小蚂蚁', '蜡烛', '棉花糖', '向日葵', '水蜜桃', '小蝴蝶', '小刺猬', '小丸子', '指甲油', '康乃馨', '糖豆', '薯片', '口红', '超短裙', '乌冬面', '冰淇淋', '棒棒糖', '长颈鹿', '豆芽', '发箍', '发卡', '发夹', '发带', '铃铛', '小馒头', '小笼包', '小甜瓜', '冬瓜', '香菇', '小兔子', '含羞草', '短靴', '睫毛膏', '小蘑菇', '跳跳糖', '小白菜', '草莓', '柠檬', '月饼', '百合', '纸鹤', '小天鹅', '云朵', '芒果', '面包', '海燕', '小猫咪', '龙猫', '唇膏', '鞋垫', '羊', '黑猫', '白猫', '万宝路', '金毛', '山水', '音响');
  647. $nicheng = $nicheng_tou[array_rand($nicheng_tou, 1)] . $nicheng_wei[array_rand($nicheng_wei, 1)] . rand(0,99);
  648. $result['nickname'] = $nicheng; //输出生成的昵称
  649. $this->success('获取成功',$result);
  650. }
  651. //微信登录
  652. public function wechatlogin(){
  653. // $nickname = input('nickname','');
  654. // $avatar = input('avatar','');
  655. $gender = input('gender',1);
  656. $wechat_openid = input('openid','');
  657. if (!$wechat_openid) {
  658. $this->error(__('Invalid parameters'));
  659. }
  660. // if($gender != 1){
  661. // $gender = 0;
  662. // }
  663. $user = \app\common\model\User::getByOpenid($wechat_openid);
  664. if ($user) {
  665. if ($user->status != 1) {
  666. $this->error(__('Account is locked'));
  667. }
  668. if ($user->frozentime > time()) {
  669. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  670. }
  671. //如果已经有账号则直接登录
  672. $ret = $this->auth->direct($user->id);
  673. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次
  674. /*if($user->gender == 1 && $user->gh_id == 0){
  675. $this->firstopen_send($user->id);
  676. }*/
  677. } else {
  678. // $this->success('选择性别', ['code' => 5]);
  679. // if (!$nickname || !$avatar) {
  680. // $this->error(__('Invalid parameters'));
  681. // }
  682. $reg_data = [
  683. // 'nickname'=>$nickname,
  684. // 'avatar'=>$avatar,
  685. // 'gender'=>$gender,
  686. 'register_from' => input('register_from',''),
  687. 'gender' => $gender,
  688. ];
  689. $ret = $this->auth->openid_register($wechat_openid,$reg_data);
  690. //亿米
  691. /*if(input('register_from','') == 'xiaomi'){
  692. $this->yimi_advert();
  693. }*/
  694. }
  695. if ($ret) {
  696. $data = ['userinfo' => $this->auth->getUserinfo()];
  697. $this->success(__('Logged in successful'), $data);
  698. } else {
  699. $this->error($this->auth->getError());
  700. }
  701. }
  702. //获取openid
  703. public function getopenid() {
  704. //code
  705. $code = $this->request->post('code', '', 'trim');// code值
  706. if (!$code) {
  707. $this->error(__('Invalid parameters'));
  708. }
  709. $config = config('wxMiniProgram');
  710. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  711. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  712. $openidInfo = json_decode($openidInfo,true);
  713. if(!isset($openidInfo['openid'])) {
  714. $this->error('用户openid获取失败', $openidInfo);
  715. }
  716. $user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->find();
  717. //$openidInfo['mobile'] = isset($user['mobile']) ? $user['mobile'] : '';
  718. $this->success('获取成功', $openidInfo);
  719. }
  720. //生成海报
  721. public function createposter() {
  722. // $image = input('image', '', 'trim');
  723. // if (!$image) {
  724. // $this->error('您的网络开小差啦~');
  725. // }
  726. $image = config('site.intro_imges');
  727. $haibao = $this->haibao($this->auth->id,['invite_no'=>$this->auth->invite_no, 'background' => $image]);
  728. return $haibao;
  729. // $this->success('success', $haibao);
  730. }
  731. //海报
  732. public function haibao($player_id,$data){
  733. //下载页二维码,没必要保留
  734. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  735. $params = [
  736. 'text' => $httpStr.'/index/index/download?code=' . $data['invite_no'],
  737. 'size' => 90,
  738. 'logo' => false,
  739. 'label' => false,
  740. 'padding' => 0,
  741. ];
  742. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  743. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  744. mk_dir($qrcode_path);
  745. $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
  746. $qrCode->writeFile($download_qrcode);
  747. //海报
  748. $result['url'] = $this->createhaibao($download_qrcode,$player_id,$data);
  749. $this->success('获取成功',$result);
  750. }
  751. public function createhaibao($download_qrcode,$player_id,$sub_data){
  752. //二维码
  753. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  754. $download_qrcode= $httpStr.'/'.$download_qrcode;
  755. $data = [
  756. /*[
  757. "left" => "15px",
  758. "top" => "400px",
  759. "type" => "img",
  760. "width" => "58px",
  761. "height" => "58px",
  762. "src" => one_domain_image($this->auth->avatar),
  763. ],
  764. [
  765. "left" => "81px",
  766. "top" => "400px",
  767. "type" => "nickname",
  768. "width" => "80px",
  769. "height" => "24px",
  770. "size" => "12px",
  771. "color" => "#000",
  772. "content" => $this->auth->nickname
  773. ],
  774. [
  775. "left" => "81px",
  776. "top" => "400px",
  777. "type" => "nickname",
  778. "width" => "80px",
  779. "height" => "24px",
  780. "size" => "12px",
  781. "color" => "#000",
  782. "content" => $this->auth->invite_no
  783. ],*/
  784. [
  785. "left" => "18px",
  786. "top" => "410px",
  787. "type" => "text",
  788. "width" => "80px",
  789. "height" => "24px",
  790. "size" => "16px",
  791. "color" => "#123354",
  792. "content" => 'GG语音'
  793. ],
  794. [
  795. "left" => "18px",
  796. "top" => "445px",
  797. "type" => "text",
  798. "width" => "80px",
  799. "height" => "24px",
  800. "size" => "10px",
  801. "color" => "#123354",
  802. "content" => '更多现金奖励等你来拿'
  803. ],
  804. [
  805. "left" => "210px",
  806. "top" => "385px",
  807. "type" => "img",
  808. "width" => "95px",
  809. "height" => "95px",
  810. "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  811. ]
  812. ];
  813. $data = json_encode($data, 320);
  814. $poster = [
  815. 'id' => $player_id,
  816. 'title' => 'GG语音',
  817. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  818. 'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
  819. 'data' => $data,
  820. 'status' => 'normal',
  821. 'weigh' => 0,
  822. 'createtime' => 1653993709,
  823. 'updatetime' => 1653994259,
  824. ];
  825. $image = new \addons\poster\library\Image();
  826. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  827. if (!$imgurl) {
  828. $this->error('生成海报出错');
  829. }
  830. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  831. return $httpStr.'/' . $imgurl;
  832. }
  833. //申请真人认证
  834. public function realauth()
  835. {
  836. try {
  837. $realName = $this->request->param('real_name','');
  838. $idCard = $this->request->param('id_card','');
  839. if ($this->auth->is_auth == 2) {
  840. $this->error('您已经真人认证过了~');
  841. }
  842. if (empty($realName)) {
  843. throw new Exception('请输入姓名');
  844. }
  845. if (empty($idCard)) {
  846. throw new Exception('请输入身份证号');
  847. }
  848. $userAuthWhere['user_id'] = ['neq',$this->auth->id];
  849. $userAuthWhere['idcard'] = $idCard;
  850. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  851. if (!empty($userAuth)) {
  852. throw new Exception('该身份证号已被其他用户实名验证过');
  853. }
  854. /*$userService = new UserService();
  855. $faceParams = [
  856. 'real_name' => $realName,
  857. 'id_card' => $idCard,
  858. 'user_id' => $this->auth->id,
  859. ];
  860. $res = $userService->faceAuth($faceParams);
  861. if (!$res['status']) {
  862. $this->error('您的网络开小差啦5~');
  863. }
  864. $rs = $res['data'];
  865. if (!$rs || $rs['code'] != 0) {
  866. $this->error('您的网络开小差啦6~');
  867. }
  868. $user_auth = [
  869. 'user_id' => $this->auth->id,
  870. 'realname' => $realName,
  871. 'idcard' => $idCard,
  872. 'certify_id' => isset($rs['result']['faceId']) ? $rs['result']['faceId'] : '',
  873. 'out_trade_no' => isset($rs['result']['orderNo']) ? $rs['result']['orderNo'] : '',
  874. 'status' => 0,
  875. 'createtime' => time(),
  876. 'updatetime' => time()
  877. ];*/
  878. $userService = new UserService();
  879. $aliParams = [
  880. 'id_card' => $idCard,
  881. 'real_name' => $realName,
  882. ];
  883. $aliCheckRes = $userService->aliCheck($aliParams);
  884. if (!$aliCheckRes['status']) {
  885. throw new Exception($aliCheckRes['msg']);
  886. }
  887. $user_auth = [
  888. 'user_id' => $this->auth->id,
  889. 'realname' => $realName,
  890. 'idcard' => $idCard,
  891. 'certify_id' => '',
  892. 'out_trade_no' => '',
  893. 'status' => 1,
  894. 'createtime' => time(),
  895. ];
  896. //开启事务
  897. Db::startTrans();
  898. //查询是否认证过
  899. $info = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  900. if ($info) {
  901. $auth_rs = Db::name('user_auth')->where(['id' => $info['id']])->setField($user_auth);
  902. } else {
  903. $auth_rs = Db::name('user_auth')->insertGetId($user_auth);
  904. }
  905. if (!$auth_rs) {
  906. Db::rollback();
  907. $this->error('您的网络开小差啦7~');
  908. }
  909. //修改用户表认证状态
  910. $user_rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', 2);
  911. if ($user_rs === false) {
  912. Db::rollback();
  913. $this->error('您的网络开小差啦8~');
  914. }
  915. Db::commit();
  916. /*$return_data = [
  917. 'face_id' => $user_auth['certify_id'],
  918. 'order_no' => $user_auth['out_trade_no'],
  919. 'user_id' => (string)$this->auth->id,
  920. 'nonce' => $rs['nonce'],
  921. 'sign' => $rs['sign'],
  922. ];*/
  923. $this->success('验证成功');
  924. } catch (Exception $e) {
  925. $this->error($e->getMessage());
  926. }
  927. }
  928. //查询真人认证结果
  929. public function getrealauthresult() {
  930. $user_auth = Db::name('user_auth')->where(['user_id' => $this->auth->id])->find();
  931. if (!$user_auth) {
  932. $this->success('尚未认证');
  933. }
  934. if ($user_auth['status'] == 1) {
  935. $this->success('真人认证通过');
  936. }
  937. if (!$user_auth['certify_id']) {
  938. $this->success('请先进行真人认证');
  939. }
  940. $tencentConfig = config('tencent_yun');
  941. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  942. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  943. //获取token
  944. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  945. $token_result = file_get_contents($token_url);
  946. if (!$token_result) {
  947. $this->error('您的网络开小差啦1~');
  948. }
  949. $token_result = json_decode($token_result, true);
  950. if ($token_result['code'] != 0) {
  951. $this->error('您的网络开小差啦2~');
  952. }
  953. $token = $token_result['access_token'];
  954. //获取签名鉴权参数ticket
  955. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  956. $ticket_result = file_get_contents($ticket_url);
  957. if (!$ticket_result) {
  958. $this->error('您的网络开小差啦3~');
  959. }
  960. $ticket_result = json_decode($ticket_result, true);
  961. if ($ticket_result['code'] != 0) {
  962. $this->error('您的网络开小差啦4~');
  963. }
  964. $ticket = $ticket_result['tickets'][0]['value'];
  965. //获取签名
  966. $sign_data = [
  967. 'wbappid' => $sercrtId,
  968. 'orderNo' => $user_auth['out_trade_no'],
  969. 'version' => '1.0.0',
  970. 'ticket' => $ticket,
  971. 'nonce' => Random::alnum(32)
  972. ];//p($sign_data);
  973. asort($sign_data); //p($sign_data);//排序
  974. $sign_string = join('', $sign_data);//p($sign_string);
  975. $sign = sha1($sign_string);//p($sign);
  976. //人脸核身结果查询
  977. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/v2/base/queryfacerecord?orderNo=' . $user_auth['out_trade_no'];
  978. $data = [
  979. 'appId' => $sercrtId,
  980. 'version' => '1.0.0',
  981. 'nonce' => $sign_data['nonce'],
  982. 'orderNo' => $user_auth['out_trade_no'],
  983. 'sign' => $sign
  984. ];
  985. $rs = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  986. if (!$rs) {
  987. $this->error('您的网络开小差啦5~');
  988. }
  989. $rs = json_decode($rs, true);
  990. if (!$rs || $rs['code'] != 0) {
  991. $this->error($rs['msg']);
  992. }
  993. if ($rs['result']['liveRate'] >= 90 && $rs['result']['similarity'] >= 90) {
  994. $edit_data['status'] = 2;
  995. $msg = '真人认证成功';
  996. } else {
  997. $edit_data['status'] = -1;
  998. $edit_data['certify_id'] = '';
  999. $edit_data['out_trade_no'] = '';
  1000. $msg = '真人认证失败';
  1001. }
  1002. $edit_data['updatetime'] = time();
  1003. //开启事务
  1004. Db::startTrans();
  1005. //修改认证信息
  1006. $result = Db::name('user_auth')->where(['user_id' => $this->auth->id, 'status' => $user_auth['status']])->setField($edit_data);
  1007. if (!$result) {
  1008. Db::rollback();
  1009. $this->error('查询认证结果失败2');
  1010. }
  1011. //修改用户信息
  1012. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('is_auth', $edit_data['status']);
  1013. if (!$rs) {
  1014. Db::rollback();
  1015. $this->error('查询认证结果失败3');
  1016. }
  1017. if ($edit_data['status'] == 2) { //通过
  1018. //tag任务赠送金币
  1019. //真人认证奖励
  1020. /*$task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,20);
  1021. if($task_rs === false){
  1022. Db::rollback();
  1023. $this->error('完成任务赠送奖励失败');
  1024. }*/
  1025. //系统消息
  1026. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  1027. } else {
  1028. //系统消息
  1029. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证审核不通过');
  1030. }
  1031. Db::commit();
  1032. $this->success($msg);
  1033. }
  1034. }