User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. if(!empty($data)){
  424. Db::name('user')->where('id',$this->auth->id)->update($data);
  425. }
  426. $this->success();
  427. }
  428. //修改用户设备id
  429. public function change_plat_unique_id(){
  430. $plat_unique_id = input_post('plat_unique_id','');
  431. $data = [
  432. 'plat_unique_id' => $plat_unique_id,
  433. ];
  434. Db::name('user')->where('id',$this->auth->id)->update($data);
  435. $this->success();
  436. }
  437. /**
  438. * 修改手机号
  439. *
  440. * @ApiMethod (POST)
  441. * @param string $mobile 手机号
  442. * @param string $captcha 验证码
  443. */
  444. public function changemobile()
  445. {
  446. $user = $this->auth->getUser();
  447. $oldcaptcha = $this->request->request('oldcaptcha');
  448. $countrycode = $this->request->request('countrycode',86,'intval');
  449. $mobile = $this->request->request('mobile');
  450. $captcha = $this->request->request('captcha');
  451. if (!$oldcaptcha || !$countrycode || !$mobile || !$captcha) {
  452. $this->error(__('Invalid parameters'));
  453. }
  454. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  455. $this->error(__('Mobile is incorrect'));
  456. }*/
  457. $simplemobile = $mobile;
  458. $mobile = $countrycode.$mobile;
  459. if($user->mobile == $mobile){
  460. $this->error('新手机号不能与旧手机号相同');
  461. }
  462. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  463. $this->error(__('Mobile already exist'));
  464. }
  465. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  466. if (!$result) {
  467. $this->error(__('Captcha is incorrect'));
  468. }
  469. $result = Sms::check($mobile, $captcha, 'changemobile');
  470. if (!$result) {
  471. $this->error(__('Captcha is incorrect'));
  472. }
  473. Sms::flush($user->mobile, 'changemobile');
  474. Sms::flush($mobile, 'changemobile');
  475. $user->mobile = $mobile;
  476. $user->simplemobile = $simplemobile;
  477. $user->save();
  478. $this->success();
  479. }
  480. /**
  481. * 苹果注册来的,绑定手机号
  482. *
  483. * @ApiMethod (POST)
  484. * @param string $mobile 手机号
  485. * @param string $captcha 验证码
  486. */
  487. public function applebindmobile()
  488. {
  489. $countrycode = $this->request->request('countrycode',86,'intval');
  490. $mobile = $this->request->param('mobile');
  491. $captcha = $this->request->param('captcha');
  492. $iosUserId = $this->request->param('ios_user_id','');
  493. if (!$countrycode || !$mobile || !$captcha || !$iosUserId) {
  494. $this->error(__('Invalid parameters'));
  495. }
  496. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  497. $this->error(__('Mobile is incorrect'));
  498. }*/
  499. $simplemobile = $mobile;
  500. $mobile = $countrycode.$mobile;
  501. $result = Sms::check($mobile, $captcha, 'changemobile');
  502. if (!$result) {
  503. $this->error(__('Captcha is incorrect'));
  504. }
  505. //检查ios_user_id绑定的用户
  506. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  507. if ($user) {
  508. if ($user['status'] == -1) {
  509. $this->error('账户已经注销');
  510. }
  511. if ($user['status'] != 1) {
  512. $this->error(__('Account is locked'));
  513. }
  514. //如果已经有账号则直接登录
  515. $ret = $this->auth->direct($user['id']);
  516. $this->success('success',$this->userInfo('return'));
  517. }
  518. //新的ios用户
  519. $where = [];
  520. $where['mobile'] = $mobile;
  521. $userData = Db::name('user')->where($where)->find();//老用户
  522. if (!empty($userData)) {
  523. if (empty($userData['ios_user_id'])) {
  524. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  525. } else {
  526. if ($userData['ios_user_id'] != $iosUserId) {
  527. $this->error('该手机号已被其他用户绑定');
  528. }
  529. }
  530. $ret = $this->auth->direct($userData['id']);
  531. } else {
  532. $extend = [
  533. 'ios_user_id' => $iosUserId,
  534. 'simplemobile' => $simplemobile,
  535. ];
  536. $ret = $this->auth->register('', '','', $mobile, $extend);
  537. }
  538. if (!$ret) {
  539. $this->error($this->auth->getError());
  540. }
  541. $this->success('success',$this->userInfo('return'));
  542. }
  543. /**
  544. * 微信注册来的,绑定手机号
  545. *
  546. * @ApiMethod (POST)
  547. * @param string $mobile 手机号
  548. * @param string $captcha 验证码
  549. */
  550. public function bindmobile()
  551. {
  552. $countrycode = $this->request->param('countrycode',86,'intval');
  553. $mobile = $this->request->param('mobile');
  554. $captcha = $this->request->param('captcha');
  555. $code = $this->request->param('code');
  556. if (!$countrycode || !$mobile || !$captcha || !$code) {
  557. $this->error(__('Invalid parameters'));
  558. }
  559. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  560. $this->error(__('Mobile is incorrect'));
  561. }*/
  562. $simplemobile = $mobile;
  563. $mobile = $countrycode.$mobile;
  564. $result = Sms::check($mobile, $captcha, 'changemobile');
  565. if (!$result) {
  566. $this->error(__('Captcha is incorrect'));
  567. }
  568. $wechatCodeWhere['code'] = $code;
  569. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  570. if (empty($wechatCode)) {
  571. $this->error('请先微信登录');
  572. }
  573. //检查appid绑定的用户
  574. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  575. if ($user) {
  576. if ($user['status'] == -1) {
  577. $this->error('账户已注销');
  578. }
  579. if ($user['status'] != 1) {
  580. $this->error(__('Account is locked'));
  581. }
  582. //如果已经有账号则直接登录
  583. $ret = $this->auth->direct($user['id']);
  584. $this->success('success',$this->userInfo('return'));
  585. }
  586. //新的openid用户
  587. $where = [];
  588. $where['mobile'] = $mobile;
  589. $userData = Db::name('user')->where($where)->find();//老用户
  590. if (!empty($userData)) {
  591. if (empty($userData['wechat_openid'])) {
  592. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  593. } else {
  594. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  595. $this->error('该手机号已被其他用户绑定');
  596. }
  597. }
  598. $ret = $this->auth->direct($userData['id']);
  599. } else {
  600. $extend = [
  601. 'wechat_openid' => $wechatCode['openid'],
  602. 'simplemobile' => $simplemobile,
  603. ];
  604. $ret = $this->auth->register('', '','', $mobile, $extend);
  605. }
  606. if (!$ret) {
  607. $this->error($this->auth->getError());
  608. }
  609. $this->success('success',$this->userInfo('return'));
  610. }
  611. /**
  612. * 手机注册来的,绑定微信
  613. *
  614. * @ApiMethod (POST)
  615. * @param string $wechat_openid
  616. */
  617. public function bindopenid()
  618. {
  619. $wechat_openid = $this->request->request('wechat_openid');
  620. if (!$wechat_openid) {
  621. $this->error(__('Invalid parameters'));
  622. }
  623. if(!empty($this->auth->wechat_openid)){
  624. //$this->error('已经绑定了微信号');
  625. }
  626. $otherUserWhere['wechat_openid'] = $wechat_openid;
  627. $otherUserWhere['id'] = ['neq',$this->auth->id];
  628. if (\app\common\model\User::where($otherUserWhere)->find()) {
  629. $this->error('该微信号已被其他用户绑定');
  630. }
  631. $user = $this->auth->getUser();
  632. $user->wechat_openid = $wechat_openid;
  633. $user->save();
  634. $this->success('绑定成功',$this->userInfo('return'));
  635. }
  636. /**
  637. * 重置密码
  638. *
  639. * @ApiMethod (POST)
  640. * @param string $mobile 手机号
  641. * @param string $newpassword 新密码
  642. * @param string $captcha 验证码
  643. */
  644. public function resetpwd()
  645. {
  646. //$type = input("type");
  647. $type = 'mobile';
  648. $countrycode = input("countrycode",86,'intval');
  649. $mobile = input("mobile");
  650. // $email = input("email");
  651. $newpassword = input("newpassword");
  652. $captcha = input("captcha");
  653. if (!$countrycode || !$mobile || !$newpassword || !$captcha) {
  654. $this->error(__('Invalid parameters'));
  655. }
  656. $mobile = $countrycode.$mobile;
  657. if ($type == 'mobile') {
  658. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  659. $this->error(__('Mobile is incorrect'));
  660. }*/
  661. $user = \app\common\model\User::getByMobile($mobile);
  662. if (!$user) {
  663. $this->error(__('User not found'));
  664. }
  665. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  666. if (!$ret) {
  667. $this->error(__('Captcha is incorrect'));
  668. }
  669. Sms::flush($mobile, 'resetpwd');
  670. }
  671. //模拟一次登录
  672. $this->auth->direct($user->id);
  673. $ret = $this->auth->changepwd($newpassword, '', true);
  674. if ($ret) {
  675. $this->success(__('Reset password successful'));
  676. } else {
  677. $this->error($this->auth->getError());
  678. }
  679. }
  680. /**
  681. * 修改密码
  682. *
  683. * @ApiMethod (POST)
  684. * @param string $newpassword 新密码
  685. * @param string $oldpassword 旧密码
  686. */
  687. public function changepwd(){
  688. $newpassword = input('newpassword');
  689. $oldpassword = input('oldpassword','');
  690. if (!$newpassword) {
  691. $this->error(__('Invalid parameters'));
  692. }
  693. if($this->auth->password && empty($oldpassword)){
  694. $this->error('原密码必填');
  695. }
  696. if(empty($this->auth->password)){
  697. $ret = $this->auth->changepwd($newpassword, '', true);
  698. }else{
  699. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  700. }
  701. if ($ret) {
  702. $this->success('设置密码成功');
  703. } else {
  704. $this->error($this->auth->getError());
  705. }
  706. }
  707. /**
  708. * 记录当前登陆的设备ID,设备信息,IP等
  709. */
  710. public function changeDeviceIp()
  711. {
  712. // 接口防并发
  713. if (!$this->apiLimit(1, 5000)) {
  714. return ;
  715. }
  716. $user = $this->auth->getUser();
  717. $ip = request()->ip();
  718. $deviceId = $this->request->request('device_id','');
  719. $phoneModel = $this->request->request('phone_model','');
  720. $brand = $this->request->request('brand','');
  721. $apiVersion = $this->request->request('api_version','');
  722. $deviceOs = $this->request->request('device_os','');
  723. if ($ip !== $user->loginip){
  724. $update = [];
  725. $update['id'] = $user->id;
  726. $update['loginip'] = $ip;
  727. \app\common\model\User::update($update);
  728. }
  729. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  730. if (empty($userDeviceInfo)){
  731. $userDeviceInfo = new UserDeviceInfo();
  732. $userDeviceInfo->user_id = $user->u_id;
  733. }
  734. $userDeviceInfo->device_os = $deviceOs;
  735. $userDeviceInfo->device_id = $deviceId;
  736. $userDeviceInfo->phone_model = $phoneModel;
  737. $userDeviceInfo->brand = $brand;
  738. $userDeviceInfo->api_version = $apiVersion;
  739. $userDeviceInfo->save();
  740. //首页接口调用,这里不反回信息
  741. // $this->success("更新成功!");
  742. }
  743. //假注销
  744. public function cancleUser(){
  745. if (!$this->request->isPost()) {
  746. $this->error(__('Invalid parameters'));
  747. }
  748. //退出im
  749. // $tenIm = new Tenim();
  750. // $tenIm->loginoutim($this->auth->id);
  751. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  752. $this->auth->logout();
  753. $this->success('注销成功');
  754. }
  755. //公众号获取openid
  756. public function getUserOpenid_gzh(){
  757. $configValue = Service::getConfig('wechat');
  758. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  759. $rs = $wechat->getOpenid();
  760. $this->success('success',$rs);
  761. }
  762. /**
  763. * 微信内H5-JSAPI支付
  764. */
  765. public function jssdkBuildConfig() {
  766. $url = $this->request->request("url");
  767. $configValue = Service::getConfig('wechat');
  768. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  769. $sign = $wechat->getSignPackage(urldecode($url));
  770. $this->success("获取成功!",$sign);
  771. }
  772. }