Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

140 rader
3.2 KiB

  1. <?php
  2. namespace common\models;
  3. use yii\db\ActiveRecord;
  4. /**
  5. * This is the model class for table "fx_user".
  6. *
  7. * @property integer $uid
  8. * @property string $openid
  9. * @property string $nickname
  10. * @property string $phone
  11. * @property string $reg_time
  12. * @property string $last_time
  13. * @property integer $status
  14. */
  15. class ShUser extends ActiveRecord
  16. {
  17. /**
  18. * @inheritdoc
  19. */
  20. public static function tableName()
  21. {
  22. return 'sh_user';
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['status'], 'integer'],
  31. [['reg_time', 'last_login'], 'safe'],
  32. [['user_name'], 'string', 'max' => 100],
  33. [['phone'], 'string', 'max' => 20],
  34. [['head_img'], 'string', 'max' => 255],
  35. [['phone'], 'unique'],
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'uid' => 'Uid',
  45. 'user_name' => 'User Name',
  46. 'phone' => 'Phone',
  47. 'head_img' => 'Head Img',
  48. 'status' => 'Status',
  49. 'reg_time' => 'Reg Time',
  50. 'last_login' => 'Last Login',
  51. ];
  52. }
  53. /**
  54. * Des:获取用户信息
  55. * Name: getUserInfoByPhone
  56. * @param $phone
  57. * @return array
  58. * @author 倪宗锋
  59. */
  60. public function getUserInfoByPhone($phone)
  61. {
  62. $userInfo = self::find()
  63. ->where(['=', 'phone', $phone])
  64. ->asArray(true)
  65. ->one();
  66. if (empty($userInfo)) {
  67. return [];
  68. }
  69. return $userInfo;
  70. }
  71. /**
  72. * Des:添加用户信息
  73. * Name: addUser
  74. * @param $params
  75. * @return bool
  76. * @author 倪宗锋
  77. */
  78. public function addUser($params)
  79. {
  80. $this->setAttributes($params);
  81. $return = $this->insert(true);
  82. return $return;
  83. }
  84. /**
  85. * Des:根据手机号获取用户ID 没有则注册一个
  86. * Name: getUidByPhone
  87. * @param $phone
  88. * @return int
  89. * @author 倪宗锋
  90. */
  91. public function getUidByPhone($phone)
  92. {
  93. $info = $this->getUserInfoByPhone($phone);
  94. if (empty($info['uid'])) {
  95. $strArr = range('a', 'z');//生成数组a-z
  96. $strArr = array_merge($strArr, $strArr, $strArr);
  97. shuffle($strArr);//打乱数组顺序
  98. $str = substr(implode('', $strArr), 0, 3);//将打乱后的数组拼接成字符串 并截取前三个字符
  99. $data = [
  100. 'user_name' => 'zz_' . substr($phone, 0, 6) . $str,
  101. 'phone' => $phone
  102. ];
  103. $this->addUser($data);
  104. $info = $this->getUserInfoByPhone($phone);
  105. }
  106. if (empty($info['uid'])) {
  107. return 0;
  108. }
  109. return $info['uid'];
  110. }
  111. /**
  112. * Des:获取用户信息
  113. * Name: getUserInfoByPhone
  114. * @param $uid )
  115. * @return array
  116. * @author 倪宗锋
  117. */
  118. public function getUserInfoByUid($uid)
  119. {
  120. $userInfo = self::find()
  121. ->where(['=', 'uid', $uid])
  122. ->asArray(true)
  123. ->one();
  124. if (empty($userInfo)) {
  125. return [];
  126. }
  127. return $userInfo;
  128. }
  129. }