MemberForm.php 8.4 KB

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