Member.php 26 KB

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