1. 微信登录思路:

    1. 在main.js 中封装公共函数,用于判断用户是否登录
    2. 在main.js 中分定义全局变量,用于存储接口地址
    3. 如果没有登录、则跳转至登录页面
    4. 进入登录页面
    5. 通过 wx.login 获取用户的 code
    6. 通过 code 获取用户的 SessionKey、OpenId 等信息【本应后台接口、但是此处使用js发送请求】
    7. 通过 openId 调用后台 Api 获取用户的信息
    8. 获取成功,则说明已经授权过了,直接登录成功
    9. 获取失败,则说明没有授权过,需要授权之后才能进行登录
    10. 用户点击页面微信登录按钮【 <button open-type="getUserInfo"></button>】
    11. 获取用户数据,然后调用后台接口写入数据库
  2. 在 applets/main.js 中添加如下

    // 封装全局登录函数
    // backpage, backtype 2个参数分别代表:
    // backpage : 登录后返回的页面
    // backtype : 打开页面的类型[1 : redirectTo 2 : switchTab]
    Vue.prototype.checkLogin = function( backpage, backtype ){
    // 同步获取本地数据(uid、随机码、用户名、头像)
    var user_id = uni.getStorageSync('user_id');
    var user_nu = uni.getStorageSync('user_nu');
    var user_nm = uni.getStorageSync('user_nm');
    var user_fa = uni.getStorageSync('user_fa');
    if( user_id == '' || user_nu == '' || user_fa == ''){
    // 使用重定向的方式跳转至登录页面
    uni.redirectTo({url:'../login/login?backpage='+backpage+'&backtype='+backtype});
    return false;
    }
    // 登录成功、已经登录返回数组 [用户 id, 用户随机码, 用户昵称, 用户表情]
    return [user_id, user_nu, user_nm, user_fa];
    }
    // 定义一个全局的请求地址
    Vue.prototype.apiServer = 'http://0608.cc/'
  3. 在 pages/login/login.vue 中添加如下

    <template>
    <view>
    <!-- login view html start -->
    <view>
    <view>
    <view class="header"><image src="/static/img/public/login-wx.png"></image></view>
    <view class="content">
    <view>申请获取以下权限</view>
    <text>获得你的公开信息(昵称,头像、地区等)</text>
    </view>
    <button class="bottom" type="primary" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">授权登录</button>
    </view>
    </view>
    <!-- login view html end -->
    </view>
    </template> <script>
    export default {
    data() {
    return {
    appid: '*************',
    secret: '*************************',
    code: '',
    sessionKey: '',
    openId: '',
    userInfo: {
    avatarUrl: '',
    city: '',
    country: '',
    gender: 1,
    language: '',
    nickName: ''
    },
    pageOption: {}
    };
    },
    methods: {
    // 第一授权获取用户信息 ===》按钮触发
    wxGetUserInfo() {
    let _self = this;
    // 1.获取用户的信息
    uni.getUserInfo({
    provider: 'weixin',
    success: ( infoRes ) => {
    console.log( infoRes )
    _self.userInfo = infoRes.userInfo
    // 2.提交数据到后台、写入数据库
    uni.request({
    url: _self.apiServer + 'appletsUserInfo',
    data: {
    openid: _self.openId,
    avatarUrl: _self.userInfo.avatarUrl,
    city: _self.userInfo.city,
    country: _self.userInfo.country,
    gender: _self.userInfo.gender,
    language: _self.userInfo.language,
    nickName: _self.userInfo.nickName
    },
    method: 'POST',
    success: res => {
    if( res.data.code != 0 )
    {
    uni.showToast({ title: res.data.msg, icon: 'none' });
    return false;
    }
    // 用户信息写入缓存
    uni.showToast({title: '登录成功'})
    uni.setStorageSync( 'user_id', res.data.res.u_id );
    uni.setStorageSync( 'user_nm', res.data.res.u_nickName );
    uni.setStorageSync( 'user_fa', res.data.res.u_avatarUrl );
    uni.setStorageSync( 'user_nu', res.data.res.u_regtime );
    // 然后跳回原页面
    if( _self.pageOption.backtype == 1 )
    {
    uni.redirectTo({ url: _self.pageOption.backpage })
    }else{
    uni.switchTab({ url: _self.pageOption.backpage })
    }
    },
    fail: () => {
    uni.showToast({ title: '用户信息操作失败', icon: 'none' });
    }
    });
    },
    fail: () => {
    uni.showToast({ title: '获取用户信息失败', icon: 'none' });
    }
    });
    return false
    },
    // 登录
    login() {
    let _self = this; // 0. 显示加载的效果
    uni.showLoading({
    title: '登录中...'
    }); // 1. wx 获取登录用户 code
    uni.login({
    provider: 'weixin',
    success: loginRes => {
    console.log(loginRes);
    _self.code = loginRes.code;
    // 2. 将用户登录code传递到后台置换用户SessionKey、OpenId等信息
    uni.request({
    url:
    'https://api.weixin.qq.com/sns/jscode2session?appid=' +
    _self.appid +
    '&secret=' +
    _self.secret +
    '&js_code=' +
    _self.code +
    '&grant_type=authorization_code',
    success: codeRes => {
    console.log(codeRes);
    _self.openId = codeRes.data.openid;
    _self.sessionKey = codeRes.data.session_key;
    // 3.通过 openId 判断用户是否授权
    uni.request({
    url: _self.apiServer + 'loginApplets',
    data: {
    openid: _self.openId
    },
    method: 'POST',
    success: openIdRes => {
    console.log(openIdRes);
    // 隐藏loading
    uni.hideLoading();
    // 还没授权登录、请先授权然后登录
    if (openIdRes.data.code == 1) {
    // 提示消息、让用户授权
    uni.showToast({ title: openIdRes.data.msg, icon: 'none' });
    }
    // 已经授权了、查询到用户的数据了
    if (openIdRes.data.code == 0) {
    // 用户信息写入缓存
    uni.showToast({title: '登录成功'})
    uni.setStorageSync( 'user_id', openIdRes.data.res.u_id );
    uni.setStorageSync( 'user_nm', openIdRes.data.res.u_nickName );
    uni.setStorageSync( 'user_fa', openIdRes.data.res.u_avatarUrl );
    uni.setStorageSync( 'user_nu', openIdRes.data.res.u_regtime );
    // 然后跳回原页面
    if( _self.pageOption.backtype == 1 )
    {
    uni.redirectTo({ url: _self.pageOption.backpage })
    }else{
    uni.switchTab({ url: _self.pageOption.backpage })
    }
    }
    },
    fail: () => {
    uni.showToast({ title: '获取授权信息失败', icon: 'none' });
    return false;
    }
    });
    },
    fail: () => {
    uni.showToast({ title: '获取 SesssionKey OpenId 失败', icon: 'none' });
    return false;
    }
    });
    },
    fail: () => {
    uni.showToast({ title: '获取 code 失败', icon: 'none' });
    return false;
    }
    });
    return false;
    }
    },
    onLoad( options ) {
    // 接收跳转的参数
    this.pageOption = options
    //默认加载
    this.login();
    }
    };
    </script> <style>
    .header {
    margin: 90rpx 0 90rpx 50rpx;
    border-bottom: 1px solid #ccc;
    text-align: center;
    width: 650rpx;
    height: 300rpx;
    line-height: 450rpx;
    } .header image {
    width: 200rpx;
    height: 200rpx;
    } .content {
    margin-left: 50rpx;
    margin-bottom: 90rpx;
    } .content text {
    display: block;
    color: #9d9d9d;
    margin-top: 40rpx;
    } .bottom {
    border-radius: 80rpx;
    margin: 70rpx 50rpx;
    font-size: 35rpx;
    }
    </style>
  4. 在 pages/my/my.vue 中添加如下:

    <template>
    <view>我的页面</view>
    </template> <script>
    var loginRes;
    export default {
    data() {
    return {};
    },
    onLoad() {
    // 加载定义好的方法
    loginRes = this.checkLogin('../my/my', 2);
    // 没有登录成功,返回空
    if (!loginRes) {
    return;
    }
    },
    methods: {}
    };
    </script> <style></style>
  5. PHP 接口 loginApplets

    public function loginApplets(Request $request, UserInfo $userInfo)
    {
    // 获取数据
    $data['u_openid'] = $request->param('openid', '');
    // 验证数据
    $rule = [
    'u_openid' => 'require|max:200|min:10'
    ];
    $message = [
    'u_openid.require' => 'openid 不能为空',
    'u_openid.max' => 'openid 格式错误',
    'u_openid.min' => 'openid 格式错误'
    ];
    $validate = Validate::rule($rule)->message($message);
    if (!$validate->check($data)) {
    return json(['code' => 1, 'msg' => $validate->getError(), 'res' => null]);
    }
    // 根据 openid 判断是否存在
    $where['u_openid'] = $data['u_openid'];
    $user = $userInfo->selOne($where);
    if (!$user) {
    return json(['code' => 1, 'msg' => '还没授权登录、请先授权然后登录', 'res' => $user]);
    }
    return json(['code' => 0, 'msg' => '已授权获取到用户的数据', 'res' => $user]);
    }
  6. PHP 接口 appletsUserInfo

    public function appletsUserInfo(Request $request, UserInfo $userInfo)
    {
    // 获取数据
    $data['u_openid'] = $request->param('openid', '');
    $data['u_avatarUrl'] = $request->param('avatarUrl', '');
    $data['u_city'] = $request->param('city', '');
    $data['u_country'] = $request->param('country', '');
    $data['u_gender'] = $request->param('gender', '');
    $data['u_language'] = $request->param('language', '');
    $data['u_nickName'] = $request->param('nickName', '');
    // 验证数据
    $rule = [
    'u_openid' => 'require|max:200|min:10',
    'u_avatarUrl' => 'require',
    'u_nickName' => 'require'
    ];
    $message = [
    'u_openid.require' => 'openid 不能为空',
    'u_openid.max' => 'openid 格式错误',
    'u_openid.min' => 'openid 格式错误',
    'u_avatarUrl.require' => '用户头像 不能为空',
    'u_nickName.max' => '用户名 格式错误',
    ];
    $validate = Validate::rule($rule)->message($message);
    if (!$validate->check($data)) {
    return json(['code' => 1, 'msg' => $validate->getError(), 'res' => null]);
    } // 根据 openid 判断是否存在
    $where['u_openid'] = $data['u_openid'];
    $user = $userInfo->selOne($where); // 存在、执行修改
    if ($user) {
    $user_res = $userInfo->updOne($where, $data);
    $res = [];
    $res['u_id'] = $user['u_id'];
    $res['u_regtime'] = $user['u_regtime'];
    } // 不存在、执行添加
    if (empty($user)) {
    $res = [];
    $res = $data;
    $res['u_regtime'] = time();
    $res['u_id'] = $userInfo->addOne($res);
    } // 判断是否添加成功
    if (empty($res['u_id'])) {
    return json(['code' => 1, 'msg' => '注册失败,返回重试', 'res' => null]);
    }
    return json(['code' => 0, 'msg' => 'ok', 'res' => $res]);
    }
  7. 完工!!!

使用 UniApp 实现小程序的微信登录的更多相关文章

  1. 微信小程序与微信公众号同一用户登录问题

    微信小程序与微信公众号同一用户登录问题 最近在做微信小程序与微信公众号登录合并的接口.整理相关资料以及个人认识的心得写了这篇文章与大家一起分享. 首先,简单说下我遇到的问题是我们的程序调用微信小程序得 ...

  2. 微信小程序API~检查登录状态

    wx.checkSession(Object object) 检查登录态是否过期. 通过 wx.login 接口获得的用户登录态拥有一定的时效性.用户越久未使用小程序,用户登录态越有可能失效.反之如果 ...

  3. java实现微信小程序服务端(登录)

    微信小程序如今被广泛使用,微信小程序按照微信官网的定义来说就是: 微信小程序是一种全新的连接用户与服务的方式,它可以在微信内被便捷地获取和传播,同时具有出色的使用体验. 这就是微信小程序的魅力所在,有 ...

  4. 微信小程序实现与登录

    一.小程序的实现原理 在小程序中,渲染层和逻辑层是分开的,双线程同时运行,渲染层和逻辑层这两个通信主体之间的通讯以及通讯主体与第三方服务器之间的通信,都是通过微信客户端进行转发.小程序启动运行两种情况 ...

  5. 微信小程序(原名微信应用号)开发工具0.9版安装教程

    微信小程序全称微信公众平台·小程序,原名微信公众平台·应用号(简称微信应用号) 声明 微信小程序开发工具类似于一个轻量级的IDE集成开发环境,目前仅开放给了少部分受微信官方邀请的人士(据说仅200个名 ...

  6. [小程序开发] 微信小程序内嵌网页web-view开发教程

    为了便于开发者灵活配置小程序,微信小程序开放了内嵌网页能力.这意味着小程序的内容不再局限于pages和large,我们可以借助内嵌网页丰富小程序的内容.下面附上详细的开发教程(含视频操作以及注意事项) ...

  7. 微信小程序之微信登陆 —— 微信小程序教程系列(20)

    简介: 微信登陆,在新建一个微信小程序Hello World项目的时候,就可以看到项目中出现了我们的微信头像,其实这个Hello World项目,就有一个简化版的微信登陆.只不过是,还没有写入到咱们自 ...

  8. 小程序获取微信用户的openid

    小程序获取微信用户的openid //index.js //获取应用实例 const app = getApp() Page({ globalData: { appid: '11121221a89e0 ...

  9. 微信小程序和微信公众号的id是一个吗

    首先,简单说下我遇到的问题是我们的程序调用微信小程序得到openid,然后通过openID得到用户的唯一标识,用户得以登录,然而,当我们调用微信公众号也同样的到openid,同一以用户两个不同的ope ...

随机推荐

  1. ios上表单默认样式

    摘自:http://blog.sina.com.cn/s/blog_7d796c0d0102uyd2.html 可惜不能直接转到博客园. input[type="button"], ...

  2. JS 把数字转换成字母

     JS 把数字转换成字母 2013-03-12 22:28:11 分类: JavaScript String.fromCharCode(addcount+65) 位运算alert(1<<0 ...

  3. 最小生成树 状压+prim hdu2489

    Minimal Ratio TreeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. Spring MVC必须知道的执行流程

    Spring MVC的执行流程 一.名词解释 1.前端控制器(DispatcherServlet) 接收请求,响应结果,相当于转发器,中央处理器 2.处理器映射器(HandlerMapping) 根据 ...

  5. Docker 入门:什么是 Docker ?

    Docker 解决了软件环境部署复杂的问题. 对于一个传统的软件工程,开发人员把写好的代码放到服务器上去运行是一件很头疼的事情,因为常常会出现环境不兼容而导致各种各样的 Bug. 比如说,开发是在 w ...

  6. 在Cent OS云服务器上部署基于TP5后端代码踩坑记录_艾孜尔江撰

    推荐使用镜像安装Cent OS系统,或者在纯净安装完成之后在完成Apache+MySQL+PHP的时候不要每个单独安装,因为这样会出一些三者之间版本不配的问题,网上各种说法都有,查起来也非常困难,版本 ...

  7. Chisel3 - Tutorial - Stack

    https://mp.weixin.qq.com/s/-AVJD1IfvNIJhmZM40DemA   实现后入先出(last in, first out)的栈.   参考链接: https://gi ...

  8. ssh-copy-id to filter out any that...ERROR: Read from socket failed: Connection reset by peer

    ssh-copy-id bigboss1 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter ...

  9. 【Linux】将javaweb项目部署到CentOS的tomcat上

    1.将项目打包成war包 build之后war文件就生成了. 2.拷贝war文件到CentOS的tomcat的项目文件夹中 用WinSCP将文件粘帖进这个文件夹中 /wocloud/tomcat_cl ...

  10. 【JSP】el、jstl、MVC、三层架构

    JSP[Java Server Pages] [概念] java服务器页面 简化的servlet设计 html语法中扩展java 跨平台 JSP全名为Java Server Pages,中文名叫jav ...