辽阳剐什教育咨询有限公司

微信小程序獲取用戶(hù)頭像+昵稱(chēng)+openid,小程序登錄!小程序發(fā)送模板消息

微信小程序獲取用戶(hù)頭像+昵稱(chēng)+openid,小程序登錄!小程序發(fā)送模板消息
  • 作者:杭州俍辰科技有限公司
  • 發(fā)表時(shí)間:2018-12-06
<view class="container">
  <view class="userinfo">
    <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像昵稱(chēng) </button>
    <block wx:else>
      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>
  <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
  </view>
//獲取應(yīng)用實(shí)例
const app = getApp()
 
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
 
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
      // 所以此處加入 callback 以防止這種情況
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在沒(méi)有 open-type=getUserInfo 版本的兼容處理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
 
    //獲取openid
    wx.login({
      success: function (res) {
        console.log(res.code)
        //發(fā)送請(qǐng)求獲取openid
        wx.request({
          url: '你的域名/openid.php?code=code', //接口地址
          data: { code: res.code },
          header: {
            'content-type': 'application/json' //默認(rèn)值
          },
          success: function (res) {
            //返回openid
            console.log(res.data.openid)
            //向數(shù)據(jù)庫(kù)注冊(cè)用戶(hù),驗(yàn)證用戶(hù)
            var that = this;
            wx.request({
              url: '你的域名/server.php?nickname=' + e.detail.userInfo.nickName + '&avatarUrl=' + e.detail.userInfo.avatarUrl + '&openid=' + res.data.openid,
              data: {
 
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                //打印用戶(hù)信息
                console.log(res.data)
              }
            })
          }
        })
      }
    })
 
     
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true,
    })
  }
})
<?php
//聲明CODE,獲取小程序傳過(guò)來(lái)的CODE
$code = $_GET["code"];
//配置appid
$appid = "你的小程序APPID";
//配置appscret
$secret = "你的小程序APPSRCRET";
//api接口
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
//獲取GET請(qǐng)求
function httpGet($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}
//發(fā)送
$str = httpGet($api);
echo $str;
?>
<?php
header("Content-type:text/html;charset=utf8");
$nickname = $_GET["nickname"];
$avatarUrl = $_GET["avatarUrl"];
$openid = $_GET["openid"];
$con = mysql_connect("數(shù)據(jù)庫(kù)地址","數(shù)據(jù)庫(kù)賬號(hào)","數(shù)據(jù)庫(kù)密碼");
mysql_select_db("數(shù)據(jù)庫(kù)名", $con);
mysql_query("INSERT INTO 表名 (nickname, avatarUrl, openid) VALUES ('$nickname', '$avatarUrl', '$openid')");
echo "注冊(cè)成功!";
mysql_close($con);
?>

微信模板消息

<form bindsubmit="submit" report-submit='true' >
  <input type='text' value='填寫(xiě)openid' name="openid"></input>
  <input type='text' value='填寫(xiě)ACCESS_TOKEN' name="token"></input>
  <input type='text' value='填寫(xiě)模板ID' name="template"></input>
  <input type='text' value='模板的第1個(gè)關(guān)鍵詞' name="keyword1"></input>
  <input type='text' value='模板的第2個(gè)關(guān)鍵詞' name="keyword2"></input>
  <input type='text' value='模板的第3個(gè)關(guān)鍵詞' name="keyword3"></input>
  <input type='text' value='模板的第4個(gè)關(guān)鍵詞' name="keyword4"></input>
  <input type='text' value='模板的第5個(gè)關(guān)鍵詞' name="keyword5"></input>
  <button form-type="submit" type="default">推送</button>
</form>
Page({
  data: {
      
  },
  
  submit: function (e) {
    var openid = e.detail.value.openid;
    var access = e.detail.value.token;
    var template = e.detail.value.template;
    var keyword1 = e.detail.value.keyword1;
    var keyword2 = e.detail.value.keyword2;
    var keyword3 = e.detail.value.keyword3;
    var keyword4 = e.detail.value.keyword4;
    var keyword5 = e.detail.value.keyword5;
    var that = this;
    wx.request({
      url: '域名/muban.php?openid=' + e.detail.value.openid + '&token=' + e.detail.value.token + '&template=' + e.detail.value.template + '&formid=' + e.detail.formId + '&keyword1=' + e.detail.value.keyword1 + '&keyword2=' + e.detail.value.keyword2 + '&keyword3=' + e.detail.value.keyword3 + '&keyword4=' + e.detail.value.keyword4 + '&keyword5=' + e.detail.value.keyword5, //接口地址,我學(xué)習(xí)就用get,建議用post
      data: {
        open_id: openid,
        tok_en: access,
        temp_late: template,
        form_id: e.detail.formId,
        keyword_1: keyword1,
        keyword_2: keyword2,
        keyword_3: keyword3,
        keyword_4: keyword4,
        keyword_5: keyword5
      },
      success: function (res) {
        
      // console.log(e.detail.formId);
      // console.log(res.data);
      }
    })
  }
})
//后端
<?php
    //GET參數(shù)
    $access_token=$_GET['token'];
    $openid=$_GET['openid'];
    $templateid=$_GET['template'];
    $formid=$_GET['formid'];
    $keyword1=$_GET['keyword1'];
    $keyword2=$_GET['keyword2'];
    $keyword3=$_GET['keyword3'];
    $keyword4=$_GET['keyword4'];
    $keyword5=$_GET['keyword5'];
    echo $keywordd1;
    //此處開(kāi)始處理數(shù)據(jù)
    $dataa=array(
        "keyword1"=>array(
                            "value"=>$keyword1,
                            "color"=>"#9b9b9b"),
        "keyword2"=>array(
                            "value"=>$keyword2,
                            "color"=>"#9b9b9b"),
        "keyword3"=>array(
                            "value"=>$keyword3,
                            "color"=>"#9b9b9b"),
        "keyword4"=>array(
                            "value"=>$keyword4,
                            "color"=>"#9b9b9b"),
        "keyword5"=>array(
                            "value"=>$keyword5,
                            "color"=>"#9b9b9b")
                );
   
    $data=array();
    $data['touser']=$openid;
    $data['template_id']=$templateid;
    $data['form_id']=$formid;
    $data['data']=$dataa;
       
    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;  
    $type="json";
        if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
            $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $data=json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 發(fā)送一個(gè)常規(guī)的Post請(qǐng)求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓異常
        }
        curl_close($curl);
        echo $output;
?>


上一條:熊掌號(hào)與SEO
下一條:微信已支持改微信號(hào):一年可修改一次