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