site_id = $siteId; } /** * Common: 小程序成员管理 查询集合 * Author: wu-hui * Time: 2022/12/29 18:38 * @return Db */ private function appletMemberCollection($type){ return Db::name('applet_member') ->where('site_id',$this->site_id) ->where('type',$type); } /** * Common: 获取成员列表 * Author: wu-hui * Time: 2022/12/30 13:51 * @return array * @throws \think\db\exception\DbException */ public function getList(){ // 参数获取 $page = input('page',1); $pageSize = input('page_size',PAGE_LIST_ROWS); // 列表获取 $field = [ 'id', 'user_id', 'nick_name', 'status', 'gmt_join', 'logon_id', 'gmt_invite', 'role' ]; $result = $this->appletMemberCollection(1) ->field($field) ->order(['gmt_join'=>'DESC','gmt_invite'=>'DESC']) ->paginate(['list_rows' => $pageSize,'page' => $page]); if($result) { $result = $result->toArray(); $list = [ 'count' => $result['total'], 'list' => $result['data'], 'page_count' => $result['last_page'], ]; return $this->success($list); } return $this->success(); } /** * Common: 同步成员 * Author: wu-hui * Time: 2022/12/30 13:46 * @param $role * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function synchronizationMember($role){ $params = ['role' => $role]; $result = (new MinCode($this->site_id))->requestApi('alipay.open.app.members.query',$params); $result = $result['alipay_open_app_members_query_response']; if($result['code'] == 10000) { $list = $result['app_member_info_list'] ?? []; $hasList = $this->appletMemberCollection(1)->field('id,logon_id')->select(); $logonIds = $ids = []; if($hasList){ $hasList = $hasList->toArray(); $logonIds = array_column($hasList,'logon_id'); $ids = array_column($hasList,'id','logon_id'); } $insertData = []; $updateData = []; foreach($list as $item){ // 判断:当前用户是 新增加成员 还是 旧的成员 if(!in_array($item['logon_id'],$logonIds)){ // 不存在 新增成员 $insertData[] = [ 'site_id' => $this->site_id, 'type' => 1, 'user_id' => $item['user_id'] ?? '', 'nick_name' => $item['nick_name'] ?? '', 'status' => $item['status'] ?? '', 'gmt_join' => $item['gmt_join'] ?? '', 'logon_id' => $item['logon_id'] ?? '', 'gmt_invite' => $item['gmt_invite'] ?? '', 'role' => $item['role'] ?? '', ]; } else if(in_array($item['logon_id'],$logonIds)){ // 存在 修改成员 $updateData[] = [ 'id' => $ids[$item['logon_id']], 'nick_name' => $item['nick_name'] ?? '', 'status' => $item['status'] ?? '', 'role' => $item['role'] ?? '' ]; } } Db::name('applet_member')->insertAll($insertData); (new NewBaseModel(['table_name'=>'applet_member']))->saveAll($updateData); return success(0,'同步完成'); }else{ return $this->error('',$result['sub_msg']); } } /** * Common: 添加成员 * Author: wu-hui * Time: 2022/12/30 14:27 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function addMember(){ $info = input('info'); $params = [ 'logon_id' => $info['logon_id'], 'role' => $info['role'], ]; $result = (new MinCode($this->site_id))->requestApi('alipay.open.app.members.create',$params); $result = $result['alipay_open_app_members_create_response']; if($result['code'] == 10000) { $this->synchronizationMember($info['role']); return success(0,'添加成功'); }else{ return $this->error('',$result['sub_msg']); } } /** * Common: 删除成员 * Author: wu-hui * Time: 2022/12/30 14:41 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function delMember(){ $id = input('id'); $info = Db::name('applet_member')->field('user_id,role')->where('id',$id)->find(); if(!$info) return $this->error('','不存在的成员!'); $params = [ 'user_id' => $info['user_id'], 'role' => $info['role'], ]; $result = (new MinCode($this->site_id))->requestApi('alipay.open.app.members.delete',$params); $result = $result['alipay_open_app_members_delete_response']; if($result['code'] == 10000) { Db::name('applet_member')->where('id',$id)->delete(); return success(0,'删除成功'); }else{ return $this->error('',$result['sub_msg']); } } }