User.php 49 KB

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