123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- // pages/newIndex/newIndex.js
- import {
- createStoreBindings
- } from "mobx-miniprogram-bindings"
- import {
- store
- } from "../../store/index"
- import {
- request,
- debounce,
- throttle
- } from "../../utils/api"
- // import Toast from '@vant/weapp/toast/toast';
- import Notify from '@vant/weapp/notify/notify';
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- pagHeight: 0,
- // 是否显示授权弹窗 => 是否登录
- showAuthorize: true,
- a: false,
- userName: "",
- phone: "",
- pwd: "",
- onPwd: "",
- userErr: "",
- phoneErr: "",
- pwdErr: "",
- onPwdErr: "",
- show: false,
- onlyCode: "",
- showLogin: true
- },
- async getLogin() {
- wx.login({
- success: (res) => {
- this.setData({
- onlyCode: res.code
- })
- request("/app-api/tutor/h5/getOpenId", "GET", {
- jsCode: res.code
- }).then(data => {
- wx.setStorageSync("openID", data)
- wx.hideToast()
- })
- },
- })
- },
- clickShowLogin() {
- this.setData({
- showLogin: !this.data.showLogin,
- phoneErr: " "
- })
- },
- onChangeUser(e) {
- let data = e.detail;
- this.setData({
- userName: data
- })
- },
- onChangePhone(e) {
- let data = e.detail;
- if (this.data.phone.length < 11) {
- this.setData({
- phone: data
- })
- } else {
- this.setData({
- phoneErr: "请检查手机号长度"
- })
- }
- },
- onChangePwd(e) {
- this.setData({
- pwd: e.detail
- })
- },
- onChangeonPwd(e) {
- this.setData({
- onPwd: e.detail
- })
- this.onPwdBlur()
- },
- userBlur(e) {
- // 是否为空
- let strL = this.data.userName.length
- if (this.data.userName === "") {
- this.setData({
- userErr: "用户名不能为空"
- })
- } else if (strL <= 3 || strL >= 10) {
- this.setData({
- userErr: "用户名长度为3-10字"
- })
- } else {
- this.setData({
- userErr: ''
- })
- }
- },
- phoneBlur(e) {
- // 是否为空
- let rex = /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/
- if (!rex.test(this.data.phone)) {
- this.setData({
- phoneErr: "请输入正确的手机号"
- })
- } else {
- this.setData({
- phoneErr: ""
- })
- }
- },
- pwdBlur() {
- let strl = this.data.pwd.length
- if (strl <= 3 || strl >= 10) {
- this.setData({
- pwdErr: "密码长度为3-10字"
- })
- } else {
- this.setData({
- pwdErr: ""
- })
- }
- },
- onPwdBlur() {
- if (this.data.pwd !== this.data.onPwd || this.data.onPwd.length === 0) {
- this.setData({
- onPwdErr: "请重新确认密码"
- })
- } else {
- this.setData({
- onPwdErr: ""
- })
- }
- },
- goSign: throttle(async function () {
- this.phoneBlur()
- if (this.data.phoneErr == "" && wx.getStorageSync("openID")) {
- const data = await request("/app-api/tutor/h5/getSystem", "GET", {
- openid: wx.getStorageSync("openID"),
- phone: this.data.phone
- })
- if (data.code == 9001) {
- // 出现错误
- this.setData({
- phoneErr: data.msg
- })
- } else if (data.code === 0) {
- // 无错误
- wx.showToast({
- title: '登录成功',
- })
- wx.setStorageSync('userInfo', data.data)
- this.setUserInfo(data.data)
- this.setData({
- showAuthorize: false,
- show: false
- })
- console.log(data.data.reserve, "data")
- if (data.data.reserve == "teacher") {
- wx.switchTab({
- url: '/pages/classes/classes',
- })
- }
- }
- }
- // 改手机号未注册
- }, 1000),
- goLogin: throttle(async function () {
- // 该手机号已注册,
- if (this.data.phoneErr == "" && wx.getStorageSync("openID") && this.data.phone !== "") {
- const data = await request("/app-api/tutor/h5/addSystem", "GET", {
- openid: wx.getStorageSync("openID"),
- phone: this.data.phone
- })
- if (data.code == 0) {
- wx.showToast({
- title: '注册成功!',
- icon: "success"
- })
- } else {
- Notify({
- type: 'warning',
- message: data.msg
- });
- }
- }
- }, 1000),
- clickClose() {
- this.setData({
- showAuthorize: !this.data.showAuthorize
- })
- },
- submitBtn(e) {
- let index = e.target.dataset.index
- // 验证是否登录
- let isUser = this.data.userInfo
- if (!isUser) {
- Notify({
- type: 'warning',
- message: '请先登录'
- });
- this.setData({
- showAuthorize: true
- })
- return;
- }
- if (index == 0) {
- wx.navigateTo({
- url: '/student/pages/notSubmit/notSubmit',
- })
- } else {
- wx.navigateTo({
- url: '/student/pages/submitWork/submitWork',
- })
- }
- // switch (index) {
- // case 0:
- // console.log(222)
- // wx.navigateTo({
- // url: '/student/pages/notSubmit/notSubmit',
- // })
- // break;
- // case 1:
- // console.log(11)
- // wx.navigateTo({
- // url: '/student/pages/notSubmit/notSubmit',
- // })
- // default:
- // break;
- // }
- },
- toAuthorize() {
- this.setData({
- show: true
- });
- },
- onClickHide() {
- this.setData({
- show: false,
- });
- wx.nextTick(() => {
- this.setData({
- userName: "",
- phone: "",
- pwd: "",
- onPwd: "",
- userErr: "",
- phoneErr: "",
- pwdErr: "",
- onPwdErr: ""
- })
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- wx.getSystemInfo({
- success: (res) => {
- console.log(res)
- this.setData({
- pagHeight: res.windowHeight
- })
- }
- })
- this.storeBindings = createStoreBindings(this, {
- store,
- // 基础数据 计算属性
- fields: ["pageHeight", "userInfo"],
- // 修改方法
- actions: ["setPageHeight", "setUserInfo"]
- })
- },
- // 初始化用户信息
- initUserInfo() {
- console.log(this.data.userInfo)
- if (!this.data.userInfo?.id) {
- this.setUserInfo(wx.getStorageSync('userInfo'))
- }
- if (this.data.userInfo) {
- // 如果已登录
- this.setData({
- showAuthorize: false
- })
- if (this.data.userInfo.reserve == "teacher") {
- console.log(this.data.userInfo, "res")
- wx.switchTab({
- url: '/pages/classes/classes',
- })
- }
- } else {
- wx.showToast({
- title: '加载中...',
- icon: "loading"
- })
- this.setData({
- showAuthorize: true
- })
- this.getLogin()
- // 如果没有opid
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- this.initUserInfo()
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- this.initUserInfo()
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|