User.php 28 KB

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