5], [['password'], 'string', 'min' => 6], ['username', 'match', 'pattern' => '/^[a-z]\w*$/i', 'message' => '用户名需要以字母开头,由数字和字母组成。'], [['username', 'nickname', ], 'required', 'on' => 'add'], [['username', 'nickname', ], 'required', 'on' => 'wxmp_signup'], [['username', 'nickname', 'real_name', 'mobile_phone', 'password'], 'required', 'on' => 'user_add'], [['mobile_phone', 'mobile_prefix', 'username', ], 'required', 'on' => 'mobile_signup'], [['mobile_phone', 'mobile_prefix', ], 'required', 'on' => 'bind_mobile'], [['username', 'password','mobile_phone'], 'required', 'on' => 'store_add'], [['mobile_phone', 'mobile_prefix'], 'required', 'on' => 'coach_add'], [['mobile_phone', 'mobile_prefix'], 'required', 'on' => 'detection_add'], [['nickname', 'avatar_url','mobile_phone','gender','birthday','level_id'], 'required', 'on' => 'admin_edit'], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'cx_mch_id' => '平台商户ID', 'username' => '用户名', 'password' => '密码', 'auth_key' => 'AUTH_KEY', 'access_token' => 'ACCESS_TOKEN', 'nickname' => '昵称', 'avatar_url' => '头像', 'mobile_phone' => '手机号', 'mobile_prefix' => '手机号国家代码', 'email' => '邮箱', 'real_name' => '真实姓名', 'desc' => '个性签名', 'gender' => '性别,0=女,1=男,2=保密', 'birthday' => '生日', 'status' => '状态,0=正常,1=封禁,2=挂起,3=注销', 'parent_id' => 'PARENT_ID', 'is_modify_un' => '用户名是否可以修改,0=否,1=是', 'is_delete' => '是否删除,0=否,1=是', 'country_id' => '国家ID', 'city_id' => '城市ID', 'type' => '用户类型', ]; } public function save() { if(!$this->validate()){ return $this->getModelError(); } if($this->model->isNewRecord){ $this->model->cx_mch_id = $this->cx_mch_id; $res = $this->checkUsername($this->username); if($res['code'] != 0) return $res; if($this->password == null) $this->password = \Yii::$app->security->generateRandomString(); $this->model->password = $this->password; $this->model->auth_key = \Yii::$app->security->generateRandomString(); $this->model->status = User::STATUS_NORMAL; $this->model->is_modify_un = $this->is_modify_un == null ? 0 : $this->is_modify_un; $this->model->is_delete = 0; } if($this->username != null) $this->model->username = $this->username; if($this->password != null) $this->model->password = $this->password; if($this->access_token != null) $this->model->access_token = $this->access_token; if($this->nickname != null) $this->model->nickname = $this->nickname; if($this->avatar_url != null) $this->model->avatar_url = $this->avatar_url; if($this->mobile_prefix != null) $this->model->mobile_prefix = $this->mobile_prefix; if(!empty($this->mobile_phone)){ if(!Validation::is_mobile($this->mobile_phone,$this->mobile_prefix)){ return $this->apiReturnError("手机号格式不正确"); } $encrypted_mobile = EncryptHelper::encryptMobilePhone($this->mobile_phone); if(!$encrypted_mobile){ return [ 'code' => 1, 'msg' => '系统内部错误,错误代码:'.SysErrCode::$encryptedMobilePhoneFailed ]; } $user_id = $this->model->isNewRecord ? 0 : $this->model->id; $res = $this->checkMobile($encrypted_mobile, $this->mobile_prefix, $user_id); if($res['code'] != 0) return $res; $this->model->mobile_phone = $encrypted_mobile; } if($this->email != null) $this->model->email = $this->email; if($this->real_name != null) $this->model->real_name = $this->real_name; if($this->desc != null) $this->model->desc = $this->desc; if($this->gender != null) $this->model->gender = $this->gender; if($this->birthday != null) $this->model->birthday = $this->birthday; if($this->parent_id != null) $this->model->parent_id = $this->parent_id; if($this->country_id != null) $this->model->country_id = $this->country_id; if($this->city_id != null) $this->model->city_id = $this->city_id; if($this->type != null) $this->model->type = $this->type; if($this->level_id != null) $this->model->level_id = $this->level_id; $this->model->updated_at = time(); if(!$this->model->save()){ return $this->getModelError($this->model); } if($this->model->isNewRecord){ //@TODO 账户初始化 } return [ 'code' => 0, 'msg' => '保存成功', 'data' => [ 'user_id' => $this->model->id, ] ]; } //检测用户名是否已经存在 public function checkUsername($username) { $temp = User::findOne([ 'username' => $username, 'is_delete' => 0 ]); if($temp != null){ return [ 'code' => 1, 'msg' => '用户名已存在' ]; } return [ 'code' => 0, 'msg' => 'ok' ]; } //检测手机号是否已经存在 public function checkMobile($mobile_phone, $mobile_prefix = "86", $user_id) { $temp = User::findOne([ 'mobile_phone' => $mobile_phone, 'mobile_prefix' => $mobile_prefix, 'is_delete' => 0 ]); if($temp != null && $temp->id != $user_id){ return [ 'code' => 1, 'msg' => '手机号已存在' ]; } return [ 'code' => 0, 'msg' => 'ok' ]; } }