| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace addons\Happiness\common\models;
- use addons\Aiyunuo\common\enums\AiyunuoEnum;
- use addons\Aiyunuo\common\models\AiyunuoStore;
- use common\enums\StatusEnum;
- use common\models\api\AccessToken;
- use common\models\base\User;
- use common\models\mall\Mall;
- use Yii;
- use Exception;
- use yii\helpers\Url;
- /**
- * This is the model class for table "qimall_addons_aiyunuo_store_admin".
- *
- * @property int $id
- * @property string $username 登录账号
- * @property string $account 账号名称
- * @property string $password 密码
- * @property int $mall_id 商城id
- * @property int $user_id 会员id
- * @property string $auth_key
- * @property string $avatar
- * @property string $access_token
- * @property int $admin_type 管理员类型1超级管理员2管理员3操作员
- * @property int $created_at
- * @property int $updated_at
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- */
- class HappinessAgentAdmin extends User
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_happiness_agent_admin}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'admin_type', 'created_at', 'updated_at', 'status'], 'integer'],
- [['username', 'account'], 'string', 'max' => 50],
- [['password'], 'string', 'max' => 255],
- [['auth_key', 'access_token'], 'string', 'max' => 128],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'username' => '登录账号',
- 'account' => '账号名称',
- 'password' => '密码',
- 'mall_id' => '商城id',
- 'user_id' => '会员id',
- 'auth_key' => 'Auth Key',
- 'access_token' => 'Access Token',
- 'admin_type' => '管理员类型1超级管理员2管理员3操作员',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- ];
- }
- /**YII登录机制实现接口 */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['access_token' => $token]);
- }
- public function getId()
- {
- return $this->id;
- }
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password, $this->password);
- }
- public static function login($params) {
- try {
- $username = $params['username'] ?? '';
- $password = $params['password'] ?? '';
- //获取会员手机号
- $user=User::find()->where(['mobile' => $username,'status'=>StatusEnum::ENABLED,'mall_id'=>$params['mall_id']])->asArray()->one();
- if (empty($user)){
- new Exception('会员不存在');
- }
- $where = ['status' => StatusEnum::ENABLED, 'user_id' => $user['id'], 'mall_id' => $params['mall_id']];
- $agent_list = HappinessAgent::findOne(['user_id' => $user['id'], 'mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED]);
- if (empty($agent_list)) throw new Exception('您不是代理');
- $admin = HappinessAgentAdmin::findOne($where);
- if (empty($admin)) {
- //若不存在登录账号则生成一个
- $pwd_str = substr($user['mobile'], -6);
- $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
- $admin_info = [
- 'username' => $user['mobile'],
- 'account' => $user['nickname'],
- 'password' => $pwd_hash_str,
- 'mall_id' => $params['mall_id'],
- 'admin_type' => 2,
- 'status' => StatusEnum::ENABLED,
- 'user_id' => $user['id'],
- ];
- $admin_model = new HappinessAgentAdmin();
- $admin_model->loadDefaultValues();
- $admin_model->attributes = $admin_info;
- $admin_model->updated_at = time();
- $admin_model->created_at = time();
- $res_ad = $admin_model->save();
- if (!$res_ad) throw new Exception('系统错误');
- $admin=$admin_model;
- }
- // 密码校验
- if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
- // 管理员类型处理
- $redirect_url = 'happiness/agent/store/index';
- $mall = Mall::findOne($params['mall_id']);
- Yii::$app->session->set('mall', $mall);
- // 执行登录
- $config_params = \Yii::$app->params;
- $happiness_login_remember_time = $config_params['happiness_login_remember_time'] ?? 86400 * 30;
- $happiness_login_no_remember_time = $config_params['happiness_login_no_remember_time'] ?? 3600 * 8;
- if (Yii::$app->happiness_agent_user->login($admin, $params['is_remember'] ? $happiness_login_remember_time : $happiness_login_no_remember_time)) {
- return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
- } else {
- throw new \Exception('登录失败');
- }
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 检测参数数据
- * @Author: 广东七件事 zal
- * @Date: 2020-04-08
- * @Time: 19:49
- * @param $arr
- * @param $data
- * @throws \Exception
- */
- private static function checkData($arr, $data)
- {
- foreach ($arr as $key => $item) {
- if (!isset($data[$key])) {
- throw new \Exception('请传参数' . $key . '->' . $item);
- }
- }
- }
- public static function del($params)
- {
- $tr = \Yii::$app->db->beginTransaction();
- try {
- $model = self::findOne($params);
- $model->status = StatusEnum::DELETE;
- if ($model->admin_type == '1' && $model->username == 'admin') throw new Exception('超级管理员不允许删除');
- $res = $model->save();
- if ($res === false) throw new Exception(self::getStaticError());
- $tr->commit();
- return $model;
- } catch (Exception $e) {
- $tr->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 修改密码
- *
- * @param string $pwd
- * @return boolean
- */
- public function changePwd(string $pwd)
- {
- $this->password = Yii::$app->security->generatePasswordHash($pwd);
- return $this->save();
- }
- }
|