Info.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\ActiveRecord;
  10. use common\helpers\Cache;
  11. use common\helpers\Tool;
  12. use common\models\DeclarationLevel;
  13. use common\models\EmployLevel;
  14. use common\models\Region;
  15. use common\models\User;
  16. use common\models\UserInfo;
  17. use yii\base\Exception;
  18. class Info {
  19. /**
  20. * @param $userName
  21. * @return mixed
  22. */
  23. public static function getUserIdByUserName($userName) {
  24. $user = UserInfo::findOneAsArray('USER_NAME=:USER_NAME AND DELETED=0', [':USER_NAME' => $userName], 'USER_ID');
  25. return $user ? $user['USER_ID'] : null;
  26. }
  27. /**
  28. * @param $userId
  29. * @return mixed|null
  30. */
  31. public static function getUserNameByUserId($userId) {
  32. $user = User::findOneAsArray('ID=:ID AND DELETED=0', [':ID' => $userId], 'USER_NAME');
  33. return $user ? $user['USER_NAME'] : null;
  34. // $user = UserInfo::findOneAsArray('USER_ID=:USER_ID AND DELETED=0', [':USER_ID' => $userId], 'USER_NAME');
  35. // return $user ? $user['USER_NAME'] : null;
  36. }
  37. /**
  38. * @param $userId
  39. * @return null
  40. */
  41. public static function getUserRealNameByUserId($userId) {
  42. $user = User::findOneAsArray('ID=:ID AND DELETED=0', [':ID' => $userId], 'REAL_NAME');
  43. return $user ? $user['REAL_NAME'] : null;
  44. }
  45. /**
  46. * @param $userId
  47. * @return array|null
  48. */
  49. public static function getBaseUserById($userId) {
  50. return User::findOneAsArray('ID=:ID', [':ID' => $userId], 'ID,USER_NAME,REAL_NAME');
  51. }
  52. /**
  53. * @param $username
  54. * @return array|null
  55. */
  56. public static function getBaseUserByUserName($username) {
  57. return User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $username]);
  58. }
  59. /**
  60. * @param $userId
  61. * @return array|\yii\db\ActiveRecord|null
  62. * @throws Exception
  63. */
  64. public static function baseInfoNowPeriod($userId) {
  65. $data = User::find()->where('ID=:ID', [':ID' => $userId])->asArray()->one();
  66. if (!$data) throw new Exception('会员不存在'.$userId);
  67. $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();
  68. if ($infoData) {
  69. $data = array_merge($data, $infoData);
  70. }
  71. if (!$data['DEC_LV']) $data['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  72. if (!$data['LAST_DEC_LV']) $data['LAST_DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  73. if (!$data['EMP_LV']) $data['EMP_LV'] = EmployLevel::getDefaultLevelId();
  74. return $data;
  75. }
  76. /**
  77. * @param $userId
  78. * @param $periodNum
  79. * @return array|\yii\db\ActiveRecord|null
  80. * @throws Exception
  81. */
  82. public static function baseInfo($userId, $periodNum = null) {
  83. if($periodNum == null){
  84. return self::baseInfoNowPeriod($userId);
  85. } else {
  86. $tableName = '{{%USER_ALL_'.$periodNum.'}}';
  87. if(ActiveRecord::isExistsTable($tableName, 'dbNetPoint')){
  88. if($data = \Yii::$app->dbNetPoint->createCommand("SELECT * FROM $tableName WHERE ID=:ID")->bindValues([':ID'=>$userId])->queryOne()) {
  89. if (!$data['DEC_LV']) $data['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  90. if (!$data['LAST_DEC_LV']) $data['LAST_DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  91. if (!$data['EMP_LV']) $data['EMP_LV'] = EmployLevel::getDefaultLevelId();
  92. if (!$data['HIGHEST_EMP_LV']) $data['HIGHEST_EMP_LV'] = EmployLevel::getDefaultLevelId();
  93. return $data;
  94. }else{
  95. return self::baseInfoNowPeriod($userId);
  96. }
  97. } else {
  98. return self::baseInfoNowPeriod($userId);
  99. }
  100. }
  101. }
  102. /**
  103. * @param $userId
  104. * @param $periodNum
  105. * @return array|\yii\db\ActiveRecord|null
  106. * @throws Exception
  107. */
  108. public static function baseInfoZh($userId, $periodNum = null) {
  109. $baseInfo = self::baseInfo($userId, $periodNum);
  110. $baseInfo['STATUS_NAME'] = \Yii::$app->params['userStatus'][$baseInfo['STATUS']]['label'];
  111. $baseInfo['LOGIN_STATUS_NAME'] = $baseInfo['ALLOW_LOGIN'] == 0 ? '禁止登录' : '允许登录';
  112. $baseInfo['PROVINCE_NAME'] = $baseInfo['PROVINCE'] ? Region::getCnName($baseInfo['PROVINCE']) : '';
  113. $baseInfo['CITY_NAME'] = $baseInfo['CITY'] ? Region::getCnName($baseInfo['CITY']) : '';
  114. $baseInfo['COUNTY_NAME'] = $baseInfo['COUNTY'] ? Region::getCnName($baseInfo['COUNTY']) : '';
  115. $baseInfo['SYSTEM_NAME'] = '';
  116. $baseInfo['DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['DEC_LV']]['LEVEL_NAME'];
  117. $baseInfo['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$baseInfo['LAST_DEC_LV']]['LEVEL_NAME'];
  118. $baseInfo['EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$baseInfo['EMP_LV']]['LEVEL_NAME'];
  119. $baseInfo['HIGHEST_EMP_LV_NAME'] = '';
  120. $baseInfo['DEC_ROLE_NAME'] = '无';
  121. $baseInfo['NATION_NAME'] = \Yii::$app->params['nation'][$baseInfo['NATION']]['name']??'';
  122. return $baseInfo;
  123. }
  124. /**
  125. * 获取民族对应的代码
  126. * @param $str
  127. * @return int|string
  128. */
  129. public static function getNationCode($str) {
  130. $str = preg_replace('/族$/', '', $str);
  131. $allNation = \Yii::$app->params['nation'];
  132. foreach ($allNation as $key => $nation) {
  133. if ($str . '族' == $nation['name']) {
  134. return $key;
  135. }
  136. }
  137. return 0;
  138. }
  139. /**
  140. * @param string $prefix
  141. * @param int $length
  142. * @return string
  143. */
  144. public static function generateUserName(string $prefix = 'HZ', int $length = 10) {
  145. $result = Tool::randomString($length, $prefix).array_rand([0,1,2,3,5,6,7,8,9],1);
  146. if (User::find()->where('USER_NAME=:USER_NAME', [':USER_NAME' => $result])->exists()) {
  147. return self::generateUserName($prefix, $length);
  148. }
  149. return $result;
  150. }
  151. /**
  152. * @param $userId
  153. * @return null
  154. */
  155. public static function getIdCardByUserId($userId) {
  156. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'ID_CARD');
  157. return $user ? $user['ID_CARD'] : null;
  158. }
  159. /**
  160. * @param $userId
  161. * @return null
  162. */
  163. public static function getStatusByUserId($userId){
  164. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'STATUS');
  165. return $user ? $user['STATUS'] : null;
  166. }
  167. /**
  168. * @param $userId
  169. * @return bool
  170. */
  171. public static function isVerified($userId) {
  172. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'VERIFIED');
  173. if ($user && $user['VERIFIED'] == 1) return true;
  174. return false;
  175. }
  176. /**
  177. * @param $idCard
  178. * @param $userName
  179. * @param bool $payPwd
  180. * @return bool|string
  181. */
  182. public static function passwordGenerator($idCard,$userName,$payPwd=false) {
  183. $systemConfig = Cache::getSystemConfig();
  184. if($payPwd){
  185. $config = $systemConfig['payPasswordRule'];
  186. }else{
  187. $config = $systemConfig['passwordRule'];
  188. }
  189. $rule = $config['VALUE'];
  190. switch ($rule) {
  191. case 'id_s_6':
  192. $password = substr($idCard, -6);
  193. break;
  194. case 'id_p_8':
  195. $password = substr($idCard, 0, 8);
  196. break;
  197. case 'user_name':
  198. $password = $userName;
  199. break;
  200. default:
  201. $password = '123456';
  202. }
  203. return $password;
  204. }
  205. /**
  206. * @param $userId
  207. * @return array|null
  208. */
  209. public static function getLastInfo($userId){
  210. $data = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'LAST_DEC_LV AS DEC_LV,EMP_LV,STATUS');
  211. return $data;
  212. }
  213. }