123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace app\common\model;
- use think\Db;
- class UserMenuGroup extends Base
- {
- public function updates($userId){
- $data = request()->post();
- $data['user_id'] = $userId;
- $result = validate('UserMenuGroup')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('UserMenuGroup')->getError();
- return false;
- }
- $id = $data['id'];
- unset($data['id']);
- Db::startTrans();
- try{
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- $id = $this->id;
- }
- if(!$ret){
- exception('操作失败');
- }
- $menus = $data['menus']?explode(',',$data['menus']):[];
- if($menus){
- $map[] = ['user_id','=',$data['user_id']];
- $map[] = ['id','<>',$id];
- $lists = Db::name('user_menu_group')->where($map)->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $omenus = $v['menus']?explode(',',$v['menus']):[];
- if(empty($omenus)){
- continue;
- }
- $nmenus = [];
- foreach ($omenus as $kk=>$vv){
- if(!in_array($vv,$menus)){
- $nmenus[] = $vv;
- }
- }
- if(count($nmenus) != count($nmenus)){
- $mes = $nmenus?implode(',',$nmenus):'';
- $res = Db::name('user_menu_group')->where('id',$v['id'])->setField('menus',$mes);
- if(!$res){
- exception('操作失败');
- }
- }
- }
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- }
|