123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace App\Service\Wx;
- use App\Model\Employee;
- use App\Model\EmployeeDepartPermission;
- use App\Model\EmployeeFoursShop;
- use App\Model\EmployeeTeamPermission;
- use App\Model\FoursShop;
- use App\Model\StorehouseEmployee;
- use App\Model\TemporaryJs;
- use App\Model\WxEmployee;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- /**
- * 区域相关
- * @package App\Models
- */
- class WxEmployeeService extends Service
- {
- public function setUser($data){
- $code = $data['code'];
- $service = new WxService();
- list($status,$openid) = $service->getOpenid($code);
- if(!$status) return [false,$openid];
- $user = WxEmployee::where('openid',$openid)->first();
- if(empty($user)) {
- $user = new WxEmployee();
- $user->mobile = '';
- $user->openid = $openid;
- $user->appid = $service->appid;
- $user->save();
- $state = 0;
- }else{
- $state = 1;
- if(empty($user->mobile) || empty($user->employee_id)) $state = 0;
- }
- return [true,['openid'=>$openid,'state'=>$state ]];
- }
- public function setMobile($data){
- $code = $data['code'];
- $openid = $data['openid'];
- $service = new WxService();
- list($status,$mobile) = $service->getMobile($code);
- if(!$status) return [false,$mobile];
- $user = WxEmployee::where('openid',$openid)->first();
- if(empty($user)) return [false,'请先登陆'];
- $user->mobile = $mobile;
- $user->save();
- return [true, $mobile];
- }
- public function login($data){
- $account = $data['account'];
- $password = $data['password'];
- $openid = $data['openid'];
- list($status,$data) = $this->loginRule([
- 'account' => $account,
- 'password' => $password,
- ]);
- if(!$status) return [false,$data];
- $user_id = $data['id'];
- $user = WxEmployee::where('openid',$openid)->first();
- if(empty($user)||empty($user->mobile)) return [false,'请先登陆'];
- $user->employee_id = $user_id;
- $user->save();
- 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(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
- if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
- list($status,$construction) = $this->isConstruction($res['id']);
- if(! $status) return [false,'账号授权的门店不存在或者过期!'];
- return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account'],'construction' => $construction ?? []]];
- }
- //是否施工技师
- public function isConstruction($emp_id){
- if(empty($emp_id)) return [false,''];
- //管理员
- if(Employee::SPECIAL_ADMIN == $emp_id){
- $foursShop = FoursShop::where('del_time',0)
- ->select('id')
- ->get()->toArray();
- return [true, array_column($foursShop,'id')];
- }
- $result = EmployeeFoursShop::where('employee_id', $emp_id)
- ->select('four_shop_id','use','expires_time')
- ->where('del_time',0)
- ->get()->toArray();
- $return = [];
- if(! empty($result)){
- date_default_timezone_set("PRC");
- $now = date("Y-m-d");
- foreach ($result as $value){
- if($value['use']){
- if(($value['expires_time'] == 0) || ($value['expires_time'] > 0 && ($now <= date("Y-m-d",$value['expires_time'])))){
- $return[] = $value['four_shop_id'];
- }
- }
- }
- if(empty($return)) return [false,''];
- return [true,$return];
- }
- return [false,''];
- }
- public function checkWxUser($userId){
- $res = Employee::where('id', $userId)
- ->where('del_time',0)
- ->where('state',Employee::USE)->get()->first();
- if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
- list($status,$msg) = $this->isConstruction($userId);
- if(! $status) return [false,'账号授权的门店不存在或者过期!'];
- return [true, $res];
- }
- public function createTemporaryJs($data,$user){
- list($status,$msg) = $this->createTemporaryJsRule($data,$user);
- if(! $status) return [false,$msg];
- try {
- DB::beginTransaction();
- $emp = Employee::where('del_time',0)
- ->where('mobile',$data['mobile'])
- ->first();
- if(! empty($emp)){
- Employee::where('id',$emp->id)
- ->update(['state' => Employee::STATUS_ONE]);
- $employee_id = $emp->id;
- }else{
- $model = new Employee();
- $model->emp_name = '外包技师:' . $data['mobile'];
- $model->state = Employee::STATUS_ONE;
- $model->is_wb = 1;
- $model->crt_id = $user['id'];
- $model->mobile = $data['mobile'];
- $model->password = Hash::make(Employee::DEFAULT_PASS);
- $model->entry_time = date("Y-m-d");
- $model->save();
- $employee_id = $model->id;
- $permisson_model = new EmployeeDepartPermission();
- $permisson_model->employee_id = $employee_id;
- $permisson_model->depart_id = 72;
- $permisson_model->save();
- }
- EmployeeFoursShop::where('employee_id',$employee_id)->update(['del_time' => time()]);
- $insert = [];
- foreach ($data['four_shop'] as $value){
- $insert[] = [
- "employee_id" => $employee_id,
- "four_shop_id" => $value['four_shop_id'],
- "use" => 1,
- 'expires_time' => $value['expires_time'],
- 'crt_time' => time(),
- 'upd_time' => time(),
- ];
- }
- EmployeeFoursShop::insert($insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true, ['account' => $data['mobile'], 'password' => Employee::DEFAULT_PASS]];
- }
- public function createTemporaryJsRule($data,$user){
- $key = TemporaryJs::LimitationSign . $user['id'];
- list($status,$msg) = $this->limitingSendRequestBackg($key,10);
- if(! $status) return [false,$msg];
- if(empty($data['mobile'])) return [false, '外包技师手机号不能为空'];
- $bool = Employee::where('del_time',0)
- ->where('mobile',$data['mobile'])
- ->where('is_wb',0)
- ->exists();
- if($bool) return [false, '手机号已绑定内部员工!'];
- if(empty($data['four_shop'])) return [false, '绑定4s店信息不能为空'];
- //验证重复项
- $four_shop_id = array_column($data['four_shop'],'four_shop_id');
- foreach ($four_shop_id as $value){
- if(! $value) return [false,'4s店不能为空!'];
- }
- $four_shop_id = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $four_shop_id);
- $arr = array_count_values($four_shop_id);
- foreach ($arr as $value){
- if($value > 1) return [false,'4s店信息不能重复'];
- }
- if($user['id'] != Employee::SPECIAL_ADMIN){
- $area_manager = $this->getAreaManagerBy4S();
- if(! in_array($user['id'], $area_manager)) return [false,'非区域经理,操作失败'];
- }
- return [true, ''];
- }
- public function wxCheck($data,$user){
- return [true,''];
- }
- public function getAreaManagerBy4S(){
- $result = FoursShop::where('del_time',0)
- ->select('regional_manager')
- ->distinct()
- ->get()->toArray();
- return array_column($result,'regional_manager');
- }
- public function getAreaManagerByStore(){
- $result = StorehouseEmployee::select('employee_id')
- ->distinct()
- ->where('del_time',0)
- ->get()->toArray();
- return array_column($result,'employee_id');
- }
- public function is_area_manager($user_id){
- $is_area = [
- 'by_4s' => 0,
- 'by_store' => 0
- ];
- $return = $this->getAreaManagerBy4S();
- if(in_array($user_id,$return)) $is_area['by_4s'] = 1;
- $return = $this->getAreaManagerByStore();
- if(in_array($user_id,$return)) $is_area['by_store'] = 1;
- return $is_area;
- }
- public function is_check($user_id){
- if($user_id == Employee::SPECIAL_ADMIN) return 1;
- $bool = EmployeeTeamPermission::where('employee_id',$user_id)
- ->exists();
- return $bool;
- }
- }
|