MsgController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * 用户api.
  4. *
  5. * @author zhangjunbao <junbaoz1314@gmail.com>
  6. * @date 2020-01-10 21:12:58
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Model\Mongo\UserRelationModel;
  11. use App\Model\UserModel;
  12. use App\Repositories\UserRepository;
  13. use App\Repositories\Utils\SmsRepository;
  14. use App\Repositories\VipRepository;
  15. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  16. use Modules\Api\Services\SmsService;
  17. use Psr\SimpleCache\CacheInterface;
  18. class MsgController extends BaseController
  19. {
  20. /**
  21. * 发送短信验证码
  22. * @return mixed
  23. * @throws \GuzzleHttp\Exception\GuzzleException
  24. * @throws \Psr\SimpleCache\InvalidArgumentException
  25. */
  26. public function sendVerfiy()
  27. {
  28. //当前用户邀请码
  29. $inputData = $this->getJsonData();
  30. $rules = [
  31. 'phone' => 'required|string|max:18',
  32. ];
  33. $validator = make(ValidatorFactoryInterface::class)
  34. ->make($inputData, $rules);
  35. if ($validator->fails()) {
  36. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  37. return json_fail($validator->errors()->first());
  38. }
  39. $phone = $inputData['phone'];
  40. $code = random(6, 1);
  41. $cache = make(CacheInterface::class);
  42. $cache->set('phone:code:' . $phone, $code, 600);
  43. if (config('app_env') == 'prod') {
  44. $smsRepository = make(SmsRepository::class);
  45. $result = $smsRepository->sendVerify($phone, $code);
  46. } else {
  47. return json_success(['result' => 'success']);
  48. }
  49. // 不管是否真的发送成功, 还是失败。统一让用户觉得发送成功了.
  50. if (isset($result['code']) && $result['code'] == 200) {
  51. return json_success($result);
  52. } else {
  53. return json_fail($result['message'], 500, $result['data']);
  54. }
  55. }
  56. }