免费国产欧美国日产_少妇AV一区二区三区无码_蜜桃精品av无码喷奶水小说_jk18禁网站视频_精产国品一二三级产品区别_被夫の上司に犯波多野结衣_78m成人手机免费看_最爽最刺激18禁视频_偷偷色噜狠狠狠狠的777米奇

易優(yōu)GEO 重磅上線 ~ 一站式GEO優(yōu)化工具,讓豆包、文心一言、DeepSeek 在回答中主動推薦你的品牌,搶占AI流量入口!  點擊查看

小程序模板網(wǎng)

小程序微信登錄+頁面跳轉(zhuǎn)數(shù)據(jù)交互

發(fā)布時間:2018-02-10 11:52 所屬欄目:小程序開發(fā)教程


一:小程序登錄 
1.在小程序全局js app.js文件中,調(diào)用小程序微信登錄接口

將小程序獲取到用戶的code POST方式發(fā)送到服務(wù)器端

 

  1. //調(diào)用登錄接口
    wx.login({
    success:function(res){
    
    console.log(res)
    if(res.code){
    //存在code
    wx.request({
    url:'http://shop.yunapply.com/home/user/saveOpenId',
    data: {code:res.code},
    method:'POST',
    header: {
    "content-type": "application/x-www-form-urlencoded"
    },
    success: function(res){
    that.globalData.userId = res.data.info
    },
    fail: function() {
    console.log('服務(wù)器請求失敗!')
    },
    })
    }else{
    console.log('獲取用戶信息失敗!'+res.errMsg)
    }
    }
    })

調(diào)用接口成功以后打印出接口對象,可以得到如下信息

這里的code,有效時間是5分鐘,需要將這個code發(fā)送到服務(wù)器,用code換取session_key API,將code換成OPENID和session_key

2.在服務(wù)器端將帶有appid、secret、js_code、grant_type數(shù)據(jù),發(fā)送到微信服務(wù)器,換取session_key和openid

 

  1. https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

這里JSCODE就是放置code的地方

3.服務(wù)器端代碼

 

  1. public function saveOpenId(){
    if (IS_POST) {
    $code = I('post.code');
    if($code){
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=XXXX&js_code=".$code."&grant_type=authorization_code";
    $res = file_get_contents($url); //獲取文件內(nèi)容或獲取網(wǎng)絡(luò)請求的內(nèi)容
    $result = json_decode($res);
    if($result->openid){
    $openid = $result->openid;
    $user = D('user');
    $userInfo = $user->where(array('openid'=>$openid))->find();
    if(!$userInfo){
    $data['openid'] = $openid;
    $data['modifytime'] = date("Y-m-d H:i:s");
    $userId = $user->add($data);
    $this->success($userId,'',true);
    }else{
    $this->success($userInfo['id'],'',true);
    }
    }
    
    }
    
    }

流程:發(fā)送帶有codeURL,獲取網(wǎng)頁內(nèi)容,判斷openid是否存在,存在就將用戶ID返回,不存在就添加用戶然后將ID返回。

wx.login成功獲取了用戶ID,再把這個ID賦值給全局變量,UserId

二:頁面跳轉(zhuǎn)數(shù)據(jù)交互  頁面攜帶參數(shù)跳轉(zhuǎn),處理參數(shù)

 

  1. <navigator url="../index/index?id=1&name=zhouqi&work=php">跳轉(zhuǎn)</navigator>

這里是普通的跳轉(zhuǎn),相當于HTML的a

參數(shù)形式key=value&key2=value2

這里是跳轉(zhuǎn)到index/index頁面

參數(shù)是

id = 1 name = zhouqi work = php

在index頁面的js中

 

  1. Page({
    data: {},
    onLoad:function(options){
    
    console.log(options)
    wx.request({
    url: 'http://shop.com/home/product/test',
    data: {id:options.id,name:options.name,work:options.work},
    success: function(res){
    console.log(res)
    },
    })

option有攜帶過來的參數(shù)情況

這樣就可以得到攜帶過來的參數(shù)

可以通過wx.request對服務(wù)器進行請求獲取數(shù)據(jù)或者其他操作

服務(wù)器端返回的數(shù)據(jù)

服務(wù)器端代碼


本文地址:http://www.szcjxy.com/wxmini/doc/course/21963.html 復制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢: 點擊咨詢
在線客服