User.php 26 KB

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