Member.php 27 KB

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