12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace app\admin\server\sms;
- use app\admin\model\SmsRecord;
- use support\Redis;
- class VerificationCodeServer
- {
- /**
- * Notes:获取验证码
- * @param $mobile
- * @param $user_id
- * User: yym
- * Date: 2022/9/18
- * @return bool
- * @throws \Exception
- */
- public static function getCode($mobile, $user_id)
- {
- SmsRecord::affairBegin();
- try {
- $action = 'get_code';//模板名称
- $template_id = config('plugin.hzdad.wbsms.app.aliyun.actions')[$action]['template_id'] ?? '';
- $params = ['time'=>date("Y-m-d H:i:s",time()),'code' => randCode(6, true)];//参数
- $areacode = "86";//国际区号,腾讯云选传,其他不传
- $sms = new \Hzdad\Wbsms\Wbsms('aliyun');
- $insert_status = SmsRecord::InsertData($mobile, '会员获取无卡结算验证码' . $params['code'], $template_id, $user_id);
- if($insert_status)
- {
- $result = $sms->sendsms($action,$mobile,$params,$areacode);
- if ($result['code'] == 200) {
- Redis::setEx('get_code_' . $mobile, 300, $params['code']);
- SmsRecord::affairCommit();
- return true;
- } else {
- //SmsRecord::InsertData($mobile, $result['msg'], $template_id, $user_id);
- throw new \Exception($result['msg'], 500);
- }
- }
- throw new \Exception('发送失败', 500);
- }catch (\Exception $exception){
- SmsRecord::affairRollback();//数据库回滚
- throw new \Exception($exception->getMessage(), 500);
- }
- }
- /**
- * Notes:验证码校验
- * @param int $mobile
- * @param int $code
- * @return bool
- * @throws \Exception
- * User: yym
- * Date: 2022/7/25
- */
- public static function checkCode(int $mobile, int $code)
- {
- if(!Redis::exists('get_code_' . $mobile))
- {
- throw new \Exception('验证码已过期,请重新获取');
- }
- if(Redis::get('get_code_' . $mobile) != $code)
- {
- throw new \Exception('验证码错误');
- }
- Redis::del('get_code_' . $mobile);
- return true;
- }
- }
|