Index.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /*
  3. * @Description: 微信公众号接口
  4. * @Version: 2.0
  5. * @Autor: 李康
  6. * @Date: 2022-01-24 11:11:15
  7. * @LastEditors: 李康
  8. * @LastEditTime: 2022-01-25 15:49:18
  9. */
  10. namespace catchAdmin\xw1\controller;
  11. use app\Request;
  12. use catchAdmin\msg\model\Msg;
  13. use catchAdmin\permissions\model\Users;
  14. use catcher\base\CatchController;
  15. use catcher\base\CatchRequest;
  16. use catcher\CatchResponse;
  17. use think\facade\Db;
  18. class Index extends CatchController
  19. {
  20. //注册信息
  21. /**
  22. * Undocumented function
  23. *
  24. * @param Request $request
  25. * @username 手机号
  26. * @smsCode 手机验证码
  27. * @password 密码
  28. * @confirmPassword 重复密码
  29. *
  30. * @return void
  31. */
  32. public function register(Request $request){
  33. header('Access-Control-Allow-Origin: *');
  34. // 通过手机号和短信验证码注册
  35. $data = $request->param();
  36. if(!$data['username']){
  37. return CatchResponse::fail('手机号码不能为空');
  38. }
  39. if(!$data['smsCode']){
  40. return CatchResponse::fail('短信验证码不能为空');
  41. }
  42. if(! $data['password']){
  43. return CatchResponse::fail('密码不能为空');
  44. }
  45. if(! $data['confirmPassword']){
  46. return CatchResponse::fail('请输入确认密码');
  47. }
  48. if ($data['password'] != $data['confirmPassword']) {
  49. return CatchResponse::fail('两次密码不一致');
  50. }
  51. // 号码是否已注册
  52. $userinfo = Db::name('users')->where(['username'=>$data['username']])->find();
  53. if ($userinfo) {
  54. return CatchResponse::fail('该号码已注册');
  55. }
  56. // 验证码有效性
  57. $res = $this->isValidSmsCode($data['username'], $data['smsCode']);
  58. if (!$res['success']) {
  59. return CatchResponse::fail($res['message']);
  60. }
  61. //设置转换密码
  62. $user=new Users();
  63. $savePwd = $user->setPasswordAttr($data['password']);
  64. $saveData = [
  65. 'username' => $data['username'],
  66. 'password' => $savePwd
  67. ];
  68. $userid = Db::name('users')->insert($saveData);
  69. if(!$userid){
  70. return CatchResponse::fail('注册失败');
  71. }
  72. return CatchResponse::success('注册成功');
  73. }
  74. //重置密码
  75. /**
  76. * Undocumented function
  77. *
  78. * @param Request $request
  79. * @username 手机号
  80. * @smsCode 手机验证码
  81. * @password 密码
  82. * @confirmPassword 重复密码
  83. * @
  84. * @return void
  85. */
  86. public function resetPassword(Request $request)
  87. {
  88. header('Access-Control-Allow-Origin: *');
  89. // 通过手机号和短信验证码注册
  90. $data = $request->param();
  91. if(!$data['username']){
  92. return CatchResponse::fail('手机号码不能为空');
  93. }
  94. if(!$data['smsCode']){
  95. return CatchResponse::fail('短信验证码不能为空');
  96. }
  97. if(! $data['password']){
  98. return CatchResponse::fail('密码不能为空');
  99. }
  100. if(! $data['confirmPassword']){
  101. return CatchResponse::fail('确认密码不能为空');
  102. }
  103. if ($data['password'] != $data['confirmPassword']) {
  104. return CatchResponse::fail('两次密码不一致');
  105. }
  106. // 判断角色、号码是否已注册
  107. $userinfo = Users::where('username',$data['username'])->find();
  108. if (empty($userinfo)) {
  109. return CatchResponse::fail('该号码未注册');
  110. }
  111. // 验证码有效性
  112. $res = $this->isValidSmsCode($data['username'], $data['smsCode']);
  113. if (!$res['success']) {
  114. return CatchResponse::fail($res['message']);
  115. }
  116. // 重置密码
  117. $user=new Users();
  118. $savePwd = $user->setPasswordAttr($data['password']);
  119. $cond = ['id' => $userinfo['id']];
  120. $res = Users::where($cond)->update(["password"=>$savePwd]);
  121. if($res === false){
  122. return CatchResponse::fail('重置失败');
  123. }
  124. return CatchResponse::success('重置成功');
  125. }
  126. /**
  127. * @username 手机号
  128. * isReset 是否重置密码 1代表重置 0未重置
  129. */
  130. //发送短信验证码
  131. public function sendSmsCode(Request $request){
  132. header('Access-Control-Allow-Origin: *');
  133. $postData = $request->param();
  134. //判断账号和密码是否存在
  135. if (!$postData['username']) {
  136. return CatchResponse::fail('手机号不存在');
  137. }
  138. if (!preg_match('/^1[3456789]\d{9}$/', $postData['username'])) {
  139. return CatchResponse::fail('手机号格式不存在');
  140. }
  141. // 是否重置密码
  142. //1代表重置
  143. //0 未重置
  144. $isReset = $postData['isReset'];
  145. // 号码是否已注册
  146. $userinfo = Db::name("users")->where('username',$postData['username'])->find();
  147. // 重置密码时
  148. if ($isReset && empty($userinfo)) {
  149. return CatchResponse::fail('该号码未注册');
  150. } elseif (!$isReset && $userinfo) {
  151. return CatchResponse::fail('该号码已注册');
  152. }
  153. $sms_code_mode = Db::name('sms_verification_code'); // 短信验证码模型
  154. // 防盗刷IP地址检测
  155. $todayTime = strtotime(date('Y-m-d'));
  156. $where = ['access_ip' => $_SERVER['REMOTE_ADDR'], 'created_at' => ['GT', $todayTime]];
  157. $count = $sms_code_mode->where($where)->count();
  158. if ($count >= 10) {
  159. return CatchResponse::fail('已达到当日获取次数上限');
  160. }
  161. // 获取短信配置
  162. $rlyunId = Db::name('sms_config')->where(['name' => 'rlyun'])->value('id');
  163. $smsConfig = Db::name('sms_config')->where(['pid' => $rlyunId])->column('key,value');
  164. $test=null;
  165. foreach ($smsConfig as $key=>$value) {
  166. $test[$value['key']]=$value['value'];
  167. }
  168. $smsConfig=$test;
  169. if (empty($smsConfig)) {
  170. return CatchResponse::fail('获取短信配置失败');
  171. }
  172. // 获取短信验证码模板信息
  173. $where = ['operator' => 'rlyun', 'code' => 'verification_code'];
  174. $tmpInfo = Db::name('sms_template')->where($where)->find();
  175. if (empty($tmpInfo)) {
  176. return CatchResponse::fail('获取短信验证码模板信息失败');
  177. }
  178. // 生成验证码
  179. $code = rand(0, 9999);
  180. $code = str_pad($code, 4, '0',STR_PAD_LEFT);
  181. // 保存验证码
  182. $time = time();
  183. $saveData = [
  184. 'mobile' => $postData['username'],
  185. 'code' => $code,
  186. 'access_ip' => $_SERVER['REMOTE_ADDR'],
  187. 'created_at' => $time,
  188. 'updated_at' => $time,
  189. ];
  190. Db::startTrans(); // 开启事务
  191. // createAdd($saveData);
  192. $res = $sms_code_mode->insert($saveData);
  193. if ($res === false) {
  194. Db::rollback();
  195. return CatchResponse::fail('发送失败,请稍后重试');
  196. }
  197. // 发送短信验证码
  198. $content = str_replace('{1}', $code, $tmpInfo['content']);
  199. $content = str_replace('{2}', '5分钟', $content);
  200. $contentData = [
  201. 'tplno' => $tmpInfo['identify'],
  202. 'tpldata' => [ $code, '5分钟' ],
  203. //'tpldata' => $content, // error test
  204. 'info' => $content,
  205. ];
  206. $res = send_sms_with_config($postData['username'], $contentData, $smsConfig,array());
  207. if (!$res['success']) {
  208. Db::rollback();
  209. Db::name('sms_send_log')->insert($res['data']);
  210. return CatchResponse::fail($res['message'] ?: '发送失败');
  211. }
  212. Db::commit();
  213. return CatchResponse::success($res['message']);
  214. }
  215. //校验密码
  216. private function isValidSmsCode( $mobile, $code ){
  217. if (!$mobile) {
  218. return ['success' => false, 'message' => '手机号码不存在'];
  219. }
  220. if (!$code) {
  221. return ['success' => false, 'message' => '验证码不存在'];
  222. }
  223. // 根据手机号码和验证码查询最后一次验证码
  224. $sms_verification_code_model = Db::name('sms_verification_code');
  225. $cond = [ 'mobile' => $mobile, 'code' => $code ];
  226. $info = $sms_verification_code_model->where($cond)->order('created_at', 'desc')->find();
  227. // 不存在
  228. if (empty($info)) {
  229. return ['success' => false, 'message' => '验证失败,请确保手机号码和验证码输入无误'];
  230. }
  231. // 超过时间(暂定5分钟)
  232. $valid_time = 60 * 5;
  233. if (time() - $info['created_at'] > $valid_time) {
  234. return ['success' => false, 'message' => '验证码已失效'];
  235. }
  236. // 超过使用次数(暂定3次)
  237. if ($info['use_times'] >= 3) {
  238. return ['success' => false, 'message' => '验证码已失效'];
  239. }
  240. // 可以使用,使用次数+1
  241. $cond = ['id' => $info['id']];
  242. $user_times=$sms_verification_code_model->where($cond)->value('use_times');
  243. $res = $sms_verification_code_model->where($cond)->update(['use_times'=>$user_times+1]);
  244. if ($res === false) {
  245. return ['success' => false, 'message' => '操作失败,请重试'];
  246. }
  247. return ['success' => true, 'message' => '验证码有效'];
  248. }
  249. }