User.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. //use addons\epay\library\Wechat;
  15. use app\common\library\Wechat;
  16. /**
  17. * 会员接口,登录,注册,修改资料等
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin','applelogin','bindmobile','applebindmobile', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 会员中心
  29. */
  30. public function index()
  31. {
  32. $this->success('', ['welcome' => $this->auth->nickname]);
  33. }
  34. /**
  35. * 会员登录
  36. *
  37. * @ApiMethod (POST)
  38. * @param string $account 账号
  39. * @param string $password 密码
  40. */
  41. public function login()
  42. {
  43. $countrycode = input('countrycode');
  44. $account = input('account');
  45. $password = input('password');
  46. if (!$countrycode || !$account || !$password) {
  47. $this->error(__('Invalid parameters'));
  48. }
  49. $account = $countrycode.$account;
  50. $ret = $this->auth->login($account, $password);
  51. if ($ret) {
  52. $data = $this->userInfo('return');
  53. $this->success(__('Logged in successful'), $data);
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 手机验证码登录
  60. *
  61. * @ApiMethod (POST)
  62. * @param string $mobile 手机号
  63. * @param string $captcha 验证码
  64. */
  65. public function mobilelogin()
  66. {
  67. $countrycode = input('countrycode');
  68. $mobile = input('mobile');
  69. $captcha = input('captcha');
  70. if (!$countrycode || !$mobile || !$captcha) {
  71. $this->error(__('Invalid parameters'));
  72. }
  73. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  74. $this->error(__('Mobile is incorrect'));
  75. }*/
  76. $simplemobile = $mobile;
  77. $mobile = $countrycode.$mobile;
  78. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  79. $this->error(__('Captcha is incorrect'));
  80. }
  81. $user = \app\common\model\User::getByMobile($mobile);
  82. if ($user) {
  83. if ($user->status == -1) {
  84. $this->error('账户已注销');
  85. }
  86. if ($user->status != 1) {
  87. $this->error(__('Account is locked'));
  88. }
  89. //如果已经有账号则直接登录
  90. $ret = $this->auth->direct($user->id);
  91. } else {
  92. $extend = [
  93. 'simplemobile' => $simplemobile,
  94. ];
  95. $ret = $this->auth->register('', '', '', $mobile, $extend);
  96. }
  97. if ($ret) {
  98. Sms::flush($mobile, 'mobilelogin');
  99. $data = $this->userInfo('return');
  100. $this->success(__('Logged in successful'), $data);
  101. } else {
  102. $this->error($this->auth->getError());
  103. }
  104. }
  105. /**
  106. * 注册会员
  107. *
  108. * @ApiMethod (POST)
  109. * @param string $username 用户名
  110. * @param string $password 密码
  111. * @param string $email 邮箱
  112. * @param string $mobile 手机号
  113. * @param string $code 验证码
  114. */
  115. /*public function register()
  116. {
  117. $username = $this->request->post('username');
  118. $password = $this->request->post('password');
  119. $email = $this->request->post('email');
  120. $mobile = $this->request->post('mobile');
  121. $code = $this->request->post('code');
  122. if (!$username || !$password) {
  123. $this->error(__('Invalid parameters'));
  124. }
  125. if ($email && !Validate::is($email, "email")) {
  126. $this->error(__('Email is incorrect'));
  127. }
  128. //if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  129. // $this->error(__('Mobile is incorrect'));
  130. //}
  131. $ret = Sms::check($mobile, $code, 'register');
  132. if (!$ret) {
  133. $this->error(__('Captcha is incorrect'));
  134. }
  135. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  136. if ($ret) {
  137. $data = $this->userInfo('return');
  138. $this->success(__('Sign up successful'), $data);
  139. } else {
  140. $this->error($this->auth->getError());
  141. }
  142. }*/
  143. //微信登录,预先假注册
  144. public function wechatlogin(){
  145. $code = $this->request->param('code','');
  146. if(!$code){
  147. $this->error(__('Invalid parameters'));
  148. }
  149. //微信
  150. $wechat = new Wechat();
  151. $wxuserinfo = $wechat->getAccessToken($code);
  152. if(!$wxuserinfo){
  153. $this->error('openid获取失败');
  154. }
  155. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  156. $this->error('openid获取失败');
  157. }
  158. $openid = $wxuserinfo['openid'];
  159. //检查用户
  160. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  161. if ($user) {
  162. if ($user['status'] == -1) {
  163. $this->error('账户已注销');
  164. }
  165. if ($user['status'] != 1) {
  166. $this->error(__('Account is locked'));
  167. }
  168. //如果已经有账号则直接登录
  169. $ret = $this->auth->direct($user['id']);
  170. if ($ret) {
  171. $userInfo = $this->auth->getUserinfo();
  172. $userInfo['is_register'] = 0;
  173. $userInfo['code'] = $code;
  174. $this->success(__('Logged in successful'), $userInfo);
  175. } else {
  176. $this->error($this->auth->getError());
  177. }
  178. } else {
  179. //记录code和openid,绑定手机号的时候更新openid
  180. $wechatCodeData = [
  181. 'code' => $code,
  182. 'openid' => $openid,
  183. 'createtime' => time(),
  184. ];
  185. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  186. if (empty($wechatCode)) {
  187. Db::name('wechat_code')->insertGetId($wechatCodeData);
  188. } else {
  189. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  190. }
  191. //直接返回
  192. $userInfo = [];
  193. $userInfo['is_register'] = 1;
  194. $userInfo['code'] = $code;
  195. $this->success('获取信息成功', $userInfo);
  196. }
  197. }
  198. /**
  199. * 运营商一键登录
  200. */
  201. public function onLogin()
  202. {
  203. $accessToken = input('accessToken');// 运营商预取号获取到的token
  204. $token = input('tokenT');// 易盾返回的token
  205. if (!$accessToken || !$token) {
  206. $this->error("参数获取失败!");
  207. }
  208. $params = array(
  209. // 运营商预取号获取到的token
  210. "accessToken" => $accessToken,
  211. // 易盾返回的token
  212. "token" => $token
  213. );
  214. // 获取密钥配置
  215. $configInfo = config("onLogin");
  216. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  217. $onret = $onlogin->check($params);
  218. // $ret = [];
  219. // $ret["code"] = 200;
  220. // $ret["msg"] = "ok";
  221. // $ret["data"] = [
  222. // "phone" => "17574504021",
  223. // "resultCode" => 0
  224. // ];
  225. if ($onret["code"] == 200) {
  226. $mobile = $onret["data"]["phone"];
  227. if (empty($mobile)) {
  228. // 取号失败,建议进行二次验证,例如短信验证码
  229. $this->error("取号登录失败,请用验证码方式登录!");
  230. } else {
  231. // 取号成功, 执行登录等流程
  232. // 用户登录逻辑 === 开始
  233. $countrycode = 86;
  234. $simplemobile = $mobile;
  235. $mobile = $countrycode.$mobile;
  236. $user = \app\common\model\User::getByMobile($mobile);
  237. if ($user) {
  238. if ($user->status == -1) {
  239. $this->error('账户已注销');
  240. }
  241. if ($user->status != 1) {
  242. $this->error(__('Account is locked'));
  243. }
  244. //如果已经有账号则直接登录
  245. $ret = $this->auth->direct($user->id);
  246. $is_register = 0;
  247. } else {
  248. $extend = [
  249. 'simplemobile' => $simplemobile,
  250. ];
  251. $ret = $this->auth->register('', '', '', $mobile, $extend);
  252. $is_register = 1;
  253. }
  254. if ($ret) {
  255. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  256. } else {
  257. $this->error($this->auth->getError());
  258. }
  259. // 用户登录逻辑 === 结束
  260. }
  261. } else {
  262. $this->error("登录失败,请用验证码方式登录!");
  263. }
  264. }
  265. //苹果登录+预注册
  266. public function applelogin(){
  267. $iosUserId = $this->request->param('ios_user_id','');
  268. if(!$iosUserId){
  269. $this->error(__('Invalid parameters'));
  270. }
  271. //检查用户
  272. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  273. if ($user) {
  274. if ($user['status'] == -1) {
  275. $this->error('账户已经注销');
  276. }
  277. if ($user['status'] != 1) {
  278. $this->error(__('Account is locked'));
  279. }
  280. //如果已经有账号则直接登录
  281. $ret = $this->auth->direct($user['id']);
  282. if ($ret) {
  283. $userInfo = $this->auth->getUserinfo();
  284. $userInfo['is_register'] = 0;
  285. $userInfo['ios_user_id'] = $iosUserId;
  286. $this->success(__('Logged in successful'), $userInfo);
  287. } else {
  288. $this->error($this->auth->getError());
  289. }
  290. } else {
  291. //直接返回
  292. $userInfo = [];
  293. $userInfo['is_register'] = 1;
  294. $userInfo['ios_user_id'] = $iosUserId;
  295. $this->success('获取信息成功', $userInfo);
  296. }
  297. }
  298. //用户详细资料
  299. public function userInfo($type = 1){
  300. $info = $this->auth->getUserinfo();
  301. if($type == 'return'){
  302. return $info;
  303. }
  304. $this->success(__('success'),$info);
  305. }
  306. /**
  307. * 退出登录
  308. * @ApiMethod (POST)
  309. */
  310. public function logout()
  311. {
  312. if (!$this->request->isPost()) {
  313. $this->error(__('Invalid parameters'));
  314. }
  315. //退出im
  316. // $tenIm = new Tenim();
  317. // $tenIm->loginoutim($this->auth->id);
  318. $this->auth->logout();
  319. $this->success(__('Logout successful'));
  320. }
  321. /**
  322. * 修改会员个人信息
  323. *
  324. * @ApiMethod (POST)
  325. * @param string $avatar 头像地址
  326. * @param string $username 用户名
  327. * @param string $nickname 昵称
  328. * @param string $bio 个人简介
  329. */
  330. public function profile()
  331. {
  332. $field_array = ['nickname','introcode','gender','birthday','attribute','shoesize','height','weight','bio','avatar','photo_images','tag_ids','hide_is_finishinfo'];
  333. $data = [];
  334. foreach($field_array as $key => $field){
  335. //前端传不了post,改了
  336. /*if(!request()->has($field,'post')){
  337. continue;
  338. }*/
  339. if(!input('?'.$field)){
  340. continue;
  341. }
  342. $newone = input($field);
  343. if($field == 'avatar'){
  344. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  345. }
  346. if($field == 'photo_images'){
  347. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  348. }
  349. $data[$field] = $newone;
  350. }
  351. //关于我,是否需要审核
  352. $user_bio_audit_switch = config('site.user_bio_audit_switch');
  353. if(isset($data['bio']) && !empty($data['bio']) && $user_bio_audit_switch == 1){
  354. if($data['bio'] != $this->auth->bio){
  355. $bio_data = [
  356. 'user_id' => $this->auth->id,
  357. 'bio' => $this->auth->bio,
  358. 'new_bio' => $data['bio'],
  359. 'createtime' => time(),
  360. ];
  361. Db::name('user_audit')->insertGetId($bio_data);
  362. }
  363. unset($data['bio']);
  364. }
  365. //
  366. if(isset($data['birthday'])){
  367. $data['birthday'] = strtotime($data['birthday']);
  368. }
  369. if(isset($data['tag_ids'])){
  370. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  371. }
  372. if(isset($data['introcode']) && !empty($data['introcode'])){
  373. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  374. if(!$intro_user){
  375. $this->error('不存在的邀请人');
  376. }
  377. if(!empty($this->auth->intro_uid)){
  378. $this->error('您已经填写过邀请人');
  379. }
  380. unset($data['introcode']);//别人的邀请码,不能改了自己的
  381. $data['intro_uid'] = $intro_user;
  382. //邀请用户注册,给邀请人奖励
  383. if(isset($data['intro_uid']) && !empty($data['intro_uid'])){
  384. $intro_money = config('site.intro_newuser_gift_moneynum') ?: 0;
  385. if($intro_money > 0){
  386. $wallet_rs = model('wallet')->lockChangeAccountRemain($data['intro_uid'],'money',$intro_money,63,'邀请'.$this->auth->username);
  387. if($wallet_rs['status'] === false){
  388. Db::rollback();
  389. $this->setError($wallet_rs['msg']);
  390. return false;
  391. }
  392. }
  393. }
  394. }
  395. //dump($data);
  396. if(empty($data)){
  397. $this->error('没有任何改变');
  398. }
  399. Db::startTrans();
  400. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  401. if($update_rs === false){
  402. Db::rollback();
  403. $this->error('修改资料失败');
  404. }
  405. //task任务
  406. //上传本人头像
  407. if(isset($data['avatar'])&& $data['avatar'] != config('site.domain_cdnurl').'/avatar.png'){
  408. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  409. if($task_rs === false){
  410. Db::rollback();
  411. $this->error('完成任务失败');
  412. }
  413. }
  414. Db::commit();
  415. $this->success();
  416. }
  417. /**
  418. * 修改会员权限
  419. */
  420. public function setpower()
  421. {
  422. $is_vip = $this->is_vip($this->auth->id);
  423. if(!$is_vip){
  424. $this->error('VIP才能设置隐私权限');
  425. }
  426. $field_array = ['yinsi','yinshen','wuhen'];
  427. $data = [];
  428. foreach($field_array as $key => $field){
  429. if(!input('?'.$field)){
  430. continue;
  431. }
  432. $newone = input($field);
  433. $data[$field] = $newone;
  434. }
  435. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  436. $this->success();
  437. }
  438. /*
  439. * 修改用户的坐标
  440. * */
  441. public function change_longlat(){
  442. $longitude = input_post('longitude',0);
  443. $latitude = input_post('latitude',0);
  444. $cityname = input_post('cityname','');
  445. $plat_unique_id = input_post('plat_unique_id','');
  446. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  447. $this->error();
  448. }*/
  449. $data = [];
  450. $longitude && $data['longitude'] = $longitude;
  451. $latitude && $data['latitude'] = $latitude;
  452. $cityname && $data['cityname'] = $cityname;
  453. $plat_unique_id && $data['plat_unique_id'] = $plat_unique_id;
  454. if(!empty($data)){
  455. Db::name('user')->where('id',$this->auth->id)->update($data);
  456. }
  457. $this->success();
  458. }
  459. //修改用户设备id
  460. public function change_plat_unique_id(){
  461. $plat_unique_id = input_post('plat_unique_id','');
  462. $data = [
  463. 'plat_unique_id' => $plat_unique_id,
  464. ];
  465. Db::name('user')->where('id',$this->auth->id)->update($data);
  466. $this->success();
  467. }
  468. /**
  469. * 修改手机号
  470. *
  471. * @ApiMethod (POST)
  472. * @param string $mobile 手机号
  473. * @param string $captcha 验证码
  474. */
  475. public function changemobile()
  476. {
  477. $user = $this->auth->getUser();
  478. $oldcaptcha = $this->request->request('oldcaptcha');
  479. $countrycode = $this->request->request('countrycode');
  480. $mobile = $this->request->request('mobile');
  481. $captcha = $this->request->request('captcha');
  482. if (!$oldcaptcha || !$countrycode || !$mobile || !$captcha) {
  483. $this->error(__('Invalid parameters'));
  484. }
  485. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  486. $this->error(__('Mobile is incorrect'));
  487. }*/
  488. $mobile = $countrycode.$mobile;
  489. if($user->mobile == $mobile){
  490. $this->error('新手机号不能与旧手机号相同');
  491. }
  492. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  493. $this->error(__('Mobile already exist'));
  494. }
  495. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  496. if (!$result) {
  497. $this->error(__('Captcha is incorrect'));
  498. }
  499. $result = Sms::check($mobile, $captcha, 'changemobile');
  500. if (!$result) {
  501. $this->error(__('Captcha is incorrect'));
  502. }
  503. $verification = $user->verification;
  504. $verification->mobile = 1;
  505. $user->verification = $verification;
  506. $user->mobile = $mobile;
  507. $user->save();
  508. Sms::flush($user->mobile, 'changemobile');
  509. Sms::flush($mobile, 'changemobile');
  510. $this->success();
  511. }
  512. /**
  513. * 苹果注册来的,绑定手机号
  514. *
  515. * @ApiMethod (POST)
  516. * @param string $mobile 手机号
  517. * @param string $captcha 验证码
  518. */
  519. public function applebindmobile()
  520. {
  521. $countrycode = $this->request->request('countrycode');
  522. $mobile = $this->request->param('mobile');
  523. $captcha = $this->request->param('captcha');
  524. $iosUserId = $this->request->param('ios_user_id','');
  525. if (!$countrycode || !$mobile || !$captcha || !$iosUserId) {
  526. $this->error(__('Invalid parameters'));
  527. }
  528. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  529. $this->error(__('Mobile is incorrect'));
  530. }*/
  531. $simplemobile = $mobile;
  532. $mobile = $countrycode.$mobile;
  533. $result = Sms::check($mobile, $captcha, 'changemobile');
  534. if (!$result) {
  535. $this->error(__('Captcha is incorrect'));
  536. }
  537. //检查ios_user_id绑定的用户
  538. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  539. if ($user) {
  540. if ($user['status'] == -1) {
  541. $this->error('账户已经注销');
  542. }
  543. if ($user['status'] != 1) {
  544. $this->error(__('Account is locked'));
  545. }
  546. //如果已经有账号则直接登录
  547. $ret = $this->auth->direct($user['id']);
  548. $this->success('success',$this->userInfo('return'));
  549. }
  550. //新的ios用户
  551. $where['mobile'] = $mobile;
  552. $userData = model('User')->where($where)->find();//老用户
  553. if (!empty($userData)) {
  554. if (empty($userData['ios_user_id'])) {
  555. model('User')->update(['ios_user_id' => $iosUserId],$where);//老用户更新ios_user_id
  556. } else {
  557. if ($userData['ios_user_id'] != $iosUserId) {
  558. $this->error('该手机号已被其他用户绑定');
  559. }
  560. }
  561. $ret = $this->auth->direct($userData['id']);
  562. } else {
  563. $extend = [
  564. 'ios_user_id' => $iosUserId,
  565. 'simplemobile' => $simplemobile,
  566. ];
  567. $ret = $this->auth->register('', '','', $mobile, $extend);
  568. }
  569. if (!$ret) {
  570. $this->error($this->auth->getError());
  571. }
  572. $this->success('success',$this->userInfo('return'));
  573. }
  574. /**
  575. * 微信注册来的,绑定手机号
  576. *
  577. * @ApiMethod (POST)
  578. * @param string $mobile 手机号
  579. * @param string $captcha 验证码
  580. */
  581. public function bindmobile()
  582. {
  583. $countrycode = $this->request->param('countrycode');
  584. $mobile = $this->request->param('mobile');
  585. $captcha = $this->request->param('captcha');
  586. $code = $this->request->param('code');
  587. if (!$countrycode || !$mobile || !$captcha || !$code) {
  588. $this->error(__('Invalid parameters'));
  589. }
  590. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  591. $this->error(__('Mobile is incorrect'));
  592. }*/
  593. $simplemobile = $mobile;
  594. $mobile = $countrycode.$mobile;
  595. $result = Sms::check($mobile, $captcha, 'changemobile');
  596. if (!$result) {
  597. $this->error(__('Captcha is incorrect'));
  598. }
  599. $wechatCodeWhere['code'] = $code;
  600. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  601. if (empty($wechatCode)) {
  602. $this->error('请先微信登录');
  603. }
  604. //检查appid绑定的用户
  605. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  606. if ($user) {
  607. if ($user['status'] == -1) {
  608. $this->error('账户已注销');
  609. }
  610. if ($user['status'] != 1) {
  611. $this->error(__('Account is locked'));
  612. }
  613. //如果已经有账号则直接登录
  614. $ret = $this->auth->direct($user['id']);
  615. $this->success('success',$this->userInfo('return'));
  616. }
  617. //新的openid用户
  618. $where['mobile'] = $mobile;
  619. $userData = model('User')->where($where)->find();//老用户
  620. if (!empty($userData)) {
  621. if (empty($userData['wechat_openid'])) {
  622. model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
  623. } else {
  624. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  625. $this->error('该手机号已被其他用户绑定');
  626. }
  627. }
  628. $ret = $this->auth->direct($userData['id']);
  629. } else {
  630. $extend = [
  631. 'wechat_openid' => $wechatCode['openid'],
  632. 'simplemobile' => $simplemobile,
  633. ];
  634. $ret = $this->auth->register('', '','', $mobile, $extend);
  635. }
  636. if (!$ret) {
  637. $this->error($this->auth->getError());
  638. }
  639. $this->success('success',$this->userInfo('return'));
  640. }
  641. /**
  642. * 手机注册来的,绑定微信
  643. *
  644. * @ApiMethod (POST)
  645. * @param string $wechat_openid
  646. */
  647. public function bindopenid()
  648. {
  649. $wechat_openid = $this->request->request('wechat_openid');
  650. if (!$wechat_openid) {
  651. $this->error(__('Invalid parameters'));
  652. }
  653. if(!empty($this->auth->wechat_openid)){
  654. $this->error('已经绑定了微信号');
  655. }
  656. $otherUserWhere['wechat_openid'] = $wechat_openid;
  657. $otherUserWhere['id'] = ['neq',$this->auth->id];
  658. if (\app\common\model\User::where($otherUserWhere)->find()) {
  659. $this->error('该微信号已被其他用户绑定');
  660. }
  661. $user = $this->auth->getUser();
  662. $user->wechat_openid = $wechat_openid;
  663. $user->save();
  664. $this->success('success',$this->userInfo('return'));
  665. }
  666. /**
  667. * 重置密码
  668. *
  669. * @ApiMethod (POST)
  670. * @param string $mobile 手机号
  671. * @param string $newpassword 新密码
  672. * @param string $captcha 验证码
  673. */
  674. public function resetpwd()
  675. {
  676. //$type = input("type");
  677. $type = 'mobile';
  678. $countrycode = input("countrycode");
  679. $mobile = input("mobile");
  680. // $email = input("email");
  681. $newpassword = input("newpassword");
  682. $captcha = input("captcha");
  683. if (!$countrycode || !$mobile || !$newpassword || !$captcha) {
  684. $this->error(__('Invalid parameters'));
  685. }
  686. $mobile = $countrycode.$mobile;
  687. if ($type == 'mobile') {
  688. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  689. $this->error(__('Mobile is incorrect'));
  690. }*/
  691. $user = \app\common\model\User::getByMobile($mobile);
  692. if (!$user) {
  693. $this->error(__('User not found'));
  694. }
  695. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  696. if (!$ret) {
  697. $this->error(__('Captcha is incorrect'));
  698. }
  699. Sms::flush($mobile, 'resetpwd');
  700. }
  701. //模拟一次登录
  702. $this->auth->direct($user->id);
  703. $ret = $this->auth->changepwd($newpassword, '', true);
  704. if ($ret) {
  705. $this->success(__('Reset password successful'));
  706. } else {
  707. $this->error($this->auth->getError());
  708. }
  709. }
  710. /**
  711. * 修改密码
  712. *
  713. * @ApiMethod (POST)
  714. * @param string $newpassword 新密码
  715. * @param string $oldpassword 旧密码
  716. */
  717. public function changepwd(){
  718. $newpassword = input('newpassword');
  719. $oldpassword = input('oldpassword','');
  720. if (!$newpassword) {
  721. $this->error(__('Invalid parameters'));
  722. }
  723. if($this->auth->password && empty($oldpassword)){
  724. $this->error('原密码必填');
  725. }
  726. if(empty($this->auth->password)){
  727. $ret = $this->auth->changepwd($newpassword, '', true);
  728. }else{
  729. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  730. }
  731. if ($ret) {
  732. $this->success('设置密码成功');
  733. } else {
  734. $this->error($this->auth->getError());
  735. }
  736. }
  737. /**
  738. * 记录当前登陆的设备ID,设备信息,IP等
  739. */
  740. public function changeDeviceIp()
  741. {
  742. // 接口防并发
  743. if (!$this->apiLimit(1, 5000)) {
  744. return ;
  745. }
  746. $user = $this->auth->getUser();
  747. $ip = request()->ip();
  748. $deviceId = $this->request->request('device_id','');
  749. $phoneModel = $this->request->request('phone_model','');
  750. $brand = $this->request->request('brand','');
  751. $apiVersion = $this->request->request('api_version','');
  752. $deviceOs = $this->request->request('device_os','');
  753. if ($ip !== $user->loginip){
  754. $update = [];
  755. $update['id'] = $user->id;
  756. $update['loginip'] = $ip;
  757. \app\common\model\User::update($update);
  758. }
  759. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  760. if (empty($userDeviceInfo)){
  761. $userDeviceInfo = new UserDeviceInfo();
  762. $userDeviceInfo->user_id = $user->u_id;
  763. }
  764. $userDeviceInfo->device_os = $deviceOs;
  765. $userDeviceInfo->device_id = $deviceId;
  766. $userDeviceInfo->phone_model = $phoneModel;
  767. $userDeviceInfo->brand = $brand;
  768. $userDeviceInfo->api_version = $apiVersion;
  769. $userDeviceInfo->save();
  770. //首页接口调用,这里不反回信息
  771. // $this->success("更新成功!");
  772. }
  773. //假注销
  774. public function cancleUser(){
  775. if (!$this->request->isPost()) {
  776. $this->error(__('Invalid parameters'));
  777. }
  778. //退出im
  779. // $tenIm = new Tenim();
  780. // $tenIm->loginoutim($this->auth->id);
  781. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  782. $this->auth->logout();
  783. $this->success('注销成功');
  784. }
  785. //公众号获取openid
  786. public function getUserOpenid_gzh(){
  787. $configValue = Service::getConfig('wechat');
  788. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  789. $rs = $wechat->getOpenid();
  790. $this->success('success',$rs);
  791. }
  792. /**
  793. * 微信内H5-JSAPI支付
  794. */
  795. public function jssdkBuildConfig() {
  796. $url = $this->request->request("url");
  797. $configValue = Service::getConfig('wechat');
  798. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  799. $sign = $wechat->getSignPackage(urldecode($url));
  800. $this->success("获取成功!",$sign);
  801. }
  802. }