User.php 50 KB

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