HappinessStoreAdmin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 string $store_id 门店id
  21. * @property int $user_id 会员id
  22. * @property string $auth_key
  23. * @property string $avatar
  24. * @property string $access_token
  25. * @property int $admin_type 管理员类型1超级管理员2管理员3操作员
  26. * @property int $created_at
  27. * @property int $updated_at
  28. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  29. */
  30. class HappinessStoreAdmin extends User
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_happiness_store_admin}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'user_id', 'admin_type', 'created_at', 'updated_at', 'status', 'store_id'], 'integer'],
  46. [['username', 'account'], 'string', 'max' => 50],
  47. [['password'], 'string', 'max' => 255],
  48. [['auth_key', 'access_token'], 'string', 'max' => 128],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'username' => '登录账号',
  59. 'account' => '账号名称',
  60. 'password' => '密码',
  61. 'mall_id' => '商城id',
  62. 'store_id' => '门店id',
  63. 'user_id' => '会员id',
  64. 'auth_key' => 'Auth Key',
  65. 'access_token' => 'Access Token',
  66. 'admin_type' => '管理员类型1超级管理员2管理员3操作员',
  67. 'created_at' => 'Created At',
  68. 'updated_at' => 'Updated At',
  69. 'status' => '状态[-1:删除;0:禁用;1启用]',
  70. ];
  71. }
  72. /**YII登录机制实现接口 */
  73. public static function findIdentity($id)
  74. {
  75. return static::findOne($id);
  76. }
  77. public static function findIdentityByAccessToken($token, $type = null)
  78. {
  79. return static::findOne(['access_token' => $token]);
  80. }
  81. public function getId()
  82. {
  83. return $this->id;
  84. }
  85. public function getAuthKey()
  86. {
  87. return $this->auth_key;
  88. }
  89. public function validateAuthKey($authKey)
  90. {
  91. return $this->getAuthKey() === $authKey;
  92. }
  93. public function validatePassword($password)
  94. {
  95. return Yii::$app->security->validatePassword($password, $this->password);
  96. }
  97. public static function login($params)
  98. {
  99. try {
  100. $username = $params['username'] ?? '';
  101. $password = $params['password'] ?? '';
  102. $where = ['status' => StatusEnum::ENABLED, 'username' => $username, 'mall_id' => $params['mall_id']];
  103. $admin = HappinessStoreAdmin::findOne($where);
  104. if (empty($admin)) throw new Exception('登录账号不存在');
  105. $store = HappinessStore::findOne(['id' => $admin['store_id'], 'mall_id' => $params['mall_id']]);
  106. if (empty($store) || $store->status == StatusEnum::DELETE) throw new Exception('门店不存在');
  107. if ($store->status == StatusEnum::DISABLED) throw new Exception('该门店已被禁用');
  108. // if (!empty($store['expire_time']) && time() > $store['expire_time']) {
  109. // throw new Exception('您的账号已过期');
  110. // }
  111. // 密码校验
  112. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  113. // 管理员类型处理
  114. $redirect_url = 'happiness/client/index/index';
  115. Yii::$app->session->set('happiness_store', $store);
  116. $mall = Mall::findOne($params['mall_id']);
  117. Yii::$app->session->set('mall', $mall);
  118. // 执行登录
  119. $config_params = \Yii::$app->params;
  120. $happiness_login_remember_time = $config_params['happiness_login_remember_time'] ?? 86400 * 30;
  121. $happiness_login_no_remember_time = $config_params['happiness_login_no_remember_time'] ?? 3600 * 8;
  122. if (Yii::$app->happiness_store_user->login($admin, $params['is_remember'] ? $happiness_login_remember_time : $happiness_login_no_remember_time)) {
  123. return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
  124. } else {
  125. throw new \Exception('登录失败');
  126. }
  127. } catch (Exception $e) {
  128. self::setStaticError($e->getMessage());
  129. return false;
  130. }
  131. }
  132. /**
  133. * 检测参数数据
  134. * @Author: 广东七件事 zal
  135. * @Date: 2020-04-08
  136. * @Time: 19:49
  137. * @param $arr
  138. * @param $data
  139. * @throws \Exception
  140. */
  141. private static function checkData($arr, $data)
  142. {
  143. foreach ($arr as $key => $item) {
  144. if (!isset($data[$key])) {
  145. throw new \Exception('请传参数' . $key . '->' . $item);
  146. }
  147. }
  148. }
  149. public static function del($params)
  150. {
  151. $tr = \Yii::$app->db->beginTransaction();
  152. try {
  153. $model = self::findOne($params);
  154. $model->status = StatusEnum::DELETE;
  155. if ($model->admin_type == '1' && $model->username == 'admin') throw new Exception('超级管理员不允许删除');
  156. $res = $model->save();
  157. if ($res === false) throw new Exception(self::getStaticError());
  158. $tr->commit();
  159. return $model;
  160. } catch (Exception $e) {
  161. $tr->rollBack();
  162. self::$static_error = $e->getMessage();
  163. return false;
  164. }
  165. }
  166. public static function loginByH5($params)
  167. {
  168. try {
  169. $username = $params['username'] ?? '';
  170. $password = $params['password'] ?? '';
  171. $where = ['status' => StatusEnum::ENABLED, 'username' => $username, 'mall_id' => $params['mall_id']];
  172. $admin = HappinessStoreAdmin::findOne($where);
  173. if (empty($admin)) throw new Exception('登录账号不存在');
  174. // 密码校验
  175. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  176. $store = HappinessStore::findOne(['id' => $admin['store_id']]);
  177. if (empty($store['status'])) {
  178. throw new Exception('门店已禁用');
  179. }
  180. // if ($store['expire_time'] > 0 && $store['expire_time'] < time()) {
  181. // throw new Exception('门店已过期');
  182. // }
  183. $where = [];
  184. $where[] = ['mall_id' => $params['mall_id']];
  185. $where[] = ['id' => $admin['user_id']];
  186. $where[] = ['status' => StatusEnum::ENABLED];
  187. $user = \common\models\user\User::getUser($where);
  188. if (empty($user)) throw new Exception('门店会员不存在');
  189. $token = Yii::$app->services->apiAccessToken->getAccessToken($user, 'happiness');
  190. if (empty($token['access_token'])) throw new Exception('登录错误');
  191. $token['access_happiness_token'] = $token['access_token'];
  192. $api_access_token = Yii::$app->services->apiAccessToken->getUserToken($user, 'api');
  193. $token['access_token'] = $api_access_token['access_token'];
  194. AccessToken::updateAll(['status' => StatusEnum::ENABLED], ['access_token' => $api_access_token['access_token']]);
  195. return $token;
  196. } catch (Exception $e) {
  197. self::setStaticError($e->getMessage());
  198. return false;
  199. }
  200. }
  201. /**
  202. * 获取门店ID获取管理员
  203. *
  204. * @param integer $mall_id
  205. * @param integer $store_id
  206. * @return static|null
  207. */
  208. public static function getAdminByStoreId(int $mall_id, int $store_id)
  209. {
  210. return static::find()
  211. ->where(compact('mall_id', 'store_id'))
  212. ->one();
  213. }
  214. /**
  215. * 修改密码
  216. *
  217. * @param string $pwd
  218. * @return boolean
  219. */
  220. public function changePwd(string $pwd)
  221. {
  222. $this->password = Yii::$app->security->generatePasswordHash($pwd);
  223. return $this->save();
  224. }
  225. }