MemberForm.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace backend\models\forms;
  3. use backend\models\MemberApi;
  4. use yii\base\Model;
  5. use common\helpers\Idcard;
  6. class MemberForm extends Model
  7. {
  8. public $id;
  9. public $is_enable;
  10. public $logins;
  11. public $pwd;
  12. public $username;
  13. public $name;
  14. public $idNo;
  15. public $address;
  16. public $mobile;
  17. public $gender;
  18. public $birthday;
  19. public $in_time;
  20. public $ib_old_login_name;
  21. public $password;
  22. public $id_no;
  23. public $ref_id;
  24. /**
  25. * @param int $id
  26. * @param array $config
  27. * @param bool $is_add
  28. * @throws \Exception
  29. */
  30. public function __construct($id, array $config = [], $is_add = false)
  31. {
  32. // 如果是添加,则跳过赋值过程
  33. if ($is_add) {
  34. return;
  35. }
  36. $id = intval($id);
  37. if ($id <= 0) {
  38. throw new \Exception('用户不存在');
  39. }
  40. $api = new MemberApi();
  41. $result = $api->getMemberInfo($id);
  42. if ($result['code'] == 1) {
  43. $data = $result['data'];
  44. $this->id = $id;
  45. $this->is_enable = $data['is_enable'];
  46. $this->logins = $data['logins'];
  47. $this->username = $data['username'];
  48. $this->name = $data['name'];
  49. $this->idNo = $data['id_no'];
  50. $this->address = $data['address'];
  51. $this->mobile = $data['mobile'];
  52. $this->gender = $data['gender'];
  53. $this->birthday = $data['birthday'];
  54. $this->in_time = $data['in_time'];
  55. } else {
  56. throw new \Exception($result['message']);
  57. }
  58. }
  59. public function rules()
  60. {
  61. return [
  62. [['is_enable', 'logins', 'pwd', 'username', 'name', 'idNo', 'address', 'mobile'], 'safe'],
  63. // add_admin_ib
  64. [['ib_old_login_name', 'logins', 'username', 'password', 'name', 'ref_id'], 'required', 'on' => ['add_admin_ib']],
  65. ['username', 'email', 'on' => ['add_admin_ib']],
  66. ['password', 'string', 'min' => 6, 'on' => ['add_admin_ib']],
  67. ['id_no', function ($attribute, $params) {
  68. $idcard = Idcard::getInstance();
  69. if (!$idcard->isChinaIDCard($this->$attribute)) {
  70. $this->addError($attribute, '身份证不正确');
  71. }
  72. }, 'on' => ['add_admin_ib']], // 缺省状态下,行内验证器不会在关联特性的输入值为空或该特性已经在其他验证中失败的情况下起效。 若你想要确保该验证器始终启用的话,你可以在定义规则时,酌情将 skipOnEmpty 以及 skipOnError属性设为 false
  73. // add_admin_admin
  74. [['username', 'name', 'password', 'mobile'], 'required', 'on' => ['add_admin_admin']],
  75. ['password', 'string', 'min' => 6, 'on' => ['add_admin_admin']],
  76. ['mobile', function ($attribute, $params) {
  77. if (!preg_match('/^1\d{10}/', $this->$attribute)) {
  78. $this->addError($attribute, '手机号码不正确');
  79. }
  80. }, 'on' => ['add_admin_admin']],
  81. // edit_admin_admin
  82. [['id', 'name', 'mobile'], 'required', 'on' => ['edit_admin_admin']],
  83. ['password', 'string', 'min' => 6, 'on' => ['edit_admin_admin']],
  84. ['mobile', function ($attribute, $params) {
  85. if (!preg_match('/^1\d{10}/', $this->$attribute)) {
  86. $this->addError($attribute, '手机号码不正确');
  87. }
  88. }, 'on' => ['edit_admin_admin']],
  89. ];
  90. }
  91. /**
  92. * @inheritdoc
  93. * @return array
  94. */
  95. public function scenarios()
  96. {
  97. $scenarios = parent::scenarios();
  98. $scenarios['add_admin_ib'] = ['ib_old_login_name', 'logins', 'username', 'mobile', 'password', 'name', 'id_no', 'address', 'ref_id'];
  99. $scenarios['add_admin_admin'] = ['username', 'name', 'password', 'mobile'];
  100. $scenarios['edit_admin_admin'] = ['id', 'name', 'password', 'mobile'];
  101. return $scenarios;
  102. }
  103. /**
  104. * @inheritdoc
  105. * @return array
  106. */
  107. public function attributeLabels()
  108. {
  109. if (in_array($this->getScenario(), ['add_admin_ib'])) {
  110. return [
  111. 'ib_old_login_name' => '用户名',
  112. 'logins' => '返佣账户',
  113. 'username' => '电子邮箱',
  114. 'mobile' => '手机',
  115. 'password' => '密码',
  116. 'name' => '姓名',
  117. 'id_no' => '证件号码',
  118. 'address' => '地址',
  119. 'ref_id' => '上级代理',
  120. ];
  121. }
  122. if (in_array($this->getScenario(), ['add_admin_admin'])) {
  123. return [
  124. 'username' => '用户名',
  125. 'name' => '姓名',
  126. 'password' => '密码',
  127. 'mobile' => '手机',
  128. ];
  129. }
  130. if (in_array($this->getScenario(), ['edit_admin_admin'])) {
  131. return [
  132. 'password' => '新密码',
  133. 'name' => '姓名',
  134. 'mobile' => '手机',
  135. ];
  136. }
  137. return [];
  138. }
  139. public function updatePassword($pwd)
  140. {
  141. if ($pwd == null || trim($pwd) === '') {
  142. $this->addError('pwd', '密码不能为空');
  143. return false;
  144. }
  145. $api = new MemberApi();
  146. $result = $api->updateMemberPassword($this->id, $pwd);
  147. if ($result['code'] == 1) {
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. public function updateIsEnable($enable)
  154. {
  155. if (!is_numeric($enable) || !in_array($enable, [0, 1])) {
  156. $this->addError('is_enable', '参数错误');
  157. return false;
  158. }
  159. $api = new MemberApi();
  160. $result = $api->updateMemberEnable($this->id, $enable);
  161. if ($result['code'] == 1) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. public function updateLogins($logins)
  168. {
  169. if ($logins == null || trim($logins) === '') {
  170. $this->addError('logins', 'MT4账户不能为空');
  171. return false;
  172. }
  173. $api = new MemberApi();
  174. $result = $api->updateMemberLogins($this->id, $logins);
  175. if ($result['code'] == 1) {
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. public function updateInfo($post)
  182. {
  183. $data = [];
  184. if (isset($post['username']) && trim($post['username']) !== '') {
  185. $data['username'] = trim($post['username']);
  186. }
  187. if (isset($post['name']) && trim($post['name']) !== '') {
  188. $data['name'] = trim($post['name']);
  189. }
  190. if (isset($post['idNo']) && trim($post['idNo']) !== '') {
  191. $data['id_no'] = trim($post['idNo']);
  192. }
  193. if (isset($post['address']) && trim($post['address']) !== '') {
  194. $data['address'] = trim($post['address']);
  195. }
  196. if (isset($post['mobile']) && trim($post['mobile']) !== '') {
  197. $data['mobile'] = trim($post['mobile']);
  198. }
  199. if (empty($data)) {
  200. $this->addError('id', '数据为空');
  201. return false;
  202. }
  203. $api = new MemberApi();
  204. $result = $api->updateMember($this->id, $data);
  205. if ($result['code'] == 1) {
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. public function delete()
  212. {
  213. $api = new MemberApi();
  214. $result = $api->deleteMember($this->id);
  215. if ($result['code'] == 1) {
  216. return true;
  217. } else {
  218. return false;
  219. }
  220. }
  221. }