微信小程序获取用户头像+昵称+openid,小程序登录!小程序发送模板消息



  
    
    
      
      {{userInfo.nickName}}
    
  
  
    {{motto}}
  
//index.js
//获取应用实例
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 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 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)
        //发送请求获取openid
        wx.request({
          url: '你的域名/openid.php?code=code', //接口地址
          data: { code: res.code },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            //返回openid
            console.log(res.data.openid)
            //向数据库注册用户,验证用户
            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) {
                //打印用户信息
                console.log(res.data)
              }
            })
          }
        })
      }
    })
 
     
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true,
    })
  }
})

微信模板消息

// pages/mubanxiaoxi/mubanxiaoxi.js 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, //接口地址,我学习就用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); } }) } }) //后端:muban.php 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); // 发送一个常规的Post请求 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; ?>

 

你可能感兴趣的:(微信小程序获取用户头像+昵称+openid,小程序登录!小程序发送模板消息)