123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- // pages/login/index.js
- import * as API_Passport from '../../api/passport.js'
- import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
- import * as RegExp from '../../utils/RegExp.js'
- import * as API_Shop from '../../api/shop.js'
- import * as Reg from '../../utils/RegExp.js'
- import * as Common from '../../utils/common.js'
- import {
- $wuxToptips,
- $wuxToast,
- $wuxLoading,
- $wuxDialog
- } from '../../wux/index.js'
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- username: '',
- password: '',
- /** 激活的标签页 */
- tabActive: 0,
- /**用户信息 */
- userInfo: {},
- regmobile_label: '手机号',
- regpassword_label: '密码',
- regpassword2_label: '确认密码',
- username_label: '用户名',
- password_label: '密码',
- /** app已存储用户信息 */
- hasUserInfo: false,
- /** 获取用户信息接口可用? */
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
- buttonClicked:false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- //注册之前删除自动登录记录,注册成功之后需要进行自动登录,因为注册时绑定了微信unionId,可以自动登录成功.如果不删除自动登录记录,自动登录不会进行
- wx.removeStorageSync('login_result')
- wx.getSetting({
- complete: (res) => {
- console.log('getsetting',res)
- if (!res.authSetting['scope.userInfo']) {
- wx.removeStorageSync('welcome')
- wx.removeStorageSync('hasAuthorize')
- wx.redirectTo({
- url: '/pages/welcome/index',
- })
- } else {
- wx.getUserInfo({
- complete: (res) => {
- console.log(res)
- this.setData({
- 'userInfo': res.userInfo
- })
- },
- })
- }
- },
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function() {
- },
- onChange(event) {
- // event.detail 为当前输入的值
- let dataKey = event.currentTarget.dataset.name
- this.setData({
- [dataKey]: event.detail
- })
- },
- login() {
- let that = this
- wx.login({
- success: function(res) {
- let param = {
- username: null,
- password: null
- }
- let ps = {
- ...param,
- ...that.data
- }
- Object.keys(param).forEach(
- n => {
- if (!ps[n]) {
- that.setData({
- [n + '_error']: true,
- [n + '_error_message']: that.data[n + '_label'] + '必填'
- })
- }
- }
- )
- let flag = true
- Object.keys(that.data).forEach(n => {
- if (n.indexOf('_error') > -1 && that.data[n]) {
- flag = false
- }
- })
- if (!flag) {
- return false
- }
- let params = {
- username: that.data.username,
- password: that.data.password,
- uuid: app.globalData.uuid,
- code: res.code,
- mini_program_type: app.globalData.miniProgramType
- }
- API_Passport.bindUser(params).then(
- response => {
- if (response.result === 'bind_success') {
- Common.autoLogin().then(res => {
- $wuxToptips().success({
- hidden: false,
- text: '绑定成功!',
- duration: 2000,
- success: () => {
- wx.switchTab({
- url: '/pages/store/index',
- })
- }
- })
- })
- }
- }
- ).catch(error => {
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: error.message,
- duration: 2000
- })
- })
- }
- })
- },
- /** 输入框失去焦点 */
- onBlur(e) {
- const that = this
- let dataKey = e.currentTarget.dataset.name
- const {
- rules,
- label
- } = e.currentTarget.dataset
- const {
- value
- } = e.detail
- if (rules) {
- that.setData({
- [dataKey + '_error']: false,
- [dataKey + '_error_message']: ''
- })
- try {
- rules.split(',').forEach(n => {
- if (n === 'required' && !value) {
- that.setData({
- [dataKey + '_error']: true,
- [dataKey + '_error_message']: label + '必填'
- })
- throw new Error()
- }
- if (n !== 'required' && !Reg[n].test(value)) {
- that.setData({
- [dataKey + '_error']: true,
- [dataKey + '_error_message']: label + '格式不正确'
- })
- }
- })
- } catch (e) {
- }
- }
- },
- /** tab标签切换 */
- tabChange(event) {
- },
- /** 获取用户信息 */
- getUserInfo(e) {
- const {
- errMsg
- } = e.detail
- if (errMsg === 'getUserInfo:ok') {
- app.globalData.userInfo = e.detail.userInfo
- wx.setStorageSync('userInfo', e.detail.userInfo)
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- }
- },
- /** 注册新用户 */
- register(e) {
- const that=this
- let param = {
- regmobile: null,
- regpassword: null,
- regpassword2: null
- }
- let ps = {
- ...param,
- ...this.data
- }
- Object.keys(param).forEach(
- n => {
- if (!ps[n]) {
- this.setData({
- [n + '_error']: true,
- [n + '_error_message']: this.data[n + '_label'] + '必填'
- })
- }
- }
- )
- let flag = true
- Object.keys(this.data).forEach(n => {
- if (n.indexOf('_error') > -1 && this.data[n]) {
- flag = false
- }
- })
- if (this.data.regpassword2 !== this.data.regpassword) {
- flag = false
- this.setData({
- regpassword2_error: true,
- regpassword2_error_message: '两次密码不一致'
- })
- }
- if (!flag) {
- return false
- }
- that.setData({
- buttonClicked:true
- })
- const para = {
- nick_name: this.data.userInfo.nickName,
- face: this.data.userInfo.avatarUrl,
- sex: this.data.userInfo.gender,
- mobile: this.data.regmobile,
- password: this.data.regpassword
- }
- API_Passport.registerBind(app.globalData.uuid, para).then(response => {
- if (response.result === 'bind_success') {
- let now = Math.round(new Date() / 1000)
- if (response.access_token) {
- //保存登录信息
- const {refresh_token_timeout,access_token_timeout}=response
- app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
- //登录信息写入本地存储
- wx.setStorageSync('authorInfo', app.globalData.authorInfo)
- }
- $wuxToptips().success({
- hidden: false,
- text: '注册成功!',
- duration: 2000,
- success: () => {
- that.setData({
- buttonClicked:false
- })
- //注册成功自动登录,在该函数中统一实现跳转逻辑
- Common.toAutoLogin()
- }
- })
- }
- }).catch(error=>{
- $wuxToptips().error({
- hidden: false,
- text: error.message,
- duration: 2000,
- success:()=>{
- that.setData({
- buttonClicked:false
- })
- }
- })
- })
- }
- })
|