1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use App\Models\Custom;
- use App\Facades\Servers\WechatMini\Mini;
- /**
- * 微信接口
- *
- * @author 刘相欣
- *
- * */
- class Wechat extends Api{
-
-
- /**
- * 小程序手机号授权 /api/wechat/phone_number
- *
- * @param string $code 授权码
- * @param string $openid 微信openid
- *
- * */
- public function phone_number(Custom $Custom){
- // 接口验签
- // $this->verify_sign();
- // 接收参数
- $code = request('code','');
- $openid = request('openid','');
- // 授权手机号结果
- $result = Mini::getUserPhone($code);
- // 如果所需字段不存在
- if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>'授权失败','data'=>['error'=>$result['error']]]);
- // 如果所需字段不存在
- if( empty($result['purePhoneNumber']) ) return json_send(['code'=>'error','msg'=>'未获取到手机号','data'=>['error'=>'未获取到手机号']]);
- // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
- $phone = $result['purePhoneNumber'];
- // 查询用户
- $custom = $Custom->getOneByPhone($phone);
- // 如果用户状态被拉黑,不允许登录
- if( !empty($custom['status']) ) return json_send(['code'=>'error','msg'=>'禁用账号','data'=>['error'=>'禁用账号']]);
- // 如果没有ID
- if( empty($custom['uid']) ) {
- // 注册账号
- $custom['uid'] = $Custom->add(['phone'=>$phone,'username'=>hide_phone($phone),'openid'=>$openid]);
- // 注册失败
- if( empty($custom['uid']) ) return json_send(['code'=>'error','msg'=>'注册失败,请重试','data'=>['error'=>'注册失败,请重试']]);
- }
- // 进行登录
- $token = $Custom->createLoginAuthcode($custom['uid'],time());
- // 返回结果
- return json_send(['code'=>'success','msg'=>'登录成功','data'=>$token]);
- }
- public function jscode2session()
- {
- $code = request('code','');
- // 授权手机号结果
- $result = Mini::jscode2session($code);
- if (!$result['openid']) return json_send(['code'=>'error','msg'=>'获取openid失败','data'=>$result['error']]);
- return json_send(['code'=>'success','msg'=>'获取openid成功','data'=>$result['openid']]);
- }
- }
|