User.php 28 KB

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