mobile.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. class Mobile extends OAuth2Client {
  8. public function __construct($ak, $sk) {
  9. parent::__construct($ak, $sk);
  10. $this->stateParam['from'] = 'mobile';
  11. }
  12. public function showLoginUrl($calback_url = '') {
  13. }
  14. public function user() {
  15. global $_GPC, $_W;
  16. $mobile = trim($_GPC['username']);
  17. $member['password'] = $_GPC['password'];
  18. pdo_delete('users_failed_login', array('lastupdate <' => TIMESTAMP-3600));
  19. $failed = pdo_get('users_failed_login', array('username' => $mobile, 'ip' => CLIENT_IP));
  20. if ($failed['count'] >= 5) {
  21. return error('-1', '输入密码错误次数超过5次,请在1小时后再登录');
  22. }
  23. if (!empty($_W['setting']['copyright']['verifycode'])) {
  24. $verify = trim($_GPC['verify']);
  25. if (empty($verify)) {
  26. return error('-1', '请输入验证码');
  27. }
  28. $result = checkcaptcha($verify);
  29. if (empty($result)) {
  30. return error('-1', '输入验证码错误');
  31. }
  32. }
  33. if (empty($mobile)) {
  34. return error('-1', '请输入要登录的手机号');
  35. }
  36. if (!preg_match(REGULAR_MOBILE, $mobile)) {
  37. return error(-1, '手机号格式不正确');
  38. }
  39. if (empty($member['password'])) {
  40. return error('-1', '请输入密码');
  41. }
  42. $user_profile = table('users_profile')->getByMobile($mobile);
  43. if (empty($user_profile)) {
  44. return error(-1, '手机号未注册');
  45. }
  46. $member['uid'] = $user_profile['uid'];
  47. $member['type'] = $this->user_type;
  48. return $member;
  49. }
  50. public function validateMobile() {
  51. global $_GPC;
  52. $mobile = $_GPC['mobile'];
  53. if (empty($mobile)) {
  54. return error(-1, '手机号不能为空');
  55. }
  56. if (!preg_match(REGULAR_MOBILE, $mobile)) {
  57. return error(-1, '手机号格式不正确');
  58. }
  59. $mobile_exists = table('users_profile')->getByMobile($mobile);
  60. if (!empty($mobile_exists)) {
  61. return error(-1, '手机号已存在');
  62. }
  63. return true;
  64. }
  65. public function register() {
  66. global $_GPC;
  67. load()->model('user');
  68. $member = array();
  69. $profile = array();
  70. $smscode = trim($_GPC['smscode']);
  71. $mobile = trim($_GPC['mobile']);
  72. $member['password'] = $_GPC['password'];
  73. if (empty($smscode)) {
  74. return error(-1, '短信验证码不能为空');
  75. }
  76. load()->model('utility');
  77. $verify_info = utility_smscode_verify(0, $mobile, $smscode);
  78. if (is_error($verify_info)) {
  79. return error(-1, $verify_info['message']);
  80. }
  81. if(istrlen($member['password']) < 8) {
  82. return error(-1, '必须输入密码,且密码长度不得低于8位。');
  83. }
  84. $member['username'] = $mobile;
  85. $member['openid'] = $mobile;
  86. $member['register_type'] = USER_REGISTER_TYPE_MOBILE;
  87. $member['owner_uid'] = intval($_GPC['owner_uid']);
  88. $profile['mobile'] = $mobile;
  89. $register = array(
  90. 'member' => $member,
  91. 'profile' => $profile
  92. );
  93. return parent::user_register($register);
  94. }
  95. public function login() {
  96. return $this->user();
  97. }
  98. public function bind() {
  99. global $_GPC, $_W;
  100. $mobile = safe_gpc_string($_GPC['mobile']);
  101. $user = table('users')->getById($_W['uid']);
  102. if (empty($user)) {
  103. return error(-1, '请先登录');
  104. }
  105. $user_profile = table('users_profile')->getByUid($_W['uid']);
  106. $param_validate = $this->paramValidate();
  107. if (is_error($param_validate)) {
  108. return $param_validate;
  109. }
  110. if (empty($user_profile)) {
  111. pdo_insert('users_profile', array('uid' => $_W['uid'], 'mobile' => $mobile));
  112. } else {
  113. pdo_update('users_profile', array('mobile' => $mobile), array('id' => $user_profile['id']));
  114. }
  115. pdo_insert('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE, 'third_nickname' => $mobile));
  116. return error(0, '绑定成功');
  117. }
  118. public function unbind() {
  119. global $_GPC, $_W;
  120. $mobile = safe_gpc_string($_GPC['mobile']);
  121. $user_profile = table('users_profile')->getByUid($_W['uid']);
  122. $param_validate = $this->paramValidate();
  123. if (is_error($param_validate)) {
  124. return $param_validate;
  125. }
  126. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  127. pdo_update('users_profile', array('mobile' => ''), array('id' => $user_profile['id']));
  128. pdo_delete('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE));
  129. return error(0, '解除绑定成功');
  130. }
  131. public function isbind() {
  132. global $_W;
  133. $bind_info = table('users_bind')->getByTypeAndUid(USER_REGISTER_TYPE_MOBILE, $_W['uid']);
  134. return !empty($bind_info['bind_sign']);
  135. }
  136. public function paramValidate() {
  137. global $_GPC;
  138. $mobile = trim($_GPC['mobile']);
  139. $image_code =trim($_GPC['imagecode']);
  140. $sms_code = trim($_GPC['smscode']);
  141. if (empty($sms_code)) {
  142. return error(-1, '短信验证码不能为空');
  143. }
  144. if (empty($image_code)) {
  145. return error(-1, '图形验证码不能为空');
  146. }
  147. $captcha = checkcaptcha($image_code);
  148. if (empty($captcha)) {
  149. return error(-1, '图形验证码错误,请重新获取');
  150. }
  151. load()->model('utility');
  152. $verify_info = utility_smscode_verify(0, $mobile, $sms_code);
  153. if (is_error($verify_info)) {
  154. return error(-1, $verify_info['message']);
  155. }
  156. }
  157. }