User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 02/03/2017
  6. * Time: 18:19
  7. */
  8. namespace app\common\models\user;
  9. use app\backend\modules\user\observers\UserObserver;
  10. use app\common\helpers\Cache;
  11. use app\common\models\BaseModel;
  12. use Illuminate\Database\Eloquent\SoftDeletes;
  13. use Illuminate\Validation\Rule;
  14. class User extends BaseModel
  15. {
  16. public $primaryKey = 'uid';
  17. public $table = 'users';
  18. public $timestamps = false;
  19. public $widgets = [];
  20. public $attributes = [
  21. 'groupid' => 0,
  22. 'type' => 1,
  23. 'remark' => '',
  24. 'endtime' => 0
  25. ];
  26. protected $guarded = [''];
  27. const ROLE_ENABLE = 2;
  28. const ROLE_DISABLE = 3;
  29. /**
  30. * User constructor.
  31. * @param array $attributes
  32. * @throws \Exception
  33. */
  34. public function __construct(array $attributes = [])
  35. {
  36. parent::__construct($attributes);
  37. if (config('app.framework') == 'platform') {
  38. unset($this->attributes['groupid']);
  39. $this->timestamps = true;
  40. $this->table = 'yz_admin_users';
  41. } else {
  42. $this->attributes = $this->getNewAttributes();
  43. }
  44. }
  45. /**
  46. * @return array|mixed
  47. * @throws \Exception
  48. */
  49. public function getNewAttributes()
  50. {
  51. if ($this->hasColumn('owner_uid')) { //用于兼容新版微擎新增的字段
  52. $this->attributes = array_merge($this->attributes, ['owner_uid' => '0']);
  53. }
  54. if ($this->hasColumn('founder_groupid')) {
  55. $this->attributes = array_merge($this->attributes, ['founder_groupid' => '0']);
  56. }
  57. if ($this->hasColumn('register_type')) {
  58. $this->attributes = array_merge($this->attributes, ['register_type' => '0']);
  59. }
  60. if ($this->hasColumn('openid')) {
  61. $this->attributes = array_merge($this->attributes, ['openid' => '']);
  62. }
  63. if ($this->hasColumn('welcome_link')) {
  64. $this->attributes = array_merge($this->attributes, ['welcome_link' => '0']);
  65. }
  66. if ($this->hasColumn('is_bind')) {
  67. $this->attributes = array_merge($this->attributes, ['is_bind' => '0']);
  68. }
  69. if ($this->hasColumn('schoolid')) {
  70. $this->attributes = array_merge($this->attributes, ['schoolid' => '0']);
  71. }
  72. if ($this->hasColumn('credit1')) {
  73. $this->attributes = array_merge($this->attributes, ['credit1' => '0']);
  74. }
  75. if ($this->hasColumn('credit2')) {
  76. $this->attributes = array_merge($this->attributes, ['credit2' => '0']);
  77. }
  78. if ($this->hasColumn('agentid')) {
  79. $this->attributes = array_merge($this->attributes, ['agentid' => '0']);
  80. }
  81. if ($this->hasColumn('uniacid')) {
  82. $this->attributes = array_merge($this->attributes, ['uniacid' => '0']);
  83. }
  84. if ($this->hasColumn('token')) {
  85. $this->attributes = array_merge($this->attributes, ['token' => '']);
  86. }
  87. if ($this->hasColumn('registration_id')) {
  88. $this->attributes = array_merge($this->attributes, ['registration_id' => '']);
  89. }
  90. return $this->attributes;
  91. }
  92. public function uniAccounts()
  93. {
  94. return $this->hasMany('app\common\models\user\UniAccountUser', 'uid', 'uid');
  95. }
  96. /*
  97. * One to one, each operator corresponds to an operator profile
  98. **/
  99. public function userProfile()
  100. {
  101. return $this->hasOne('app\common\models\user\UserProfile', 'uid', 'uid');
  102. }
  103. /*
  104. * One to one, account each operator corresponds to an operator
  105. **/
  106. public function uniAccount()
  107. {
  108. return $this->belongsTo('app\common\models\user\UniAccountUser', 'uid', 'uid');
  109. }
  110. /*
  111. * One to one, one operator has only one role
  112. **/
  113. public function userRole()
  114. {
  115. return $this->hasOne('app\common\models\user\YzUserRole', 'user_id', 'uid');
  116. }
  117. /*
  118. * One to many, one operator has multiple operating privileges
  119. **/
  120. public function permissions()
  121. {
  122. return $this->hasMany('app\common\models\user\YzPermission', 'item_id', 'uid')
  123. ->where('type', '=', YzPermission::TYPE_USER);
  124. }
  125. /**
  126. * 排出供应商操作员
  127. * @param $query
  128. * @return mixed
  129. */
  130. public function scopeNoOperator($query)
  131. {
  132. /*if (Schema::hasTable('yz_supplier')) {
  133. $ids = DB::table('yz_supplier')->select('uid')->get();
  134. return $query->whereNotIn('uid',$ids);
  135. }*/
  136. return $query;
  137. }
  138. /**
  139. * @param $query
  140. * @return mixed
  141. */
  142. public function scopeRecords($query)
  143. {
  144. // return $query->whereHas('uniAccount', function ($query) {
  145. // return $query->uniacid()->where('role', '!=', 'clerk');
  146. // })
  147. // 平台商城的系统权限页面之前会显示管理员,现改为只显示操作员。全局搜没发现此方法在其他地方有用到,若要更改,请在Backend->modules->user->UserController->Index添加判断
  148. // $role_list = UniAccountUser::uniacid()->where('role', '!=', 'clerk')->pluck('uid')->toArray() ? : [-1];
  149. $role_list = UniAccountUser::uniacid()->where('role','operator')->pluck('uid')->toArray() ? : [-1];
  150. return $query->whereIn('uid',$role_list)
  151. ->with(['userProfile' => function ($profile) {
  152. return $profile->select('uid', 'realname', 'mobile');
  153. }])
  154. ->with(['userRole' => function ($userRole) {
  155. return $userRole->select('user_id', 'role_id')
  156. ->with(['role' => function ($role) {
  157. return $role->select('id', 'name')->uniacid();
  158. }]);
  159. }]);
  160. }
  161. public function scopeSearch($query, array $keyword)
  162. {
  163. if ($keyword['keyword']) {
  164. $query = $query->whereHas('userProfile', function ($profile) use ($keyword) {
  165. return $profile->select('uid', 'realname', 'mobile')
  166. ->where('realname', 'like', '%' . $keyword['keyword'] . '%')
  167. ->orWhere('mobile', 'like', '%' . $keyword['keyword'] . '%');
  168. })->orWhere('username', 'like', '%' . $keyword['keyword'] . '%');
  169. }
  170. if ($keyword['status']) {
  171. $query = $query->where('status', $keyword['status']);
  172. }
  173. if ($keyword['role_id']) {
  174. $query = $query->whereHas('userRole', function ($userRole) use ($keyword) {
  175. return $userRole->where('role_id', $keyword['role_id']);
  176. });
  177. }
  178. return $query;
  179. }
  180. /*
  181. * Get operator information through operator ID
  182. *
  183. * @parms int $userId
  184. *
  185. * @return object
  186. **/
  187. public static function getUserById($userId)
  188. {
  189. return self::where('uid', $userId)
  190. ->with(['userProfile' => function ($profile) {
  191. return $profile->select('uid', 'realname', 'mobile');
  192. }])
  193. ->with(['userRole' => function ($userRole) {
  194. return $userRole->select('user_id', 'role_id')
  195. ->with(['role' => function ($role) {
  196. return $role->select('id', 'name')->uniacid();
  197. }]);
  198. }])
  199. ->with(['permissions' => function ($userPermission) {
  200. return $userPermission->select('permission', 'item_id');
  201. }])
  202. ->whereHas('uniAccount', function ($query) {
  203. return $query->uniacid()->where('role', '!=', 'clerk');
  204. })
  205. ->first();
  206. }
  207. static $userPermission = [];
  208. /**
  209. * 获取并组合用户权限
  210. *
  211. * @return array
  212. */
  213. public static function userPermissionCache()
  214. {
  215. if (!isset(static::$userPermission[\YunShop::app()->uid])) {
  216. $cacheKey = 'permissions.' . \YunShop::app()->uid;
  217. if (!Cache::has($cacheKey)) {
  218. $userPermission = self::userPermission();
  219. Cache::put($cacheKey, $userPermission, 4200);
  220. } else {
  221. $userPermission = Cache::get($cacheKey);
  222. }
  223. static::$userPermission[\YunShop::app()->uid] = $userPermission;
  224. }
  225. return static::$userPermission[\YunShop::app()->uid];
  226. }
  227. /**
  228. * 获取用户数据库中的所有权限
  229. *
  230. * @return array
  231. */
  232. public static function userPermission()
  233. {
  234. set_time_limit(0);
  235. $userPermissionsModel = self::userPermissionByUid(\YunShop::app()->uid);
  236. if (!isset($userPermissionsModel)) {
  237. return [];
  238. }
  239. $permissions = [];
  240. if (!$userPermissionsModel->permissions->isEmpty()) {
  241. foreach ($userPermissionsModel->permissions as $permission) {
  242. $permissions[] = $permission->permission;
  243. }
  244. }
  245. if ($userPermissionsModel->userRole && !$userPermissionsModel->userRole->permissions->isEmpty()) {
  246. foreach ($userPermissionsModel->userRole->permissions as $permission) {
  247. !in_array($permission->permission, $permissions) && $permissions[] = $permission->permission;
  248. }
  249. }
  250. return $permissions;
  251. }
  252. public static function userPermissionByUid($uid)
  253. {
  254. $model = static::with([
  255. 'userRole' => function ($query) {
  256. return $query->with('permissions');
  257. },
  258. 'permissions'
  259. ])->where('uid', $uid)->first();
  260. return $model;
  261. }
  262. /**
  263. * 定义字段名
  264. *
  265. * @return array
  266. */
  267. public function atributeNames()
  268. {
  269. return [
  270. 'username' => "操作员用户名",
  271. 'password' => "操作员密码"
  272. ];
  273. }
  274. /**
  275. * 字段规则
  276. *
  277. * @return array
  278. */
  279. public function rules()
  280. {
  281. return [
  282. 'username' => ['required', Rule::unique($this->table)->ignore($this->id)],
  283. 'password' => 'required'
  284. ];
  285. }
  286. /**
  287. * 在boot()方法里注册下模型观察类
  288. * boot()和observe()方法都是从Model类继承来的
  289. * 主要是observe()来注册模型观察类,可以用TestMember::observe(new TestMemberObserve())
  290. * 并放在代码逻辑其他地方如路由都行,这里放在这个TestMember Model的boot()方法里自启动。
  291. */
  292. public static function boot()
  293. {
  294. parent::boot();
  295. //注册观察者
  296. static::observe(new UserObserver());
  297. }
  298. }