Info.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/11/2
  6. * Time: 上午9:16
  7. */
  8. namespace common\helpers\user;
  9. use common\components\ActiveQuery;
  10. use common\components\ActiveRecord;
  11. use common\helpers\Cache;
  12. use common\helpers\Tool;
  13. use common\models\Config;
  14. use common\models\PerfMonth;
  15. use common\models\PerfPeriod;
  16. use common\models\Period;
  17. use common\models\DeclarationLevel;
  18. use common\models\DecRole;
  19. use common\models\EmployLevel;
  20. use common\models\OpenBank;
  21. use common\models\Region;
  22. use common\models\User;
  23. use common\models\UserBonus;
  24. use common\models\UserInfo;
  25. use common\models\UserNetwork;
  26. use common\models\UserRelation;
  27. use common\models\UserSystem;
  28. use common\models\UserWallet;
  29. use yii\base\Exception;
  30. class Info {
  31. /**
  32. * 获取用户ID
  33. * @param $userName
  34. * @return mixed
  35. */
  36. public static function getUserIdByUserName($userName) {
  37. $user = UserInfo::findOneAsArray('USER_NAME=:USER_NAME AND DELETED=0', [':USER_NAME' => $userName], 'USER_ID');
  38. return $user ? $user['USER_ID'] : null;
  39. }
  40. /**
  41. * 获取会员名通过ID
  42. * @param $userId
  43. * @return mixed|null
  44. */
  45. public static function getUserNameByUserId($userId) {
  46. $user = UserInfo::findOneAsArray('USER_ID=:USER_ID AND DELETED=0', [':USER_ID' => $userId], 'USER_NAME');
  47. return $user ? $user['USER_NAME'] : null;
  48. }
  49. /**
  50. * 获取会员姓名通过ID
  51. * @param $userId
  52. * @return null
  53. */
  54. public static function getUserRealNameByUserId($userId) {
  55. $user = User::findOneAsArray('ID=:ID AND DELETED=0', [':ID' => $userId], 'REAL_NAME');
  56. return $user ? $user['REAL_NAME'] : null;
  57. }
  58. /**
  59. * 获取手机号通过ID
  60. * @param $userId
  61. * @return null
  62. */
  63. public static function getUserMobileByUserId($userId) {
  64. $user = User::findOneAsArray('ID=:ID AND DELETED=0', [':ID' => $userId], 'MOBILE');
  65. return $user ? $user['MOBILE'] : null;
  66. }
  67. /**
  68. * 通过用户ID获取用户的编号和名称
  69. * @param $userId
  70. * @return array|null
  71. */
  72. public static function getBaseUserById($userId) {
  73. return User::findOneAsArray('ID=:ID', [':ID' => $userId], 'ID,USER_NAME,REAL_NAME');
  74. }
  75. /**
  76. * 通过用户ID获取用户的编号和名称
  77. * @param $username
  78. * @return array|null
  79. */
  80. public static function getBaseUserByUserName($username) {
  81. return User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $username]);
  82. }
  83. /**
  84. * 当前期数的会员信息
  85. * @param $userId
  86. * @return array|\yii\db\ActiveRecord|null
  87. * @throws Exception
  88. */
  89. public static function baseInfoNowPeriod($userId) {
  90. $data = User::find()->where('ID=:ID', [':ID' => $userId])->asArray()->one();
  91. if (!$data) throw new Exception('会员不存在'.$userId);
  92. $infoData = UserInfo::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->select('ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,SYSTEM_ID,REG_TYPE,REG_NAME,CREDIT_CODE,PREMISES,LEGAL_PERSON,CLOSE_LOGIN,IS_BIND_MAIN,HIGHEST_EMP_LV,HIGHEST_EMP_LV_PERIOD,SHOULD_REG_TYPE,IS_SYSTEM_LEADER,ALLOW_TRANSFER,TRANSFER_PROP,IS_GROUP_LEADER,GROUP_LEADER_AT,SHOW_EMP_LV')->asArray()->one();
  93. if ($infoData) {
  94. $data = array_merge($data, $infoData);
  95. }
  96. if (!$data['DEC_LV']) $data['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  97. if (!$data['LAST_DEC_LV']) $data['LAST_DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  98. if (!$data['EMP_LV']) $data['EMP_LV'] = EmployLevel::getDefaultLevelId();
  99. return $data;
  100. }
  101. /**
  102. * 获取会员信息,通过用户编号
  103. * @param $userId
  104. * @return array|\yii\db\ActiveRecord|null
  105. * @throws Exception
  106. */
  107. public static function baseInfoByUserName($userName) {
  108. $data = User::find()->where('USER_NAME=:USER_NAME', [':USER_NAME' => $userName])->asArray()->one();
  109. if (!$data) throw new Exception('会员不存在:'.$userName);
  110. $userId = $data['ID'];
  111. $infoData = UserInfo::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->select('ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,SYSTEM_ID,REG_TYPE,REG_NAME,CREDIT_CODE,PREMISES,LEGAL_PERSON,CLOSE_LOGIN,IS_BIND_MAIN,HIGHEST_EMP_LV,HIGHEST_EMP_LV_PERIOD,SHOULD_REG_TYPE,IS_SYSTEM_LEADER,ALLOW_TRANSFER,TRANSFER_PROP,IS_GROUP_LEADER,GROUP_LEADER_AT,SHOW_EMP_LV')->asArray()->one();
  112. if ($infoData) {
  113. $data = array_merge($data, $infoData);
  114. }
  115. if (!$data['DEC_LV']) $data['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  116. if (!$data['LAST_DEC_LV']) $data['LAST_DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  117. if (!$data['EMP_LV']) $data['EMP_LV'] = EmployLevel::getDefaultLevelId();
  118. return $data;
  119. }
  120. /**
  121. * 基本信息
  122. * @param $userId
  123. * @param $periodNum
  124. * @return array|\yii\db\ActiveRecord|null
  125. * @throws Exception
  126. */
  127. public static function baseInfo($userId, $periodNum = null) {
  128. if($periodNum == null){
  129. return self::baseInfoNowPeriod($userId);
  130. } else {
  131. // 获取当前期数
  132. $tableName = '{{%USER_ALL_'.$periodNum.'}}';
  133. if(ActiveRecord::isExistsTable($tableName, 'dbNetPoint')){
  134. if($data = \Yii::$app->dbNetPoint->createCommand("SELECT * FROM $tableName WHERE ID=:ID")->bindValues([':ID'=>$userId])->queryOne()) {
  135. if (!$data['DEC_LV']) $data['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  136. if (!$data['LAST_DEC_LV']) $data['LAST_DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  137. if (!$data['EMP_LV']) $data['EMP_LV'] = EmployLevel::getDefaultLevelId();
  138. if (!$data['HIGHEST_EMP_LV']) $data['HIGHEST_EMP_LV'] = EmployLevel::getDefaultLevelId();
  139. return $data;
  140. }else{
  141. return self::baseInfoNowPeriod($userId);
  142. }
  143. } else {
  144. return self::baseInfoNowPeriod($userId);
  145. }
  146. }
  147. }
  148. public static function baseInfoZhByUserName($userName) {
  149. $baseInfo = self::baseInfoByUserName($userName);
  150. // 状态处理
  151. $baseInfo['STATUS_NAME'] = \Yii::$app->params['userStatus'][$baseInfo['STATUS']]['label'];
  152. //登录状态
  153. $baseInfo['LOGIN_STATUS_NAME'] = $baseInfo['ALLOW_LOGIN'] == 0 ? '禁止登录' : '允许登录';
  154. // 子公司
  155. $baseInfo['SUB_COM_NAME'] = '';
  156. $baseInfo['PROVINCE_NAME'] = $baseInfo['PROVINCE'] ? Region::getCnName($baseInfo['PROVINCE']) : '';
  157. $baseInfo['CITY_NAME'] = $baseInfo['CITY'] ? Region::getCnName($baseInfo['CITY']) : '';
  158. $baseInfo['COUNTY_NAME'] = $baseInfo['COUNTY'] ? Region::getCnName($baseInfo['COUNTY']) : '';
  159. $baseInfo['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE'] ? Region::getCnName($baseInfo['BANK_PROVINCE']) : '';
  160. $baseInfo['BANK_CITY_NAME'] = $baseInfo['BANK_CITY'] ? Region::getCnName($baseInfo['BANK_CITY']) : '';
  161. $baseInfo['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY'] ? Region::getCnName($baseInfo['BANK_COUNTY']) : '';
  162. $baseInfo['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK'] ? OpenBank::getCnName($baseInfo['OPEN_BANK']) : '';
  163. // 获取会员体系
  164. $baseInfo['SYSTEM_NAME'] = '';
  165. // 获取会员级别名称
  166. $baseInfo['DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['DEC_LV']]['LEVEL_NAME'];
  167. $baseInfo['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['LAST_DEC_LV']]['LEVEL_NAME'];
  168. $baseInfo['EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$baseInfo['EMP_LV']]['LEVEL_NAME'];
  169. $baseInfo['HIGHEST_EMP_LV_NAME'] = '';
  170. // 获取会员报单级别
  171. $baseInfo['DEC_ROLE_NAME'] = '无';
  172. //民族
  173. $baseInfo['NATION_NAME'] = \Yii::$app->params['nation'][$baseInfo['NATION']]['name']??'';
  174. return $baseInfo;
  175. }
  176. /**
  177. * 基本信息含中文地区和中文子公司
  178. * @param $userId
  179. * @param $periodNum
  180. * @return array|\yii\db\ActiveRecord|null
  181. * @throws Exception
  182. */
  183. public static function baseInfoZh($userId, $periodNum = null) {
  184. $baseInfo = self::baseInfo($userId, $periodNum);
  185. // 状态处理
  186. $baseInfo['STATUS_NAME'] = \Yii::$app->params['userStatus'][$baseInfo['STATUS']]['label'];
  187. //登录状态
  188. $baseInfo['LOGIN_STATUS_NAME'] = $baseInfo['ALLOW_LOGIN'] == 0 ? '禁止登录' : '允许登录';
  189. // 子公司
  190. $baseInfo['SUB_COM_NAME'] = '';
  191. $baseInfo['PROVINCE_NAME'] = $baseInfo['PROVINCE'] ? Region::getCnName($baseInfo['PROVINCE']) : '';
  192. $baseInfo['CITY_NAME'] = $baseInfo['CITY'] ? Region::getCnName($baseInfo['CITY']) : '';
  193. $baseInfo['COUNTY_NAME'] = $baseInfo['COUNTY'] ? Region::getCnName($baseInfo['COUNTY']) : '';
  194. $baseInfo['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE'] ? Region::getCnName($baseInfo['BANK_PROVINCE']) : '';
  195. $baseInfo['BANK_CITY_NAME'] = $baseInfo['BANK_CITY'] ? Region::getCnName($baseInfo['BANK_CITY']) : '';
  196. $baseInfo['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY'] ? Region::getCnName($baseInfo['BANK_COUNTY']) : '';
  197. $baseInfo['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK'] ? OpenBank::getCnName($baseInfo['OPEN_BANK']) : '';
  198. // 获取会员体系
  199. $baseInfo['SYSTEM_NAME'] = '';
  200. // 获取会员级别名称
  201. $baseInfo['DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['DEC_LV']]['LEVEL_NAME'];
  202. $baseInfo['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['LAST_DEC_LV']]['LEVEL_NAME'];
  203. $baseInfo['EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$baseInfo['EMP_LV']]['LEVEL_NAME'];
  204. $baseInfo['HIGHEST_EMP_LV_NAME'] = '';
  205. // 获取会员报单级别
  206. $baseInfo['DEC_ROLE_NAME'] = '无';
  207. //民族
  208. $baseInfo['NATION_NAME'] = \Yii::$app->params['nation'][$baseInfo['NATION']]['name']??'';
  209. return $baseInfo;
  210. }
  211. /**
  212. * 基本信息及余额
  213. * @param $userId
  214. * @param $periodNum
  215. * @return array
  216. */
  217. public static function baseInfoWithBalance($userId, $periodNum = null) {
  218. $baseInfo = self::baseInfoZh($userId, $periodNum);
  219. if ($baseInfo) {
  220. $userBonus = self::BalanceInfo($userId);
  221. }
  222. return array_merge($baseInfo, $userBonus);
  223. }
  224. /**
  225. * 获取余额信息
  226. * @param $userId
  227. * @return array|int[]|null
  228. */
  229. public static function BalanceInfo($userId) {
  230. $userBonus = [
  231. 'BONUS' => 0,
  232. 'BONUS_FREEZE' => 0,
  233. 'CF' => 0,
  234. 'LX' => 0,
  235. 'QY_TOTAL' => 0,
  236. 'YC_TOTAL' => 0,
  237. 'FX_TOTAL' => 0,
  238. 'LS_TOTAL' => 0,
  239. 'CF_TOTAL' => 0,
  240. 'LX_TOTAL' => 0,
  241. 'FL_TOTAL' => 0,
  242. 'BT_TOTAL' => 0,
  243. 'FW_TOTAL' => 0,
  244. 'CASH' => 0,
  245. ];
  246. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  247. $userBonus = $userBonusResult;
  248. }
  249. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  250. $userBonus['CASH'] = $userCashResult['CASH'];
  251. }
  252. return $userBonus;
  253. }
  254. /**
  255. * 基本信息带着网络和节点信息
  256. * @param $userId
  257. * @param $periodNum
  258. * @return array|null|\yii\db\ActiveRecord
  259. */
  260. public static function baseInfoWithNet($userId, $periodNum = null) {
  261. $baseInfo = self::baseInfoZh($userId, $periodNum);
  262. $baseInfo['CON_USER_NAME'] = '';
  263. $baseInfo['CON_REAL_NAME'] = '';
  264. $baseInfo['LOCATION'] = '';
  265. $baseInfo['REC_USER_NAME'] = '';
  266. $baseInfo['REC_REAL_NAME'] = '';
  267. $baseInfo['DEC_USER_NAME'] = '';
  268. $baseInfo['DEC_REAL_NAME'] = '';
  269. $baseInfo['DEC_DEC_LV'] = '';
  270. $baseInfo['DEC_DEC_LV_NAME'] = '';
  271. if ($baseInfo) {
  272. if (isset($baseInfo['CON_UID']) && $baseInfo['CON_UID']) {
  273. // 安置网上级
  274. //$netParentUserInfo = UserNetwork::getFirstParentUserInfo($userId);
  275. $netParentBaseInfo = self::baseInfo($baseInfo['CON_UID']);
  276. $networkParent = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId]);
  277. //$baseInfo['CON_UID'] = $baseInfo['CON_UID'];
  278. $baseInfo['CON_USER_NAME'] = $netParentBaseInfo['USER_NAME'];
  279. $baseInfo['CON_REAL_NAME'] = $netParentBaseInfo['REAL_NAME'];
  280. $baseInfo['LOCATION'] = $networkParent['RELATIVE_LOCATION'];
  281. }
  282. if (isset($baseInfo['REC_UID']) && $baseInfo['REC_UID']) {
  283. // 推荐网上级
  284. //$relationParentUserInfo = UserRelation::getFirstParentUserInfo($userId);
  285. $relationParentBaseInfo = self::baseInfo($baseInfo['REC_UID']);
  286. //$baseInfo['REC_UID'] = $relationParentUserInfo['USER_ID'];
  287. $baseInfo['REC_USER_NAME'] = $relationParentBaseInfo['USER_NAME'];
  288. $baseInfo['REC_REAL_NAME'] = $relationParentBaseInfo['REAL_NAME'];
  289. }
  290. if (isset($baseInfo['DEC_ID']) && $baseInfo['DEC_ID']) {
  291. $decBaseInfo = self::baseInfoZh($baseInfo['DEC_ID']);
  292. $baseInfo['DEC_USER_NAME'] = $decBaseInfo['USER_NAME'];
  293. $baseInfo['DEC_REAL_NAME'] = $decBaseInfo['REAL_NAME'];
  294. $baseInfo['DEC_DEC_LV'] = $decBaseInfo['DEC_LV'];
  295. $baseInfo['DEC_DEC_LV_NAME'] = $decBaseInfo['DEC_LV_NAME'];
  296. $baseInfo['DEC_DEC_ROLE_NAME'] = $decBaseInfo['DEC_ROLE_NAME'];
  297. $baseInfo['DEC_DEC_ROLE_ID'] = $decBaseInfo['DEC_ROLE_ID'];
  298. }
  299. }
  300. return $baseInfo;
  301. }
  302. /**
  303. * 基本信息含网络和余额
  304. * @param $userId
  305. * @param $periodNum
  306. * @return array
  307. */
  308. public static function baseInfoWithNetAndBalance($userId, $periodNum = null) {
  309. return array_merge(self::baseInfoWithNet($userId), self::BalanceInfo($userId));
  310. }
  311. /**
  312. * 通过用户名获取用户信息从INFO表
  313. * @param $userName
  314. * @return array|null|\yii\db\ActiveRecord
  315. */
  316. public static function getUserByUserNameFromUserInfoTable($userName) {
  317. return UserInfo::findOneAsArray(['USER_NAME' => $userName]);
  318. }
  319. /**
  320. * 获取报单级别
  321. * @param $userId
  322. * @param null $periodNum
  323. * @return mixed
  324. * @throws \yii\db\Exception
  325. */
  326. public static function getDecLv($userId, $periodNum = null) {
  327. if ($periodNum === null) {
  328. $data = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'DEC_LV');
  329. $decLv = $data['DEC_LV'];
  330. } else {
  331. $period = Period::instance();
  332. $yearMonth = $period->getYearMonth($periodNum);
  333. $data = PerfPeriod::find()->yearMonth($yearMonth)->select('LAST_DEC_LV')->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => $userId, ':PERIOD_NUM' => $periodNum])->asArray()->one();
  334. $decLv = $data ? $data['LAST_DEC_LV'] : DeclarationLevel::getDefaultLevelId();
  335. }
  336. return $decLv;
  337. }
  338. /**
  339. * 获取聘级
  340. * @param $userId
  341. * @param null $periodNum
  342. * @return mixed
  343. * @throws \yii\db\Exception
  344. */
  345. public static function getEmpLv($userId, $periodNum = null) {
  346. if ($periodNum === null) {
  347. $data = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'EMP_LV');
  348. $empLv = $data['EMP_LV'];
  349. } else {
  350. $period = Period::instance();
  351. $yearMonth = $period->getYearMonth($periodNum);
  352. $data = PerfMonth::find()->yearMonth($yearMonth)->select('LAST_EMP_LV')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH' => $yearMonth])->asArray()->one();
  353. $empLv = $data ? $data['LAST_EMP_LV'] : EmployLevel::getDefaultLevelId();
  354. }
  355. return $empLv;
  356. }
  357. /**
  358. * 获取最高聘级
  359. * @param $userId
  360. * @return mixed
  361. */
  362. public static function getHighEmpLv($userId){
  363. $data = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'HIGHEST_EMP_LV');
  364. $empLv = $data ? $data['HIGHEST_EMP_LV'] : EmployLevel::getDefaultLevelId();
  365. return $empLv;
  366. }
  367. /**
  368. * 获取报单级别名称
  369. * @param $userId
  370. * @param null $periodNum
  371. * @return mixed
  372. * @throws \yii\db\Exception
  373. */
  374. public static function getDecLvName($userId, $periodNum = null) {
  375. if (!$decLv = self::getDecLv($userId, $periodNum)) {
  376. $decLv = DeclarationLevel::getDefaultLevelId();
  377. }
  378. return Cache::getDecLevelConfig()[$decLv]['LEVEL_NAME'];
  379. }
  380. /**
  381. * 获取聘级名称
  382. * @param $userId
  383. * @param null $periodNum
  384. * @return mixed
  385. * @throws \yii\db\Exception
  386. */
  387. public static function getEmpLvName($userId, $periodNum = null) {
  388. if (!$empLv = self::getEmpLv($userId, $periodNum)) {
  389. $empLv = EmployLevel::getDefaultLevelId();
  390. }
  391. return Cache::getEmpLevelConfig()[$empLv]['LEVEL_NAME'];
  392. }
  393. /**
  394. * 获取民族对应的代码
  395. * @param $str
  396. * @return int|string
  397. */
  398. public static function getNationCode($str) {
  399. $str = preg_replace('/族$/', '', $str);
  400. $allNation = \Yii::$app->params['nation'];
  401. foreach ($allNation as $key => $nation) {
  402. if ($str . '族' == $nation['name']) {
  403. return $key;
  404. }
  405. }
  406. return 0;
  407. }
  408. /**
  409. * 生成用户名
  410. * @param string $prefix
  411. * @param int $length
  412. * @return string
  413. */
  414. public static function generateUserName(string $prefix = 'HZ', int $length = 10) {
  415. $result = Tool::randomString($length, $prefix).array_rand([0,1,2,3,5,6,7,8,9],1);
  416. if(substr($result,-1) == '4'){
  417. return self::generateUserName($prefix, $length);
  418. }
  419. if (User::find()->where('USER_NAME=:USER_NAME', [':USER_NAME' => $result])->exists()) {
  420. return self::generateUserName($prefix, $length);
  421. }
  422. return $result;
  423. }
  424. /**
  425. * 新增加的 生成前端姓名里用户名
  426. * @param string $prefix
  427. * @param int $length
  428. * @return string
  429. */
  430. public static function generateWebUserName(string $prefix = 'HZ', int $length = 10) {
  431. $result = Tool::randomString($length, $prefix).array_rand([0,1,2,3,5,6,7,8,9],1);
  432. if (substr($result ,-1) == '4'){
  433. return self::generateWebUserName($prefix, $length);
  434. }
  435. if (User::find()->where('USER_NAME=:USER_NAME', [':USER_NAME' => $result])->exists()) {
  436. return self::generateWebUserName($prefix, $length);
  437. }
  438. return $result;
  439. }
  440. /**
  441. * 定位会员的子公司
  442. * @param $userArea
  443. * @return array|bool
  444. */
  445. public static function location($userArea){
  446. if(!is_array($userArea)){
  447. $userArea = json_decode($userArea, true);
  448. }
  449. if(!$userArea || !isset($userArea[0])){
  450. return false;
  451. }
  452. //获取会员的所在省份
  453. $userProvince = $userArea[0];
  454. $list = [];
  455. if(!$list){
  456. return false;
  457. }
  458. $matched = [];
  459. $userCity = isset($userArea[1]) ? intval($userArea[1]) : 0;
  460. $userCounty = isset($userArea[2]) ? intval($userArea[2]) : 0;
  461. foreach($list as $row){
  462. $manageRange = json_decode($row['MANAGE_RANGE'], true);
  463. foreach($manageRange as $manage){
  464. // $manage = ["370000","371000","371082"] or ["210000",""];
  465. $pro = isset($manage[0]) ? intval($manage[0]) : 0;
  466. $city = isset($manage[1]) ? intval($manage[1]) : 0;
  467. $county = isset($manage[2]) ? intval($manage[2]) : 0;
  468. if($userProvince == $pro && $userCity == $city && $userCounty == $county){
  469. $matched = $row;
  470. break;
  471. }
  472. if($userProvince == $pro && $userCity == $city && $county == 0){
  473. $matched = $row;
  474. break;
  475. }
  476. if($userProvince == $pro && $city == 0 && $county == 0){
  477. $matched = $row;
  478. break;
  479. }
  480. }
  481. }
  482. unset($userCounty, $userCity, $userArea, $userProvince, $list);
  483. return $matched;
  484. }
  485. /**
  486. * 通过id获取身份证号
  487. * @param $userId
  488. * @return null
  489. */
  490. public static function getIdCardByUserId($userId) {
  491. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'ID_CARD');
  492. return $user ? $user['ID_CARD'] : null;
  493. }
  494. /**
  495. * 通过id获取状态
  496. * @param $userId
  497. * @return null
  498. */
  499. public static function getStatusByUserId($userId){
  500. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'STATUS');
  501. return $user ? $user['STATUS'] : null;
  502. }
  503. /**
  504. * 通过id获取期数
  505. * @param $userId
  506. * @return null
  507. */
  508. public static function getPeriodNumByUserId($userId) {
  509. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'PERIOD_AT');
  510. return $user ? $user['PERIOD_AT'] : null;
  511. }
  512. /**
  513. * 是否实名认证
  514. * @param $userId
  515. * @return bool
  516. */
  517. public static function isVerified($userId) {
  518. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'VERIFIED');
  519. if ($user && $user['VERIFIED'] == 1) return true;
  520. return false;
  521. }
  522. /**
  523. * 根据商城设置获取密码
  524. * @param $idCard
  525. * @param $userName
  526. * @param bool $payPwd
  527. * @return bool|string
  528. */
  529. public static function passwordGenerator($idCard,$userName,$payPwd=false) {
  530. $systemConfig = Cache::getSystemConfig();
  531. if($payPwd){
  532. $config = $systemConfig['payPasswordRule'];
  533. }else{
  534. $config = $systemConfig['passwordRule'];
  535. }
  536. $rule = $config['VALUE']; //id_s_6 , id_p_8, user_name 后台设置,分别为身份证的后6位,前8位或者用户名
  537. switch ($rule) {
  538. case 'id_s_6':
  539. $password = substr($idCard, -6);
  540. break;
  541. case 'id_p_8':
  542. $password = substr($idCard, 0, 8);
  543. break;
  544. case 'user_name':
  545. $password = $userName;
  546. break;
  547. default:
  548. $password = '123456';
  549. }
  550. return $password;
  551. }
  552. // 简单身份证号校验
  553. public static function simpleIdCardCheck($idCard) {
  554. $regx = '/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/';
  555. if (!preg_match($regx, $idCard)) {
  556. return false;
  557. }
  558. return true;
  559. }
  560. /**
  561. * 当时会员级别状态等
  562. * @param $userId
  563. * @return array|null
  564. */
  565. public static function getLastInfo($userId){
  566. $data = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'LAST_DEC_LV AS DEC_LV,EMP_LV,STATUS');
  567. return $data;
  568. }
  569. /**
  570. * 是否合作会员
  571. * @param $userId
  572. * @return bool
  573. */
  574. public static function isUnion($userId){
  575. $data = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'IS_UNION');
  576. if ($data && $data['IS_UNION'] == 1) return true;
  577. return false;
  578. }
  579. }