🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 获取手机验证码 ~~~ /** * 获取手机验证码 */ public function getPhoneCode(){ return json(['code'=>0,'msg'=>'success']); $phone = input('param.phone'); $code = input('param.code'); if(empty($phone)) return json(['code'=>1,'msg'=>'手机号码参数错误']); if(empty($code)) return json(['code'=>1,'msg'=>'验证码参数错误']); $TemplateParam = Array( // 短信模板中字段的值 "code"=>$code, "product"=>"dsd" ); $response = dysms($phone,'SMS_105290006',$TemplateParam); if($response['Code']=='OK'){ return json(['code'=>0,'msg'=>'success']); //发送成功,如两分钟未收到,请重新点击发送 }else{ return json(['code'=>1,'msg'=>'error']); } } ~~~ 注释:通过公共函数dysms发送短信,具体可以参考阿里云的短信发送说明文档 ## 获取code > /user/controller/Login.php > public function bindingUser ~~~ // 获取code的 $code = Request::instance()->param('code'); $xcx_openid = Request::instance()->param('openid'); if(empty($xcx_openid)) return $this->fetch('binding_user'); // 如果cookie缓存里面已经保存着openid,则不会再次获取新的openid if (empty($code)) { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header('Location:https://wy.yanhongw.com/api/wechat/getCode/?url='.$url); exit; }else{ // 防止刷新时导致重复带入过期code导致获取openid错误 $options = array ( 'token' => 'ooczfh1408592787', // 填写你设定的key 'appid' => 'wx886cec052b64822e', // 填写高级调用功能的appid 'appsecret' => 'afbed07e362c9eb7987a396624bb8c6c' // 填写高级调用功能的密钥 ); $wechat = new WechatApi($options); $result = $wechat->getOauthAccessToken($code); if(empty($result)){ header('Location:https://wy.yanhongw.com/user/login/bindingUser/?openid='.$xcx_openid); // header('Location:https://www.baidu.com'); exit; } // access_token,expires_in,refresh_token,openid,scope } ~~~ ~~~ // 获取code public function getCode(){ ob_start(); $myurl = Request::instance()->param('url'); // $myurl = 'https://wy.yanhongw.com/user/wechat/getOpenId'; $options = array ( 'token' => 'ooczfh1408592787', // 填写你设定的key 'appid' => 'wx886cec052b64822e', // 填写高级调用功能的appid 'appsecret' => 'afbed07e362c9eb7987a396624bb8c6c' // 填写高级调用功能的密钥 ); $wechat = new WechatApi($options); $callback = 'https://wy.yanhongw.com/oauth.php?url= '.$myurl; $url = $wechat->getOauthRedirect($callback,$state='',$scope='snsapi_base'); header("Location:".$url); exit; } ~~~