User.php 28 KB

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