|
|
@@ -0,0 +1,145 @@
|
|
|
+<?php
|
|
|
+defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
+
|
|
|
+class Wechat extends Lyapi_Controller{
|
|
|
+ // 注意:登录接口不能受基础控制器的登录校验,可以覆盖构造方法或单独处理
|
|
|
+ public function __construct() {
|
|
|
+ // 这里不执行登录校验,只加载缓存驱动
|
|
|
+ parent::__construct(); // 暂时注释,或者使用一个新的不校验的基类
|
|
|
+ // 简便做法:复制 Lyapi_Controller 的部分代码但不调用 _check_api_auth
|
|
|
+ // $this->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,
|
|
|
+ '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('用户不存在','500');
|
|
|
+ }
|
|
|
+ $len = count($user_info_list);
|
|
|
+ if($len > 1){
|
|
|
+ $this->_json_error('微信绑定错误,请联系管理员','500');
|
|
|
+ }
|
|
|
+ $user_info = $user_info_list[0];
|
|
|
+ $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,
|
|
|
+ 'lypower'=>implode(',',$all_arr)
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|