| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace backend\models\forms;
- use backend\models\MemberApi;
- use yii\base\Model;
- use common\helpers\Idcard;
- class MemberForm extends Model
- {
- public $id;
- public $is_enable;
- public $logins;
- public $pwd;
- public $username;
- public $name;
- public $idNo;
- public $address;
- public $mobile;
- public $gender;
- public $birthday;
- public $in_time;
- public $ib_old_login_name;
- public $password;
- public $id_no;
- public $ref_id;
- public $group_sn;
- /**
- * @param int $id
- * @param array $config
- * @param bool $is_add
- * @throws \Exception
- */
- public function __construct($id, array $config = [], $is_add = false)
- {
- // 如果是添加,则跳过赋值过程
- if ($is_add) {
- return;
- }
-
- $id = intval($id);
- if ($id <= 0) {
- throw new \Exception('用户不存在');
- }
- $api = new MemberApi();
- $result = $api->getMemberInfo($id);
- if ($result['code'] == 1) {
- $data = $result['data'];
- $this->id = $id;
- $this->is_enable = $data['is_enable'];
- $this->logins = $data['logins'];
- $this->username = $data['username'];
- $this->name = $data['name'];
- $this->idNo = $data['id_no'];
- $this->address = $data['address'];
- $this->mobile = $data['mobile'];
- $this->gender = $data['gender'];
- $this->birthday = $data['birthday'];
- $this->in_time = $data['in_time'];
- $this->group_sn = $data['group_sn'];
- } else {
- throw new \Exception($result['message']);
- }
- }
- public function rules()
- {
- return [
- [['is_enable', 'logins', 'pwd', 'username', 'name', 'idNo', 'address'], 'safe'],
-
- // add_admin_ib
- [['ib_old_login_name', 'logins', 'username', 'password', 'name', 'ref_id', 'group_sn'], 'required', 'on' => ['add_admin_ib']],
- ['username', 'email', 'on' => ['add_admin_ib']],
- ['password', 'string', 'min' => 6, 'on' => ['add_admin_ib']],
- ['id_no', function ($attribute, $params) {
- $idcard = Idcard::getInstance();
- if (!$idcard->isChinaIDCard($this->$attribute)) {
- $this->addError($attribute, '身份证不正确');
- }
- }, 'on' => ['add_admin_ib']], // 缺省状态下,行内验证器不会在关联特性的输入值为空或该特性已经在其他验证中失败的情况下起效。 若你想要确保该验证器始终启用的话,你可以在定义规则时,酌情将 skipOnEmpty 以及 skipOnError属性设为 false
- // add_admin_member
- [[ 'logins', 'username', 'password','name'], 'required', 'on' => ['add_admin_member']],
- ['username', 'email', 'on' => ['add_admin_member']],
- ['password', 'string', 'min' => 6, 'on' => ['add_admin_member']],
- ['id_no', function ($attribute, $params) {
- $idcard = Idcard::getInstance();
- if (!$idcard->isChinaIDCard($this->$attribute)) {
- $this->addError($attribute, '身份证不正确');
- }
- }, 'on' => ['add_admin_member']],
- // add_admin_admin
- [['username', 'name', 'password', 'mobile'], 'required', 'on' => ['add_admin_admin']],
- ['password', 'string', 'min' => 6, 'on' => ['add_admin_admin']],
- ['mobile', function ($attribute, $params) {
- if (!preg_match('/^1\d{10}/', $this->$attribute)) {
- $this->addError($attribute, '手机号码不正确');
- }
- }, 'on' => ['add_admin_admin']],
- // edit_admin_admin
- [['id', 'name', 'mobile'], 'required', 'on' => ['edit_admin_admin']],
- ['password', 'string', 'min' => 6, 'on' => ['edit_admin_admin']],
- ['mobile', function ($attribute, $params) {
- if (!preg_match('/^1\d{10}/', $this->$attribute)) {
- $this->addError($attribute, '手机号码不正确');
- }
- }, 'on' => ['edit_admin_admin']],
- ];
- }
- /**
- * @inheritdoc
- * @return array
- */
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- $scenarios['add_admin_ib'] = ['ib_old_login_name', 'logins', 'username', 'password', 'name', 'id_no', 'address', 'ref_id' , 'group_sn'];
- $scenarios['add_admin_member'] = ['logins', 'username', 'password', 'name', 'id_no', 'address'];
- $scenarios['add_admin_admin'] = ['username', 'name', 'password', 'mobile'];
- $scenarios['edit_admin_admin'] = ['id', 'name', 'password', 'mobile'];
- return $scenarios;
- }
- /**
- * @inheritdoc
- * @return array
- */
- public function attributeLabels()
- {
- if (in_array($this->getScenario(), ['add_admin_ib'])) {
- return [
- 'ib_old_login_name' => '用户名',
- 'logins' => '返佣账户',
- 'username' => '电子邮箱',
- 'mobile' => '手机',
- 'password' => '密码',
- 'name' => '姓名',
- 'id_no' => '证件号码',
- 'address' => '地址',
- 'ref_id' => '上级代理',
- 'group_sn' => 'MT4开户编号',
- ];
- }
- if (in_array($this->getScenario(), ['add_admin_admin'])) {
- return [
- 'username' => '用户名',
- 'name' => '姓名',
- 'password' => '密码',
- 'mobile' => '手机',
- ];
- }
- if (in_array($this->getScenario(), ['edit_admin_admin'])) {
- return [
- 'password' => '新密码',
- 'name' => '姓名',
- 'mobile' => '手机',
- ];
- }
-
- return [];
- }
- public function updatePassword($pwd)
- {
- if ($pwd == null || trim($pwd) === '') {
- $this->addError('pwd', '密码不能为空');
- return false;
- }
- $api = new MemberApi();
- $result = $api->updateMemberPassword($this->id, $pwd);
- if ($result['code'] == 1) {
- return true;
- } else {
- return false;
- }
- }
- public function updateIsEnable($enable)
- {
- if (!is_numeric($enable) || !in_array($enable, [0, 1])) {
- $this->addError('is_enable', '参数错误');
- return false;
- }
- $api = new MemberApi();
- $result = $api->updateMemberEnable($this->id, $enable);
- if ($result['code'] == 1) {
- return true;
- } else {
- return false;
- }
- }
- public function updateLogins($logins)
- {
- if ($logins == null || trim($logins) === '') {
- $this->addError('logins', 'MT4账户不能为空');
- return false;
- }
- $api = new MemberApi();
- $result = $api->updateMemberLogins($this->id, $logins);
- if ($result['code'] == 1) {
- return true;
- } else {
- return false;
- }
- }
- public function updateInfo($post)
- {
- $data = [];
- if (isset($post['username']) && trim($post['username']) !== '') {
- $data['username'] = trim($post['username']);
- }
- if (isset($post['name']) && trim($post['name']) !== '') {
- $data['name'] = trim($post['name']);
- }
- if (isset($post['idNo']) && trim($post['idNo']) !== '') {
- $data['id_no'] = trim($post['idNo']);
- }
- if (isset($post['address']) && trim($post['address']) !== '') {
- $data['address'] = trim($post['address']);
- }
- if (isset($post['mobile']) && trim($post['mobile']) !== '') {
- $data['mobile'] = trim($post['mobile']);
- }
- if (empty($data)) {
- $this->addError('id', '数据为空');
- return false;
- }
- $api = new MemberApi();
- $result = $api->updateMember($this->id, $data);
- if ($result['code'] == 1) {
- return true;
- } else {
- return false;
- }
- }
- public function delete()
- {
- $api = new MemberApi();
- $result = $api->deleteMember($this->id);
- if ($result['code'] == 1) {
- return true;
- } else {
- return false;
- }
- }
- }
|