User.php 26 KB

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