User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. ->find();
  82. if (empty($useInfo) || $useInfo['is_delete']) {
  83. $this->error = '登录失败, 该用户不存在或已删除';
  84. return false;
  85. }
  86. // 验证密码是否正确
  87. if (!password_verify($data['password'], $useInfo['password'])) {
  88. $this->error = '登录失败, 用户名或密码错误';
  89. return false;
  90. }
  91. return $useInfo;
  92. }
  93. /**
  94. * 获取用户列表
  95. * @param array $param
  96. * @return \think\Paginator
  97. * @throws \think\db\exception\DbException
  98. */
  99. public function getList($param = [])
  100. {
  101. // 查询模型
  102. $query = $this->getNewQuery();
  103. // 查询参数
  104. $params = $this->setQueryDefaultValue($param, ['search' => '']);
  105. // 关键词搜索
  106. !empty($params['search']) && $query->where('user_name|real_name', 'like', "%{$params['search']}%");
  107. // 查询列表记录
  108. $list = $query->with(['role'])
  109. ->where('is_delete', '=', '0')
  110. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  111. ->paginate(15);
  112. // 整理所有角色id
  113. foreach ($list as &$item) {
  114. $item['roleIds'] = helper::getArrayColumn($item['role'], 'role_id');
  115. }
  116. return $list;
  117. }
  118. /**
  119. * 新增记录
  120. * @param array $data
  121. * @return bool
  122. */
  123. public function add(array $data)
  124. {
  125. if (self::checkExist($data['user_name'])) {
  126. $this->error = '用户名已存在';
  127. return false;
  128. }
  129. if ($data['password'] !== $data['password_confirm']) {
  130. $this->error = '确认密码不正确';
  131. return false;
  132. }
  133. if (empty($data['roles'])) {
  134. $this->error = '请选择所属角色';
  135. return false;
  136. }
  137. // 整理数据
  138. $data['password'] = encryption_hash($data['password']);
  139. $data['store_id'] = self::$storeId;
  140. $data['is_super'] = 0;
  141. // 事务处理
  142. $this->transaction(function () use ($data) {
  143. // 新增管理员记录
  144. $this->save($data);
  145. // 新增角色关系记录
  146. UserRole::increased((int)$this['store_user_id'], $data['roles']);
  147. });
  148. return true;
  149. }
  150. /**
  151. * 更新记录
  152. * @param array $data
  153. * @return bool
  154. */
  155. public function edit(array $data)
  156. {
  157. if ($this['user_name'] !== $data['user_name']
  158. && self::checkExist($data['user_name'])) {
  159. $this->error = '用户名已存在';
  160. return false;
  161. }
  162. if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
  163. $this->error = '确认密码不正确';
  164. return false;
  165. }
  166. if (empty($data['roles']) && !$this['is_super']) {
  167. $this->error = '请选择所属角色';
  168. return false;
  169. }
  170. if (!empty($data['password'])) {
  171. $data['password'] = encryption_hash($data['password']);
  172. } else {
  173. unset($data['password']);
  174. }
  175. $this->transaction(function () use ($data) {
  176. // 更新管理员记录
  177. $this->save($data);
  178. // 更新角色关系记录
  179. !$this['is_super'] && UserRole::updates((int)$this['store_user_id'], $data['roles']);
  180. });
  181. return true;
  182. }
  183. /**
  184. * 软删除
  185. * @return false|int
  186. */
  187. public function setDelete()
  188. {
  189. if ($this['is_super']) {
  190. $this->error = '超级管理员不允许删除';
  191. return false;
  192. }
  193. return $this->transaction(function () {
  194. // 删除对应的角色关系
  195. UserRole::deleteAll([['store_user_id', '=', (int)$this['store_user_id']]]);
  196. return $this->save(['is_delete' => 1]);
  197. });
  198. }
  199. /**
  200. * 更新当前管理员信息
  201. * @param array $data
  202. * @return bool
  203. */
  204. public function renew(array $data)
  205. {
  206. if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
  207. $this->error = '确认密码不正确';
  208. return false;
  209. }
  210. if ($this['user_name'] !== $data['user_name']
  211. && self::checkExist($data['user_name'])) {
  212. $this->error = '用户名已存在';
  213. return false;
  214. }
  215. !empty($data['password']) && $data['password'] = encryption_hash($data['password']);
  216. // 更新管理员信息
  217. if ($this->save($data) === false) {
  218. return false;
  219. }
  220. // 更新登录状态
  221. StoreUserService::update($this->toArray());
  222. return true;
  223. }
  224. }