|
|
@@ -2,98 +2,98 @@
|
|
|
|
|
|
namespace App\Service\Weixin;
|
|
|
|
|
|
+use App\Model\CustomerSupply;
|
|
|
use App\Model\Employee;
|
|
|
use App\Model\WxEmployeeOfficial;
|
|
|
use App\Service\Service;
|
|
|
-use App\Model\WxEmployee;
|
|
|
-use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
class WxEmployeeService extends Service
|
|
|
{
|
|
|
public function setUser($data){
|
|
|
- if(empty($data['openid'])) return [false, 'openId不能为空!'];
|
|
|
+ if(empty($data['openid'])) return [false, 'openid不能为空'];
|
|
|
+ if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误'];
|
|
|
$openid = $data['openid'];
|
|
|
- $user = WxEmployeeOfficial::where('openid',$openid)->first();
|
|
|
+ $appid = config("wx_msg.f_appid");
|
|
|
+ $user = WxEmployeeOfficial::where('openid',$openid)
|
|
|
+ ->where('type', $data['login_type'])
|
|
|
+ ->where('appid', $appid)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $state = 0;
|
|
|
if(empty($user)) {
|
|
|
$user = new WxEmployeeOfficial();
|
|
|
$user->openid = $openid;
|
|
|
- $user->appid = WeixinService::APPID;
|
|
|
+ $user->type = $data['login_type'];
|
|
|
+ $user->appid = $appid;
|
|
|
$user->save();
|
|
|
- $state = 0;
|
|
|
}else{
|
|
|
- $state = 1;
|
|
|
- if(empty($user->employee_id)) $state = 0;
|
|
|
+ if(! empty($user->employee_id)) $state = 1;
|
|
|
}
|
|
|
|
|
|
- return [true,['openid'=>$openid, 'state'=>$state ]];
|
|
|
+ return [true, ['openid'=>$openid, 'state'=>$state]];
|
|
|
}
|
|
|
|
|
|
- public function login($data,$openid){
|
|
|
- if(empty($data['account'])) return [false, '账号不能为空'];
|
|
|
- if(empty($data['password'])) return [false, '密码不能为空'];
|
|
|
- if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空'];
|
|
|
-
|
|
|
- $account = $data['account'];
|
|
|
- $password = $data['password'];
|
|
|
- list($status,$data) = $this->loginRule([
|
|
|
- 'account' => $account,
|
|
|
- 'password' => $password,
|
|
|
- ]);
|
|
|
- if(! $status) return [false, $data];
|
|
|
+ public function login($data, $openid){
|
|
|
+ list($status, $a_data) = $this->loginRule($data, $openid);
|
|
|
+ if(! $status) return [false, $a_data];
|
|
|
+ list($user, $customer) = $a_data;
|
|
|
|
|
|
- $user_id = $data['id'];
|
|
|
- $user = WxEmployeeOfficial::where('openid',$openid)->first();
|
|
|
+ try{
|
|
|
+ $user->employee_id = $customer['id'];
|
|
|
+ $user->save();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ return [false, $exception->getMessage()];
|
|
|
+ }
|
|
|
|
|
|
- if(empty($user)) {
|
|
|
- $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
|
|
|
- if($bool) return [false,'该账号已经与其他微信用户绑定!'];
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
|
|
|
+ public function loginRule($data, $openid){
|
|
|
+ if(empty($data['account'])) return [false, '账号不能为空'];
|
|
|
+ if(empty($data['password'])) return [false, '密码不能为空'];
|
|
|
+ if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空'];
|
|
|
+ if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误'];
|
|
|
+
|
|
|
+ $appid = config("wx_msg.f_appid");
|
|
|
+ $user = WxEmployeeOfficial::where('openid',$openid)
|
|
|
+ ->where('type', $data['login_type'])
|
|
|
+ ->where('appid',$appid)
|
|
|
+ ->first();
|
|
|
+ if(! empty($user)) {
|
|
|
+ if(! empty($user->employee_id)) return [false, '微信用户已绑定系统账号'];
|
|
|
+ }else{
|
|
|
$user = new WxEmployeeOfficial();
|
|
|
$user->openid = $openid;
|
|
|
- $user->appid = WeixinService::APPID;
|
|
|
- $user->employee_id = $user_id;
|
|
|
+ $user->type = $data['login_type'];
|
|
|
+ $user->appid = $appid;
|
|
|
$user->save();
|
|
|
- }else{
|
|
|
- if(empty($user->employee_id)){
|
|
|
- $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
|
|
|
- if($bool) return [false,'该账号已经与其他微信用户绑定!'];
|
|
|
- $user->employee_id = $user_id;
|
|
|
- $user->save();
|
|
|
- }
|
|
|
- if(! empty($user->employee_id) && $user->employee_id != $user_id) return [false,'该账号已经与其他微信用户绑定!'];
|
|
|
}
|
|
|
|
|
|
- return [true, $data];
|
|
|
- }
|
|
|
-
|
|
|
- public function loginRule($data){
|
|
|
- if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
|
|
|
- if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
|
|
|
-
|
|
|
- $account = $data['account'];
|
|
|
- $res = Employee::where('del_time',0)
|
|
|
- ->where(function ($query)use($account) {
|
|
|
- $query->where('account', $account)
|
|
|
- ->orWhere('mobile', $account);
|
|
|
- })
|
|
|
- ->get()->toArray();
|
|
|
-
|
|
|
- if(empty($res)) return [false,'账号不存在或已被删除!'];
|
|
|
- if(count($res) > 1) return [false,'手机号绑定多个账户!'];
|
|
|
-
|
|
|
- $res = reset($res);
|
|
|
- if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
|
|
|
- if(empty($res['password'])){
|
|
|
- if(empty($res['mobile'])) return [false, '用户手机号码信息不能为空'];
|
|
|
- $lastFour = substr($res['mobile'], -4);
|
|
|
- if($lastFour != $data['password']) return [false,'密码错误!'];
|
|
|
-
|
|
|
- Employee::where('id', $res['id'])
|
|
|
- ->update(['password' => Hash::make($data['password'])]);
|
|
|
+ if($data['login_type'] == WxEmployeeOfficial::login_type_one){
|
|
|
+ //供应商
|
|
|
+ $customer = CustomerSupply::where('del_time',0)
|
|
|
+ ->where('account', $data['account'])
|
|
|
+ ->where('password', $data['password'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false, '账号或者密码错误,绑定失败'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
}else{
|
|
|
- if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
|
|
|
+ //企业内部员工
|
|
|
+ $customer = Employee::where('del_time',0)
|
|
|
+ ->where('account', $data['account'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false,'账号不存在,绑定失败'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ if($customer['state'] == Employee::NOT_USE) return [false,'账号停用,绑定失败'];
|
|
|
+ $lastFour = substr($customer['mobile'], -4);
|
|
|
+ if($lastFour != $data['password']) return [false,'密码错误,绑定失败'];
|
|
|
}
|
|
|
+ $bool = WxEmployeeOfficial::where('employee_id',$customer['id'])
|
|
|
+ ->where('type', $data['login_type'])
|
|
|
+ ->where('appid',$appid)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false, '系统账号已被其他微信用户绑定,绑定失败'];
|
|
|
|
|
|
- return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
|
|
|
+ return [true, [$user, $customer]];
|
|
|
}
|
|
|
}
|