欢迎您光临本站
faddei头像
faddei

2021-07-11

评论者
微信小程序
0 1

wxml代码

<view class="getmobilebg" wx:if="{{mobile==''}}"></view>
<view class="getmobile" wx:if="{{mobile==''}}">
    <view class="titles">速美雅居智能家装请求获取您的手机号码</view>
    <button class="btmb btmb1" bindtap="hidemobile">暂不提醒</button>
    <button class="btmb btmb2" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">前往授权</button>
</view>


wxss代码

.getmobilebg {width:100%;height:100vh;background:rgba(0,0,0,0.6);position:fixed;z-index:21;top:0px;left:0px;}
.getmobile {width:100%;height:30vh;background:#fff;position:fixed;z-index:22;bottom:0px;left:0px;border-bottom:1px solid #dedede;}
.titles {width:86%;float:left;margin-left:7%;height:80rpx;line-height:80rpx;font-size:32rpx;color:#555;margin-top:46rpx;margin-bottom:40rpx;}
.btmb {width:39%;float:left;margin-left:7%;font-size:30rpx;padding:5rpx 0;}
.btmb1 {color:#555;background:#fff;}
.btmb2 {color:#fff;background:#33c282;}


js代码

app.globalData.wwwurl为定义的接口域名地址

const app = getApp()
Page({
  data: {
    mobile: ''
  },
  //暂不提醒
  hidemobile: function(){
    this.setData({
      mobile: true
    })
  },
  //获取用户手机号
  getPhoneNumber(e) {
    var that = this
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.login({
        success: function(res) {
          if (res.code) {
            wx.request({
              url: app.globalData.wwwurl + '/Userapi/getWechatInfo',
              data: {
                code: res.code
              },
              method: "post",
              success: function(res) {
                wx.request({
                  url: app.globalData.wwwurl + '/Userapi/getmobile',
                  data: {
                    user_id: wx.getStorageSync('user_id'),
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    sessionKey: res.data.data.session_key
                  },
                  method: "post",
                  success: function(res) {
                    that.setData({
                      mobile: res.data.data.mobile
                    })
                  }
                })
              }
            })
          }
        }
      })
    }
  }
})


php接口代码(TP5为例)

//获取session_key
public function getWechatInfo()
{
    $appid  = config('wechat.appid');    //appid
    $secret = config('wechat.secret');    //secret
    $code   = input('code');            //小程序传来的code值
    //根据code获取用户信息,并插入数据库
    $infourl = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
    $info = $this->getCurl($infourl);
    $data['session_key'] = $info['session_key'];
    return returnMsg('200','登录成功',$data);
}
//获取手机号并入库
public function getmobile()
{
    $user_id = input('user_id');
    $appid = config('wechat.appid');
    $encryptedData = input('encryptedData');
    $iv = input('iv');
    $sessionKey = input('sessionKey');
    $res = $this->decryptData($appid, $encryptedData, $iv, $sessionKey, $data);
    $data['mobile'] = '';
    if($res['phoneNumber']){
        $data['mobile'] = $res['phoneNumber'];
        @Db::name('user')->where(['user_id'=>$user_id])->update($data);
    }
    return returnMsg('200','success',$data);
}
public function decryptData($appid, $encryptedData, $iv, $sessionKey, &$data)
{
    $aesKey    = base64_decode($sessionKey);
    $aesIV     = base64_decode($iv);
    $aesCipher = base64_decode($encryptedData);
    $result    = openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
    $resultarr = json_decode($result,true);
    return $resultarr;
}



版权声明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。

350

精彩推荐

暂无评论

文明用语