Modules_Api_Controllers_MsgController.proxy.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. use \Hyperf\Di\Aop\ProxyTrait;
  21. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  22. function __construct()
  23. {
  24. if (method_exists(parent::class, '__construct')) {
  25. parent::__construct(...func_get_args());
  26. }
  27. $this->__handlePropertyHandler(__CLASS__);
  28. }
  29. /**
  30. * 发送短信验证码
  31. * @return mixed
  32. * @throws \GuzzleHttp\Exception\GuzzleException
  33. * @throws \Psr\SimpleCache\InvalidArgumentException
  34. */
  35. public function sendVerfiy()
  36. {
  37. //当前用户邀请码
  38. $inputData = $this->getJsonData();
  39. $rules = ['phone' => 'required|string|max:18'];
  40. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  41. if ($validator->fails()) {
  42. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  43. return json_fail($validator->errors()->first());
  44. }
  45. $phone = $inputData['phone'];
  46. $code = random(6, 1);
  47. $cache = make(CacheInterface::class);
  48. $cache->set('phone:code:' . $phone, $code, 600);
  49. if (config('app_env') == 'prod') {
  50. $smsRepository = make(SmsRepository::class);
  51. $result = $smsRepository->sendVerify($phone, $code);
  52. } else {
  53. return json_success(['result' => 'success']);
  54. }
  55. // 不管是否真的发送成功, 还是失败。统一让用户觉得发送成功了.
  56. if (isset($result['code']) && $result['code'] == 200) {
  57. return json_success($result);
  58. } else {
  59. return json_fail($result['message'], 500, $result['data']);
  60. }
  61. }
  62. }