Explorar el Código

[智价云] 微信手机号授权登录

tangyuanwang hace 3 días
padre
commit
3e6f543d84

+ 81 - 16
app/Http/Controllers/Api/Login.php

@@ -6,6 +6,9 @@ use App\Models\Api\Personnel\Employee as EmployeeModel;
 use App\Http\Requests\Api\Login as Request;
 use App\Facades\Servers\Sms\VerifyCode as Sms;
 use Illuminate\Support\Facades\Cache;
+use App\Facades\Servers\Wechat\OpenPlat;
+use App\Models\Api\Personnel\EmployeeOpenid as EmployeeOpenidModel;
+use Illuminate\Support\Facades\DB;
 
 /**
  * API登录控制器
@@ -35,7 +38,7 @@ class Login extends Api
 		// 接收数据
 		$password    = $Request->input('password', '');
 		// 查询用户
-		$admin        = $EmployeeModel->Where('employee_code', $employee_code)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
+		$admin        = $EmployeeModel->Where('employee_code', $employee_code)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin || $admin['status'])    return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 用户不存在
@@ -45,10 +48,8 @@ class Login extends Api
 		// 比对密码
 		if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 登录
-		$accessToken                         = $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
-		// 比对密码
+		$accessToken                         = $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
 		if (isset($accessToken['error']))     return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
-		// 获取权限列表
 		$accessToken['username']            = $admin['name'];
 		$accessToken['is_system_admin']			= 0;
 		// 表单令牌
@@ -94,7 +95,7 @@ class Login extends Api
 		// 接收数据
 		$password    = $Request->input('password', '');
 		// 查询用户
-		$user_info        = $EmployeeModel->where('mobile', $phone)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
+		$user_info        = $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$user_info || $user_info['status'])    return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 用户不存在
@@ -104,10 +105,8 @@ class Login extends Api
 		// 比对密码
 		if (md5($password) != $user_info['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 登录
-		$accessToken                         = $EmployeeModel->Login($user_info['id'],$user_info['company_id'], 'api');
-		// 比对密码
+		$accessToken                         = $EmployeeModel->Login($user_info['id'], $user_info['company_id'], 'api');
 		if (isset($accessToken['error']))     return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
-		// 获取权限列表
 		$accessToken['username']            = $user_info['name'];
 		$accessToken['is_system_admin']			= 0;
 		// 表单令牌
@@ -163,7 +162,7 @@ class Login extends Api
 		// 接收数据
 		$password    = $Request->input('password', '');
 		// 查询用户
-		$admin    	= $EmployeeModel->where('email', $email)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('email', $email)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 用户不存在
@@ -173,10 +172,8 @@ class Login extends Api
 		// 比对密码
 		if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
-		// 比对密码
+		$accessToken 						= $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
-		// 获取权限列表
 		$accessToken['username']			= $admin['name'];
 		$accessToken['is_system_admin']			= 0;
 		// 表单令牌
@@ -205,7 +202,7 @@ class Login extends Api
 		if (!$session)                     return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
 		if ($session['code'] != $code || $session['mobile'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
 		// 查询用户
-		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id','id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '账号不存在']);
 		// 用户不存在
@@ -213,10 +210,78 @@ class Login extends Api
 		// 转数组
 		$admin    	= $admin->toArray();
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['id'],$admin['company_id'], 'api');
-		// 比对密码
+		$accessToken 						= $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
+		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
+		$accessToken['username']			= $admin['name'];
+		$accessToken['is_system_admin']			= 0;
+		// 表单令牌
+		return    	json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
+	}
+
+
+	/**
+	 * 小程序手机号授权				/api/login/wechat_phone
+	 * 
+	 * @param	string		$code		授权码
+	 * 
+	 * */
+	public function wechat_phone(Request $Request,  EmployeeModel $EmployeeModel, EmployeeOpenidModel $EmployeeOpenidModel)
+	{
+		// 验证规则
+		$Request->scene('wechat_phone')->validate();
+		$company_id = request('access_token.company_id', '0');
+		// 接收数据
+		$auth_code	= request('auth_code', '');
+		$open_code    = $Request->input('open_code', '');
+		$appId = config('wechat.mini.app_id','');//小程序APPID
+		// 授权结果
+		$result									= OpenPlat::getUserPhone($auth_code, $appId);
+		// 如果所需字段不存在
+		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'];
+		// 查询用户
+		$phone_user_data = $EmployeeOpenidModel->where(['mobile' => $phone])->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		if (!$phone_user_data) return json_send(['code' => 'error', 'msg' => '手机号码未授权']);
+		if ($phone_user_data['status']) 				return json_send(['code' => 'error', 'msg' => '该账号已停用']);
+		$uid = $phone_user_data->employee_id;
+		//获取用户的openid
+		$tokenData							    = OpenPlat::jscode2session($open_code, $appId);
+		if (!isset($tokenData['openid']) || !$tokenData['openid'])             return json_send(['code' => 'error', 'msg' => '获取openid失败', 'data' => $tokenData]);
+		if (!isset($tokenData['unionid']) || !$tokenData['unionid'])            return json_send(['code' => 'error', 'msg' => '获取unionid失败', 'data' => $tokenData]);
+
+		$user_open_data = $EmployeeOpenidModel->where(['mini_openid' => $tokenData['openid']])->first();
+		if (!$user_open_data) {
+			//自动绑定手机号码用户
+			try {
+				$phone_user_data->band_wechat = 1; //绑定微信0=未绑定1=已绑定
+				$phone_user_data->save();
+				$EmployeeOpenidModel->insertGetId([
+					'company_id' => $company_id,
+					'employee_id' => $uid,
+					'unionid' => $tokenData['unionid'] ?? '',
+					'mini_openid' => $tokenData['openid'],
+					'type' => '3',
+					'insert_time' => time()
+				]);
+				DB::commit();
+			} catch (\Exception $e) {
+				DB::rollBack();
+				return json_send(['code' => 'error', 'msg' => '绑定失败', 'data' => $e->getMessage()]);
+			}
+		} else {
+			$user_open_data->unionid = $tokenData['unionid'] ?? '';
+			$user_open_data->mini_openid = $tokenData['openid'];
+			$user_open_data->type = '3';
+			$user_open_data->update_time = time();
+			$user_open_data->save();
+		}
+		// 转数组
+		$admin    	= $phone_user_data->toArray();
+		// 登录
+		$accessToken 						= $EmployeeModel->Login($admin['id'], $admin['company_id'], 'api');
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
-		// 获取权限列表
 		$accessToken['username']			= $admin['name'];
 		$accessToken['is_system_admin']			= 0;
 		// 表单令牌

+ 6 - 1
app/Http/Requests/Api/Login.php

@@ -25,6 +25,8 @@ class Login extends BaseRequest
             'password'  =>  'required',
             'phone'    =>  'required',
             'employee_code'    =>  'required',
+            'auth_code' => 'required',
+            'open_code' => 'required',
         ];
     }
 
@@ -34,7 +36,8 @@ class Login extends BaseRequest
         'mobile'                 => ['phone', 'password'],
         'send_code'              => ['phone'],
         'email'                  => ['email', 'password'],
-        'mobile_code'            => ['phone', 'code']
+        'mobile_code'            => ['phone', 'code'],
+        'wechat_phone'           => ['auth_code', 'open_code']
     ];
 
     /**
@@ -48,6 +51,8 @@ class Login extends BaseRequest
             'password.required'  =>  '请输入登录密码',
             'phone.required'    =>  '请输入手机号码',
             'employee_code.required'    =>  '请输入员工编号',
+            'auth_code.required' =>  '请输入微信授权码',
+            'open_code.required' =>  '请输入微信平台code',
         ];
     }
 }

+ 151 - 0
app/Models/Api/Personnel/EmployeeOpenid.php

@@ -0,0 +1,151 @@
+<?php
+
+namespace App\Models\Api\Personnel;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * 员工openid模型
+ * @author 唐远望
+ * @version 1.0
+ * @date 2026-03-10
+ */
+class EmployeeOpenid extends Model
+{
+    use HasFactory;
+    // 与模型关联的表名
+    protected $table = 'personnel_employee_openid';
+    // 是否主动维护时间戳
+    public $timestamps = false;
+    // 定义时间戳字段名
+    // const CREATED_AT = 'insert_time';
+    // const UPDATED_AT = 'update_time';
+
+
+    /**
+     * 添加
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-04
+     */
+    public function addEmployeeOpenid_content($data)
+    {
+        $insert_data = [
+            'employee_id' => $data['employee_id'],
+            'unionid'    => $data['unionid'],
+            'openid' => $data['openid'],
+            'type'  => isset($data['type']) ? $data['type'] : '3',
+            'insert_time' => time(),
+        ];
+        $EmployeeOpenid_id = $this->insertGetId($insert_data);
+        return $EmployeeOpenid_id;
+    }
+
+
+
+    /**
+     * 写入数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-04
+     * @param $data
+     * @return bool
+     */
+    public function addEmployeeOpenid($data)
+    {
+        DB::beginTransaction();
+        try {
+            $insert_data = [
+                'employee_id' => $data['employee_id'],
+                'unionid'    => $data['unionid'],
+                'openid' => $data['openid'],
+                'type'  => isset($data['type']) ? $data['type'] : '3',
+                'insert_time' => time(),
+            ];
+            $EmployeeOpenid_id = $this->insertGetId($insert_data);
+            DB::commit();
+            return $EmployeeOpenid_id;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            // 错误处理...
+            return false;
+        }
+    }
+
+
+    /**
+     * 编辑内容
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-04
+     * @param $data
+     * @return bool
+     */
+    public function editEmployeeOpenid_content($where, $data)
+    {
+        $EmployeeOpenid = $this->where($where)->first();
+        if (!$EmployeeOpenid) {
+            return false;
+        }
+        $EmployeeOpenid->employee_id = $data['employee_id'];
+        $EmployeeOpenid->unionid = $data['unionid'];
+        $EmployeeOpenid->openid = $data['openid'];
+        $EmployeeOpenid->type = isset($data['type']) ? $data['type'] : '3';
+        $EmployeeOpenid->update_time = time();
+        $EmployeeOpenid->save();
+        return true;
+    }
+
+
+
+    /**
+     * 更新数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-04
+     * @param $data
+     * @return bool
+     */
+    public function updateEmployeeOpenid($EmployeeOpenid, $data)
+    {
+        DB::beginTransaction();
+        try {
+            $EmployeeOpenid->employee_id = $data['employee_id'];
+            $EmployeeOpenid->unionid = $data['unionid'];
+            $EmployeeOpenid->openid = $data['openid'];
+            $EmployeeOpenid->type = isset($data['type']) ? $data['type'] : '3';
+            $EmployeeOpenid->update_time = time();
+            $EmployeeOpenid->save();
+
+            DB::commit();
+            return true;
+            // 成功处理...
+        } catch (\Exception $e) {
+            DB::rollBack();
+            print_r($e->getMessage());
+            exit;
+            // 错误处理...
+            return false;
+        }
+    }
+    /**
+     * 删除数据
+     * @author 唐远望
+     * @version 1.0
+     * @date 2025-12-04
+     * @param $id
+     * @return bool
+     */
+    public function deleteEmployeeOpenid($where)
+    {
+        $EmployeeOpenid = $this->where($where)->first();
+        if (!$EmployeeOpenid) {
+            return false;
+        }
+        $EmployeeOpenid->delete();
+        return true;
+    }
+}

+ 2 - 0
routes/api.php

@@ -26,6 +26,8 @@ Route::any('login/send_code',[App\Http\Controllers\Api\Login::class,'send_code']
 Route::any('login/email', [App\Http\Controllers\Api\Login::class, 'email']);
 //手机验证码登录
 Route::any('login/mobile_code', [App\Http\Controllers\Api\Login::class,'mobile_code']);
+//微信手机号授权登录
+Route::any('login/wechat_phone', [App\Http\Controllers\Api\Login::class, 'wechat_phone']);
 
 // ------违规处理------
 // 低价挂网商品违规处理-列表