HappinessAgentAdmin.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace addons\Happiness\common\models;
  3. use addons\Aiyunuo\common\enums\AiyunuoEnum;
  4. use addons\Aiyunuo\common\models\AiyunuoStore;
  5. use common\enums\StatusEnum;
  6. use common\models\api\AccessToken;
  7. use common\models\base\User;
  8. use common\models\mall\Mall;
  9. use Yii;
  10. use Exception;
  11. use yii\helpers\Url;
  12. /**
  13. * This is the model class for table "qimall_addons_aiyunuo_store_admin".
  14. *
  15. * @property int $id
  16. * @property string $username 登录账号
  17. * @property string $account 账号名称
  18. * @property string $password 密码
  19. * @property int $mall_id 商城id
  20. * @property int $user_id 会员id
  21. * @property string $auth_key
  22. * @property string $avatar
  23. * @property string $access_token
  24. * @property int $admin_type 管理员类型1超级管理员2管理员3操作员
  25. * @property int $created_at
  26. * @property int $updated_at
  27. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  28. */
  29. class HappinessAgentAdmin extends User
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_happiness_agent_admin}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'user_id', 'admin_type', 'created_at', 'updated_at', 'status'], 'integer'],
  45. [['username', 'account'], 'string', 'max' => 50],
  46. [['password'], 'string', 'max' => 255],
  47. [['auth_key', 'access_token'], 'string', 'max' => 128],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'username' => '登录账号',
  58. 'account' => '账号名称',
  59. 'password' => '密码',
  60. 'mall_id' => '商城id',
  61. 'user_id' => '会员id',
  62. 'auth_key' => 'Auth Key',
  63. 'access_token' => 'Access Token',
  64. 'admin_type' => '管理员类型1超级管理员2管理员3操作员',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. 'status' => '状态[-1:删除;0:禁用;1启用]',
  68. ];
  69. }
  70. /**YII登录机制实现接口 */
  71. public static function findIdentity($id)
  72. {
  73. return static::findOne($id);
  74. }
  75. public static function findIdentityByAccessToken($token, $type = null)
  76. {
  77. return static::findOne(['access_token' => $token]);
  78. }
  79. public function getId()
  80. {
  81. return $this->id;
  82. }
  83. public function getAuthKey()
  84. {
  85. return $this->auth_key;
  86. }
  87. public function validateAuthKey($authKey)
  88. {
  89. return $this->getAuthKey() === $authKey;
  90. }
  91. public function validatePassword($password)
  92. {
  93. return Yii::$app->security->validatePassword($password, $this->password);
  94. }
  95. public static function login($params) {
  96. try {
  97. $username = $params['username'] ?? '';
  98. $password = $params['password'] ?? '';
  99. //获取会员手机号
  100. $user=User::find()->where(['mobile' => $username,'status'=>StatusEnum::ENABLED,'mall_id'=>$params['mall_id']])->asArray()->one();
  101. if (empty($user)){
  102. new Exception('会员不存在');
  103. }
  104. $where = ['status' => StatusEnum::ENABLED, 'user_id' => $user['id'], 'mall_id' => $params['mall_id']];
  105. $agent_list = HappinessAgent::findOne(['user_id' => $user['id'], 'mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED]);
  106. if (empty($agent_list)) throw new Exception('您不是代理');
  107. $admin = HappinessAgentAdmin::findOne($where);
  108. if (empty($admin)) {
  109. //若不存在登录账号则生成一个
  110. $pwd_str = substr($user['mobile'], -6);
  111. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  112. $admin_info = [
  113. 'username' => $user['mobile'],
  114. 'account' => $user['nickname'],
  115. 'password' => $pwd_hash_str,
  116. 'mall_id' => $params['mall_id'],
  117. 'admin_type' => 2,
  118. 'status' => StatusEnum::ENABLED,
  119. 'user_id' => $user['id'],
  120. ];
  121. $admin_model = new HappinessAgentAdmin();
  122. $admin_model->loadDefaultValues();
  123. $admin_model->attributes = $admin_info;
  124. $admin_model->updated_at = time();
  125. $admin_model->created_at = time();
  126. $res_ad = $admin_model->save();
  127. if (!$res_ad) throw new Exception('系统错误');
  128. $admin=$admin_model;
  129. }
  130. // 密码校验
  131. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  132. // 管理员类型处理
  133. $redirect_url = 'happiness/agent/store/index';
  134. $mall = Mall::findOne($params['mall_id']);
  135. Yii::$app->session->set('mall', $mall);
  136. // 执行登录
  137. $config_params = \Yii::$app->params;
  138. $happiness_login_remember_time = $config_params['happiness_login_remember_time'] ?? 86400 * 30;
  139. $happiness_login_no_remember_time = $config_params['happiness_login_no_remember_time'] ?? 3600 * 8;
  140. if (Yii::$app->happiness_agent_user->login($admin, $params['is_remember'] ? $happiness_login_remember_time : $happiness_login_no_remember_time)) {
  141. return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
  142. } else {
  143. throw new \Exception('登录失败');
  144. }
  145. } catch (Exception $e) {
  146. self::setStaticError($e->getMessage());
  147. return false;
  148. }
  149. }
  150. /**
  151. * 检测参数数据
  152. * @Author: 广东七件事 zal
  153. * @Date: 2020-04-08
  154. * @Time: 19:49
  155. * @param $arr
  156. * @param $data
  157. * @throws \Exception
  158. */
  159. private static function checkData($arr, $data)
  160. {
  161. foreach ($arr as $key => $item) {
  162. if (!isset($data[$key])) {
  163. throw new \Exception('请传参数' . $key . '->' . $item);
  164. }
  165. }
  166. }
  167. public static function del($params)
  168. {
  169. $tr = \Yii::$app->db->beginTransaction();
  170. try {
  171. $model = self::findOne($params);
  172. $model->status = StatusEnum::DELETE;
  173. if ($model->admin_type == '1' && $model->username == 'admin') throw new Exception('超级管理员不允许删除');
  174. $res = $model->save();
  175. if ($res === false) throw new Exception(self::getStaticError());
  176. $tr->commit();
  177. return $model;
  178. } catch (Exception $e) {
  179. $tr->rollBack();
  180. self::$static_error = $e->getMessage();
  181. return false;
  182. }
  183. }
  184. /**
  185. * 修改密码
  186. *
  187. * @param string $pwd
  188. * @return boolean
  189. */
  190. public function changePwd(string $pwd)
  191. {
  192. $this->password = Yii::$app->security->generatePasswordHash($pwd);
  193. return $this->save();
  194. }
  195. }