* @date 2020-01-10 21:12:58 */ declare(strict_types=1); namespace Modules\Api\Controllers; use App\Model\Mongo\UserRelationModel; use App\Model\UserModel; use App\Repositories\UserRepository; use App\Repositories\Utils\SmsRepository; use App\Repositories\VipRepository; use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Modules\Api\Services\SmsService; use Psr\SimpleCache\CacheInterface; class MsgController extends BaseController { /** * 发送短信验证码 * @return mixed * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Psr\SimpleCache\InvalidArgumentException */ public function sendVerfiy() { //当前用户邀请码 $inputData = $this->getJsonData(); $rules = [ 'phone' => 'required|string|max:18', ]; $validator = make(ValidatorFactoryInterface::class) ->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $phone = $inputData['phone']; $code = random(6, 1); $cache = make(CacheInterface::class); $cache->set('phone:code:' . $phone, $code, 600); if (config('app_env') == 'prod') { $smsRepository = make(SmsRepository::class); $result = $smsRepository->sendVerify($phone, $code); } else { return json_success(['result' => 'success']); } // 不管是否真的发送成功, 还是失败。统一让用户觉得发送成功了. if (isset($result['code']) && $result['code'] == 200) { return json_success($result); } else { return json_fail($result['message'], 500, $result['data']); } } }