User.php 21 KB

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