VerificationCodesController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Crypt;
  7. use Illuminate\Support\Facades\DB;
  8. use Overtrue\EasySms\EasySms;
  9. use App\Http\Requests\VerificationCodeRequest;
  10. use Illuminate\Support\Carbon;
  11. use Illuminate\Support\Facades\Cache;
  12. use GuzzleHttp\Client;
  13. use GuzzleHttp\Psr7;
  14. use Ixudra\Curl\Facades\Curl;
  15. use Illuminate\Support\Facades\Log;
  16. class VerificationCodesController extends Controller
  17. {
  18. /*
  19. * 获取卖帮帮的access_token值
  20. */
  21. public function token()
  22. {
  23. // $client_id = config('config.client_id');
  24. // $secret = config('config.secret');
  25. // $client = new \GuzzleHttp\Client();
  26. // $url = 'https://sapi.365mbb.com/supplier/v1/token';
  27. // $array = [
  28. // 'form_params' => [
  29. // 'mobile' => $re,
  30. // 'dwbs_weapp' => $client_id,
  31. // 'dwbs_weapp_secret' =>$secret,
  32. // ]
  33. // ];
  34. // $response = $client->request('post', $url, $array); #打印响应信息
  35. // $ad = json_decode($response->getBody()->getContents());
  36. // if ($ad->data !== '') {
  37. // $expiredAt = Carbon::now()->addMinutes(525600);
  38. // // 缓存验证码。
  39. // Cache::put("token", ['access_token' => $ad->data->access_token], $expiredAt);
  40. // return $this->response->array([
  41. // 'code' => 200,
  42. // 'message'=>'ok',
  43. // 'data'=>Cache::get('token')
  44. // ])->setStatusCode(201);
  45. // }
  46. // return $response;
  47. }
  48. public function Guzzle($phone,$k)
  49. {
  50. // $phone=13289326767;
  51. $client_id = config('config.dwbs_weapp');
  52. $secret = config('config.dwbs_weapp_secret');
  53. $client=new Client();
  54. $url='http://api.app.cliu.cc/api/index/info';
  55. $array = [
  56. 'form_params' => [
  57. 'mobile' => $phone,
  58. 'dwbs_weapp' => $client_id,
  59. 'dwbs_weapp_secret' =>$secret,
  60. ]
  61. ];
  62. $response = $client->request('POST', $url,$array);
  63. $ad = json_decode($response->getBody()->getContents());
  64. return $ad;
  65. }
  66. public function store(Request $request, EasySms $easySms)
  67. {
  68. // return $this->response()->array([
  69. // 'code' => 400,
  70. // 'msg' => '暂定服务',
  71. // 'data' => []
  72. // ]);
  73. // Cache::put('token',['access_token'=>'ssss']);
  74. if ($request->phone==18333333333){
  75. $code=521520;
  76. $key = 'verificationCode_' . str_random(15);
  77. $expiredAt = Carbon::now()->addMinutes(10);
  78. // 缓存验证码 10分钟过期。
  79. // Cache::put($key, ['phone' => $phone, 'code' => $code, 'data' => $data], $expiredAt);
  80. return $this->response->array([
  81. 'code' => 200,
  82. 'msg' => '发送成功',
  83. 'data' => [
  84. 'verify_key' => $key,
  85. 'code'=>$code,
  86. 'expired_at' => $expiredAt->toDateTimeString(),
  87. ]
  88. ])->setStatusCode(201);
  89. }
  90. else{
  91. $phone = $request->phone;
  92. Log::error('{-----手机号-------}'.'"'.$phone.'"'.time());
  93. $mobiles=mb_substr($phone,0,3).'****'.mb_substr($phone,'-4');
  94. $userss = User::where('phone',$mobiles)->get();
  95. $s=[];
  96. foreach ($userss as $k=>$v){
  97. $p=Crypt::decryptString($v->phone_code);
  98. if ($p==$phone){
  99. $s=User::find($v->id)->toArray();
  100. }
  101. }
  102. if (!$s){
  103. return $this->response()->array([
  104. 'code' => 400,
  105. 'msg' => '无此用户',
  106. 'data' => []
  107. ]);
  108. $sms=DB::table('phone_code')->where('phone',$phone)->where('time','>',Carbon::now()->toDateTimeString())->first();
  109. if ($sms){
  110. return $this->error(40056,'','验证码已经成功发送,请注意查收短信开头为【大卫博士学位争霸赛】的短信');
  111. }
  112. $data = $this->Guzzle($phone,1);
  113. if ($data->data==null){
  114. return $this->response()->array([
  115. 'code' => 400,
  116. 'msg' => '无此用户',
  117. 'data' => []
  118. ]);
  119. }
  120. if ($data->code == 400) {
  121. return $this->response()->array([
  122. 'code' => 400,
  123. 'msg' => '无此用户',
  124. 'data' => []
  125. ]);
  126. }
  127. if ($data->code == 402 || $data->code == 406) {
  128. return $this->response()->array([
  129. 'code' => 402,
  130. 'msg' => '查询出错,请稍后重试',
  131. 'data' => []
  132. ]);
  133. }
  134. $data=$data->data;
  135. }else{
  136. // if ($s->level_name=='政委'){
  137. $data=$s;
  138. // }else{
  139. // return $this->error(600);
  140. // }
  141. }
  142. $time = 10;
  143. // 生成6位随机数,左侧补0
  144. // $code = str_pad(random_int(1, 999999), 6, 0, STR_PAD_LEFT);
  145. $code = mb_substr($phone,3, 2) . mb_substr($phone,8,2) . mb_substr($phone,6,2);
  146. // return $code;
  147. try {
  148. $result = $easySms->send($phone, [
  149. 'content' => "【大卫博士学位争霸赛】您的验证码为{$code},请在{$time}分钟内输入验证码!如非本人操作,请忽略此条短信。"
  150. ]);
  151. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  152. $message = $exception->getException('qcloud')->getMessage();
  153. Log::error('{------手机号------}'.$phone.'{-------错误信息为-----}'.$message);
  154. return $this->response->errorInternal($message ?: '短信发送异常');
  155. }
  156. $key = 'verificationCode_' . str_random(15);
  157. $expiredAt = Carbon::now()->addMinutes(10);
  158. // 缓存验证码 10分钟过期。
  159. Cache::put($key, ['phone' => $phone,'id'=>$s['id'], 'code' => $code, 'data' => $data], $expiredAt);
  160. // $re=DB::table('phone_code')->where('phone',$phone)->first();
  161. // if (empty($re)){
  162. // $da['phone']=$phone;
  163. // $da['phone_code']=$code;
  164. // $da['time']=$expiredAt;
  165. // $da['data']=json_encode($data);
  166. // $da['created_at']=Carbon::now()->toDateTimeString();
  167. // $da['updated_at']=Carbon::now()->toDateTimeString();
  168. // DB::table('phone_code')->insert($da);
  169. // }else{
  170. //// $da['phone']=$phone;
  171. // $da['phone_code']=$code;
  172. // $da['time']=$expiredAt;
  173. // $da['data']=json_encode($data);
  174. //// $da['created_at']=Carbon::now()->toDateTimeString();
  175. // $da['updated_at']=Carbon::now()->toDateTimeString();
  176. // DB::table('phone_code')->where('id',$re->id)->update($da);
  177. // }
  178. return $this->response->array([
  179. 'code' => 200,
  180. 'msg' => '发送成功',
  181. 'data' => [
  182. 'verify_key' => $key,
  183. 'code'=>$code,
  184. 'expired_at' => $expiredAt->toDateTimeString(),
  185. ]
  186. ])->setStatusCode(201);
  187. }
  188. }
  189. }