| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use think\facade\Db; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  | use think\Model; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Member extends Base | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     public const STATUS_NORMAL = 1;//正常
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     public const STATUS_DISABLE = 0;//禁用
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     public const MANAGER_ROLE_ID = 1;//角色id 2 为管理员
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     public const ANENT_ROLE_ID = 2;//角色id 2 为代理商
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     public const STAFF_ROLE_ID = 3;//角色id 2 为工作人员
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function getList($limit = 40) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::alias('m') | 
					
						
							|  |  |  |             ->leftjoin('auth_group g', 'g.id=m.group_id') | 
					
						
							|  |  |  |             ->field('m.id,m.username,m.login_time,m.group_id,g.title') | 
					
						
							|  |  |  |             ->order('m.id', 'asc') | 
					
						
							|  |  |  |             ->paginate($limit); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取所有代理商 | 
					
						
							|  |  |  |      * */ | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     public static function getAgentAll() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $subQuery = Db::name('member') | 
					
						
							|  |  |  |             ->field('id,business_code,nickname') | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |             ->whereRaw('(find_in_set("' . Member::ANENT_ROLE_ID . '", roles))') | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |             ->buildSql(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |         return Db::table($subQuery . ' a') | 
					
						
							|  |  |  |             ->join("business b", "a.business_code = b.code") | 
					
						
							|  |  |  |             ->field("a.*") | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |             ->order('a.id', 'desc') | 
					
						
							|  |  |  |             ->select(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 根据角色分组返回用户 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |      * @param int $groupId 角色分组ID | 
					
						
							|  |  |  |      * @param int $limit 每页数量 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function getListByGroup($groupId, $limit = 40) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::alias('m') | 
					
						
							|  |  |  |             ->leftjoin('auth_group g', 'g.id=m.group_id') | 
					
						
							|  |  |  |             ->field('m.id,m.username,m.login_time,m.group_id,g.title') | 
					
						
							|  |  |  |             ->where('m.group_id', '=', $groupId) | 
					
						
							|  |  |  |             ->order('m.id', 'asc') | 
					
						
							|  |  |  |             ->paginate($limit); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //根据用户名获取管理账号
 | 
					
						
							|  |  |  |     public static function getByUserName($username) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::where('username', trim($username)) | 
					
						
							|  |  |  |             ->findOrEmpty() | 
					
						
							|  |  |  |             ->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //根据ID获取管理账户和相关权限
 | 
					
						
							|  |  |  |     public static function getMemberAndRulesByID($memberId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::alias('m') | 
					
						
							|  |  |  |             ->join('auth_group g', 'm.group_id = g.id', 'LEFT') | 
					
						
							|  |  |  |             ->field('m.group_id,g.rules') | 
					
						
							|  |  |  |             ->where('m.id', $memberId) | 
					
						
							|  |  |  |             ->findOrEmpty() | 
					
						
							|  |  |  |             ->toArray(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function updateCates($id, $cates) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cates = implode(',', $cates); | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |         $data = ['cates' => $cates]; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         self::updateById($id, $data); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 验证当前用户是否是渠道商 | 
					
						
							|  |  |  |      * @param string $roles | 
					
						
							| 
									
										
										
										
											2021-12-01 16:53:44 +08:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function is_agency(string $roles) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (empty($roles)) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $roles = explode(",", $roles); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //包含管理员就返回false
 | 
					
						
							|  |  |  |         if (in_array(self::MANAGER_ROLE_ID, $roles)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (in_array(self::STAFF_ROLE_ID, $roles) || in_array(self::ANENT_ROLE_ID, $roles)) { | 
					
						
							| 
									
										
										
										
											2021-12-01 16:53:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static function onAfterInsert ($obj) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $obj->create_time = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  |         $obj->save(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function hasStaff($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return self::where("pid",$id)->count(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | } |