Member.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. use common\helpers\Idcard;
  5. use backend\helpers\MailHelper;
  6. /**
  7. * This is the model class for table "crm_member".
  8. *
  9. * @property integer $id
  10. * @property integer $type
  11. * @property integer $is_enable
  12. * @property string $username
  13. * @property string $password
  14. * @property string $ip
  15. * @property string $logins
  16. * @property string $name
  17. * @property integer $gender
  18. * @property string $id_no
  19. * @property string $birthday
  20. * @property string $address
  21. * @property string $mobile
  22. * @property string $main_login
  23. * @property string $random_code
  24. * @property integer $random_code_time
  25. * @property string $avatar
  26. * @property integer $ref_id
  27. * @property string $ref_path
  28. * @property integer $in_time
  29. * @property string $ib_old_login_name
  30. */
  31. class Member extends \yii\db\ActiveRecord
  32. {
  33. const MEMBER_TYPE_USER = 1;
  34. const MEMBER_TYPE_IB = 2;
  35. const MEMBER_TYPE_ADMIN = 99;
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName()
  40. {
  41. return 'crm_member';
  42. }
  43. /**
  44. * @return \yii\db\Connection the database connection used by this AR class.
  45. */
  46. public static function getDb()
  47. {
  48. return Yii::$app->get('dbXcrm');
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function rules()
  54. {
  55. return [
  56. [['type', 'username', 'password'], 'required'],
  57. [['type', 'is_enable', 'gender', 'random_code_time', 'ref_id', 'in_time','group_sn'], 'integer'],
  58. [['birthday'], 'safe'],
  59. [['username', 'password', 'ip', 'logins', 'name', 'id_no', 'address', 'mobile', 'main_login', 'random_code', 'avatar', 'ib_old_login_name','address1'], 'string', 'max' => 255],
  60. [['ref_path'], 'string', 'max' => 3000],
  61. ];
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function attributeLabels()
  67. {
  68. return [
  69. 'id' => 'ID',
  70. 'type' => 'Type',
  71. 'is_enable' => 'Is Enable',
  72. 'username' => 'Username',
  73. 'password' => 'Password',
  74. 'ip' => 'Ip',
  75. 'logins' => 'MT4 login account',
  76. 'name' => 'Name',
  77. 'gender' => 'Gender',
  78. 'id_no' => 'Id No',
  79. 'birthday' => 'Birthday',
  80. 'address' => 'Address',
  81. 'mobile' => 'Mobile',
  82. 'main_login' => 'Main Login',
  83. 'random_code' => 'Random Code',
  84. 'random_code_time' => 'Random Code Time',
  85. 'avatar' => 'Avatar',
  86. 'ref_id' => 'Ref ID',
  87. 'ref_path' => 'Ref Path',
  88. 'in_time' => 'In Time',
  89. 'ib_old_login_name' => 'Ib Old Login Name',
  90. 'group_sn' =>'group sn',
  91. 'address1' =>'address1',
  92. ];
  93. }
  94. /**
  95. * @param string $username
  96. * @param int $type
  97. * @return static
  98. */
  99. public function findByUserName($username, $type)
  100. {
  101. if ($type == null) {
  102. return self::findOne(['username' => strtolower($username)]);
  103. } else {
  104. return self::findOne(['username' => strtolower($username), 'type' => $type]);
  105. }
  106. }
  107. /**
  108. * @param string $username
  109. * @return static
  110. */
  111. public function findByIbOldLoginName($username)
  112. {
  113. return self::findOne(['ib_old_login_name' => strtolower($username)]);
  114. }
  115. /**
  116. * @param int $member_id
  117. * @return array
  118. */
  119. public function getLogins($member_id)
  120. {
  121. $member = Member::find()->select('logins')->where(['id' => $member_id])->asArray()->limit(1)->one();
  122. return explode(',', $member['logins']);
  123. }
  124. /**
  125. * @param string $password
  126. * @return string
  127. */
  128. public function hash($password)
  129. {
  130. return md5($password);
  131. }
  132. /**
  133. * @param int $id
  134. * @return array|null|\yii\db\ActiveRecord
  135. */
  136. public static function findById($id)
  137. {
  138. return static::find()->where(['id'=> $id])->asArray()->limit(1)->one();
  139. }
  140. /**
  141. * @param int $login
  142. * @param int $type
  143. * @param string|array $orderBy
  144. * @return array|null|\yii\db\ActiveRecord
  145. */
  146. public static function findByLogin($login, $type = null, $orderBy = null)
  147. {
  148. $login = (int) $login;
  149. $query = static::find()->where("FIND_IN_SET({$login}, logins)")->limit(1);
  150. if ($type !== null) {
  151. $query->andWhere(['type' => $type]);
  152. }
  153. if ($orderBy !== null) {
  154. $query->orderBy($orderBy);
  155. }
  156. $result = $query->asArray()->one();
  157. return $result;
  158. }
  159. /**
  160. * 修改密码
  161. * @param int $id
  162. * @param int $type
  163. * @param string $oldPassword
  164. * @param string $newPassword
  165. * @return array
  166. */
  167. public function changePassword($id, $type, $oldPassword, $newPassword)
  168. {
  169. $result = ['code' => 0, 'message' => ''];
  170. $where = [
  171. 'AND',
  172. ['=', 'id', $id],
  173. ['=', 'type', $type],
  174. ];
  175. /** @var Member $member */
  176. $member = self::find()->where($where)->limit(1)->one();
  177. if (!$member) {
  178. $result['message'] = '找不到相应的用户';
  179. return $result;
  180. }
  181. // 判断旧密码是否正确
  182. if ($this->hash($oldPassword) !== $member->password) {
  183. $result['message'] = '旧密码不正确';
  184. return $result;
  185. }
  186. // 修改成新密码
  187. $member->password = $this->hash($newPassword);
  188. if ($member->save()) {
  189. $result['code'] = 1;
  190. } else {
  191. $result['message'] = '修改失败';
  192. }
  193. return $result;
  194. }
  195. /**
  196. * 获取下级代理商
  197. * @param int $id
  198. * @param bool $includeSelf 是否包含自己,默认否
  199. * @param bool $hasPassword 字段里是包含password字段,默认否
  200. * @return array
  201. */
  202. public function findChildren($id, $includeSelf = false, $hasPassword = false)
  203. {
  204. $member = static::findById($id);
  205. if (!$member) {
  206. return [];
  207. }
  208. if ($member['ref_path']) {
  209. $like = $member['ref_path'] . $id . ',';
  210. } else {
  211. $like = $id . ',';
  212. }
  213. $sql = "SELECT * FROM " . self::tableName() . " WHERE ref_path LIKE '" . $like . "%' ORDER BY id ASC";
  214. $list = self::getDb()->createCommand($sql)->queryAll();
  215. if ($includeSelf) {
  216. $list[] = $member;
  217. }
  218. foreach ($list as $k => $v) {
  219. if (!$hasPassword) {
  220. unset($list[$k]['password']);
  221. }
  222. }
  223. return $list;
  224. }
  225. /**
  226. * 获取下级代理商,包含自己
  227. * @param int $id
  228. * @return array
  229. */
  230. public function findChildrenIncludeSelf($id)
  231. {
  232. return $this->findChildren($id, true);
  233. }
  234. public static function findParents($id)
  235. {
  236. $member = static::find()->where(['id' => $id])->asArray()->limit(1)->one();
  237. if ($member == null) {
  238. return [];
  239. }
  240. $result = [$member];
  241. $temp = static::findParents($member['ref_id']);
  242. if (!empty($temp)) {
  243. $result = array_merge($temp, $result);
  244. }
  245. return $result;
  246. }
  247. /**
  248. * @param string $email
  249. * @return bool
  250. */
  251. public static function checkEmailExist($email)
  252. {
  253. return static::find()->where(['username' => $email])->exists();
  254. }
  255. /**
  256. * @param string $idNo
  257. * @return bool
  258. */
  259. public static function checkIdNoExist($idNo)
  260. {
  261. return static::find()->where(['id_no' => $idNo, 'type' => 2])->exists();
  262. }
  263. /**
  264. * 名下代理
  265. * @param array $post
  266. * @return array
  267. */
  268. public function getView($post)
  269. {
  270. $result = ['code' => 0, 'data' => [], 'message' => ''];
  271. $member_id = $post['member_id'];
  272. $id = isset($post['id']) ? $post['id'] : '';
  273. if (!$id) {
  274. $result['message'] = '参数错误';
  275. return $result;
  276. }
  277. $ib = static::findById($id);
  278. if (!$ib) {
  279. $result['message'] = '参数错误.';
  280. return $result;
  281. }
  282. $ibs = $this->findChildrenIncludeSelf($member_id);
  283. $id_arr = array_column($ibs, 'id');
  284. if (!in_array($id, $id_arr)) {
  285. $result['message'] = '您没有权限查看这个页面';
  286. return $result;
  287. }
  288. $logins = array_map('trim', explode(',', trim($ib['logins'])));
  289. $mt4Users = Mt4Users::find()->where(['and', ['in', 'LOGIN', $logins]])->asArray()->all();
  290. foreach ($mt4Users as $k => $v) {
  291. $mt4Users[$k]['BALANCE'] = round($v['BALANCE'], 2);
  292. }
  293. $mt4Users2 = [];
  294. foreach ($mt4Users as $k => $v) {
  295. $mt4Users2[$v['LOGIN']] = $v;
  296. }
  297. $mt4Users = $mt4Users2;
  298. // 总入金和总出金
  299. $mt4Trades = new Mt4Trades();
  300. $depositSum = $mt4Trades->getDepositSumByLogins($logins);
  301. $withdrawSum = $mt4Trades->getWithdrawSumByLogins($logins);
  302. // 直属MT4账户总数
  303. $directlyUserCount = UserMember::directlyUserCount($ib['id']);
  304. $depositSumByDay = $mt4Trades->getDepositSumByDayByLogins($logins);
  305. $equity = 0;
  306. foreach ($mt4Users as $k => $v) {
  307. $equity += $v['EQUITY'];
  308. }
  309. $data = [
  310. 'ib' => $ib,
  311. 'mt4Users' => $mt4Users,
  312. 'equity' => $equity,
  313. 'directlyUserCount' => $directlyUserCount,
  314. 'depositSum' => round($depositSum, 5),
  315. 'withdrawSum' => round($withdrawSum, 5),
  316. 'depositSumByDay' => $depositSumByDay,
  317. ];
  318. $result['data'] = $data;
  319. $result['code'] = 1;
  320. return $result;
  321. }
  322. /**
  323. * 根据类型统计用户数量
  324. * @param int $type
  325. * @return int
  326. */
  327. public static function countByType($type)
  328. {
  329. $type = (int) $type;
  330. return static::find()->where(['type' => $type])->count();
  331. }
  332. /**
  333. * 统计XTrader用户数量
  334. * @return int
  335. */
  336. public static function xTraderCount()
  337. {
  338. return static::countByType(static::MEMBER_TYPE_USER);
  339. }
  340. /**
  341. * 统计XBroker用户数量
  342. * @return int
  343. */
  344. public static function xBokerCount()
  345. {
  346. return static::countByType(static::MEMBER_TYPE_IB);
  347. }
  348. /**
  349. * 统计后台用户数量
  350. * @return int
  351. */
  352. public static function adminCount()
  353. {
  354. return static::countByType(static::MEMBER_TYPE_ADMIN);
  355. }
  356. /**
  357. * 后台代理商列表数据
  358. * @param array $post
  359. * @return array
  360. */
  361. public function getAdminIbList($post)
  362. {
  363. $result = $this->getAdminList($post, static::MEMBER_TYPE_IB);
  364. if ($result['code'] == 0) {
  365. return $result;
  366. }
  367. // 数据处理
  368. $data = $result['data']['data'];
  369. if ($data) {
  370. $refIds = array_column($data, 'ref_id');
  371. $ibList = static::find()->select(['id', 'name'])->where(['in', 'id', $refIds])->asArray()->all();
  372. foreach ($data as $k => $v) {
  373. $data[$k]['IBNAME'] = '';
  374. foreach ($ibList as $k2 => $v2) {
  375. if ($v['ref_id'] == $v2['id']) {
  376. $data[$k]['IBNAME'] = $v2['name'];
  377. break;
  378. }
  379. }
  380. }
  381. $result['data']['data'] = $data;
  382. }
  383. return $result;
  384. }
  385. /**
  386. * 后台列表数据
  387. * @param array $post
  388. * @param int $type
  389. * @return array
  390. */
  391. public function getAdminList($post, $type)
  392. {
  393. $result = ['code' => 0, 'data' => [], 'message' => ''];
  394. if (!in_array($type, [static::MEMBER_TYPE_USER, static::MEMBER_TYPE_IB, static::MEMBER_TYPE_ADMIN])) {
  395. return $result;
  396. }
  397. $id = isset($post['id']) ? (int) $post['id'] : 0;
  398. $order = isset($post['order']) ? strtolower($post['order']) : '';
  399. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  400. $search = isset($post['search']) ? $post['search'] : '';
  401. $start = isset($post['start']) ? (int) $post['start'] : 0;
  402. $length = isset($post['length']) ? (int) $post['length'] : 20;
  403. $draw = isset($post['draw']) ? $post['draw'] : 1;
  404. $where = ['and', ['=', 'type', $type]];
  405. // 名下客户
  406. if ($id) {
  407. $ibs = $this->findChildrenIncludeSelf($id);
  408. $id_arr = array_column($ibs, 'id');
  409. $where[] = ['in', 'id', $id_arr];
  410. }
  411. // 搜索
  412. if ($search) {
  413. if (filter_var($search, FILTER_VALIDATE_IP) !== false) {
  414. $where[] = ['=', 'ip', $search];
  415. } elseif (is_numeric($search)) {
  416. // 用户名也可能是数字
  417. $where[] = [
  418. 'or',
  419. ['like', 'logins', $search],
  420. ['like', 'username', $search],
  421. ['like', 'name', $search],
  422. ];
  423. } else {
  424. $where[] = [
  425. 'or',
  426. ['like', 'username', $search],
  427. ['like', 'name', $search],
  428. ];
  429. }
  430. }
  431. // 排序
  432. $allowOrderColumn = ['id', 'ib_old_login_name', 'logins', 'username', 'name', 'mobile', 'is_enable', 'in_time'];
  433. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  434. if ($orderBy == 'asc') {
  435. $orderCondition = [$order => SORT_ASC];
  436. } else {
  437. $orderCondition = [$order => SORT_DESC];
  438. }
  439. } else {
  440. $orderCondition = ['id' => SORT_DESC];
  441. }
  442. $query = static::find();
  443. $query->where($where)
  444. ->orderBy($orderCondition);
  445. $count = $query->count();
  446. $query->offset($start)->limit($length);
  447. $list = $query->asArray()->all();
  448. if ($count) {
  449. foreach ($list as $k => $v) {
  450. unset($list[$k]['password']);
  451. }
  452. }
  453. $data['data'] = $list;
  454. $data['draw'] = $draw;
  455. $data['recordsFiltered'] = $count;
  456. $data['recordsTotal'] = $count;
  457. $result['data'] = $data;
  458. $result['code'] = 1;
  459. return $result;
  460. }
  461. /**
  462. * 添加代理商
  463. * @param array $post
  464. * @return array
  465. */
  466. public function addAdminIb($post)
  467. {
  468. $result = ['code' => 0, 'data' => [], 'message' => ''];
  469. // 验证
  470. $ref = static::find()->where(['id' => $post['ref_id']])->limit(1)->asArray()->one();
  471. if (!$ref || $ref['type'] != static::MEMBER_TYPE_IB) {
  472. $result['message'] = '上级代理不存在';
  473. return $result;
  474. }
  475. $ib_old_login_name = static::find()->select(['ib_old_login_name'])->where(['ib_old_login_name' => $post['ib_old_login_name']])->limit(1)->asArray()->scalar();
  476. if ($ib_old_login_name) {
  477. $result['message'] = '用户名已存在';
  478. return $result;
  479. }
  480. $username = static::find()->select(['username'])->where(['username' => $post['username']])->limit(1)->asArray()->scalar();
  481. if ($username) {
  482. $result['message'] = '电子邮箱已存在';
  483. return $result;
  484. }
  485. $idno = static::find()->select(['id_no'])->where(['id_no' => $post['id_no']])->limit(1)->asArray()->scalar();
  486. if ($idno) {
  487. $result['message'] = '身份证已存在';
  488. return $result;
  489. }
  490. // 字段数据处理
  491. $attributes = $post;
  492. if (!empty($attributes['password'])) {
  493. $attributes['password'] = $this->hash($attributes['password']);
  494. }
  495. if (!empty($attributes['id_no'])) {
  496. $idcard = Idcard::getInstance();
  497. if ($idcard->isChinaIDCard($attributes['id_no'])) {
  498. $attributes['birthday'] = $idcard->birthday;
  499. $attributes['gender'] = $idcard->getChinaIDCardSex($attributes['id_no']) === '男' ? 1 : 2;
  500. } else {
  501. $attributes['id_no'] = '';
  502. }
  503. }
  504. $attributes['ref_path'] = $ref['ref_path'] . $attributes['ref_id'] . ',';
  505. $attributes['in_time'] = round(microtime(true) * 1000);
  506. $attributes['type'] = static::MEMBER_TYPE_IB;
  507. $this->setAttributes($attributes);
  508. if ($this->save()) {
  509. $result['code'] = 1;
  510. // 发送短信
  511. if (!empty($post['isSendMail']) && $post['isSendMail'] === 'on') {
  512. }
  513. } else {
  514. $errors = $this->getFirstErrors();
  515. $error = reset($errors);
  516. $result['message'] = !empty($error) ? $error : '保存失败';
  517. }
  518. return $result;
  519. }
  520. /**
  521. * 添加用户
  522. * @param array $post
  523. * @return array
  524. */
  525. public function addAdminMember($post)
  526. {
  527. $result = ['code' => 0, 'data' => [], 'message' => ''];
  528. $username = static::find()->select(['username'])->where(['username' => $post['username']])->limit(1)->asArray()->scalar();
  529. if ($username) {
  530. $result['message'] = '电子邮箱已存在';
  531. return $result;
  532. }
  533. $idno = static::find()->select(['id_no'])->where(['id_no' => $post['id_no']])->limit(1)->asArray()->scalar();
  534. if ($idno) {
  535. $result['message'] = '身份证已存在';
  536. return $result;
  537. }
  538. // 字段数据处理
  539. $attributes = $post;
  540. if (!empty($attributes['password'])) {
  541. $attributes['password'] = $this->hash($attributes['password']);
  542. }
  543. if (!empty($attributes['id_no'])) {
  544. $idcard = Idcard::getInstance();
  545. if ($idcard->isChinaIDCard($attributes['id_no'])) {
  546. $attributes['birthday'] = $idcard->birthday;
  547. $attributes['gender'] = $idcard->getChinaIDCardSex($attributes['id_no']) === '男' ? 1 : 2;
  548. } else {
  549. $attributes['id_no'] = '';
  550. }
  551. }
  552. $attributes['in_time'] = round(microtime(true) * 1000);
  553. $attributes['type'] = static::MEMBER_TYPE_USER;
  554. $this->setAttributes($attributes);
  555. if ($this->save()) {
  556. $result['code'] = 1;
  557. // 发送短信
  558. if (!empty($post['isSendMail']) && $post['isSendMail'] === 'on') {
  559. }
  560. } else {
  561. $errors = $this->getFirstErrors();
  562. $error = reset($errors);
  563. $result['message'] = !empty($error) ? $error : '保存失败';
  564. }
  565. return $result;
  566. }
  567. /**
  568. * 后台代理商详情
  569. * @param array $post
  570. * @return array
  571. */
  572. public function getAdminIbView($post)
  573. {
  574. $result = ['code' => 0, 'data' => [], 'message' => ''];
  575. $id = isset($post['id']) ? intval($post['id']) : 0;
  576. if (!$id) {
  577. $result['message'] = '参数错误';
  578. return $result;
  579. }
  580. $member = static::findById($id);
  581. if (!$member) {
  582. $result['message'] = '该代理商不存在';
  583. return $result;
  584. }
  585. $member['birthday'] = date('Y-m-d', strtotime($member['birthday']));
  586. $member['in_time'] = date('Y-m-d H:i:s', $member['in_time'] / 1000);
  587. $signins = Signin::find()->where(['member_id' => $id])->orderBy(['id' => SORT_DESC])->limit(10)->asArray()->all();
  588. foreach ($signins as $k => $v) {
  589. $signins[$k]['login_time'] = date('Y-m-d H:i:s', $v['in_time'] / 1000);
  590. }
  591. $bank_info = MemberBankInfo::find()->where(['member_id' => $id])->limit(1)->asArray()->one();
  592. $data = [
  593. 'member' => $member,
  594. 'signins' => $signins,
  595. 'bank_info' => $bank_info,
  596. ];
  597. $result['data'] = $data;
  598. $result['code'] = 1;
  599. return $result;
  600. }
  601. /**
  602. * 后台管理员列表数据
  603. * @param array $post
  604. * @return array
  605. */
  606. public function getAdminAdminList($post)
  607. {
  608. $result = $this->getAdminList($post, static::MEMBER_TYPE_ADMIN);
  609. if ($result['code'] == 0) {
  610. return $result;
  611. }
  612. // 数据处理
  613. $data = $result['data']['data'];
  614. if ($data) {
  615. $ids = array_column($data, 'id');
  616. $signs = Signin::find()->select(['member_id', 'ip'])->where(['in', 'member_id', $ids])->orderBy(['id' => SORT_DESC])->asArray()->all();
  617. foreach ($data as $k => $v) {
  618. $data[$k]['last_login_ip'] = '';
  619. foreach ($signs as $k2 => $v2) {
  620. if ($v['id'] == $v2['member_id']) {
  621. $data[$k]['last_login_ip'] = $v2['ip'];
  622. break;
  623. }
  624. }
  625. }
  626. $result['data']['data'] = $data;
  627. }
  628. return $result;
  629. }
  630. /**
  631. * 后台添加管理员
  632. * @param array $post
  633. * @return array
  634. */
  635. public function addAdminAdmin($post)
  636. {
  637. $result = ['code' => 0, 'data' => [], 'message' => ''];
  638. // 验证
  639. $username = static::find()->select(['username'])->where(['username' => $post['username']])->limit(1)->asArray()->scalar();
  640. if ($username) {
  641. $result['message'] = '用户名已存在';
  642. return $result;
  643. }
  644. $mobile = static::find()->select(['mobile'])->where(['mobile' => $post['mobile']])->limit(1)->asArray()->scalar();
  645. if ($mobile) {
  646. $result['message'] = '手机号已存在';
  647. return $result;
  648. }
  649. // 字段数据处理
  650. $attributes = $post;
  651. if (!empty($attributes['password'])) {
  652. $attributes['password'] = $this->hash($attributes['password']);
  653. }
  654. $attributes['in_time'] = round(microtime(true) * 1000);
  655. $attributes['type'] = static::MEMBER_TYPE_ADMIN;
  656. $this->setAttributes($attributes);
  657. if ($this->save()) {
  658. $result['code'] = 1;
  659. } else {
  660. $errors = $this->getFirstErrors();
  661. $error = reset($errors);
  662. $result['message'] = !empty($error) ? $error : '保存失败';
  663. }
  664. return $result;
  665. }
  666. /**
  667. * 后台编辑管理员
  668. * @param array $post
  669. * @return array
  670. */
  671. public function editAdminAdmin($post)
  672. {
  673. $result = ['code' => 0, 'data' => [], 'message' => ''];
  674. // 验证
  675. $memberModel = static::find()->where(['id' => $post['id'], 'type' => static::MEMBER_TYPE_ADMIN])->limit(1)->one();
  676. if (!$memberModel) {
  677. $result['message'] = '该管理员不存在';
  678. return $result;
  679. }
  680. $mobile = static::find()->select(['mobile'])->where(['mobile' => $post['mobile']])->limit(1)->asArray()->scalar();
  681. if ($mobile && $mobile != $memberModel['mobile']) {
  682. $result['message'] = '手机号已存在';
  683. return $result;
  684. }
  685. // 字段数据处理
  686. $attributes = $post;
  687. if (!empty($attributes['password'])) {
  688. $attributes['password'] = $this->hash($attributes['password']);
  689. } else {
  690. // 为空则不修改密码
  691. unset($attributes['password']);
  692. }
  693. $attributes['in_time'] = round(microtime(true) * 1000);
  694. $memberModel->setAttributes($attributes);
  695. if ($memberModel->save()) {
  696. $result['code'] = 1;
  697. } else {
  698. $errors = $memberModel->getFirstErrors();
  699. $error = reset($errors);
  700. $result['message'] = !empty($error) ? $error : '保存失败';
  701. }
  702. return $result;
  703. }
  704. /**
  705. * 删除用户
  706. * @param int $id
  707. * @return bool
  708. */
  709. public static function deleteById($id)
  710. {
  711. $result = ['code' => 0, 'data' => [], 'message' => ''];
  712. $member = static::find()->where(['id' => $id])->one();
  713. if ($member == null) {
  714. $result['message'] = '用户不存在';
  715. return $result;
  716. }
  717. if ($member->type == 2) {
  718. $id = $member->id;
  719. $agents = static::find()->where(['ref_id' => $id])->asArray()->all();
  720. if ($agents) {
  721. $result['message'] = '代理有发展下级, 不能删除';
  722. return $result;
  723. }
  724. }
  725. $transaction = static::getDb()->beginTransaction();
  726. try {
  727. $member->delete();
  728. Deposit::deleteAll(['member_id' => $member->id]);
  729. Withdraw::deleteAll(['member_id' => $member->id]);
  730. ModifyLever::deleteAll(['member_id' => $member->id]);
  731. NoticeRead::deleteAll(['member_id' => $member->id]);
  732. Signin::deleteAll(['member_id' => $member->id]);
  733. Transfer::deleteAll(['member_id' => $member->id]);
  734. $transaction->commit();
  735. $result['code'] = 1;
  736. $result['message'] = '删除成功';
  737. return $result;
  738. } catch (\Exception $e) {
  739. $transaction->rollBack();
  740. }
  741. $result['message'] = '删除成功';
  742. return $result;
  743. }
  744. /**
  745. * @param int $id
  746. * @return array
  747. */
  748. public function findDirectlyChildren($id)
  749. {
  750. $list = static::find()->where(['ref_id' => $id])->asArray()->all();
  751. $result = $list;
  752. foreach ($list as $k => $v) {
  753. $sub_result = static::findDirectlyChildren($v['id']);
  754. if ($sub_result) {
  755. $result = array_merge($result, $sub_result);
  756. }
  757. }
  758. return $result;
  759. }
  760. /**
  761. * 是否为同名账户,判断身份证号,只能转入XTrader
  762. * @param int $fromLogin
  763. * @param int $toLogin
  764. * @param int $type
  765. * @return array
  766. */
  767. public static function isSameAccount($fromLogin, $toLogin, $type)
  768. {
  769. $result = ['code' => 0, 'data' => [], 'message' => '转出账户和转入账户不是同名账户'];
  770. $fromLogin = (int) $fromLogin;
  771. $toLogin = (int) $toLogin;
  772. $type = (int) $type;
  773. if ($fromLogin == $toLogin) {
  774. $result['message'] = '转出账户和转入账户相同';
  775. return $result;
  776. }
  777. $fromMember = static::findByLogin($fromLogin, $type);
  778. $toMember = static::findByLogin($toLogin, static::MEMBER_TYPE_USER);
  779. if (!$fromMember) {
  780. $result['message'] = '转出账户不存在';
  781. return $result;
  782. }
  783. if (!$toMember) {
  784. $result['message'] = '转入账户不存在';
  785. return $result;
  786. }
  787. // 按身份证号判断
  788. if ($fromMember['id_no'] && $toMember['id_no'] && strtoupper($fromMember['id_no']) == strtoupper($toMember['id_no'])) {
  789. $result['code'] = 1;
  790. return $result;
  791. }
  792. return $result;
  793. }
  794. }