Manageredit.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 fecadmin\block\account;
  10. use Yii;
  11. use fec\helpers\CRequest;
  12. use fec\helpers\CUrl;
  13. use fec\helpers\CDB;
  14. use fec\helpers\CModel;
  15. use fecadmin\models\AdminUser\AdminUserForm;
  16. use fecadmin\models\AdminRole;
  17. use fecadmin\models\AdminUserRole;
  18. /**
  19. * @author Terry Zhao <2358269014@qq.com>
  20. * @since 1.0
  21. */
  22. class Manageredit{
  23. public $_param;
  24. public $_paramKey;
  25. public $_one;
  26. public function __construct(){
  27. $this->_paramKey = 'id';
  28. }
  29. # 初始化数据
  30. public function initParam(){
  31. $val = $this->_param[$this->_paramKey];
  32. if($val){
  33. $this->_one = AdminUserForm::find()->where([$this->_paramKey => $val])->one();
  34. }else{
  35. $this->_one = new AdminUserForm;
  36. }
  37. //$obj = $this->_obj;
  38. }
  39. # 传递给前端的数据 显示编辑form
  40. public function getLastData(){
  41. $request_param = CRequest::param();
  42. $this->_param = $request_param;
  43. $this->initParam();
  44. $role_ids = $this->getUserRoleIds();
  45. return [
  46. 'editBar' => $this->getEditBar(),
  47. 'role_ids'=>$role_ids,
  48. 'saveUrl' => CUrl::getUrl('fecadmin/account/managereditsave'),
  49. ];
  50. }
  51. # 保存
  52. public function save(){
  53. $request_param = CRequest::param();
  54. $this->_param = $request_param['editFormData'];
  55. $this->initParam();
  56. $model = $this->_one;
  57. $model->attributes = $this->_param;
  58. # 不存在则重置
  59. if(!$this->_param['access_token']){
  60. $model->access_token = '';
  61. }
  62. if(!$this->_param['auth_key']){
  63. $model->auth_key = '';
  64. }
  65. if($model[$this->_paramKey]){
  66. if ($model->validate()) {
  67. #不允许编辑admin
  68. /*
  69. if($model[$this->_paramKey] == 2){
  70. echo json_encode(array(
  71. "statusCode"=>"300",
  72. "message"=>"you can not update Admin User,you only can update other Account",
  73. ));
  74. exit;
  75. }
  76. */
  77. $model->save();
  78. $this->saveUserRole($model[$this->_paramKey]);
  79. echo json_encode(array(
  80. "statusCode"=>"200",
  81. "message"=>"update",
  82. ));
  83. exit;
  84. }
  85. }else{
  86. if ($model->validate()) {
  87. $model->save();
  88. $user_id = Yii::$app->db->getLastInsertID();
  89. $this->saveUserRole($user_id);
  90. echo json_encode(array(
  91. "statusCode"=>"200",
  92. "message"=>"insert",
  93. ));
  94. exit;
  95. }
  96. }
  97. $errors = $model->errors;
  98. echo json_encode(["statusCode"=>"300",
  99. "message" => CModel::getErrorStr($errors),
  100. ]);
  101. exit;
  102. }
  103. public function saveUserRole($user_id){
  104. $role = CRequest::param("role");
  105. $role_ids = [];
  106. if(!empty($role)){
  107. //var_dump($role);
  108. $innerTransaction = Yii::$app->db->beginTransaction();
  109. try {
  110. foreach($role as $k=>$role_id){
  111. $one = AdminUserRole::findOne([
  112. 'role_id' => $role_id,
  113. 'user_id' => $user_id,
  114. ]);
  115. $role_ids[] = $role_id;
  116. if(!$one['id']){
  117. $one = new AdminUserRole;
  118. $one->role_id = $role_id;
  119. $one->user_id = $user_id;
  120. $one->save();
  121. }
  122. }
  123. $table = AdminUserRole::tableName();
  124. if(!empty($role_ids) && is_array($role_ids)){
  125. AdminUserRole::deleteAll([
  126. 'and',
  127. ['user_id' => $user_id],
  128. ['not in', 'role_id', $role_ids],
  129. ]);
  130. }else{
  131. $innerTransaction->rollBack();
  132. echo json_encode([
  133. "statusCode"=>"300",
  134. "message" => "您至少要勾选一个用户权限组",
  135. ]);
  136. exit;
  137. }
  138. //CDB::deleteBySql($sql,$sql_data);
  139. $innerTransaction->commit();
  140. } catch (Exception $e) {
  141. $innerTransaction->rollBack();
  142. echo json_encode(["statusCode"=>"300",
  143. "message" => 'Save User Role Fail !',
  144. ]);
  145. exit;
  146. }
  147. }else{
  148. echo json_encode([
  149. "statusCode"=>"300",
  150. "message" => "您至少要勾选一个用户权限组",
  151. ]);
  152. exit;
  153. }
  154. }
  155. # 批量删除
  156. public function delete(){
  157. //$request_param = CRequest::param();
  158. //$this->_param = $request_param;
  159. //$this->initParam();
  160. # admin 用户不能删除
  161. if($id = CRequest::param('id')){
  162. $model = AdminUserForm::findOne(['id' => $id]);
  163. if($model->id){
  164. # 不允许删除admin
  165. if($model->username == 'admin'){
  166. echo json_encode(["statusCode"=>"300",
  167. "message" => 'You can not delete Admin User!',
  168. ]);
  169. exit;
  170. }
  171. $innerTransaction = Yii::$app->db->beginTransaction();
  172. try {
  173. $model->delete();
  174. AdminUserRole::deleteAll(['user_id'=> $model->id]);
  175. $innerTransaction->commit();
  176. } catch (Exception $e) {
  177. $innerTransaction->rollBack();
  178. echo json_encode(["statusCode"=>"300",
  179. "message" => 'Delete Fail !',
  180. ]);
  181. exit;
  182. }
  183. echo json_encode(["statusCode"=>"200",
  184. "message" => 'Delete Success!',
  185. ]);
  186. exit;
  187. }else{
  188. echo json_encode(["statusCode"=>"300",
  189. "message" => "id => $id , is not exist",
  190. ]);
  191. exit;
  192. }
  193. }else if($ids = CRequest::param('ids')){
  194. $id_arr = explode(",",$ids);
  195. # 不允许删除admin
  196. $adminUser = AdminUserForm::findOne(['username' => 'admin']);
  197. $adminUserId = $adminUser->id;
  198. if(in_array($adminUserId,$id_arr)){
  199. echo json_encode(["statusCode"=>"300",
  200. "message" => 'You can not delete Admin User!',
  201. ]);
  202. exit;
  203. }
  204. $innerTransaction = Yii::$app->db->beginTransaction();
  205. try {
  206. AdminUserForm::deleteAll(['in','id',$id_arr]);
  207. AdminUserRole::deleteAll(['in','user_id',$id_arr]);
  208. $innerTransaction->commit();
  209. } catch (Exception $e) {
  210. $innerTransaction->rollBack();
  211. echo json_encode(["statusCode"=>"300",
  212. "message" => 'Delete All Fail !',
  213. ]);
  214. exit;
  215. }
  216. echo json_encode(["statusCode"=>"200",
  217. "message" => "$ids Delete Success!",
  218. ]);
  219. exit;
  220. }
  221. echo json_encode(["statusCode"=>"300",
  222. "message" => "id or ids Param is not Exist!",
  223. ]);
  224. exit;
  225. }
  226. public function getEditArr(){
  227. return [
  228. [
  229. 'label'=>'用户名',
  230. 'name'=>'username',
  231. 'display'=>[
  232. 'type' => 'inputString',
  233. ],
  234. 'require' => 1,
  235. ],
  236. [
  237. 'label'=>'密码',
  238. 'name'=>'password',
  239. 'display'=>[
  240. 'type' => 'inputPassword',
  241. ],
  242. 'require' => 0,
  243. ],
  244. [
  245. 'label'=>'邮箱',
  246. 'name'=>'email',
  247. 'require' => 0,
  248. 'display'=>[
  249. 'type' => 'inputEmail',
  250. ],
  251. ],
  252. [
  253. 'label'=>'姓名',
  254. 'name'=>'person',
  255. 'require' => 0,
  256. 'display'=>[
  257. 'type' => 'inputString',
  258. ],
  259. ],
  260. [
  261. 'label'=>'员工编号',
  262. 'name'=>'code',
  263. 'require' => 1,
  264. 'display'=>[
  265. 'type' => 'inputString',
  266. ],
  267. ],
  268. [
  269. 'label'=>'用户状态',
  270. 'name'=>'status',
  271. 'display'=>[
  272. 'type' => 'select',
  273. 'data' => [
  274. AdminUserForm::STATUS_ACTIVE => '激活',
  275. AdminUserForm::STATUS_DELETED => '关闭',
  276. ]
  277. ],
  278. 'require' => 1,
  279. 'default' => AdminUserForm::STATUS_ACTIVE,
  280. ],
  281. //[
  282. // 'label'=>'权限',
  283. // 'name'=>'role',
  284. // 'display'=>[
  285. // 'type' => 'select',
  286. // 'data' => AdminRole::getAdminRoleArr(),
  287. // ],
  288. //],
  289. [
  290. 'label'=>'出生日期',
  291. 'name'=>'birth_date',
  292. 'display'=>[
  293. 'type' => 'inputDate',
  294. ],
  295. ],
  296. [
  297. 'name'=>'auth_key',
  298. 'display'=>[
  299. 'type' => 'inputString',
  300. ],
  301. ],
  302. [
  303. 'name'=>'access_token',
  304. 'display'=>[
  305. 'type' => 'inputString',
  306. ],
  307. ],
  308. ];
  309. }
  310. public function getUserRoleIds(){
  311. $user = $this->_one;
  312. $user_id = $user['id'];
  313. $roles = AdminUserRole::find()->asArray()
  314. ->where(['user_id' => $user_id])
  315. ->all()
  316. ;
  317. $role_ids = [];
  318. if(!empty($roles)){
  319. foreach($roles as $r){
  320. $role_ids[] = $r['role_id'];
  321. }
  322. }
  323. return $role_ids;
  324. }
  325. public function getEditBar(){
  326. $editArr = $this->getEditArr();
  327. $str = '';
  328. if($this->_param[$this->_paramKey]){
  329. $str = '<input type="hidden" value="'.$this->_param[$this->_paramKey].'" size="30" name="editFormData['.$this->_paramKey.']" class="textInput ">';
  330. }
  331. foreach($editArr as $column){
  332. $name = $column['name'];
  333. $require = $column['require'] ? 'required' : '';
  334. $label = $column['label'] ? $column['label'] : $this->_one->getAttributeLabel($name);
  335. $display = isset($column['display']) ? $column['display'] : '';
  336. if(empty($display)){
  337. $display = ['type' => 'inputString'];
  338. }
  339. //var_dump($this->_one['id']);
  340. $value = $this->_one[$name] ? $this->_one[$name] : $column['default'];
  341. $display_type = isset($display['type']) ? $display['type'] : 'inputString';
  342. if($display_type == 'inputString'){
  343. $str .='<p>
  344. <label>'.$label.':</label>
  345. <input type="text" value="'.$value.'" size="30" name="editFormData['.$name.']" class="textInput '.$require.' ">
  346. </p>';
  347. }else if($display_type == 'inputDate'){
  348. $str .='<p>
  349. <label>'.$label.':</label>
  350. <input type="text" value="'.($value ? date("Y-m-d",strtotime($value)) : '').'" size="30" name="editFormData['.$name.']" class="date textInput '.$require.' ">
  351. </p>';
  352. }else if($display_type == 'inputEmail'){
  353. $str .='<p>
  354. <label>'.$label.':</label>
  355. <input type="text" value="'.$value.'" size="30" name="editFormData['.$name.']" class="email textInput '.$require.' ">
  356. </p>';
  357. }else if($display_type == 'inputPassword'){
  358. $str .='<p>
  359. <label>'.$label.':</label>
  360. <input type="password" value="" size="30" name="editFormData['.$name.']" class=" textInput '.$require.' ">
  361. </p>';
  362. }else if($display_type == 'select'){
  363. $data = isset($display['data']) ? $display['data'] : '';
  364. //var_dump($data);
  365. //echo $value;
  366. $select_str = '';
  367. if(is_array($data)){
  368. $select_str .= '<select class="combox '.$require.'" name="editFormData['.$name.']" >';
  369. $select_str .='<option value="">'.$label.'</option>';
  370. foreach($data as $k => $v){
  371. if($value == $k){
  372. //echo $value."#".$k;
  373. $select_str .='<option selected="selected" value="'.$k.'">'.$v.'</option>';
  374. }else{
  375. $select_str .='<option value="'.$k.'">'.$v.'</option>';
  376. }
  377. }
  378. $select_str .= '</select>';
  379. }
  380. $str .='<p>
  381. <label>'.$label.':</label>
  382. '.$select_str.'
  383. </p>';
  384. }
  385. }
  386. return $str;
  387. }
  388. }