User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('status',0)->find();
  317. if(!empty($check_exist)){
  318. $this->error('信息已提交等待审核,请勿重复提交');
  319. }
  320. if($data['bio'] != $this->auth->bio){
  321. $bio_data = [
  322. 'user_id' => $this->auth->id,
  323. 'bio' => $this->auth->bio,
  324. 'new_bio' => $data['bio'],
  325. 'createtime' => time(),
  326. ];
  327. Db::name('user_audit')->insertGetId($bio_data);
  328. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  329. }
  330. unset($data['bio']);
  331. }
  332. //
  333. if(isset($data['birthday'])){
  334. $data['birthday'] = strtotime($data['birthday']);
  335. }
  336. if(isset($data['tag_ids'])){
  337. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  338. }
  339. if(isset($data['introcode']) && !empty($data['introcode'])){
  340. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  341. if(!$intro_user){
  342. $this->error('不存在的邀请人');
  343. }
  344. if(!empty($this->auth->intro_uid)){
  345. $this->error('您已经填写过邀请人');
  346. }
  347. unset($data['introcode']);//别人的邀请码,不能改了自己的
  348. $data['intro_uid'] = $intro_user;
  349. //邀请用户注册,给邀请人奖励
  350. if(isset($data['intro_uid']) && !empty($data['intro_uid'])){
  351. $intro_money = config('site.intro_newuser_gift_moneynum') ?: 0;
  352. if($intro_money > 0){
  353. $wallet_rs = model('wallet')->lockChangeAccountRemain($data['intro_uid'],'money',$intro_money,63,'邀请'.$this->auth->username);
  354. if($wallet_rs['status'] === false){
  355. Db::rollback();
  356. $this->setError($wallet_rs['msg']);
  357. return false;
  358. }
  359. }
  360. }
  361. }
  362. //dump($data);
  363. if(empty($data)){
  364. $this->success();
  365. }
  366. Db::startTrans();
  367. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  368. if($update_rs === false){
  369. Db::rollback();
  370. $this->error('修改资料失败');
  371. }
  372. //task任务
  373. //上传本人头像
  374. if(isset($data['avatar'])&& $data['avatar'] != config('site.domain_cdnurl').'/avatar.png'){
  375. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  376. if($task_rs === false){
  377. Db::rollback();
  378. $this->error('完成任务失败');
  379. }
  380. }
  381. Db::commit();
  382. $this->success();
  383. }
  384. /**
  385. * 修改会员权限
  386. */
  387. public function setpower()
  388. {
  389. $is_vip = $this->is_vip($this->auth->id);
  390. if(!$is_vip){
  391. $this->error('VIP才能设置隐私权限');
  392. }
  393. $field_array = ['yinsi','yinshen','wuhen'];
  394. $data = [];
  395. foreach($field_array as $key => $field){
  396. if(!input('?'.$field)){
  397. continue;
  398. }
  399. $newone = input($field);
  400. $data[$field] = $newone;
  401. }
  402. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  403. $this->success();
  404. }
  405. /*
  406. * 修改用户的坐标
  407. * */
  408. public function change_longlat(){
  409. $longitude = input_post('longitude',0);
  410. $latitude = input_post('latitude',0);
  411. $cityname = input_post('cityname','');
  412. $provincename = input_post('provincename','');
  413. $plat_unique_id = input_post('plat_unique_id','');
  414. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  415. $this->error();
  416. }*/
  417. $data = [];
  418. $longitude && $data['longitude'] = $longitude;
  419. $latitude && $data['latitude'] = $latitude;
  420. $cityname && $data['cityname'] = $cityname;
  421. $provincename && $data['provincename'] = $provincename;
  422. $plat_unique_id && $data['plat_unique_id'] = $plat_unique_id;
  423. //传入了城市,但是没传入省,直接省名改空
  424. if(isset($data['cityname']) && !isset($data['provincename'])){
  425. $data['provincename'] = '';
  426. }
  427. //没传入城市,但是传入省,直接城市名改空
  428. if(!isset($data['cityname']) && isset($data['provincename'])){
  429. $data['cityname'] = '';
  430. }
  431. if(!empty($data)){
  432. Db::name('user')->where('id',$this->auth->id)->update($data);
  433. }
  434. $this->success();
  435. }
  436. //修改用户设备id
  437. public function change_plat_unique_id(){
  438. $plat_unique_id = input_post('plat_unique_id','');
  439. $data = [
  440. 'plat_unique_id' => $plat_unique_id,
  441. ];
  442. Db::name('user')->where('id',$this->auth->id)->update($data);
  443. $this->success();
  444. }
  445. /**
  446. * 修改手机号
  447. *
  448. * @ApiMethod (POST)
  449. * @param string $mobile 手机号
  450. * @param string $captcha 验证码
  451. */
  452. public function changemobile()
  453. {
  454. $user = $this->auth->getUser();
  455. $oldcaptcha = $this->request->request('oldcaptcha');
  456. $countrycode = $this->request->request('countrycode',86,'intval');
  457. $mobile = $this->request->request('mobile');
  458. $captcha = $this->request->request('captcha');
  459. if (!$oldcaptcha || !$countrycode || !$mobile || !$captcha) {
  460. $this->error(__('Invalid parameters'));
  461. }
  462. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  463. $this->error(__('Mobile is incorrect'));
  464. }*/
  465. $simplemobile = $mobile;
  466. $mobile = $countrycode.$mobile;
  467. if($user->mobile == $mobile){
  468. $this->error('新手机号不能与旧手机号相同');
  469. }
  470. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  471. $this->error(__('Mobile already exist'));
  472. }
  473. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  474. if (!$result) {
  475. $this->error(__('Captcha is incorrect'));
  476. }
  477. $result = Sms::check($mobile, $captcha, 'changemobile');
  478. if (!$result) {
  479. $this->error(__('Captcha is incorrect'));
  480. }
  481. Sms::flush($user->mobile, 'changemobile');
  482. Sms::flush($mobile, 'changemobile');
  483. $user->mobile = $mobile;
  484. $user->simplemobile = $simplemobile;
  485. $user->save();
  486. $this->success();
  487. }
  488. /**
  489. * 苹果注册来的,绑定手机号
  490. *
  491. * @ApiMethod (POST)
  492. * @param string $mobile 手机号
  493. * @param string $captcha 验证码
  494. */
  495. public function applebindmobile()
  496. {
  497. $countrycode = $this->request->request('countrycode',86,'intval');
  498. $mobile = $this->request->param('mobile');
  499. $captcha = $this->request->param('captcha');
  500. $iosUserId = $this->request->param('ios_user_id','');
  501. if (!$countrycode || !$mobile || !$captcha || !$iosUserId) {
  502. $this->error(__('Invalid parameters'));
  503. }
  504. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  505. $this->error(__('Mobile is incorrect'));
  506. }*/
  507. $simplemobile = $mobile;
  508. $mobile = $countrycode.$mobile;
  509. $result = Sms::check($mobile, $captcha, 'changemobile');
  510. if (!$result) {
  511. $this->error(__('Captcha is incorrect'));
  512. }
  513. //检查ios_user_id绑定的用户
  514. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  515. if ($user) {
  516. if ($user['status'] == -1) {
  517. $this->error('账户已经注销');
  518. }
  519. if ($user['status'] != 1) {
  520. $this->error(__('Account is locked'));
  521. }
  522. //如果已经有账号则直接登录
  523. $ret = $this->auth->direct($user['id']);
  524. $this->success('success',$this->userInfo('return'));
  525. }
  526. //新的ios用户
  527. $where = [];
  528. $where['mobile'] = $mobile;
  529. $userData = Db::name('user')->where($where)->find();//老用户
  530. if (!empty($userData)) {
  531. if (empty($userData['ios_user_id'])) {
  532. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  533. } else {
  534. if ($userData['ios_user_id'] != $iosUserId) {
  535. $this->error('该手机号已被其他用户绑定');
  536. }
  537. }
  538. $ret = $this->auth->direct($userData['id']);
  539. } else {
  540. $extend = [
  541. 'ios_user_id' => $iosUserId,
  542. 'simplemobile' => $simplemobile,
  543. ];
  544. $ret = $this->auth->register('', '','', $mobile, $extend);
  545. }
  546. if (!$ret) {
  547. $this->error($this->auth->getError());
  548. }
  549. $this->success('success',$this->userInfo('return'));
  550. }
  551. /**
  552. * 微信注册来的,绑定手机号
  553. *
  554. * @ApiMethod (POST)
  555. * @param string $mobile 手机号
  556. * @param string $captcha 验证码
  557. */
  558. public function bindmobile()
  559. {
  560. $countrycode = $this->request->param('countrycode',86,'intval');
  561. $mobile = $this->request->param('mobile');
  562. $captcha = $this->request->param('captcha');
  563. $code = $this->request->param('code');
  564. if (!$countrycode || !$mobile || !$captcha || !$code) {
  565. $this->error(__('Invalid parameters'));
  566. }
  567. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  568. $this->error(__('Mobile is incorrect'));
  569. }*/
  570. $simplemobile = $mobile;
  571. $mobile = $countrycode.$mobile;
  572. $result = Sms::check($mobile, $captcha, 'changemobile');
  573. if (!$result) {
  574. $this->error(__('Captcha is incorrect'));
  575. }
  576. $wechatCodeWhere['code'] = $code;
  577. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  578. if (empty($wechatCode)) {
  579. $this->error('请先微信登录');
  580. }
  581. //检查appid绑定的用户
  582. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  583. if ($user) {
  584. if ($user['status'] == -1) {
  585. $this->error('账户已注销');
  586. }
  587. if ($user['status'] != 1) {
  588. $this->error(__('Account is locked'));
  589. }
  590. //如果已经有账号则直接登录
  591. $ret = $this->auth->direct($user['id']);
  592. $this->success('success',$this->userInfo('return'));
  593. }
  594. //新的openid用户
  595. $where = [];
  596. $where['mobile'] = $mobile;
  597. $userData = Db::name('user')->where($where)->find();//老用户
  598. if (!empty($userData)) {
  599. if (empty($userData['wechat_openid'])) {
  600. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  601. } else {
  602. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  603. $this->error('该手机号已被其他用户绑定');
  604. }
  605. }
  606. $ret = $this->auth->direct($userData['id']);
  607. } else {
  608. $extend = [
  609. 'wechat_openid' => $wechatCode['openid'],
  610. 'simplemobile' => $simplemobile,
  611. ];
  612. $ret = $this->auth->register('', '','', $mobile, $extend);
  613. }
  614. if (!$ret) {
  615. $this->error($this->auth->getError());
  616. }
  617. $this->success('success',$this->userInfo('return'));
  618. }
  619. /**
  620. * 手机注册来的,绑定微信
  621. *
  622. * @ApiMethod (POST)
  623. * @param string $wechat_openid
  624. */
  625. public function bindopenid()
  626. {
  627. $wechat_openid = $this->request->request('wechat_openid');
  628. if (!$wechat_openid) {
  629. $this->error(__('Invalid parameters'));
  630. }
  631. if(!empty($this->auth->wechat_openid)){
  632. //$this->error('已经绑定了微信号');
  633. }
  634. $otherUserWhere['wechat_openid'] = $wechat_openid;
  635. $otherUserWhere['id'] = ['neq',$this->auth->id];
  636. if (\app\common\model\User::where($otherUserWhere)->find()) {
  637. $this->error('该微信号已被其他用户绑定');
  638. }
  639. $user = $this->auth->getUser();
  640. $user->wechat_openid = $wechat_openid;
  641. $user->save();
  642. $this->success('绑定成功',$this->userInfo('return'));
  643. }
  644. /**
  645. * 重置密码
  646. *
  647. * @ApiMethod (POST)
  648. * @param string $mobile 手机号
  649. * @param string $newpassword 新密码
  650. * @param string $captcha 验证码
  651. */
  652. public function resetpwd()
  653. {
  654. //$type = input("type");
  655. $type = 'mobile';
  656. $countrycode = input("countrycode",86,'intval');
  657. $mobile = input("mobile");
  658. // $email = input("email");
  659. $newpassword = input("newpassword");
  660. $captcha = input("captcha");
  661. if (!$countrycode || !$mobile || !$newpassword || !$captcha) {
  662. $this->error(__('Invalid parameters'));
  663. }
  664. $mobile = $countrycode.$mobile;
  665. if ($type == 'mobile') {
  666. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  667. $this->error(__('Mobile is incorrect'));
  668. }*/
  669. $user = \app\common\model\User::getByMobile($mobile);
  670. if (!$user) {
  671. $this->error(__('User not found'));
  672. }
  673. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  674. if (!$ret) {
  675. $this->error(__('Captcha is incorrect'));
  676. }
  677. Sms::flush($mobile, 'resetpwd');
  678. }
  679. //模拟一次登录
  680. $this->auth->direct($user->id);
  681. $ret = $this->auth->changepwd($newpassword, '', true);
  682. if ($ret) {
  683. $this->success(__('Reset password successful'));
  684. } else {
  685. $this->error($this->auth->getError());
  686. }
  687. }
  688. /**
  689. * 修改密码
  690. *
  691. * @ApiMethod (POST)
  692. * @param string $newpassword 新密码
  693. * @param string $oldpassword 旧密码
  694. */
  695. public function changepwd(){
  696. $newpassword = input('newpassword');
  697. $oldpassword = input('oldpassword','');
  698. if (!$newpassword) {
  699. $this->error(__('Invalid parameters'));
  700. }
  701. if($this->auth->password && empty($oldpassword)){
  702. $this->error('原密码必填');
  703. }
  704. if(empty($this->auth->password)){
  705. $ret = $this->auth->changepwd($newpassword, '', true);
  706. }else{
  707. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  708. }
  709. if ($ret) {
  710. $this->success('设置密码成功');
  711. } else {
  712. $this->error($this->auth->getError());
  713. }
  714. }
  715. /**
  716. * 记录当前登陆的设备ID,设备信息,IP等
  717. */
  718. public function changeDeviceIp()
  719. {
  720. // 接口防并发
  721. if (!$this->apiLimit(1, 5000)) {
  722. return ;
  723. }
  724. $user = $this->auth->getUser();
  725. $ip = request()->ip();
  726. $deviceId = $this->request->request('device_id','');
  727. $phoneModel = $this->request->request('phone_model','');
  728. $brand = $this->request->request('brand','');
  729. $apiVersion = $this->request->request('api_version','');
  730. $deviceOs = $this->request->request('device_os','');
  731. if ($ip !== $user->loginip){
  732. $update = [];
  733. $update['id'] = $user->id;
  734. $update['loginip'] = $ip;
  735. \app\common\model\User::update($update);
  736. }
  737. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  738. if (empty($userDeviceInfo)){
  739. $userDeviceInfo = new UserDeviceInfo();
  740. $userDeviceInfo->user_id = $user->u_id;
  741. }
  742. $userDeviceInfo->device_os = $deviceOs;
  743. $userDeviceInfo->device_id = $deviceId;
  744. $userDeviceInfo->phone_model = $phoneModel;
  745. $userDeviceInfo->brand = $brand;
  746. $userDeviceInfo->api_version = $apiVersion;
  747. $userDeviceInfo->save();
  748. //首页接口调用,这里不反回信息
  749. // $this->success("更新成功!");
  750. }
  751. //假注销
  752. public function cancleUser(){
  753. if (!$this->request->isPost()) {
  754. $this->error(__('Invalid parameters'));
  755. }
  756. //退出im
  757. // $tenIm = new Tenim();
  758. // $tenIm->loginoutim($this->auth->id);
  759. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  760. $this->auth->logout();
  761. $this->success('注销成功');
  762. }
  763. //公众号获取openid
  764. public function getUserOpenid_gzh(){
  765. $configValue = Service::getConfig('wechat');
  766. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  767. $rs = $wechat->getOpenid();
  768. $this->success('success',$rs);
  769. }
  770. /**
  771. * 微信内H5-JSAPI支付
  772. */
  773. public function jssdkBuildConfig() {
  774. $url = $this->request->request("url");
  775. $configValue = Service::getConfig('wechat');
  776. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  777. $sign = $wechat->getSignPackage(urldecode($url));
  778. $this->success("获取成功!",$sign);
  779. }
  780. }