// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model\store; use cores\BaseModel; use think\Session; /** * 商家用户模型 * Class User * @package app\common\model */ class User extends BaseModel { // 定义表名 protected $name = 'store_user'; // 定义主键 protected $pk = 'store_user_id'; /** * 关联商家记录表 * @return \think\model\relation\BelongsTo */ public function store() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\Store", 'store_id'); } /** * 关联用户角色表表 * @return \think\model\relation\BelongsToMany */ public function role() { return $this->belongsToMany('Role', 'StoreUserRole'); } /** * 关联父级用户 * @return \think\model\relation\BelongsTo */ public function parentUser() { return $this->belongsTo('User','parent_id','store_user_id'); } /** * 验证用户名是否重复 * @param string $userName * @return bool */ public static function checkExist(string $userName) { return (bool)static::withoutGlobalScope() ->where('user_name', '=', $userName) ->where('is_delete', '=', 0) ->value('store_user_id'); } /** * 商家用户详情 * @param $where * @param array $with * @return array|static|null */ public static function detail($where, array $with = []) { return static::get($where, $with); } /** * 保存登录状态 * @param $user * @throws \think\Exception */ public function loginState($user) { /** @var \app\common\model\Wxapp $wxapp */ $wxapp = $user['wxapp']; // 保存登录状态 Session::set('yoshop_store', [ 'user' => [ 'store_user_id' => $user['store_user_id'], 'user_name' => $user['user_name'], 'is_super' => $user['is_super'], 'status' => $user['status'], 'level' => $user['level'], 'parent_id' => $user['parent_id'], 'merchant_id' => $user['merchant_id'], ], 'wxapp' => $wxapp->toArray(), 'is_login' => true, ]); } /** * 切换状态 * @param $status * @return false|int */ public function setStatus($status) { if($status == 1 && $this['stop_date'] == date("Y-m-d")){ return '当天已到达收款上限,停止接单'; } if($bool = $this->save(['status' => $status])){ $session = Session::get('yoshop_store'); $session['user']['status'] = $status; // 保存登录状态 Session::set('yoshop_store', $session); $bool = true; } return $bool; } /** * 获取全部 * @param array $user * @return mixed */ public static function getAll($user = []) { $filter = []; if($user['is_super'] == 0) { $filter[] = ['store_user_id', 'in', \app\store\model\store\User::getScope($user)]; } $model = new static; //return $model->where('is_delete', '=', 0)->where('is_super', '=', 0)->where($filter)->order(['store_user_id' => 'asc'])->select(); return $model->where('is_delete', '=', 0)->where($filter)->order(['store_user_id' => 'asc'])->select(); } /** * 获取在线人员 * @return mixed */ public static function getOnline() { $model = new static; //在线,并且当天未停止接单 $data = $model->where('status', '=', 1)->where('stop_date', '!=', date('Y-m-d'))->order(['store_user_id' => 'asc'])->select()->toArray(); return array_column($data,'store_user_id'); } /** * 切换状态 * @param $status * @param $user * @return false|int */ public function changeStatus($status,$user) { if($status == 1 && $this['stop_date'] == date("Y-m-d")){ return '当天已到达收款上限,停止接单'; } if($bool = $this->save(['status' => $status])){ //如果是自己则修改在线状态的session if($this['store_user_id'] == $user['store_user_id']) { $session = Session::get('yoshop_store'); $session['user']['status'] = $status; // 保存登录状态 Session::set('yoshop_store', $session); } $bool = true; } return $bool; } /** * 修改收款金额 * @param $data * @return bool */ public function updateMoney($data) { if ($data['money'] <= 0) { $this->error = '收款金额必须大于0'; return false; } return $this->save([ 'max_money' => $data['money'], ]) !== false; } }