|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
-
- namespace common\models;
-
- use yii\db\ActiveRecord;
-
- /**
- * This is the model class for table "fx_user".
- *
- * @property integer $uid
- * @property string $openid
- * @property string $nickname
- * @property string $phone
- * @property string $reg_time
- * @property string $last_time
- * @property integer $status
- */
- class ShUser extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'sh_user';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['status'], 'integer'],
- [['reg_time', 'last_login'], 'safe'],
- [['user_name'], 'string', 'max' => 100],
- [['phone'], 'string', 'max' => 20],
- [['head_img'], 'string', 'max' => 255],
- [['phone'], 'unique'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'uid' => 'Uid',
- 'user_name' => 'User Name',
- 'phone' => 'Phone',
- 'head_img' => 'Head Img',
- 'status' => 'Status',
- 'reg_time' => 'Reg Time',
- 'last_login' => 'Last Login',
- ];
- }
-
- /**
- * Des:获取用户信息
- * Name: getUserInfoByPhone
- * @param $phone
- * @return array
- * @author 倪宗锋
- */
- public function getUserInfoByPhone($phone)
- {
- $userInfo = self::find()
- ->where(['=', 'phone', $phone])
- ->asArray(true)
- ->one();
- if (empty($userInfo)) {
- return [];
- }
- return $userInfo;
- }
-
- /**
- * Des:添加用户信息
- * Name: addUser
- * @param $params
- * @return bool
- * @author 倪宗锋
- */
- public function addUser($params)
- {
- $this->setAttributes($params);
- $return = $this->insert(true);
- return $return;
- }
-
-
- /**
- * Des:根据手机号获取用户ID 没有则注册一个
- * Name: getUidByPhone
- * @param $phone
- * @return int
- * @author 倪宗锋
- */
- public function getUidByPhone($phone)
- {
- $info = $this->getUserInfoByPhone($phone);
- if (empty($info['uid'])) {
- $strArr = range('a', 'z');//生成数组a-z
- $strArr = array_merge($strArr, $strArr, $strArr);
- shuffle($strArr);//打乱数组顺序
- $str = substr(implode('', $strArr), 0, 3);//将打乱后的数组拼接成字符串 并截取前三个字符
- $data = [
- 'user_name' => 'zz_' . substr($phone, 0, 6) . $str,
- 'phone' => $phone
- ];
- $this->addUser($data);
- $info = $this->getUserInfoByPhone($phone);
- }
- if (empty($info['uid'])) {
- return 0;
- }
- return $info['uid'];
- }
-
- /**
- * Des:获取用户信息
- * Name: getUserInfoByPhone
- * @param $uid )
- * @return array
- * @author 倪宗锋
- */
- public function getUserInfoByUid($uid)
- {
- $userInfo = self::find()
- ->where(['=', 'uid', $uid])
- ->asArray(true)
- ->one();
- if (empty($userInfo)) {
- return [];
- }
- return $userInfo;
- }
- }
|