Connectsms.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /*
  3. * 手机验证码
  4. */
  5. namespace app\home\controller;
  6. use think\facade\Lang;
  7. /**
  8. * ============================================================================
  9. *
  10. * ============================================================================
  11. *
  12. * ----------------------------------------------------------------------------
  13. *
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Connectsms extends BaseMall
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/login.lang.php');
  23. }
  24. /**
  25. * 短信动态码
  26. */
  27. public function get_captcha()
  28. {
  29. header("Content-Type: text/html;charset=utf-8");
  30. $sms_mobile = input('param.sms_mobile');
  31. if (strlen($sms_mobile) == 11) {
  32. $log_type = input('param.type'); //短信类型:1为注册,2为登录,3为找回密码
  33. $member_model = model('member');
  34. $member = $member_model->getMemberInfo(array('member_mobile' => $sms_mobile));
  35. $sms_captcha = rand(100000, 999999);
  36. $log_msg = lang('ds_you_in') . '' . date("Y-m-d");
  37. switch ($log_type) {
  38. case '1':
  39. if (config('ds_config.sms_register') != 1) {
  40. echo lang('system_obile_registration_function');
  41. exit;
  42. }
  43. if (!empty($member)) {
  44. //检查手机号是否已被注册
  45. echo lang('change_another_number');;
  46. exit;
  47. }
  48. $mailmt_code = 'register';
  49. break;
  50. case '2':
  51. if (config('ds_config.sms_login') != 1) {
  52. echo lang('enable_mobile_phone_login');
  53. exit;
  54. }
  55. if (empty($member)) {
  56. //检查手机号是否已绑定会员
  57. echo lang('check_correct_number');
  58. exit;
  59. }
  60. $mailmt_code = 'login';
  61. break;
  62. case '3':
  63. if (config('ds_config.sms_password') != 1) {
  64. echo lang('mobile_back_password');
  65. exit;
  66. }
  67. if (empty($member)) {
  68. //检查手机号是否已绑定会员
  69. echo lang('check_correct_number');
  70. exit;
  71. }
  72. $mailmt_code = 'reset_password';
  73. break;
  74. default:
  75. echo lang('param_error');
  76. exit;
  77. break;
  78. }
  79. $smslog_model = model('smslog');
  80. $mailtemplates_model = model('mailtemplates');
  81. $tpl_info = $mailtemplates_model->getTplInfo(array('mailmt_code' => $mailmt_code));
  82. $param = array();
  83. $param['code'] = $sms_captcha;
  84. $ten_param = array($sms_captcha);
  85. $message = ds_replace_text($tpl_info['mailmt_content'], $param);
  86. $smslog_param = array(
  87. 'ali_template_code' => $tpl_info['ali_template_code'],
  88. 'ali_template_param' => $param,
  89. 'ten_template_code' => $tpl_info['ten_template_code'],
  90. 'ten_template_param' => $ten_param,
  91. 'message' => $message,
  92. );
  93. $result = $smslog_model->sendSms($sms_mobile, $smslog_param, $log_type, $sms_captcha, $member['member_id'], $member['member_name']);
  94. if ($result['state']) {
  95. session('sms_mobile', $sms_mobile);
  96. session('sms_captcha', $sms_captcha);
  97. echo 'true';
  98. //exit;
  99. } else {
  100. echo $result['message'];
  101. exit;
  102. }
  103. } else {
  104. echo lang('phone_length_incorrect');
  105. exit;
  106. }
  107. }
  108. /**
  109. * 验证注册动态码
  110. */
  111. public function check_captcha()
  112. {
  113. $state = lang('validation_fails');
  114. $phone = input('get.phone');
  115. $captcha = input('get.sms_captcha');
  116. if (strlen($phone) == 11 && strlen($captcha) == 6) {
  117. $state = 'true';
  118. $condition = array();
  119. $condition[] = array('smslog_phone', '=', $phone);
  120. $condition[] = array('smslog_captcha', '=', $captcha);
  121. $condition[] = array('smslog_type', '=', 1);
  122. $smslog_model = model('smslog');
  123. $sms_log = $smslog_model->getSmsInfo($condition);
  124. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  125. $state = lang('dynamic_code_expired');
  126. }
  127. }
  128. exit($state);
  129. }
  130. /**
  131. * 登录
  132. */
  133. public function login()
  134. {
  135. if (!config('ds_config.sms_login') && config('ds_config.captcha_status_login') == 1 && !captcha_check(input('post.captcha_mobile'))) {
  136. ds_json_encode(10001, lang('image_verification_code_error'));
  137. }
  138. if (request()->isPost()) {
  139. if (config('ds_config.sms_login') != 1) {
  140. ds_json_encode(10001, lang('enable_mobile_phone_login'));
  141. }
  142. $phone = input('post.sms_mobile');
  143. $captcha = input('post.sms_captcha');
  144. $condition = array();
  145. $condition[] = array('smslog_phone', '=', $phone);
  146. $condition[] = array('smslog_captcha', '=', $captcha);
  147. $condition[] = array('smslog_type', '=', 2);
  148. $smslog_model = model('smslog');
  149. $sms_log = $smslog_model->getSmsInfo($condition);
  150. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  151. ds_json_encode(10001, lang('dynamic_code_expired'));
  152. }
  153. $member_model = model('member');
  154. $member = $member_model->getMemberInfo(array('member_mobile' => $phone)); //检查手机号是否已被注册
  155. if (!empty($member)) {
  156. if (!$member['member_state']) { //1为启用 0 为禁用
  157. ds_json_encode(10001, lang('login_index_account_stop'));
  158. }
  159. $member_model->createSession($member); //自动登录
  160. $reload = input('param.ref_url');
  161. if (empty($reload)) {
  162. $reload = (string)url('Member/index');
  163. }
  164. ds_json_encode(10000, lang('login_index_login_success'), '', '', false);
  165. }
  166. }
  167. }
  168. /**
  169. * 找回密码
  170. */
  171. public function find_password()
  172. {
  173. if (config('ds_config.sms_password') != 1) {
  174. ds_json_encode(10001, lang('mobile_back_password'));
  175. }
  176. $sms_mobile = trim(input('sms_mobile'));
  177. $sms_captcha = trim(input('sms_captcha'));
  178. $member_password = trim(input('member_password'));
  179. //判断验证码是否正确
  180. if ($sms_captcha != session('sms_captcha')) {
  181. ds_json_encode(10001, lang('login_index_wrong_checkcode'));
  182. }
  183. if ($sms_mobile != session('sms_mobile')) {
  184. ds_json_encode(10001, lang('receive_number_inconsistent'));
  185. }
  186. $condition = array();
  187. $condition[] = array('smslog_phone', '=', $sms_mobile);
  188. $condition[] = array('smslog_captcha', '=', $sms_captcha);
  189. $condition[] = array('smslog_type', '=', 3);
  190. $smslog_model = model('smslog');
  191. $sms_log = $smslog_model->getSmsInfo($condition);
  192. if (empty($sms_log) || ($sms_log['smslog_smstime'] < TIMESTAMP - 1800)) { //半小时内进行验证为有效
  193. ds_json_encode(10001, lang('dynamic_code_expired'));
  194. }
  195. $member_model = model('member');
  196. $member = $member_model->getMemberInfo(array('member_mobile' => $sms_mobile)); //检查手机号是否已被注册
  197. if (!empty($member)) {
  198. if (!$member['member_state']) { //1为启用 0 为禁用
  199. ds_json_encode(10001, lang('login_index_account_stop'));
  200. }
  201. $member_model->editMember(array('member_id' => $member['member_id']), array('member_password' => md5($member_password)), $member['member_id']);
  202. $member_model->createSession($member); //自动登录
  203. ds_json_encode(10000, lang('password_changed_successfully'));
  204. }
  205. }
  206. }