小程序如何获取手机号 thinkphp3.2.3框架

本篇文章给大家带来的内容是关于小程序如何获取手机号( thinkphp3.2.3框架),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1、直接上代码php

namespace Home\Controller;
use Think\Controller;
class ApiController extends Controller {
    /**
 * error code 说明.
 * 
    • -41001: encodingAesKey 非法
    • -41003: aes 解密失败
    • -41004: 解密后得到的buffer非法
    • -41005: base64加密失败
    • -41016: base64解密失败
    */ public static $OK = 0; public static $IllegalAesKey = -41001; public static $IllegalIv = -41002; public static $IllegalBuffer = -41003; public static $DecodeBase64Error = -41004; // 小程序 public static $appid = 'XXX'; //小程序appid public static $secret = 'XXX'; //小程序秘钥

public $sessionKey ='';

// 获取openId session-key 等
public function getopenId($value='')
{   

    $code = I('post.code');
    $appid = self::$appid;
    $secret = self::$secret;
    $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='. $appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
    $result = httpGet($url);
    $res = json_decode($result);
    // session(['sessionKey'=>$res,'expire'=>7200]);
    $this->ajaxReturn($res);


}

// 获取小程序手机号api 接口,对应下面小程序 js
public function getPhoneNumber($value='')
{   

   $encryptedData = I('get.encryptedData');
   $iv = I('get.iv');
   $this->sessionKey=I('get.session_key');
   $res = $this->decryptData($encryptedData,$iv);
   // $res = json_decode($res);
   if($res->phoneNumber){
        // $res->phoneNumbe 就是手机号可以 写入数据库或者做其他操作
   }

   $this->ajaxReturn(['msg'=>$res,'status'=>'1']); //把手机号返回

}

// 小程序解密

public function decryptData($encryptedData,$iv)
{
if (strlen($this->sessionKey) != 24) {
return self::$IllegalAesKey;
}
$aesKey=base64_decode($this->sessionKey);

    if (strlen($iv) != 24) {
        return self::$IllegalIv;
    }
    $aesIV=base64_decode($iv);

    $aesCipher=base64_decode($encryptedData);

    $result=openssl_decrypt( $aesCipher,"AES-128-CBC",$aesKey,1,$aesIV);

    $dataObj=json_decode( $result );
    if( $dataObj  == NULL )
    {
        return self::$IllegalBuffer;
    }
    if( $dataObj->watermark->appid != self::$appid )
    {
        return self::$IllegalBuffer;
    }

    return  $dataObj;
    // return self::$OK;
}

function httpGet($url) {
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_TIMEOUT,500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,CURLOPT_URL,$url);

$res = curl_exec($curl);
curl_close($curl);

return $res;

}

}

//2、小程序

2.1在app.js 启动页面里先login

// 登录
// if (!wx.getStorageSync('session_key') || wx.getStorageSync('time') < Date.parse(new Date())){ // 判断session_key是不是存在获者过期
wx.login({
success: res => {
console.log(res)
// 发送 res.code 到后台换取 openId,sessionKey,unionId
wx.request({
url: 'https://www.zhixiaobing.com/index.php?m=&amp;c=api&amp;a=getopenId&#39;,header: { "Content-Type": "application/x-www-form-urlencoded" },method: 'post',data: { code: res.code },success: function (res) {
console.log(res.data);
wx.setStorageSync('openid',res.data.openid)
wx.setStorageSync('session_key',res.data.session_key)
wx.setStorageSync('time',parseInt(Date.parse(new Date())) + 7200)
}
})

    }
  })</pre><p>//2.2 在小程序模板里写组件</p><pre ><button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" >//这是官方的组件点击会弹出授权页面</pre><p>在js里写下面的函数</p><pre >getPhoneNumber: function (e) {
var that =this;

var session_key = wx.getStorageSync(&#39;session_key&#39;)
if (e.detail.errMsg == &#39;getPhoneNumber:fail user deny&#39;) {
  wx.showModal({
    title: &#39;提示&#39;,showCancel: false,content: &#39;未授权&#39;,success: function (res) { }
  })
} else {//确认授权
  wx.request({
    url: &#39;https://www.showoow.com/index.php?m=mini&amp;c=api&amp;a=getPhoneNumber&amp;openid=&#39; + wx.getStorageSync(&#39;openid&#39;),//openid是app.js 已经存的
    header: {"Content-Type": "application/x-www-form-urlencoded" },method: "get",data: {
      encryptedData: e.detail.encryptedData,iv: e.detail.iv,session_key:session_key
    },success:function(res){
      if (res.data.msg.phoneNumber){
        console.log(res);
        wx.showModal({
          title: &#39;提示&#39;,content: &#39;授权成功&#39;,success: function () {
            wx.setStorageSync(&#39;phoneNumber&#39;,res.data.msg.phoneNumber);
            var time = Date.parse(new Date()) + 60 * 60 * 24 * 2
            wx.setStorageSync(&#39;exp&#39;,time );
          }
        })
        setTimeout(function(){
          wx.navigateTo({
            url: &#39;/pages/form/form&#39;,})
        },1500);
        that.setData({
          show:&#39;show&#39;,hiden:&#39;&#39;
        })
      }else{
        wx.showToast({
          title: &#39;授权失败&#39;,icon:&#39;loading&#39;
        })
      }

    },fail:function(){
      wx.showToast({
        title: &#39;授权失败&#39;,icon: &#39;loading&#39;
      })
    }
  })

}

},

到此小程序获取手机号完结,在公司写了个小程序,可以正常获取手机号

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的
(1)创建模版:/App/Home/View/Form/edit.html   <FORM method=\"post\" action=\"__URL__/update\">
自定义配置文件user.php: <?php return array(    \'sex\'=>\'man\', ); config.php: <?php return array(
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦后台中系统设置),以方便站长在后台修改配置文件;那么这个功能是如果实现的呢?在ThinkPHP中有没有捷径可走呢?答案肯定是有的。下面大概说一下这个功能
废话不多说先上图预览下,即本博客的分页; 这个分页类是在thinkphp框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;
在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助。
thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。
配置数据库:/app/Common/Conf/config.php 方法一: // 添加数据库配置信息 \'DB_TYPE\'   => \'mysql\',// 数据库类型
/app/Home/Controller/IndexController.class.php
(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_data` (   `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
(1)控制器设置:/app/Home/Controller/IndexController.class.php <?php namespace HomeController; use ThinkController;
(1)普通模式 http://localhost/index.php?m=module&a=action&var=value m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。
入库的时候用htmlspecialchars()处理含有html代码的内容 输出的时候用htmlspecialchars_decode()处理含有html代码的内容
<?php define(\'APP_NAME\',\'app\'); define(\'APP_PATH\',\'./app/\'); define(\'APP_DEBUG\',TRUE); // 开启调试模式
(1)创建控制器中定义read方法:/App/Home/Controller/FormController.class.php public function read($id=0){
一、实现不同字段相同的查询条件 $User = M(\"User\"); // 实例化User对象 $map[\'name|title\'] = \'thinkphp\';
如果你的数据完全是内部操作写入而不是通过表单的话(也就是说可以充分信任数据的安全),那么可以直接使用add方法,如:
查询表达式的使用格式: $map[\'字段名\'] = array(\'表达式\',\'查询条件\'); 表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
一、使用字符串作为查询条件 $User = M(\"User\"); // 实例化User对象 $User->where(\'type=1 AND status=1\')->select();