Main.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace User\Src;
  3. use Dever;
  4. class Main
  5. {
  6. /**
  7. * 获取验证码
  8. * @return mixed
  9. */
  10. public function getCode()
  11. {
  12. return Dever::load('sms/api.sendCode', 'code', $this->getMobile());
  13. }
  14. /**
  15. * 检测验证码
  16. * @return mixed
  17. */
  18. private function checkCode()
  19. {
  20. return Dever::load('sms/api.checkCode', 'code', $this->getMobile(), Dever::input('code'), 1);
  21. }
  22. /**
  23. * 获取手机号
  24. * @return mixed
  25. */
  26. private function getMobile()
  27. {
  28. $rule = Dever::rule('mobile');
  29. $mobile = Dever::input('mobile');
  30. if (!$mobile) {
  31. Dever::alert('手机号不能为空');
  32. }
  33. if (!preg_match($rule, $mobile)) {
  34. Dever::alert('请填写正确的手机号');
  35. }
  36. return $mobile;
  37. }
  38. /**
  39. * 登录以及注册
  40. * @return mixed
  41. */
  42. public function login()
  43. {
  44. $password = Dever::input('password');
  45. if ($password) {
  46. $param['mobile'] = $this->getMobile();
  47. } else {
  48. $param['mobile'] = $this->checkCode();
  49. }
  50. $param['project_id'] = Dever::input('project_id', 1);
  51. $info = Dever::db('user/info')->one($param);
  52. $param['system_source'] = Dever::input('system_source', 1);
  53. if ($info) {
  54. $password = sha1($password);
  55. if ($password != $info['password']) {
  56. Dever::alert('您的账号或密码错误');
  57. }
  58. $uid = $info['id'];
  59. } else {
  60. $param['name'] = Dever::input('name');
  61. if (!$param['name']) {
  62. $param['name'] = Dever::hide($param['mobile']);
  63. }
  64. if ($password) {
  65. $param['password'] = $password;
  66. }
  67. $uid = Dever::db('user/info')->insert($param);
  68. if ($uid) {
  69. Dever::score($uid, 'reg', '账号注册');
  70. if (Dever::project('message')) {
  71. $project = Dever::db('user/project')->find($param['project_id']);
  72. $content = '亲爱的' . $param['name'] . ',欢迎来到' . $project['name'];
  73. Dever::load('message/lib/data')->push(-1, $uid, '欢迎语', $content, 1);
  74. }
  75. $invite = Dever::input('invite');
  76. if ($invite && Dever::project('invite')) {
  77. Dever::load('invite/api')->setRelation($uid, false, $invite);
  78. }
  79. }
  80. }
  81. # 记录登录日志
  82. $log['uid'] = $uid;
  83. $log['project_id'] = $param['project_id'];
  84. $log['system_source'] = $param['system_source'];
  85. Dever::db('user/login')->insert($log);
  86. Dever::load('user/lib/info')->setProject($uid, $param['project_id']);
  87. return Dever::load('user/lib/info')->getSign($uid);
  88. }
  89. }