User.php 26 KB

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