User.php 27 KB

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