MemberForm.php 9.2 KB

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