User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms;
  5. use app\common\model\Area;
  6. use app\common\service\WxOpenService;
  7. use fast\Random;
  8. use think\Config;
  9. use think\Db;
  10. use think\Session;
  11. use think\Validate;
  12. use app\common\model\User as UserModel;
  13. use app\common\library\Ems;
  14. /**
  15. * 会员接口
  16. */
  17. class User extends Api
  18. {
  19. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd','wx_bind_qr','wx_bind_qr_check'];
  20. protected $noNeedRight='*';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. if (!Config::get('fastadmin.usercenter')) {
  25. $this->error(__('User center already closed'));
  26. }
  27. }
  28. /**
  29. * 会员信息
  30. * @ApiReturnParams (name=id,description=用户ID)
  31. * @ApiReturnParams (name=username,description=用户名)
  32. * @ApiReturnParams (name=nickname,description=昵称)
  33. * @ApiReturnParams (name=mobile,description=手机号)
  34. * @ApiReturnParams (name=avatar,description=头像)
  35. * @ApiReturnParams (name=age,description=年龄)
  36. * @ApiReturnParams (name=gender,description="性别1男2女")
  37. * @ApiReturnParams (name=level_text,description=会员级别标题)
  38. * @ApiReturnParams (name=level,description="会员级别,0普通用户10钻石20白金30金卡")
  39. * @ApiReturnParams (name=level_expire,description="会员级别到期时间")
  40. * @ApiReturnParams (name=money,description=余额)
  41. * @ApiReturnParams (name=has_follow,description=是否关注)
  42. * @ApiReturnParams (name=verification,description=认证信息)
  43. * @ApiReturnParams (name=province,description=省对象)
  44. * @ApiReturnParams (name=city,description=市对象)
  45. * @ApiReturnParams (name=county,description=县对象)
  46. * @ApiReturnParams (name=live_addr,description=居住地)
  47. * @ApiReturnParams (name=com_name,description=公司名称)
  48. * @ApiReturnParams (name=wx_account,description=微信号)
  49. * @ApiReturnParams (name=level_expire,description=会员到期时间)
  50. */
  51. public function index()
  52. {
  53. $user=$this->auth->getUser();
  54. if(!$user['userinfo']){
  55. $user->userinfo()->save([]);
  56. }
  57. $user=$user
  58. ->append([
  59. 'verification',
  60. 'userinfo',
  61. ]);
  62. $this->success('', $user);
  63. }
  64. /**
  65. * 会员登录
  66. *
  67. * @ApiMethod (POST)
  68. * @param string $account 账号
  69. * @param string $password 密码
  70. */
  71. public function login()
  72. {
  73. $account = $this->request->post('account');
  74. $password = $this->request->post('password');
  75. if (!$account || !$password) {
  76. $this->error(__('Invalid parameters'));
  77. }
  78. $ret = $this->auth->login($account, $password);
  79. if ($ret) {
  80. $data = ['userinfo' => $this->auth->getUserinfo()];
  81. $this->success(__('Logged in successful'), $data);
  82. } else {
  83. $this->error($this->auth->getError());
  84. }
  85. }
  86. /**
  87. * 手机验证码登录
  88. *
  89. * @ApiMethod (POST)
  90. * @param string $mobile 手机号
  91. * @param string $captcha 验证码
  92. */
  93. public function mobilelogin()
  94. {
  95. $mobile = $this->request->post('mobile');
  96. $captcha = $this->request->post('captcha');
  97. if (!$mobile || !$captcha) {
  98. $this->error(__('Invalid parameters'));
  99. }
  100. if (!Validate::regex($mobile, "^1\d{10}$")) {
  101. $this->error(__('Mobile is incorrect'));
  102. }
  103. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  104. $this->error(__('Captcha is incorrect'));
  105. }
  106. $user = UserModel::getByMobile($mobile);
  107. if ($user) {
  108. if ($user->status != 'normal') {
  109. $this->error(__('Account is locked'));
  110. }
  111. //如果已经有账号则直接登录
  112. $ret = $this->auth->direct($user->id);
  113. } else {
  114. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  115. }
  116. if ($ret) {
  117. Sms::flush($mobile, 'mobilelogin');
  118. $data = ['userinfo' => $this->auth->getUserinfo()];
  119. $this->success(__('Logged in successful'), $data);
  120. } else {
  121. $this->error($this->auth->getError());
  122. }
  123. }
  124. /**
  125. * 注册会员
  126. *
  127. * @ApiMethod (POST)
  128. * @ApiParams (name=mobile,description="手机号")
  129. * @ApiParams (name=username,description="用户名")
  130. * @ApiParams (name=password,description="密码")
  131. * @ApiParams (name=password_confirm,description="确认密码")
  132. * @ApiParams (name=live_addr,description="居住地")
  133. * @ApiParams (name=com_name,description="公司名称")
  134. * @ApiParams (name=email,description="邮箱")
  135. * @ApiParams (name=code,description="验证码")
  136. * @ApiParams (name=openid,description="绑定的微信openid")
  137. */
  138. public function register()
  139. {
  140. $input=$this->_validate([
  141. 'mobile'=>['require','mobile'],
  142. 'username'=>['require'],
  143. 'password|密码'=>['require','min:6'],
  144. 'password_confirm|确认密码'=>['require','confirm:password'],
  145. 'live_addr'=>['max:200'],
  146. 'com_name'=>['max:100'],
  147. 'email'=>['email'],
  148. 'code'=>['require','integer'],
  149. ]);
  150. $username = $this->request->post('username');
  151. $password = $this->request->post('password');
  152. $email = $this->request->post('email');
  153. $mobile = $this->request->post('mobile');
  154. $code = $this->request->post('code');
  155. if (!$username || !$password) {
  156. $this->error(__('Invalid parameters'));
  157. }
  158. if ($email && !Validate::is($email, "email")) {
  159. $this->error(__('Email is incorrect'));
  160. }
  161. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  162. $this->error(__('Mobile is incorrect'));
  163. }
  164. $ret = Sms::check($mobile, $code, 'register');
  165. if (!$ret) {
  166. $this->error(__('Captcha is incorrect'));
  167. }
  168. Db::startTrans();
  169. $ret = $this->auth->register($username, $password, $email?:null, $mobile?:null,[
  170. 'live_addr'=>$input['live_addr']??null,
  171. 'com_name'=>$input['com_name']??null,
  172. 'openid' =>$input['openid']??null
  173. ]);
  174. if ($ret) {
  175. Db::commit();
  176. $data = ['userinfo' => $this->auth->getUserinfo()];
  177. $this->success(__('Sign up successful'), $data);
  178. } else {
  179. Db::rollback();
  180. $this->error($this->auth->getError());
  181. }
  182. }
  183. /**
  184. * 修改会员个人信息
  185. *
  186. * @ApiMethod (POST)
  187. * @ApiParams (name=avatar,description=头像地址)
  188. * @ApiParams (name=nickname,description=昵称)
  189. * @ApiParams (name=bio,description=个人简介)
  190. * @ApiParams (name=age,description=年龄)
  191. * @ApiParams (name=gender,description="性别1男2女")
  192. * @ApiParams (name=county_id,description=区县ID)
  193. * @ApiParams (name=wx_account,description=微信号)
  194. * @ApiParams (name=live_addr,description=居住地)
  195. * @ApiParams (name=com_name,description=公司名称)
  196. */
  197. public function profile()
  198. {
  199. $data=$this->_validate([
  200. 'avatar|头像'=>['require','url'],
  201. 'nickname|昵称'=>['require','max:12'],
  202. 'age|年龄'=>['require','integer','gt:0'],
  203. //'county_id|地区'=>['require','integer','gt:0'],
  204. 'county_id|地区'=>['require'],
  205. 'gender|性别'=>['require','integer','in:1,2'],
  206. 'bio|性别'=>['max:100'],
  207. ]);
  208. $user = $this->auth->getUser();
  209. Db::startTrans();
  210. $user= UserModel::lock(true)->find($user->id);
  211. $nickname = $data['nickname']??'';
  212. $bio = $data['bio'];
  213. $avatar = $data['avatar']??'';
  214. if ($nickname) {
  215. /*$exists = UserModel::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  216. if ($exists) {
  217. $this->error(__('Nickname already exists'));
  218. }*/
  219. $user->nickname = $nickname;
  220. }
  221. if($bio) {
  222. $user->bio = $bio;
  223. }
  224. if($avatar) {
  225. $user->avatar = $avatar;
  226. }
  227. if(!empty($data['age'])){
  228. $user->age=$data['age'];
  229. }
  230. if(isset($data['gender'])){
  231. $user->gender=$data['gender'];
  232. }
  233. if(!empty($data['wx_account'])){
  234. $user['wx_account']=$data['wx_account'];
  235. }
  236. if(!empty($data['live_addr'])){
  237. $user['live_addr']=$data['live_addr'];
  238. }
  239. if(!empty($data['com_name'])){
  240. $user['com_name']=$data['com_name'];
  241. }
  242. if(!empty($data['county_id'])){
  243. $county=Area::area()->where('name|shortname',$data['county_id'])->find();
  244. if(!$county) {
  245. $this->error('地区不存在');
  246. }
  247. $user->county_id=$county['id'];
  248. $user->city_id=$county['pid'];
  249. $user->province_id=Area::where('id',$county['pid'])->value('pid');
  250. }
  251. $user->save();
  252. $custom=array_column($user->userinfo->custom,'value','key');
  253. foreach (config('site.userApprove')?:[] as $key=>$value){
  254. if(!empty($data['custom'][$key])){
  255. $custom[$key]=$data['custom'][$key];
  256. }
  257. }
  258. if($custom) {
  259. $user->userinfo->save(['custom' => $custom]);
  260. }
  261. Db::commit();
  262. $this->success('',[]);
  263. }
  264. /**
  265. * 生成绑定码
  266. * @ApiParams (name=phone,description="手机号,event等于1时需要")
  267. * @ApiParams (name=sms_code,description="手机验证码,event等于1时需要")
  268. * @ApiParams (name=event,description="1注册2个人中心绑定3扫码登录")
  269. * @ApiParams (name=callback_url,description="扫完码跳转url")
  270. * @ApiReturnParams (name=url,description=二维码链接)
  271. * @ApiReturnParams (name=key,description="扫码登录key")
  272. */
  273. public function wx_bind_qr(){
  274. $this->_validate([
  275. 'callback_url'=>['require','url'],
  276. ]);
  277. $data=$this->checkBindParam();
  278. if($data['event']==3){
  279. $data['phone']=session_create_id();
  280. }
  281. $info=[];
  282. $info['url']=WxOpenService::getBindUrl($data['phone'],$data['event'],$data['callback_url']);
  283. $this->success('',$info);
  284. }
  285. protected function checkBindParam(){
  286. $data=$this->_validate([
  287. 'phone|手机号'=>['requireIf:event,1','mobile'],
  288. 'sms_code|手机验证码'=>['requireIf:event,1','integer'],
  289. 'event'=>['require','in:1,2,3']
  290. ]);
  291. if($data['event']==2){
  292. $user=$this->auth->getUser();
  293. if(!$user){
  294. $this->error('请登录');
  295. }
  296. $data['phone']=$user['mobile'];
  297. }
  298. if($data['event']==1){
  299. $checkSms=Sms::check($data['phone'],$data['sms_code'],'register');
  300. if(!$checkSms){
  301. $this->error('验证码错误');
  302. }
  303. }
  304. return $data;
  305. }
  306. /**
  307. * 检查是否已绑定
  308. * @ApiParams (name=phone,description="手机号,event等于1时需要")
  309. * @ApiParams (name=sms_code,description="手机验证码,event等于1时需要")
  310. * @ApiParams (name=event,description="1注册2个人中心绑定3扫码登录")
  311. * @ApiParams (name=code,description="微信回传的code")
  312. * @ApiParams (name=key,description="扫码登录key")
  313. * @ApiReturnParams (name=bind,description="是否绑定成功")
  314. * @ApiReturnParams (name=openid,description="openid")
  315. * @ApiReturnParams (name=user,description="用户信息,仅扫码登录有")
  316. * @ApiReturnParams (name="user.token",description="token")
  317. */
  318. public function wx_bind_qr_check(){
  319. $this->_validate([
  320. 'code'=>['require'],
  321. ]);
  322. $data=$this->checkBindParam();
  323. if($data['event']==1){
  324. $key=$data['phone'];
  325. }elseif ($data['event']==2){
  326. $key=$this->auth->getUser()['mobile'];
  327. }elseif($data['event']==3){
  328. $key=$data['key'];
  329. }
  330. list($res,$openid)=WxOpenService::bind($key,$data['event']);
  331. if(!$res){
  332. $this->error($openid);
  333. }
  334. $info=[
  335. 'bind'=>false,
  336. 'openid'=>'',
  337. 'user'=>null
  338. ];
  339. if(in_array($data['event'],[1,2])) {
  340. $info['openid'] = $openid;
  341. if ($info['openid']) {
  342. $info['bind'] = true;
  343. }
  344. }elseif ($data['event']==3){
  345. $user=\app\common\model\User::where('openid',$openid)->find();
  346. if(!$user){
  347. $this->error('用户未注册',['unregister'=>1]);
  348. }
  349. if($user['status']=='hidden'){
  350. $this->error('用户被禁用');
  351. }
  352. $this->auth->direct($user['id']);
  353. $info['user']=$this->auth->getUserinfo();
  354. }
  355. $this->success('',$info);
  356. }
  357. /**
  358. * 密码方式修改密码
  359. * @ApiParams (name=old_pwd,description=旧密码)
  360. * @ApiParams (name=new_pwd,description=旧密码)
  361. * @ApiParams (name=new_pwd_confirm,description=旧密码确认密码)
  362. */
  363. public function changepwd(){
  364. $user=$this->auth->getUser();
  365. $data=$this->_validate([
  366. 'old_pwd|旧密码'=>['require','min:6'],
  367. 'new_pwd|新密码'=>['require','min:6'],
  368. 'new_pwd_confirm|确认新密码'=>['require','min:6','confirm:new_pwd'],
  369. ]);
  370. if ($user->password != $this->auth->getEncryptPassword($data['old_pwd'], $user->salt)) {
  371. $this->error('密码错误');
  372. }
  373. $this->auth->changepwd($data['new_pwd']);
  374. $this->success();
  375. }
  376. /**
  377. * 重置密码
  378. *
  379. * @ApiMethod (POST)
  380. * @param string mobile 手机号重置
  381. * @param string email 邮箱重置
  382. * @param string newpassword 新密码
  383. * @param string newpassword_confirm 新密码
  384. * @param string captcha 验证码
  385. */
  386. public function resetpwd()
  387. {
  388. $this->_validate([
  389. 'newpassword'=>['require','min:6'],
  390. 'newpassword_confirm'=>['require','min:6'],
  391. ]);
  392. $mobile = $this->request->post("mobile");
  393. $email = $this->request->post("email");
  394. $newpassword = $this->request->post("newpassword");
  395. $newpassword_confirm = $this->request->post("newpassword_confirm");
  396. $captcha = $this->request->post("captcha");
  397. if (!$newpassword || !$captcha || !$newpassword_confirm) {
  398. $this->error(__('Invalid parameters'));
  399. }
  400. if($newpassword!==$newpassword_confirm){
  401. $this->error('两次密码不一致');
  402. }
  403. $type = $mobile?'mobile':'email';
  404. //验证Token
  405. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  406. $this->error(__('Password must be 6 to 30 characters'));
  407. }
  408. if ($type == 'mobile') {
  409. if (!Validate::regex($mobile, "^1\d{10}$")) {
  410. $this->error(__('Mobile is incorrect'));
  411. }
  412. $user = UserModel::getByMobile($mobile);
  413. if (!$user) {
  414. $this->error(__('用户不存在'));
  415. }
  416. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  417. if (!$ret) {
  418. $this->error(__('Captcha is incorrect'));
  419. }
  420. Sms::flush($mobile, 'resetpwd');
  421. } elseif($type=='email') {
  422. if (!Validate::is($email, "email")) {
  423. $this->error(__('Email is incorrect'));
  424. }
  425. $user = UserModel::getByEmail($email);
  426. if (!$user) {
  427. $this->error(__('用户不存在'));
  428. }
  429. $ret = Ems::check($email, $captcha, 'resetpwd');
  430. if (!$ret) {
  431. $this->error(__('Captcha is incorrect'));
  432. }
  433. Ems::flush($email, 'resetpwd');
  434. }else{
  435. $this->error('无法完成重置');
  436. }
  437. //模拟一次登录
  438. $this->auth->direct($user->id);
  439. $ret = $this->auth->changepwd($newpassword, '', true);
  440. if ($ret) {
  441. $this->success(__('Reset password successful'));
  442. } else {
  443. $this->error($this->auth->getError());
  444. }
  445. }
  446. }