load->driver('cache'); // 加载缓存驱动 $this->load->_model("Model_wechat","wechat"); $this->load->_model("Model_user","user"); $this->load->_model("Model_power","power"); } public function bduser(){ if($this->input->method(TRUE) != 'POST'){ $this->_json_error('请求方式错误','500'); } $json_str = $this->input->raw_input_stream; $data = json_decode($json_str,true); if(empty($data['code'])){ $this->_json_error('参数错误','500'); } $code = $data['code']; $r = $this->wechat->getopenid($code); if($r['code'] == -1){ $this->_json_error('获取openid失败','500'); } $user_info = $this->user->read($this->userinfo['userid']); if(empty($user_info['wxopenid'])){ $wxopenid = []; }else{ $wxopenid = json_decode($user_info['wxopenid'],true); } $openid = $r['data']['openid']; $wxopenid[] = $r['data']['openid']; $wxopenid = array_unique($wxopenid); $auth_token = $this->input->get_request_header('Auth-Token', TRUE); $this->cache->delete($auth_token); $this->db->where('id',$user_info['id'])->update('user',array('wxopenid' => json_encode($wxopenid))); $power = $this->power->read($user_info['power']); if(empty($power)){ $this->_json_error('角色未设置','500'); } if(empty($power['lyapiid'])){ $this->_json_error('权限未设置','500'); } $lyapiids = explode("|",trim($power['lyapiid'],"|")); $res = $this->power->_lyapi(); $lyapi_list = $res['lyapi_list']; $all_arr = []; foreach($lyapi_list as $v){ if(in_array($v['id'],$lyapiids)){ $all_arr[] = $v['shortname']; } } $this->cache->save($openid, [ 'userid'=>$user_info['id'], 'username'=>$user_info['userid'], 'mobile'=>'', 'token'=>$openid, 'power'=>$all_arr ], 7200); $this->_json_error('绑定成功','200',[ 'username'=>$user_info['userid'], 'mobile'=>'', 'token'=>$openid, 'is_wxbd'=>1, 'lypower'=>implode(',',$all_arr) ]); } public function wxlogin(){ if($this->input->method(TRUE) != 'POST'){ $this->_json_error('请求方式错误','500'); } $json_str = $this->input->raw_input_stream; $data = json_decode($json_str,true); $auth_token = $this->input->get_request_header('Auth-Token', TRUE); if(!empty($auth_token)){ $this->cache->delete($auth_token); } $code = $data['code']; $r = $this->wechat->getopenid($code); if($r['code'] == -1){ $this->_json_error('获取openid失败','500'); } $openid = $r['data']['openid']; $user_info_list = $this->user->find_all("wxopenid like '%{$openid}%'"); if(empty($user_info_list)){ $this->_json_error('微信未绑定erp账号','500'); } $len = count($user_info_list); if($len > 1){ $this->_json_error('微信绑定错误,请联系管理员','500'); } $user_info = $user_info_list[0]; $wxopenid = json_decode($user_info['wxopenid'],true); if(!in_array($openid,$wxopenid)){ $this->_json_error('微信未绑定','500'); } $power = $this->power->read($user_info['power']); if(empty($power)){ $this->_json_error('角色未设置','500'); } if(empty($power['lyapiid'])){ $this->_json_error('权限未设置','500'); } $lyapiids = explode("|",trim($power['lyapiid'],"|")); $res = $this->power->_lyapi(); $lyapi_list = $res['lyapi_list']; $all_arr = []; foreach($lyapi_list as $v){ if(in_array($v['id'],$lyapiids)){ $all_arr[] = $v['shortname']; } } $this->cache->save($openid, [ 'userid'=>$user_info['id'], 'username'=>$user_info['userid'], 'mobile'=>'', 'token'=>$openid, 'power'=>$all_arr ], 7200); $this->_json_error('绑定成功','200',[ 'username'=>$user_info['userid'], 'mobile'=>'', 'token'=>$openid, 'is_wxbd'=>1, 'lypower'=>implode(',',$all_arr) ]); } }