切换导航条
此项目
正在载入...
登录
李忠强
/
temporaryfood
·
提交
转到一个项目
GitLab
转到仪表盘
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
何书鹏
3 years ago
提交
34d4839ea1ff87fc8aecf26f73dfb04b57fef6f9
1 个父辈
7e7ddc31
用户授权获取手机号
隐藏空白字符变更
内嵌
并排对比
正在显示
1 个修改的文件
包含
38 行增加
和
24 行删除
application/api/controller/User.php
application/api/controller/User.php
查看文件 @
34d4839
...
...
@@ -5,8 +5,8 @@ namespace app\api\controller;
use
app\api\model\Tax
;
use
app\api\model\UserAddress
;
use
app\api\model\UserCoupon
;
use
app\api\model\UserInvitation
;
use
app\common\controller\Api
;
use
EasyWeChat\Factory
;
use
fast\Http
;
use
think\Config
;
use
think\Db
;
...
...
@@ -16,7 +16,7 @@ use think\Db;
*/
class
User
extends
Api
{
protected
$noNeedLogin
=
[
'third'
,
'joinUs'
];
protected
$noNeedLogin
=
[
'third'
,
'joinUs'
,
'getPhoneNumber'
];
protected
$noNeedRight
=
'*'
;
public
function
_initialize
()
...
...
@@ -132,33 +132,47 @@ class User extends Api
}
}
/**
* 获取微信手机号
*
* @ApiMethod (POST)
* @param string $token token
* @param string $code Code码
* @ApiTitle (用户授权获取手机号)
* @ApiMethod (POST)
* @ApiParams (name="sessionKey", type="string", required=true, description="小程序sessionKey")
* @ApiParams (name="iv", type="string", required=true, description="小程序iv")
* @ApiParams (name="encryptedData", type="string", required=true, description="小程序encryptedData")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"mobile": "13580006666", //没有区号的手机号
}
})
*/
public
function
thirdMobile
()
public
function
getPhoneNumber
()
{
$code
=
$this
->
request
->
post
(
'code'
);
if
(
!
$code
){
$this
->
error
(
'后台所需参数缺失请完善参数'
);
$param
=
$this
->
request
->
param
();
$validate
=
new
\think\Validate
([
'sessionKey'
=>
'require'
,
'iv'
=>
'require'
,
'encryptedData'
=>
'require'
,
]);
$validate
->
message
([
'sessionKey.require'
=>
'sessionKey参数错误!'
,
'iv.require'
=>
'iv参数错误!'
,
'encryptedData.require'
=>
'encryptData参数错误!'
,
]);
if
(
!
$validate
->
check
(
$param
))
{
$this
->
error
(
$validate
->
getError
());
}
$token
=
$this
->
getToken
();
$param
=
[];
$param
[
'code'
]
=
$code
;
$param
=
json_encode
(
$param
);
$wxapi
=
Http
::
post
(
'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='
.
$token
,
$param
);
$wxapi
=
json_decode
(
$wxapi
,
true
);
if
(
$wxapi
[
'errcode'
]
!=
0
){
$this
->
error
(
$wxapi
[
'errmsg'
]);
}
$mobile
=
$wxapi
[
'phone_info'
][
'purePhoneNumber'
];
// 获取小程序配置
$app
=
Factory
::
miniProgram
([
'app_id'
=>
Config
::
get
(
'site.appid'
),
'secret'
=>
Config
::
get
(
'site.secret'
),
]);
$res
=
$app
->
encryptor
->
decryptData
(
$param
[
'sessionKey'
],
$param
[
'iv'
],
$param
[
'encryptedData'
]);
// 更新手机号
$user
=
$this
->
auth
->
getUser
();
$user
->
mobile
=
$mobile
;
$user
->
isUpdate
()
->
save
();
$user
->
mobile
=
$res
[
'purePhoneNumber'
];
$user
->
save
();
$this
->
success
(
'授权成功'
,[
'mobile'
=>
$res
[
'purePhoneNumber'
]]);
}
/**
...
...
请
注册
或
登录
后发表评论