后台操作
public function login(){ $post = $this->request->param(); $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$post[\'appid\']."&secret=".$post[\'secret\']."&js_code=".$post[\'code\']."&grant_type=authorization_code"; $html = file_get_contents($url); $arr = json_decode($html,true); if (false == DB::name(\'users\')->where([\'openid\' => $arr[\'openid\']])->find()) { // Db::query(\'SET nicheng utf8mb4\');$post[\'nickname\'] = $post[\'userinfo\'][\'nickName\']; $post[\'create_time\'] = time(); $post[\'update_time\'] = time(); $post[\'thumb\'] = $post[\'userinfo\'][\'avatarUrl\'];$post[\'sex\'] = \'保密\';$post[\'openid\'] = $arr[\'openid\'];$post[\'users_cate_id\'] = db("users_cate")->where("name","会员")->value("id"); model(\'users\')->allowField(true)->save($post); $id = model(\'users\')->id; $data[\'qrcode\'] = $this->qrcode($id); db("users")->where("id",$id)->update($data); }else{ DB::name(\'users\')->where(\'openid\',$arr[\'openid\'])->update([\'login_time\'=>time(),\'nickname\'=>$post[\'userinfo\'][\'nickName\'],\'thumb\'=>$post[\'userinfo\'][\'avatarUrl\']]); } $info = Db::name(\'users\')->where(\'openid\',$arr[\'openid\'])->find(); $data = [ \'token\'=>createToken($info[\'id\']), \'openid\' => $arr[\'openid\'] ]; return json_success(1,"登录成功",$data); } //获取用户经销商信息 及生成推广二维码 public function qrcode($uid) { $info = db("users")->where(\'id\',$uid)->find(); //拿到openid 查找用户表内是否有该用户 没有则拒绝生成二维码 有则查看是否已生成二维码 有生成则发送数据 没有则生成 $dealer = $info[\'qrcode\']; // $dealer = 1; if($dealer==NULL){ if($dealer == \'\'){ $appid = \'***************\'; $secret = \'****************\';//AppSecret(小程序密钥) $url_access_token = \'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=\'.$appid.\'&secret=\'.$secret; $json_access_token = $this -> sendCmd($url_access_token,array()); $arr_access_token = json_decode($json_access_token,true); $access_token = $arr_access_token[\'access_token\']; if(!empty($access_token)) { $url = \'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=\'.$access_token; $data = \'{"path": "pages/my/my?uid=\'.$dealer.\'", "width": 430}\'; $result = $this -> sendCmd($url,$data); $name = $uid.time(); file_put_contents(\'./qrcode/code-\'.$name.\'.jpg\',$result); //存储二维码路径 $arr[\'qrcode\'] = \'/qrcode/code-\'.$name.\'.jpg\'; return $arr[\'qrcode\']; } else { return \'\'; } }else{ return $info[\'qrcode\']; } }else{ return $info[\'qrcode\']; } } /** * 发起请求 * @param string $url 请求地址 * @param string $data 请求数据包 * @return string 请求返回数据 */ function sendCmd($url,$data) { $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_HTTPHEADER, array(\'Expect:\')); //解决数据包大不能提交 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo \'Errno\'.curl_error($curl); } curl_close($curl); // 关键CURL会话 return $tmpInfo; // 返回数据 }