作者 郭文星

123

@@ -24,68 +24,89 @@ class Login extends Base @@ -24,68 +24,89 @@ class Login extends Base
24 { 24 {
25 $appid = $this->AppID; 25 $appid = $this->AppID;
26 $AppSecret = $this->AppSecret; 26 $AppSecret = $this->AppSecret;
27 - $code = $this->request->param("code");  
28 -// $encryptedData = $this->request->param("encryptedData");  
29 -// $iv = $this->request->param("iv");  
30 -// $type = $this->request->param("type");  
31 -//  
32 -// if ($type == 1) {  
33 -// $encryptedData = urldecode($encryptedData);  
34 -// $iv = urldecode($iv); 27 + $post = $this->request->post();
  28 + $code = $post['code'];// I('post.code');
  29 + $encryptedData = $post['encryptedData'];//I('post.encryptedData');
  30 + $iv = $post['iv'];//I('post.iv');
  31 +
  32 + if ($post['type'] == 1) {
  33 + $encryptedData = urldecode($encryptedData);
  34 + $iv = urldecode($iv);
35 // file_put_contents("ccc.txt", "授权A:" . date("Y-m-d H:i:s") . ":" . $encryptedData . PHP_EOL, FILE_APPEND); 35 // file_put_contents("ccc.txt", "授权A:" . date("Y-m-d H:i:s") . ":" . $encryptedData . PHP_EOL, FILE_APPEND);
36 -//// file_put_contents("ccc.txt", "授权B:" . date("Y-m-d H:i:s") . ":" . $iv . PHP_EOL, FILE_APPEND);  
37 -//  
38 -// } 36 +// file_put_contents("ccc.txt", "授权B:" . date("Y-m-d H:i:s") . ":" . $iv . PHP_EOL, FILE_APPEND);
  37 +
  38 + }
39 39
40 $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code"; 40 $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
41 $res = json_decode(http_request($url), true); 41 $res = json_decode(http_request($url), true);
42 - print_r($res);return;  
43 if (!$res) { 42 if (!$res) {
44 $res = json_decode(http_request($url), true); 43 $res = json_decode(http_request($url), true);
45 } 44 }
46 - if(!empty($res['errcode'])){  
47 - $ajax['code'] = 0;  
48 - $ajax['info'] = $res['errcode'];  
49 - $errCode = $res['errcode'] == "-41001" || $res['errcode'] == "-41003" ? "授权失败,请尝试重新授权" : $res['errcode'];  
50 - $this->error($errCode);  
51 - }  
52 45
53 $sessionKey = $res['session_key']; 46 $sessionKey = $res['session_key'];
54 $openid = $res['openid'];//获取用户openid 47 $openid = $res['openid'];//获取用户openid
55 - //$unionid = $res['unionid'];//获取用户openid 48 + $unionid = $res['unionid'];//获取用户openid
56 // file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . ":" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND); 49 // file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . ":" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
57 50
58 require_once '../extend/lib/WXBizDataCrypt.class.php'; 51 require_once '../extend/lib/WXBizDataCrypt.class.php';
59 52
60 $pc = new WXBizDataCrypt($appid, $sessionKey); 53 $pc = new WXBizDataCrypt($appid, $sessionKey);
  54 + $errCode = $pc->decryptData($encryptedData, $iv, $data);
61 55
62 - //$errCode = $pc->decryptData($encryptedData, $iv, $data);  
63 -  
64 - //$data = json_decode($data, true); 56 + $data = json_decode($data, true);
65 // file_put_contents("ccc.txt", "授权2:" . date("Y-m-d H:i:s") . ":" . $errCode . PHP_EOL, FILE_APPEND); 57 // file_put_contents("ccc.txt", "授权2:" . date("Y-m-d H:i:s") . ":" . $errCode . PHP_EOL, FILE_APPEND);
66 // file_put_contents("ccc.txt", "授权3:" . date("Y-m-d H:i:s") . ":" . json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND); 58 // file_put_contents("ccc.txt", "授权3:" . date("Y-m-d H:i:s") . ":" . json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
67 59
68 - 60 + if ($errCode != 0) {
  61 + $ajax['code'] = 0;
  62 + $ajax['info'] = $errCode;
  63 + $errCode = $errCode == "-41001" || $errCode == "-41003" ? "授权失败,请尝试重新授权" : $errCode;
  64 + $this->error($errCode);
  65 + }
69 if ($openid) { 66 if ($openid) {
70 $openid_info = [ 67 $openid_info = [
71 "openid" => $openid, 68 "openid" => $openid,
  69 + "unionid" => $unionid,
  70 + "from" => "wx",
72 ]; 71 ];
73 - //print_r($openid_info);return; 72 + if (!empty($data['nickName'])) {
  73 + $openid_info['nickName'] = $data['nickName'];
  74 + }
  75 + if (!empty($data['gender'])) {
  76 + $openid_info['gender'] = $data['gender'];
  77 + }
  78 + if (!empty($data['avatarUrl'])) {
  79 + $openid_info['avatarUrl'] = $data['avatarUrl'];
  80 + }
  81 + if (!empty($data['country'])) {
  82 + $openid_info['country'] = $data['country'];
  83 + }
  84 + if (!empty($data['province'])) {
  85 + $openid_info['province'] = $data['province'];
  86 + }
  87 + if (!empty($data['city'])) {
  88 + $openid_info['city'] = $data['city'];
  89 + }
  90 + if (!empty($data['phoneNumber'])) {
  91 + $openid_info['phoneNumber'] = $data['phoneNumber'];
  92 + }
74 insert_openid_info($openid_info);//更新下微信用户信息到数据库 93 insert_openid_info($openid_info);//更新下微信用户信息到数据库
75 } 94 }
76 95
77 //直接通过unionid 查找用户信息 96 //直接通过unionid 查找用户信息
78 $userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find(); 97 $userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find();
79 - if (empty($userres)) {  
80 98
  99 + if (empty($userres)) {
81 //如果通过小程序openid找不到会员 100 //如果通过小程序openid找不到会员
82 //注册处理 101 //注册处理
83 $extend_data = [ 102 $extend_data = [
84 - //"avatar" => $openid_info["avatarUrl"], 103 + "nickname" => $openid_info['nickName'],
  104 + "avatar" => $openid_info["avatarUrl"],
85 "wx_xcx_openid" => $openid, 105 "wx_xcx_openid" => $openid,
  106 + "unionid" => $unionid,
86 ]; 107 ];
87 $username = $openid ? $openid : suiji_num("TY"); 108 $username = $openid ? $openid : suiji_num("TY");
88 - $ret = $this->auth->register($username, "a123456", '', "1345233948", $extend_data); 109 + $ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);
89 if ($ret) { 110 if ($ret) {
90 $return_data = $this->auth->getUserinfo(); 111 $return_data = $this->auth->getUserinfo();
91 112
@@ -126,6 +147,13 @@ class Login extends Base @@ -126,6 +147,13 @@ class Login extends Base
126 if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) { 147 if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) {
127 $update_data['mobile'] = $openid_info['phoneNumber']; 148 $update_data['mobile'] = $openid_info['phoneNumber'];
128 } 149 }
  150 +// if (!empty($openid_info['phoneNumber'])) {
  151 +// $update_data['username'] = $openid_info['phoneNumber'];
  152 +// }
  153 +// if (!empty($openid_info['nickName']) && $openid_info['nickName']!="微信用户") {
  154 +// $update_data['nickname'] = $openid_info['nickName'];
  155 +// }
  156 +
129 157
130 if (!empty($openid_info['phoneNumber'])) { 158 if (!empty($openid_info['phoneNumber'])) {
131 //这里是已注册过的用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去 159 //这里是已注册过的用户,有手机号,需要判断下手机号有没有在区域管理员中存在,若存在就将id绑定过去
@@ -138,9 +166,23 @@ class Login extends Base @@ -138,9 +166,23 @@ class Login extends Base
138 } 166 }
139 } 167 }
140 } 168 }
  169 +
141 Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data); 170 Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data);
142 $this->auth->direct($userres['id']); 171 $this->auth->direct($userres['id']);
143 $return_data = $this->auth->getUserinfo(); 172 $return_data = $this->auth->getUserinfo();
  173 +
  174 + $is_mng_user = 0;//是否是部门管理员 0不是 1是
  175 + $mng_area = [];//管辖部门(多个)
  176 + $area = Db::name("hc_area_code_mng")->alias("a")
  177 + ->join("hc_area_code b", "b.code=a.mng_code")
  178 + ->where("a.mng_user_id", $return_data['id'])->field("b.code,b.name")->select();
  179 + if (!empty($area)) {
  180 + $is_mng_user = 1;
  181 + $mng_area = $area;
  182 + }
  183 + $return_data['is_mng_user'] = $is_mng_user;
  184 + $return_data['mng_area'] = $mng_area;
  185 +
144 $this->success(__('登录成功'), $return_data); 186 $this->success(__('登录成功'), $return_data);
145 } 187 }
146 } 188 }