当前位置:首页 > 新闻动态 > 网站文章

微信小程序开发应用之静默登录

来源: 浏览:135 时间:2023-08-11

1.微信小程序开发静默登入,通过获取用户的open_id来实现静默登入,获取open_id,不需要用户同意,获取code再通过code获取微信的open_id。


wx.login({


success(res) {


_this.setData({


code:res.code,


});


console.log(res.code);


}


});


获取code后用code去获取用户的open_id


/**获取open_id */

public function weixin_login($code) {


$ret['code'] = 200;


$app_id = 'xxxx';


$app_secret = 'xxxx';


$code = $code;

//微信前端登陆获取的CODE

//server调用微信提供的jsoncode2session接口获取openid, session_key

$url = "https://api.weixin.qq.com/sns/jscode2session";


$params = array(


'appid' => $app_id,


'secret' => $app_secret,


'js_code' => $code,


'grant_type' => 'authorization_code'


);


$res = makeRequest($url, $params);//curl


if ($res['code'] !== 200 || !isset($res['result']) || !isset($res['result'])) {


$ret['code'] = 500;


$ret['msg'] = '调用jsoncode2session失败';



}


$reqData = json_decode($res['result'], true);


if (!isset($reqData['session_key'])) {


$ret['code'] = 500;


$ret['msg'] = '获得session_key失败';


$ret['error'] = $reqData;


}


//$ret['session_key'] = $reqData['session_key'];

$ret = $reqData['openid'];


return $ret;


}



2.获取用户头像、昵称 信息


getUserProfile() {


const app = this


console.log("wx.canIUse('getUserProfile')",wx.canIUse('getUserProfile'));



wx.canIUse('getUserProfile') && wx.getUserProfile({


lang: 'zh_CN',


desc: '获取用户相关信息',


success({


userInfo


}) {


console.log('用户同意了授权')


console.log('userInfo:', userInfo)


App.getUserInfo(userInfo, () => {


// 跳转回原页面

app.onNavigateBack(1)


});


},


fail() {


console.log('用户拒绝了授权')


}


})


},



/**
   * 授权登录
   */

getUserInfo(userInfo, callback) {


let App = this;


wx.showLoading({


title: "正在登录",


mask: true


});


// 执行微信登录

App._post_form('Member/userInfo', {


user_info:JSON.stringify(userInfo),


}, result => {


// 执行回调函数

callback && callback();


}, false, () => {


wx.hideLoading();


});


},


3. 获取用户手机号,新版的必须要用户点击button才能触发获取手机


通过code 在去获取手机


去授权


getPhoneNumber (e) {


let _this=this


// app.wx_login(e.detail.code);


app._post_form('Member/get_phone', {


phone_code:e.detail.code,


}, result => {


}, false, () => {


wx.hideLoading();


});


},


先获取access_token


/**获取AccessToken */

function access_token(){


$app_id = 'xxxx';


$app_secret = xxxx;


$url = "https://api.weixin.qq.com/cgi-bin/token";


$params = array(


'appid' => $app_id,


'secret' => $app_secret,


'grant_type' => 'client_credential'


);


$res = makeRequest($url, $params);


$reqData = json_decode($res['result'], true);


return $reqData['access_token'];


}


通过access_token 和code 获取手机


public function weixin_phone($code) {


// $ret['code'] = 200;

$key=$this->access_token();


$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$key;


$params = array(


'code' => $code,


);


$res = makeRequest($url, $params,'json');//curl params数据必须是json


$reqData = json_decode($res['result'], true);


return $reqData['phone_info']['phoneNumber'];


}



  • 消息订阅,这里以状态审核为例子

红圈就是发送数据的键名


public function submessage(){


/** @var *
     * 微信小程序消息订阅
     */


$access_token = $this->access_token();


$url_2="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;


$template_id = "xxx";//模板id


$data['touser'] = $openid ;//open_id


$data['template_id'] = $template_id ;


$data['page'] = "pages/login/index" ; //用户点击消息跳转页面


$data['data'] = array(


'thing7'=>array('value'=>$realname),


'phrase2'=>array('value'=>'审核通过'),//字数不能超过5个


'time3'=>array('value'=>date('Y-m-d H:i', time())),


'thing4'=>array('value'=>'身份验证审核'),


);


//$data['data'] = json_encode($data['data']);

//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版

$data['miniprogram_state'] = 'trial';


//print_r($data);

$result = $this->posturl($url_2,$data);


if($result['errmsg']=='ok'){


return 1;


}else{


return   $result['errmsg'] ;


}




地址 · ADDRESS

地址:建邺区新城科技园嘉陵江东街18号2层

邮箱:309474043@qq.Com

点击查看更多案例

联系 · CALL TEL

400-8793-956

售后专线:025-65016872

业务QQ:309474043    售后QQ:1850555641

©南京安优网络科技有限公司 版权所有   苏ICP备12071769号-4  网站地图