AdminUser.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\adminUser;
  10. use Yii;
  11. use fecshop\services\Service;
  12. /**
  13. * AdminUser services. 用来给后台的用户提供数据。
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class AdminUser extends Service
  18. {
  19. public $numPerPage = 20;
  20. /**
  21. * language attribute.
  22. */
  23. protected $_lang_attr = [];
  24. protected $_modelName = '\fecshop\models\mysqldb\AdminUser';
  25. protected $_model;
  26. protected $_userFormModelName = '\fecshop\models\mysqldb\adminUser\AdminUserForm';
  27. protected $_userFormModel;
  28. protected $_userPassResetModelName = '\fecshop\models\mysqldb\adminUser\AdminUserResetPassword';
  29. protected $_userPassResetModel;
  30. public function init()
  31. {
  32. parent::init();
  33. list($this->_modelName, $this->_model) = \Yii::mapGet($this->_modelName);
  34. list($this->_userFormModelName, $this->_userFormModel) = \Yii::mapGet($this->_userFormModelName);
  35. list($this->_userPassResetModelName, $this->_userPassResetModel) = \Yii::mapGet($this->_userPassResetModelName);
  36. }
  37. /**
  38. * @param $data array
  39. * @return boolean
  40. * update current user password
  41. */
  42. public function resetCurrentPassword($data){
  43. $this->_userPassResetModel->attributes = $data;
  44. if ($this->_userPassResetModel->validate()) {
  45. $this->_userPassResetModel->updatePassword();
  46. return true;
  47. } else {
  48. $errors = $this->_userPassResetModel->errors;
  49. Yii::$service->helper->errors->addByModelErrors($errors);
  50. return false;
  51. }
  52. }
  53. public function getPrimaryKey()
  54. {
  55. return 'id';
  56. }
  57. public function getActiveStatus(){
  58. $model = $this->_model;
  59. return $model::STATUS_ACTIVE;
  60. }
  61. public function getDeleteStatus(){
  62. $model = $this->_model;
  63. return $model::STATUS_DELETED;
  64. }
  65. public function getByPrimaryKey($primaryKey)
  66. {
  67. if ($primaryKey) {
  68. $one = $this->_model->findOne($primaryKey);
  69. foreach ($this->_lang_attr as $attrName) {
  70. if (isset($one[$attrName])) {
  71. $one[$attrName] = unserialize($one[$attrName]);
  72. }
  73. }
  74. return $one;
  75. } else {
  76. return new $this->_modelName();
  77. }
  78. }
  79. /*
  80. * example filter:
  81. * [
  82. * 'numPerPage' => 20,
  83. * 'pageNum' => 1,
  84. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  85. 'where' => [
  86. ['>','price',1],
  87. ['<=','price',10]
  88. * ['sku' => 'uk10001'],
  89. * ],
  90. * 'asArray' => true,
  91. * ]
  92. */
  93. public function coll($filter = '')
  94. {
  95. $query = $this->_model->find();
  96. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  97. $coll = $query->all();
  98. if (!empty($coll)) {
  99. foreach ($coll as $k => $one) {
  100. foreach ($this->_lang_attr as $attr) {
  101. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  102. }
  103. $coll[$k] = $one;
  104. }
  105. }
  106. //var_dump($one);
  107. return [
  108. 'coll' => $coll,
  109. 'count'=> $query->limit(null)->offset(null)->count(),
  110. ];
  111. }
  112. /**
  113. * @param $data array, user form data
  114. * @param $roles array, role id array
  115. * @return boolean
  116. * 保存用户的信息,以及用户的role信息。
  117. */
  118. public function saveUserAndRole($data, $roles){
  119. $user_id = $this->save($data);
  120. if (!$user_id) {
  121. return false;
  122. }
  123. if (Yii::$service->admin->userRole->saveUserRole($user_id, $roles)) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * @param $data array, user form data
  130. * @return mix ,return save user id | null
  131. * 保存用户的信息。
  132. */
  133. public function save($data) {
  134. $primaryKey = $this->getPrimaryKey();
  135. $user_id = 0;
  136. if ($data[$primaryKey]) {
  137. $this->_userFormModel = $this->_userFormModel->findOne($data[$primaryKey]);
  138. }
  139. $this->_userFormModel->attributes = $data;
  140. if (!$data['access_token']) {
  141. $this->_userFormModel->access_token = '';
  142. }
  143. if (!$data['auth_key']) {
  144. $this->_userFormModel->auth_key = '';
  145. }
  146. if (!$data['password'] && !$data['id']) {
  147. Yii::$service->helper->errors->add("password can not empty");
  148. return null;
  149. }
  150. if ($this->_userFormModel[$primaryKey]) {
  151. if ($this->_userFormModel->validate()) {
  152. $this->_userFormModel->save();
  153. $user_id = $this->_userFormModel[$primaryKey];
  154. } else {
  155. $errors = $this->_userFormModel->errors;
  156. Yii::$service->helper->errors->addByModelErrors($errors);
  157. return null;
  158. }
  159. } else {
  160. if ($this->_userFormModel->validate()) {
  161. $this->_userFormModel->save();
  162. $user_id = Yii::$app->db->getLastInsertID();
  163. } else {
  164. $errors = $this->_userFormModel->errors;
  165. Yii::$service->helper->errors->addByModelErrors($errors);
  166. return null;
  167. }
  168. }
  169. return $user_id;
  170. }
  171. public function removeUserAndRole($ids) {
  172. $removeIds = $this->remove($ids);
  173. if (is_array($removeIds) && !empty($removeIds)) {
  174. Yii::$service->admin->userRole->deleteByUserIds($removeIds);
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. public function remove($ids){
  181. if (!$ids) {
  182. Yii::$service->helper->errors->add('remove id is empty');
  183. return null;
  184. }
  185. $removeIds = [];
  186. if (is_array($ids) && !empty($ids)) {
  187. foreach ($ids as $id) {
  188. $model = $this->_model->findOne($id);
  189. if ($model->username !== 'admin') {
  190. $model->delete();
  191. $removeIds[] = $id;
  192. } else {
  193. Yii::$service->helper->errors->add('you can not delete admin user');
  194. }
  195. }
  196. } else {
  197. $id = $ids;
  198. $model = $this->_model->findOne($id);
  199. if ($model->username !== 'admin') {
  200. $model->delete();
  201. $removeIds[] = $id;
  202. } else {
  203. Yii::$service->helper->errors->add('you can not delete admin user');
  204. }
  205. }
  206. return $removeIds;
  207. }
  208. /**
  209. * @param $ids | Int Array
  210. * @return 得到相应用户的数组。
  211. */
  212. public function getIdAndNameArrByIds($ids)
  213. {
  214. $user_coll = $this->_model->find()
  215. ->asArray()
  216. ->select(['id', 'username'])
  217. ->where([
  218. 'in', 'id', $ids,
  219. ])->all();
  220. $users = [];
  221. foreach ($user_coll as $one) {
  222. $users[$one['id']] = $one['username'];
  223. }
  224. return $users;
  225. }
  226. }