User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model\store;
  13. use app\common\library\helper;
  14. use app\common\model\store\User as StoreUserModel;
  15. use app\admin\service\store\User as StoreUserService;
  16. /**
  17. * 商家用户模型
  18. * Class StoreUser
  19. * @package app\store\model
  20. */
  21. class User extends StoreUserModel
  22. {
  23. /**
  24. * 隐藏的字段
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'password',
  29. ];
  30. // 用户登录token
  31. private $token;
  32. /**
  33. * 商家用户登录
  34. * @param array $data
  35. * @return array|bool|null|\think\Model
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function login(array $data)
  41. {
  42. // 验证用户名密码是否正确
  43. if (!$userInfo = $this->getUserInfoByLogin($data)) {
  44. return false;
  45. }
  46. // 验证商城状态是否正常
  47. if (empty($userInfo['store']) || $userInfo['store']['is_delete']) {
  48. $this->error = '登录失败, 未找到当前商城信息';
  49. return false;
  50. }
  51. if ($userInfo['store']['is_recycle']) {
  52. $this->error = '登录失败, 当前商城已删除';
  53. return false;
  54. }
  55. // 记录登录状态, 并记录token
  56. $this->token = StoreUserService::login($userInfo->toArray());
  57. return $userInfo;
  58. }
  59. /**
  60. * 返回生成的token
  61. * @return mixed
  62. */
  63. public function getToken()
  64. {
  65. return $this->token;
  66. }
  67. /**
  68. * 获取登录用户信息
  69. * @param array $data
  70. * @return array|bool|null|\think\Model
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. private function getUserInfoByLogin(array $data)
  76. {
  77. // 用户信息
  78. $useInfo = static::withoutGlobalScope()
  79. ->with(['store'])
  80. ->where('user_name', '=', trim($data['username']))
  81. ->where('is_delete', '=', 0)
  82. ->find();
  83. if (empty($useInfo) || $useInfo['is_delete']) {
  84. $this->error = '登录失败, 该用户不存在或已删除';
  85. return false;
  86. }
  87. // 验证密码是否正确
  88. if (!password_verify($data['password'], $useInfo['password'])) {
  89. $this->error = '登录失败, 用户名或密码错误';
  90. return false;
  91. }
  92. return $useInfo;
  93. }
  94. /**
  95. * 获取用户列表
  96. * @param array $param
  97. * @return \think\Paginator
  98. * @throws \think\db\exception\DbException
  99. */
  100. public function getList($param = [])
  101. {
  102. // 查询模型
  103. $query = $this->getNewQuery();
  104. // 查询参数
  105. $params = $this->setQueryDefaultValue($param, ['search' => '']);
  106. // 关键词搜索
  107. !empty($params['search']) && $query->where('user_name|real_name', 'like', "%{$params['search']}%");
  108. // 查询列表记录
  109. $list = $query->with(['role'])
  110. ->where('is_delete', '=', '0')
  111. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  112. ->paginate(15);
  113. // 整理所有角色id
  114. foreach ($list as &$item) {
  115. $item['roleIds'] = helper::getArrayColumn($item['role'], 'role_id');
  116. }
  117. return $list;
  118. }
  119. /**
  120. * 新增记录
  121. * @param array $data
  122. * @return bool
  123. */
  124. public function add(array $data)
  125. {
  126. $data['user_name'] = strtolower($data['user_name']);
  127. if (self::checkExist($data['user_name'])) {
  128. $this->error = '用户名已存在';
  129. return false;
  130. }
  131. if ($data['password'] !== $data['password_confirm']) {
  132. $this->error = '确认密码不正确';
  133. return false;
  134. }
  135. if (empty($data['roles'])) {
  136. $this->error = '请选择所属角色';
  137. return false;
  138. }
  139. // 整理数据
  140. $data['password'] = encryption_hash($data['password']);
  141. $data['store_id'] = self::$storeId;
  142. $data['is_super'] = 0;
  143. // 事务处理
  144. $this->transaction(function () use ($data) {
  145. // 新增管理员记录
  146. $this->save($data);
  147. // 新增角色关系记录
  148. UserRole::increased((int)$this['store_user_id'], $data['roles']);
  149. });
  150. return true;
  151. }
  152. /**
  153. * 更新记录
  154. * @param array $data
  155. * @return bool
  156. */
  157. public function edit(array $data)
  158. {
  159. $data['user_name'] = strtolower($data['user_name']);
  160. if ($this['user_name'] !== $data['user_name']
  161. && self::checkExist($data['user_name'])) {
  162. $this->error = '用户名已存在';
  163. return false;
  164. }
  165. if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
  166. $this->error = '确认密码不正确';
  167. return false;
  168. }
  169. if (empty($data['roles']) && !$this['is_super']) {
  170. $this->error = '请选择所属角色';
  171. return false;
  172. }
  173. if (!empty($data['password'])) {
  174. $data['password'] = encryption_hash($data['password']);
  175. } else {
  176. unset($data['password']);
  177. }
  178. $this->transaction(function () use ($data) {
  179. // 更新管理员记录
  180. $this->save($data);
  181. // 更新角色关系记录
  182. !$this['is_super'] && UserRole::updates((int)$this['store_user_id'], $data['roles']);
  183. });
  184. return true;
  185. }
  186. /**
  187. * 软删除
  188. * @return false|int
  189. */
  190. public function setDelete()
  191. {
  192. if ($this['is_super']) {
  193. $this->error = '超级管理员不允许删除';
  194. return false;
  195. }
  196. return $this->transaction(function () {
  197. // 删除对应的角色关系
  198. UserRole::deleteAll([['store_user_id', '=', (int)$this['store_user_id']]]);
  199. return $this->save(['is_delete' => 1]);
  200. });
  201. }
  202. /**
  203. * 更新当前管理员信息
  204. * @param array $data
  205. * @return bool
  206. */
  207. public function renew(array $data)
  208. {
  209. if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
  210. $this->error = '确认密码不正确';
  211. return false;
  212. }
  213. if ($this['user_name'] !== $data['user_name']
  214. && self::checkExist($data['user_name'])) {
  215. $this->error = '用户名已存在';
  216. return false;
  217. }
  218. !empty($data['password']) && $data['password'] = encryption_hash($data['password']);
  219. // 更新管理员信息
  220. if ($this->save($data) === false) {
  221. return false;
  222. }
  223. // 更新登录状态
  224. StoreUserService::update($this->toArray());
  225. return true;
  226. }
  227. }