UserModel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. /**
  3. * CommentModel.php UTF-8
  4. * 评论
  5. *
  6. * @date : 2017/11/24 16:21
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\user;
  13. use huo\model\agent\AgentRateModel;
  14. use huo\model\common\CommonModel;
  15. use huo\model\member\MemberModel;
  16. use huolib\constant\AgentConst;
  17. use huolib\constant\CommonConst;
  18. use huolib\constant\MemConst;
  19. use think\Cache;
  20. class UserModel extends CommonModel {
  21. CONST USER_STATUS_UNVERIFIED = 1;
  22. CONST USER_STATUS_ACTIVATED = 2;
  23. CONST USER_STATUS_BLOCKED = 3;
  24. CONST USER_SWITCH_YES = 1;
  25. CONST USER_SWITCH_NO = 2;
  26. protected $name = 'user';
  27. protected $type = ['ext_info' => 'array'];
  28. // 开启自动写入时间戳字段
  29. protected $autoWriteTimestamp = true;
  30. protected $agent_role = '23';
  31. protected $cache_tag = 'agent_id_name';
  32. /**
  33. * 关联agent_ext表
  34. *
  35. * @return \think\model\relation\HasOne
  36. */
  37. public function ext() {
  38. return $this->hasOne(AgentExtModel::className(), 'agent_id', 'id', [], 'left')->setEagerlyType(0);
  39. }
  40. /**
  41. * 获取余额
  42. *
  43. * @return \think\model\relation\HasOne
  44. */
  45. public function remain() {
  46. return $this->hasOne(AgentExtModel::className(), 'agent_id', 'id')
  47. ->field('agent_id, share_total,frozen_amount,share_remain');
  48. }
  49. /**
  50. * 关联父渠道
  51. *
  52. * @return \think\model\relation\HasOne
  53. */
  54. public function parent() {
  55. return $this->hasOne(UserModel::className(), 'id', 'parent_id')->field('id,user_nicename,user_login');
  56. }
  57. /**
  58. * 关联玩家
  59. *
  60. * @return \think\model\relation\HasOne
  61. */
  62. public function mem() {
  63. return $this->hasOne(MemberModel::className(), 'id', 'mem_id')->field('id,username,nickname,mobile,real_name');
  64. }
  65. /**
  66. * 折扣
  67. *
  68. * @return \think\model\relation\HasOne
  69. */
  70. public function rate() {
  71. return $this->hasOne(AgentRateModel::className(), 'agent_id', 'id', [], 'left')
  72. ->field('agent_rate,sub_agent_rate')
  73. ->setEagerlyType(0);
  74. }
  75. /**
  76. * 设置用户支付密码
  77. *
  78. * @param string $value
  79. *
  80. * @return string
  81. */
  82. public function setPayPwdAttr($value) {
  83. if (!empty($value)) {
  84. return cmf_password($value);
  85. }
  86. return $value;
  87. }
  88. /**
  89. * 设置用户密码
  90. *
  91. * @param string $value
  92. *
  93. * @return string
  94. */
  95. public function setUserPassAttr($value = '') {
  96. if (!empty($value)) {
  97. return cmf_password($value);
  98. }
  99. return $value;
  100. }
  101. /**
  102. * 获取状态文字
  103. *
  104. * @param $value
  105. *
  106. * @return mixed
  107. */
  108. public function getStatusTextAttr($value) {
  109. $_user_statuses = array(
  110. self::USER_STATUS_UNVERIFIED => '未验证',
  111. self::USER_STATUS_ACTIVATED => '正常',
  112. self::USER_STATUS_BLOCKED => '已拉黑'
  113. );
  114. return isset($_user_statuses[$value]) ? $_user_statuses[$value] : '';
  115. }
  116. /**
  117. * 获取切量文字
  118. *
  119. * @param $value
  120. *
  121. * @return mixed
  122. */
  123. public function getSwitchTextAttr($value) {
  124. $_user_switchs = array(
  125. self::USER_SWITCH_YES => '切量',
  126. self::USER_SWITCH_NO => '不切'
  127. );
  128. return isset($_user_switchs[$value]) ? $_user_switchs[$value] : '';
  129. }
  130. /**
  131. * 通过代理ID获取信息
  132. *
  133. * @param $user_id
  134. *
  135. * @return array|false
  136. */
  137. public function getUserInfo($user_id) {
  138. $_map = array();
  139. if (empty($user_id) || $user_id < 0) {
  140. return false;
  141. }
  142. $_map['id'] = $user_id;
  143. $_info = $this->where($_map)->find();
  144. if (false === $_info) {
  145. return false;
  146. }
  147. if (is_object($_info)) {
  148. return $_info->toArray();
  149. } else {
  150. return $_info;
  151. }
  152. }
  153. /**
  154. * @param array $data
  155. *
  156. * @return bool|mixed
  157. */
  158. public function addData($data = []) {
  159. $_data['user_login'] = get_val($data, 'user_login', '');
  160. $_data['user_pass'] = get_val($data, 'user_pass', '');
  161. $_data['pay_pwd'] = get_val($data, 'pay_pwd', '');
  162. $_data['user_nicename'] = get_val($data, 'user_nicename', '');
  163. $_data['user_email'] = get_val($data, 'user_email', '');
  164. $_data['user_url'] = get_val($data, 'user_url', '');
  165. $_data['avatar'] = get_val($data, 'avatar', '');
  166. $_data['sex'] = get_val($data, 'sex', MemConst::GENDER_N);
  167. $_data['birthday'] = get_val($data, 'birthday', 0);
  168. $_data['signature'] = get_val($data, 'signature', '');
  169. $_data['last_login_ip'] = get_val($data, 'last_login_ip', request()->ip());
  170. $_data['last_login_time'] = get_val($data, 'last_login_time', time());
  171. $_data['user_activation_key'] = get_val($data, 'user_activation_key', '');
  172. $_data['user_status'] = get_val($data, 'user_status', MemConst::STATUS_NORMAL);
  173. $_data['score'] = get_val($data, 'score', 0);
  174. $_data['role_id'] = get_val($data, 'role_id', 0);
  175. $_data['coin'] = get_val($data, 'coin', 0);
  176. $_data['mobile'] = get_val($data, 'mobile', '');
  177. $_data['qq'] = get_val($data, 'qq', '');
  178. $_data['linkman'] = get_val($data, 'linkman', '');
  179. $_data['parent_id'] = get_val($data, 'parent_id', 1);
  180. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  181. $_data['cp_id'] = get_val($data, 'cp_id', 0);
  182. $_ext_info = get_val($_data, 'ext_info', []);
  183. $_agent_charge_type = get_val($data, 'agent_charge_type', '');
  184. if (!empty($_agent_charge_type)) {
  185. $_ext_info['agent_charge_type'] = $_agent_charge_type;
  186. }
  187. $_price = get_val($data, 'price', '');
  188. if (!empty($_price)) {
  189. $_ext_info['price'] = $_price;
  190. }
  191. $_wx_id = get_val($data, 'wx_id', '');
  192. if (!empty($_wx_id)) {
  193. $_ext_info['wx_id'] = $_wx_id;
  194. }
  195. if ($_obj = self::create($_data, true)) {
  196. $_ext_data['agent_id'] = $_obj->id;
  197. /* 写入扩展 */
  198. $_obj->ext()->save($_ext_data);
  199. if (isset($_data['role_id']) && AgentConst::AGENT_ROLE_MP_MEMBER != $_data['role_id']) {
  200. /* 小游戏玩家渠道不更新该缓存 20200313 chenbingling*/
  201. Cache::clear($this->cache_tag);
  202. }
  203. return $_obj->id;
  204. } else {
  205. return false;
  206. }
  207. }
  208. /**
  209. * 更新数据
  210. *
  211. * @param array $data
  212. *
  213. * @param int $user_id
  214. *
  215. * @return bool|mixed
  216. */
  217. public function updateData($data = [], $user_id) {
  218. $_map['id'] = $user_id;
  219. $_data = $data;
  220. $_rs = self::update($_data, $_map, true);
  221. if (false == $_rs) {
  222. return false;
  223. } else {
  224. Cache::clear($this->cache_tag);
  225. return true;
  226. }
  227. }
  228. /**
  229. * 通过ID获取名称
  230. *
  231. * @param $id
  232. *
  233. * @return mixed
  234. */
  235. public function getNameById($id) {
  236. if (empty($id)) {
  237. return '';
  238. }
  239. $_map['id'] = $id;
  240. return $this->where($_map)->value('user_login');
  241. }
  242. /**
  243. * @param mixed $agent_ids
  244. * @param bool $inc_me
  245. *
  246. * @return array
  247. */
  248. public function getSubAgents($agent_ids, $inc_me = false, $role = 0) {
  249. if (!(is_array($agent_ids) || is_numeric($agent_ids))) {
  250. return [];
  251. }
  252. $_agent_ids = $agent_ids;
  253. if (is_numeric($agent_ids)) {
  254. $_agent_ids = [$agent_ids];
  255. }
  256. $_map['parent_id'] = ['in', $_agent_ids];
  257. if (!empty($role)) {
  258. $_map['role_id'] = $role;
  259. }
  260. $_id_arr = $this->where($_map)->column('id');
  261. if ($inc_me) {
  262. $_id_arr = array_merge($_agent_ids, $_id_arr);
  263. }
  264. return $_id_arr;
  265. }
  266. /**
  267. * @param mixed $role_ids
  268. *
  269. * @return array
  270. */
  271. public function getIdsByRoleId($role_ids) {
  272. if (!(is_array($role_ids) || is_numeric($role_ids))) {
  273. return [];
  274. }
  275. $_role_ids = $role_ids;
  276. if (is_numeric($role_ids)) {
  277. $_role_ids = [$role_ids];
  278. }
  279. $_map['role_id'] = ['in', $_role_ids];
  280. $_id_arr = $this->where($_map)->column('id');
  281. return $_id_arr;
  282. }
  283. /**
  284. * 渠道ID 名称对应表
  285. *
  286. * @param array $where
  287. * @param bool $inc_nice
  288. *
  289. * @return array
  290. */
  291. public function getIdNames($where, $inc_nice = false) {
  292. $_tag = $this->cache_tag;
  293. $_cache_key = md5($_tag.json_encode($where));
  294. $_field = 'user_login';
  295. if ($inc_nice) {
  296. $_cache_key = 'inc_nice'.$_cache_key;
  297. $_field = "concat(user_login,'(',user_nicename,')') as name";
  298. }
  299. $_agents = $this->where($where)
  300. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  301. ->column($_field, 'id');
  302. return $_agents;
  303. }
  304. /**
  305. * 获取总渠道列表
  306. * @param $where
  307. * @param false $inc_nice
  308. * @return array|false|string
  309. */
  310. public function getNames($where, $inc_nice = false)
  311. {
  312. $_tag = $this->cache_tag;
  313. $_cache_key = md5($_tag . json_encode($where));
  314. $_field = 'user_login';
  315. if ($inc_nice) {
  316. $_cache_key = 'inc_nice' . $_cache_key;
  317. $_field = "user_nicename as name";
  318. }
  319. return $this->where($where)
  320. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  321. ->column($_field, 'id');
  322. }
  323. /**
  324. * 总计
  325. *
  326. * @param int $_start_time
  327. * @param int $_end_time
  328. *
  329. * @param array $map
  330. *
  331. * @return int
  332. */
  333. public function agentTotal($_start_time = 0, $_end_time = 0, $map = []) {
  334. $_role_types = [AgentConst::ROLE_TYPE_GROUP, AgentConst::ROLE_TYPE_AGENT];
  335. $_role_ids = (new RoleModel())->getIdsByRoleType($_role_types);
  336. $where['role_id'] = ['in', $_role_ids];
  337. if (!empty($_start_time) && !empty($_end_time)) {
  338. $where['create_time'] = ['between', [$_start_time, $_end_time]];
  339. }
  340. return $this->where($where)->where($map)->count('id');
  341. }
  342. /**
  343. * 昨日新增
  344. *
  345. * @return int
  346. */
  347. public function agentYesterdayCount() {
  348. return $this->getDayAddedCount(strtotime('yesterday'));
  349. }
  350. /**
  351. * 今日新增
  352. *
  353. * @return int
  354. */
  355. public function agentTodayCount() {
  356. return $this->getDayAddedCount(strtotime('today'));
  357. }
  358. /**
  359. * 一天新增
  360. *
  361. * @param int $time 时间戳,0点
  362. *
  363. * @return int
  364. */
  365. public function getDayAddedCount($time) {
  366. // $where['role_id'] = ['like', "%{$this->agent_role}%"];
  367. // $where['create_time'] = ['between', [$time, $time + 24 * 3600 - 1]];
  368. return $this->agentTotal($time, $time + 24 * 3600 - 1);
  369. // return $this->where($where)->count('id');
  370. }
  371. /**
  372. * 上线的渠道数
  373. *
  374. * @param array $where
  375. *
  376. * @return int
  377. */
  378. public function onlineCount($where) {
  379. $_map = $where;
  380. $_map['user_status'] = MemConst::STATUS_NORMAL;
  381. return $this->agentTotal(0, time(), $_map);
  382. // return $this->where($_map)->count('id');
  383. }
  384. /**
  385. * 校验支付密码是否正确
  386. *
  387. * @param string $pay_password 提交的支付密码
  388. *
  389. * @return bool
  390. */
  391. public function verifyPayPassword($pay_password) {
  392. if (cmf_password($pay_password) == $this->pay_pwd) {
  393. return true;
  394. }
  395. return false;
  396. }
  397. /**
  398. * 根据用户名获取ID
  399. *
  400. * @param $user_login
  401. *
  402. * @return mixed
  403. */
  404. public function getIdByName($user_login) {
  405. return $this->where(['user_login' => $user_login])->value('id');
  406. }
  407. /**
  408. * 根据用户昵称获取IDs
  409. *
  410. * @param $user_nicename
  411. *
  412. * @return mixed
  413. */
  414. public function getIdsByNickName($user_nicename) {
  415. return $this->where(['user_nicename' => $user_nicename])->column('id');
  416. }
  417. /**
  418. * 根据玩家ID获取渠道ID
  419. *
  420. * @param int $mem_id 玩家ID
  421. *
  422. * @return int
  423. */
  424. public function getIdByMemId($mem_id) {
  425. return $this->where(['mem_id' => $mem_id])->value('id');
  426. }
  427. /**
  428. * 根据渠道ID获取玩家ID
  429. *
  430. * @param int $id 玩家渠道ID
  431. *
  432. * @return int
  433. */
  434. public function getMemIdById($id) {
  435. return $this->where(['id' => $id])->value('mem_id');
  436. }
  437. /**
  438. * 根据玩家ID获取渠道信息
  439. *
  440. * @param $mem_id
  441. *
  442. * @return array|bool|false|\PDOStatement|string|\think\Model
  443. */
  444. public function getInfoByMemId($mem_id) {
  445. $_map['mem_id'] = $mem_id;
  446. $_info = $this->useGlobalScope(false)->where($_map)->find();
  447. if (false === $_info) {
  448. return false;
  449. }
  450. if (is_object($_info)) {
  451. return $_info->toArray();
  452. } else {
  453. return $_info;
  454. }
  455. }
  456. /**
  457. * 获取子渠道
  458. *
  459. * @param $parent_id
  460. * @param array $where
  461. *
  462. * @return array
  463. */
  464. public function getIdsByParentId($parent_id, $where = []) {
  465. $_map = $where;
  466. $_map['parent_id'] = $parent_id;
  467. return $this->where($_map)->column('id');
  468. }
  469. /**
  470. * 获取子渠道
  471. *
  472. * @param $mem_ids
  473. * @param array $where
  474. *
  475. * @return array
  476. */
  477. public function getIdsByMemIds($mem_ids, $where = []) {
  478. if (empty($mem_ids)) {
  479. return [];
  480. }
  481. $_map = $where;
  482. $_map['mem_id'] = ['in', $mem_ids];
  483. return $this->where($_map)->column('id');
  484. }
  485. /**
  486. * 获取子渠道
  487. *
  488. * @param $ids
  489. * @param array $where
  490. *
  491. * @return array
  492. */
  493. public function getMemIdsByIds($ids, $where = []) {
  494. if (empty($mem_ids)) {
  495. return [];
  496. }
  497. $_map = $where;
  498. $_map['id'] = ['in', $ids];
  499. return $this->where($_map)->column('mem_id');
  500. }
  501. /**
  502. * 根据cp_id获取一级渠道id
  503. *
  504. * @param $cp_id
  505. * @param int $role_id
  506. *
  507. * @return array
  508. */
  509. public function getIdsByCpId($cp_id, $role_id = AgentConst::AGENT_ROLE_MP_GROUP) {
  510. $_map = [
  511. 'role_id' => $role_id,
  512. 'cp_id' => $cp_id
  513. ];
  514. return $this->where($_map)->column('id');
  515. }
  516. /***
  517. * 获取父id
  518. *
  519. * @param $id
  520. *
  521. * @return array
  522. */
  523. public function getParentIdById($id) {
  524. $_map['id'] = $id;
  525. return $this->where($_map)->value('parent_id');
  526. }
  527. /***
  528. * 获取父id
  529. *
  530. * @param $id
  531. *
  532. * @return array
  533. */
  534. public function getRoleIdById($id) {
  535. $_map['id'] = $id;
  536. return $this->where($_map)->value('role_id');
  537. }
  538. }