作者 郭文星

123

@@ -2,11 +2,11 @@ @@ -2,11 +2,11 @@
2 /** 2 /**
3 * Created by PhpStorm. 3 * Created by PhpStorm.
4 * Login: Kevin 4 * Login: Kevin
5 - * Date: 2022/06/12  
6 - * Time: 15:34 5 + * Date: 2023/03/22
  6 + * Time: 14:10
7 */ 7 */
8 8
9 -namespace app\api\controller\v1; 9 +namespace app\api\controller\v2;
10 10
11 use lib\WXBizDataCrypt; 11 use lib\WXBizDataCrypt;
12 use think\Db; 12 use think\Db;
@@ -17,204 +17,66 @@ header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac @@ -17,204 +17,66 @@ header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac
17 header('Access-Control-Allow-Methods: GET, POST, PUT'); 17 header('Access-Control-Allow-Methods: GET, POST, PUT');
18 18
19 class Login extends Base 19 class Login extends Base
20 -{ /**  
21 - * 12.小程序授权注册用户、返回用户信息  
22 - */ 20 +{
  21 + /**
  22 + * 1、小程序授权注册用户、返回用户信息
  23 + */
23 public function get_user_by_shouquan() 24 public function get_user_by_shouquan()
24 { 25 {
  26 + print_r(123);return;
25 $appid = $this->AppID; 27 $appid = $this->AppID;
26 $AppSecret = $this->AppSecret; 28 $AppSecret = $this->AppSecret;
27 $post = $this->request->post(); 29 $post = $this->request->post();
28 $code = $post['code'];// I('post.code'); 30 $code = $post['code'];// I('post.code');
29 $encryptedData = $post['encryptedData'];//I('post.encryptedData'); 31 $encryptedData = $post['encryptedData'];//I('post.encryptedData');
30 $iv = $post['iv'];//I('post.iv'); 32 $iv = $post['iv'];//I('post.iv');
31 -  
32 -  
33 - $encryptedData = urldecode($encryptedData);  
34 - $iv = urldecode($iv); 33 + //file_put_contents("ccc_v2.txt", "授权0-提交的内容:" . date("Y-m-d H:i:s") . json_encode($post) . PHP_EOL, FILE_APPEND);
35 $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code"; 34 $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
36 - $res = json_decode(http_request($url), true); 35 + file_put_contents("1.txt","8-1. ".($code)."\r\n",FILE_APPEND);
  36 + $result = http_request($url);
  37 + file_put_contents("111111111111222.txt","3-1. ".($result)."\r\n",FILE_APPEND);
  38 + $res = json_decode($result, true);
  39 + print_r($res);return;
37 if (!$res) { 40 if (!$res) {
38 - $res = json_decode(http_request($url), true);  
39 - }  
40 -  
41 - $sessionKey = $res['session_key'];  
42 - $openid = $res['openid'];//获取用户openid  
43 - //$unionid = $res['unionid'];//获取用户openid  
44 -// file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . ":" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);  
45 -  
46 - require_once '../extend/lib/WXBizDataCrypt.class.php';  
47 -  
48 - $pc = new WXBizDataCrypt($appid, $sessionKey);  
49 - $errCode = $pc->decryptData($encryptedData, $iv, $data);  
50 -  
51 - $data = json_decode($data, true);  
52 -// file_put_contents("ccc.txt", "授权2:" . date("Y-m-d H:i:s") . ":" . $errCode . PHP_EOL, FILE_APPEND);  
53 -// file_put_contents("ccc.txt", "授权3:" . date("Y-m-d H:i:s") . ":" . json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);  
54 -  
55 - if ($errCode != 0) {  
56 - $ajax['code'] = 0;  
57 - $ajax['info'] = $errCode;  
58 - $errCode = $errCode == "-41001" || $errCode == "-41003" ? "授权失败,请尝试重新授权" : $errCode;  
59 - $this->error($errCode);  
60 - }  
61 - if ($openid) {  
62 - $openid_info = [  
63 - "openid" => $openid,  
64 - //"unionid" => $unionid,  
65 - "from" => "wx",  
66 - ];  
67 - if (!empty($data['nickName'])) {  
68 - $openid_info['nickName'] = $data['nickName'];  
69 - }  
70 - if (!empty($data['gender'])) {  
71 - $openid_info['gender'] = $data['gender'];  
72 - }  
73 - if (!empty($data['avatarUrl'])) {  
74 - $openid_info['avatarUrl'] = $data['avatarUrl'];  
75 - }  
76 - if (!empty($data['country'])) {  
77 - $openid_info['country'] = $data['country'];  
78 - }  
79 - if (!empty($data['province'])) {  
80 - $openid_info['province'] = $data['province'];  
81 - }  
82 - if (!empty($data['city'])) {  
83 - $openid_info['city'] = $data['city'];  
84 - }  
85 - if (!empty($data['phoneNumber'])) {  
86 - $openid_info['phoneNumber'] = $data['phoneNumber'];  
87 - }  
88 - insert_openid_info($openid_info);//更新下微信用户信息到数据库  
89 - }  
90 -  
91 - //直接通过unionid 查找用户信息  
92 - $userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find();  
93 -  
94 - if (empty($userres)) {  
95 - //如果通过小程序openid找不到会员  
96 - //注册处理  
97 - $extend_data = [  
98 - "wx_xcx_openid" => $openid,  
99 - ];  
100 - $username = $openid ? $openid : suiji_num("TY");  
101 - $ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);  
102 - if ($ret) {  
103 - $return_data = $this->auth->getUserinfo();  
104 -  
105 - $is_mng_user = 0;//是否是社区管理员 0不是 1是  
106 - $mng_area = [];//管辖区域(多个)  
107 - $area = Db::name("hc_area_code_mng")->alias("a")  
108 - ->join("hc_area_code b", "b.code=a.mng_code")  
109 - ->where("a.mng_user_id", $return_data['id'])->field("b.code,b.name")->select();  
110 - if (!empty($area)) {  
111 - $is_mng_user = 1;  
112 - $mng_area = $area;  
113 - }  
114 - $return_data['is_mng_user'] = $is_mng_user;  
115 - $return_data['mng_area'] = $mng_area;  
116 -  
117 - if (empty($return_data['mobile'])) {  
118 - $this->success("授权成功,请继续认证手机号后才能正常使用", $return_data, 2);  
119 - } else {  
120 - //这里是新注册用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去  
121 - $bind = Db::name("hc_area_code_mng")->where("mng_admin_phone", $return_data['mobile'])->order("id desc")->find();  
122 - if (!empty($bind)) {  
123 - Db::name("hc_area_code_mng")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);  
124 - if ($bind['mng_admin_name']) {  
125 - //将管理员姓名更新到用户表中  
126 - Db::name("user")->where("id", $return_data['id'])->update(["username" => $bind['mng_admin_name']]);  
127 - }  
128 - }  
129 - }  
130 -  
131 - $this->success("登录成功!", $return_data);  
132 - } else {  
133 - $this->error($this->auth->getError());  
134 - }  
135 - } else {  
136 - //小程序openid找到了会员  
137 - $update_data = [];  
138 - $update_data['logintime'] = time();  
139 - if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) {  
140 - $update_data['mobile'] = $openid_info['phoneNumber'];  
141 - }  
142 -// if (!empty($openid_info['phoneNumber'])) {  
143 -// $update_data['username'] = $openid_info['phoneNumber'];  
144 -// }  
145 -// if (!empty($openid_info['nickName']) && $openid_info['nickName']!="微信用户") {  
146 -// $update_data['nickname'] = $openid_info['nickName'];  
147 -// }  
148 -  
149 -  
150 - if (!empty($openid_info['phoneNumber'])) {  
151 - //这里是已注册过的用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去  
152 - $bind = Db::name("hc_area_code_mng")->where("mng_admin_phone", $openid_info['phoneNumber'])->order("id desc")->find();  
153 - if (!empty($bind)) {  
154 - Db::name("hc_area_code_mng")->where("mng_admin_phone", $openid_info['phoneNumber'])->update(["mng_user_id" => $userres['id']]);  
155 - if ($bind['mng_admin_name']) {  
156 - //将管理员姓名更新到用户表中  
157 - Db::name("user")->where("id", $userres['id'])->update(["username" => $bind['mng_admin_name']]);  
158 - } 41 + $this->error("授权失败,请尝试重新授权");
  42 + //$res = json_decode(send_post($url), true);
  43 + }else{
  44 + //查询到openid 通过openid查询到用户
  45 + $user=Db::name("user")->where('wx_xcx_openid',$res['openid'])->find();
  46 + if($user){
  47 + //查询到了用户信息
  48 + $this->bind($user['id'],$user['moblie']);
  49 + }else{
  50 + //未查询到用户信息 添加用户信息
  51 + $username = $res['openid'] ? $res['openid'] : suiji_num("TY");
  52 + $extend_data = [
  53 + "avatar" => "/default.png",
  54 + "wx_xcx_openid" => $res['openid'],
  55 + ];
  56 + $ret = $this->auth->register($username, "a123456", '','', $extend_data);
  57 + if($ret){
  58 + $return_data = $this->auth->getUserinfo();
  59 + $this->success('查询用户信息成功2',$return_data);
159 } 60 }
160 } 61 }
161 -  
162 - Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data);  
163 - $this->auth->direct($userres['id']);  
164 - $return_data = $this->auth->getUserinfo();  
165 -  
166 - $is_mng_user = 0;//是否是部门管理员 0不是 1是  
167 - $mng_area = [];//管辖部门(多个)  
168 - $area = Db::name("hc_area_code_mng")->alias("a")  
169 - ->join("hc_area_code b", "b.code=a.mng_code")  
170 - ->where("a.mng_user_id", $return_data['id'])->field("b.code,b.name")->select();  
171 - if (!empty($area)) {  
172 - $is_mng_user = 1;  
173 - $mng_area = $area;  
174 - }  
175 - $return_data['is_mng_user'] = $is_mng_user;  
176 - $return_data['mng_area'] = $mng_area;  
177 -  
178 - $this->success(__('登录成功'), $return_data);  
179 } 62 }
180 - }  
181 63
182 - /**  
183 - * 给后台添加用户时自动注册用  
184 - */  
185 - public function auto_regist_user($username, $password = "a123456", $email = "", $mobile, $extend_data)  
186 - {  
187 - $ret = $this->auth->register($username, $password, '', $mobile, $extend_data);  
188 - if ($ret) {  
189 - $return_data = $this->auth->getUserinfo();  
190 - return array_callback(true, "用户添加成功", $return_data['id']);  
191 - } else {  
192 - return array_callback(false, $this->auth->getError());  
193 - }  
194 } 64 }
195 - public function get_user_by_phone() 65 +
  66 + public function getuserphonenumber()
196 { 67 {
197 $user_id=$this->auth->id; 68 $user_id=$this->auth->id;
198 $appid = $this->AppID; 69 $appid = $this->AppID;
199 $AppSecret = $this->AppSecret; 70 $AppSecret = $this->AppSecret;
200 $post = $this->request->post(); 71 $post = $this->request->post();
201 - $code = $post['code']; 72 + $code = $post['code'];// I('post.code');
202 $AccessToken=$this->getwxAccessToken($appid,$AppSecret); 73 $AccessToken=$this->getwxAccessToken($appid,$AppSecret);
203 $phone=$this->getwxPhoneNumber($code,$AccessToken); 74 $phone=$this->getwxPhoneNumber($code,$AccessToken);
204 $phone=json_decode($phone,true); 75 $phone=json_decode($phone,true);
205 $moblie=$phone['phoneNumber']; 76 $moblie=$phone['phoneNumber'];
206 if($moblie){ 77 if($moblie){
207 - $res=Db::name("user")->where("id", $user_id)->update(["mobile" => $moblie]);  
208 - $this->auth->direct($user_id);  
209 - $return_data = $this->auth->getUserinfo();  
210 -  
211 - if($res){  
212 - $this->success("登录成功!",$return_data);  
213 -  
214 - }else{  
215 - $this->error("无法获取手机号,登录失败");  
216 -  
217 - } 78 + Db::name("user")->where("id", $user_id)->update(["mobile" => $moblie]);
  79 + $this->bind($user_id,$moblie);
218 }else{ 80 }else{
219 $this->error("无法获取手机号,登录失败"); 81 $this->error("无法获取手机号,登录失败");
220 } 82 }
@@ -270,4 +132,64 @@ class Login extends Base @@ -270,4 +132,64 @@ class Login extends Base
270 curl_close($curl); 132 curl_close($curl);
271 return $return_str; 133 return $return_str;
272 } 134 }
273 -} 135 +
  136 + //查询权限
  137 + public function bind($user_id,$phoneNumber){
  138 + //跟新手机号
  139 + Db::name("user")->where("id", $user_id)->update(["mobile" => $phoneNumber]);
  140 +
  141 + //[农场]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂中存在,若存在就将id绑定过去
  142 + $bind = Db::name("farm_manager")->where("mng_admin_phone", $phoneNumber)->order("id desc")->find();
  143 +
  144 + if (!empty($bind)) {
  145 + Db::name("farm_manager")->where("mng_admin_phone", $phoneNumber)->update(["mng_user_id" =>$user_id]);
  146 + if ($bind['mng_admin_name']) {
  147 + //将管理员姓名更新到用户表中
  148 + Db::name("user")->where("id", $user_id)->update(["username" => $bind['mng_admin_name']]);
  149 + }
  150 + }
  151 +
  152 + //[食堂]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂管理员中存在,若存在就将id绑定过去
  153 + $bind2 = Db::name("farm_canteenmanager")->where("mng_admin_phone", $phoneNumber)->order("id desc")->find();
  154 +
  155 + if (!empty($bind2)) {
  156 + Db::name("farm_canteenmanager")->where("mng_admin_phone", $phoneNumber)->update(["mng_user_id" => $user_id]);
  157 + if ($bind2['mng_admin_name']) {
  158 + //将管理员姓名更新到用户表中
  159 + Db::name("user")->where("id", $user_id)->update(["username" => $bind2['mng_admin_name']]);
  160 + }
  161 + }
  162 +
  163 +
  164 +
  165 + $is_mng_user = 0;//是否是农场管理员 0不是 1是
  166 + $mng_area = [];//管辖区域(多个)
  167 + $area = Db::name("farm_manager")->alias("a")
  168 + ->join("farm_farm_list b", "b.id=a.farm_farm_list_id")
  169 + ->where("a.mng_user_id", $user_id)->field("b.id as code,b.name")->select();
  170 + if (!empty($area)) {
  171 + $is_mng_user = 1;
  172 + $mng_area = $area;
  173 + }
  174 + $return_data['is_mng_user'] = $is_mng_user;
  175 + $return_data['mng_area'] = $mng_area;
  176 +
  177 + $is_mng_user2 = 0;//是否是食堂管理员 0不是 1是
  178 + $mng_area2 = [];//管辖区域(多个)
  179 + $area2 = Db::name("farm_canteenmanager")->alias("a")
  180 + ->join("farm_canteen b", "b.id=a.farm_canteen_id")
  181 + ->where("a.mng_user_id", $user_id)->field("b.id as code,b.name")->select();
  182 + if (!empty($area2)) {
  183 + $is_mng_user2 = 1;
  184 + $mng_area2 = $area2;
  185 + }
  186 + $return_data['is_mng_user2'] = $is_mng_user2;
  187 + $return_data['mng_area2'] = $mng_area2;
  188 +
  189 + $return_data['avatar'] =full_image($return_data['avatar']);
  190 + $res=$this->auth->direct($user_id);
  191 + $return_data = $this->auth->getUserinfo();
  192 + $this->success(__('登录成功'), $return_data);
  193 + }
  194 +
  195 +}