Member.php 27 KB

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