123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // pages/login/login.js
- const app = getApp()
- const util = require("../../utils/util")
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- isPage: '',
- isId: ''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.showLoading({
- title: '加载中',
- mask: true
- })
- if(options.isPage){
- this.setData({
- isPage: options.isPage,
- isId: options.isId
- })
- }
- this.getVersion()
- },
- //判断版本号
- getVersion: function () {
- util.doPost(
- 'getVersion', {
- appId: app.globalData.appId
- }
- ).then(res => {
- if (res.version != app.globalData.ver) {
- wx.setStorageSync('isShow', false)
- } else {
- wx.setStorageSync('isShow', true)
- this.getOpenId()
- }
- })
- },
- // 登录
- getOpenId: function () {
- wx.login({
- success: res => {
- util.doPost(
- 'getOpenId', {
- appId: app.globalData.appId,
- code: res.code
- },
- ).then(res => {
- if (res.success == 1) {
- wx.setStorageSync('openId', res.data.openId)
- wx.hideLoading({
- })
- }
- })
- },
- fail: res => {
- wx.hideLoading({
- success: (res) => {
- wx.showToast({
- title: '提示',
- icon: 'none',
- content: '连接失败,请重试'
- })
- },
- })
- }
- })
- },
- //授权手机号
- getPhoneNumber: function (e) {
- const that = this;
- if (e.detail.errMsg == "getPhoneNumber:ok") {
- wx.showLoading({
- title: '正在登陆',
- mask: true
- })
- util.doPost(
- 'getPhoneNumber', {
- encryptedData: e.detail.encryptedData,
- iv: e.detail.iv,
- openId: wx.getStorageSync('openId'),
- }
- ).then(rs => {
- if (rs.success > 0) {
- wx.setStorageSync('phone', rs.data.phoneNumber);
- // wx.setStorageSync('phone', '13871019618')
- this.autoLogin();
- } else {
- wx.showToast({
- title: '手机号验证失败',
- })
- }
- })
- }
- that.setData({
- model: false,
- localtion: true
- })
- },
- autoLogin: function () {
- util.doPost(
- 'autoLogin', {
- openId: wx.getStorageSync('openId'),
- }
- ).then(rs => {
- wx.hideLoading()
- if (rs.success > 0) {
- wx.setStorageSync('sso-token', rs.data.token);
- wx.showToast({
- title: '登录成功',
- })
- if(this.data.isPage){
- wx.redirectTo({
- url: '/pages/'+this.data.isPage+"/"+this.data.isPage+"?proId="+this.data.isId,
- })
- } else {
- wx.switchTab({
- url: '/pages/index/index'
- })
- }
-
- } else {
- wx.showToast({
- title: rs.errMsg,
- icon: 'none',
- duration: 3000
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|