Info.php 24 KB

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