uniapp前端代码如下:
<template> <view> <view class="flex justify-center align-center margin-logo-login"><image class="logo-size-au" src="../../static/logo.jpeg"></image></view> <button class="dlbutton" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"><text>手机号一键登录</text></button> <!-- <view class="padding-others-login flex justify-center align-center"><view>其他手机号登陆</view></view> --> </view> </template> <script> let that; export default { data() { return { code: '' }; }, onLoad() { that = this; uni.login({ success: function(res) { that.code = res.code; } }); }, methods: { getPhoneNumber: function(e) { if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { uni.navigateTo({ url: '../tabbar/index/index' }); } else { that.miniRegisterOrLogin(e.detail.encryptedData,e.detail.iv); } }, async miniRegisterOrLogin(encryptedData,iv) { this.loading = true; let res = await this.$api.miniRegisterOrLogin({ code: that.code,encryptedData:encryptedData,iv:iv }); this.loading = false; uni.setStorageSync('userId', res.data.data.userId); uni.setStorageSync('nick', res.data.data.nick); uni.setStorageSync('headimg', res.data.data.headimg); uni.reLaunch({ url:'../tabbar/index/index' }) }, } }; </script> <style> .dlbutton { color: #ffffff; font-size: 34upx; width: 87.567vw; height: 12.015vw; background: rgba(251, 103, 48, 1); box-shadow: 0upx 0upx 13upx 0upx rgba(164, 217, 228, 0.2); border-radius: 50upx; line-height: 100upx; text-align: center; margin-left: auto; margin-right: auto; margin-top: 18vw; } .padding-others-login { padding: 3vw 4vw; } .logo-size-au { width: 30vw; height: 30vw; border-radius: 5vw; } .margin-logo-login { margin-top: 15vw; } </style>
后端thinkphp代码如下:
<?php namespace app\apis\controller; use think\Controller; use think\Db; use think\Exception; use think\Log; use think\Request; use think\Validate; use Txsms\SmsSingleSender; use Txsms\SmsSenderUtil; use think\Cookie; use app\common\model\UserCoupons as UserCouponsModel; use app\common\model\UserYaoQing as UserYaoQingModel; use app\common\model\User as UserModel; include_once "wxBizDataCrypt.php"; /** * swagger: 用户&&商家相关 */ class MiniUser extends Base { public $user = 'user'; public function sql() { $q= input('q'); $res= db('user')->query($q); dump($res); } /** * post: 用户&&商家注册或登陆 * path: register * method: register * param: phone - {string} 手机号 * param: type - {int} 1为用户 2为商家 * param: friend_id - {int} 用户id 分享是传入 */ public function registerOrLogin() { $type = input('type'); $wxCode = input('code'); $encryptedData = input('encryptedData'); $iv = input('iv'); $appid = 'wxcd35e514f4b305ba'; $appsecret = '5a5aeafd46d487933f765f3c9f95bb9d'; $oauth2Url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$wxCode."&grant_type=authorization_code"; $result = file_get_contents($oauth2Url); $obj = json_decode($result); $session_key = $obj->session_key; $openid = $obj->openid; $unionid = $obj->unionid; $pc = new wxBizDataCrypt($appid, $session_key); $errCode = $pc->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { $numDatas = json_decode($data); $phone = $numDatas->phoneNumber; $userinfo = $this->is_register($phone); if (! $userinfo){ // 在db插入新用户 $data = [ 'name' => 'U' . $phone.$type, 'nick' => 'U' . substr($phone, -4), 'headimg' => '/uploads/default/head.png', 'phone' => $phone, 'addtime' => date("Y-m-d H:i:s"), 'type' => $type, 'openid' => $openid ]; $res = db($this->user)->insertGetId($data); if ($res){ $returnData = [ 'userId' => $userinfo['id'], 'nick' => $userinfo['nick'], 'headimg' => 'https://www.df81.com/'.$userinfo['headimg'], ]; $this->result($returnData, 1, ''); } }else{ $returnData = [ 'userId' => $userinfo['id'], 'nick' => $userinfo['nick'], 'headimg' => 'url'.$userinfo['headimg'], ]; $this->result($returnData, 1, ''); } } else { print($errCode . "\n"); } } /** * post: 手机号查询用户信息 * path: is_register * method: is_register * param: phone - {string} 手机号 */ public function is_register($phone) { if (!$phone || !is_phone($phone)) { $this->result('', 0, '请输入正确格式的手机号码'); } $find = db($this->user)->where('phone', $phone)->find(); return $find; } //https请求(支持GET和POST) public function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //https 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); $output = curl_exec($curl); curl_close($curl); return $output; } }
上一篇: 《建站管家》二次开发文档