User.php 17 KB

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