Browse Source

用户手机号

lizhen_gitee 1 year ago
parent
commit
c5675efbbf

+ 31 - 24
application/api/controller/User.php

@@ -3,21 +3,15 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
-use app\common\library\Ems;
-use app\common\library\Sms;
-use fast\Random;
-use think\Config;
-use think\Validate;
-
 use think\Db;
-use miniprogram\wxBizDataCrypt;
+use app\common\library\Wechat;
 
 /**
  * 会员接口
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getUserOpenid','wxMiniProgramLogin'];
+    protected $noNeedLogin = ['getUserOpenid','wxMiniProgramLogin'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -51,28 +45,34 @@ class User extends Api
      */
     public function profile()
     {
-        $user = $this->auth->getUser();
-        $nickname = $this->request->post('nickname');
-        $nickname = $this->request->post('mobile');
+        $nickname = $this->request->post('nickname','');
+        $mobile = $this->request->post('mobile','');
+        $introcode = $this->request->post('introcode','');
         $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
-        if ($username) {
-            $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
+
+        if ($mobile) {
+            $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find();
             if ($exists) {
-                $this->error(__('Username already exists'));
+                $this->error('手机号已经被他人注册');
             }
-            $user->username = $username;
         }
-        if ($nickname) {
-            $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
-            if ($exists) {
-                $this->error(__('Nickname already exists'));
+        if ($introcode) {
+            $exists = \app\common\model\User::where('introcode', $introcode)->find();
+            if (!$exists) {
+                $this->error('不存在的邀请码');
             }
-            $user->nickname = $nickname;
         }
-        $user->bio = $bio;
-        $user->avatar = $avatar;
-        $user->save();
-        $this->success();
+
+        $data = [
+            'nickname'  => $nickname,
+            'mobile'    => $mobile,
+            'introcode' => $introcode,
+            'avatar'    => $avatar,
+        ];
+
+        Db::name('user')->where('id',$this->auth->id)->update($data);
+
+        $this->success(1);
     }
 
     //用户详细资料
@@ -101,6 +101,13 @@ class User extends Api
         if(!isset($openidInfo['openid'])) {
             $this->error('用户openid获取失败',$openidInfo);
         }
+
+        //手机号
+        $wechat = new Wechat();
+        $mobile = $wechat->getPhoneNumber($code);
+        dump($mobile);
+
+
         //  获取的结果存入数据库
         $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
         if($find) {

+ 2 - 2
application/common/library/Auth.php

@@ -166,7 +166,7 @@ class Auth
         $ip = request()->ip();
         $time = time();
 
-//        $introcode = User::column("introcode");
+        $introcode = User::column("introcode");
 
         $data = [
 //            'username' => $username,
@@ -175,7 +175,7 @@ class Auth
 //            'mobile'   => $mobile,
             'nickname'  => $this->get_rand_nick_name(),
             'avatar'    => '/assets/img/avatarlogo.png',
-//            'introcode' => $this->getUinqueNo(8, $introcode),
+            'introcode' => $this->getUinqueNo(8, $introcode),
         ];
         $params = array_merge($data, [
 //            'salt'      => Random::alnum(),

+ 187 - 0
application/common/library/Wechat.php

@@ -0,0 +1,187 @@
+<?php
+
+namespace app\common\library;
+
+use fast\Http;
+use think\Cache;
+use think\Session;
+
+/**
+ * 微信授权
+ *
+ */
+class Wechat
+{
+    private $app_id = '';
+    private $app_secret = '';
+
+    private $scope = 'snsapi_userinfo';
+
+    public function __construct()
+    {
+        $wxConfig = config('wxMiniProgram');
+        $this->app_id = $wxConfig['appid'];
+        $this->app_secret = $wxConfig['secret'];
+    }
+
+    /**
+     * 获取微信授权链接
+     *
+     * @return string
+     */
+    /*public function getAuthorizeUrl()
+    {
+        $redirect_uri = addon_url('epay/api/wechat', [], true, true);
+        $redirect_uri = urlencode($redirect_uri);
+        $state = \fast\Random::alnum();
+        Session::set('state', $state);
+        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect";
+    }*/
+
+    /**
+     * 获取微信openid
+     *
+     * @return mixed|string
+     */
+
+    /*
+     array(5) {
+                  ["access_token"] => string(89) "49_r4I-StVANS8uYWTUHG86GJ-g1iH6mMFWy-9LeOta--2l6Bzg8LVDQFu8VSARu87atETzVJFZ-fndy-aQqEb8wQ"
+                  ["expires_in"] => int(7200)
+                  ["refresh_token"] => string(89) "49_8Vfa-imGcRmUwcESvpvMcEkiuh8kmD_movl9bIz9DV5GonZrqfIya5NgT7G-NMeJ7KTuCjMrSJi4BtojjDxnCQ"
+                  ["openid"] => string(28) "o8lxjwRjlDortQKhTk1dpHjQxcBU"
+                  ["scope"] => string(15) "snsapi_userinfo"
+            }
+     * */
+
+    public function getOpenid($code = '')
+    {
+        $openid = Session::get('openid');
+        if (!$openid) {
+
+            $token = $this->getAccessToken($code);
+            $openid = isset($token['openid']) ? $token['openid'] : '';
+            if ($openid) {
+                Session::set("openid", $openid);
+            }
+        }
+        return $openid;
+    }
+
+    /*
+    array(2) {
+              ["ret"] => bool(true)
+              ["msg"] => string(307) "{"openid":"o8lxjwRjlDortQKhTk1dpHjQxcBU","nickname":"科","sex":1,"language":"zh_CN","city":"临沂","province":"山东","country":"中国","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/LGYWIv4F5vxZ2zCM9GEUynoQeJ6ibX9IfoKPAWLlGIugn1mgaAMPuqxzPBDQ3ktLEv2ia7HmOeJYTg5LofG8YlwQ\/132","privilege":[]}"
+            }
+     */
+    /*public function getwxuserinfo($code = '')
+    {
+        $wxuserinfo = Session::get('wxuserinfo');
+        if (!$wxuserinfo) {
+
+            $token = $this->getAccessToken($code);
+
+            $openid = isset($token['openid']) ? $token['openid'] : '';
+
+            $access_token = isset($token['access_token']) ? $token['access_token'] : '';
+
+            $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
+            if ($ret['ret']) {
+                $wxuserinfo = json_decode($ret['msg'], true);
+                Session::set('wxuserinfo', $wxuserinfo);
+            }else{
+                $wxuserinfo = [];
+            }
+
+        }
+        return $wxuserinfo;
+    }*/
+
+
+    /**
+     * 获取授权token网页授权
+     *
+     * @param string $code
+     * @return mixed|string
+     */
+    public function getAccessToken($code = '')
+    {
+        $params = [
+            'appid'      => $this->app_id,
+            'secret'     => $this->app_secret,
+            'code'       => $code,
+            'grant_type' => 'authorization_code'
+        ];
+        $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+
+    public function getPublicAccessToken(){
+        $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+
+            return $ar['access_token'];
+        }
+    }
+
+    public function getPhoneNumber($code = ''){
+        $accessToken = $this->getPublicAccessToken();
+        $getPhoneUrl = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
+
+        $params = [
+            'code'=>$code
+        ];
+
+        $ret = Http::sendRequest($getPhoneUrl, $params, 'POST');
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+
+    }
+
+    //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
+    /*public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
+
+        $params = [
+            "touser"     => $openid,
+            "template_id"       => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
+            'url' => 'https://yanglaoweb.lanmaonet.com',
+            "data" => [
+                "first" => ["value"=>$first,"color"=>$color],
+                "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
+                "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
+                "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
+                "remark" => ["value"=>$remark,"color"=>$color],
+            ],
+            'miniprogram' => [],
+        ];
+
+        $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
+
+    }*/
+
+    /*public function getJsticket($code = '')
+    {
+        $jsticket = Session::get('jsticket');
+        if (!$jsticket) {
+            $token = $this->getAccessToken($code);
+            $params = [
+                'access_token' => 'token',
+                'type'         => 'jsapi',
+            ];
+            $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
+            if ($ret['ret']) {
+                $ar = json_decode($ret['msg'], true);
+                return $ar;
+            }
+        }
+        return $jsticket;
+    }*/
+}