newIndex.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // pages/newIndex/newIndex.js
  2. import {
  3. createStoreBindings
  4. } from "mobx-miniprogram-bindings"
  5. import {
  6. store
  7. } from "../../store/index"
  8. import {
  9. request,
  10. debounce,
  11. throttle
  12. } from "../../utils/api"
  13. // import Toast from '@vant/weapp/toast/toast';
  14. import Notify from '@vant/weapp/notify/notify';
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. pagHeight: 0,
  21. // 是否显示授权弹窗 => 是否登录
  22. showAuthorize: true,
  23. a: false,
  24. userName: "",
  25. phone: "",
  26. pwd: "",
  27. onPwd: "",
  28. userErr: "",
  29. phoneErr: "",
  30. pwdErr: "",
  31. onPwdErr: "",
  32. show: false,
  33. onlyCode: "",
  34. showLogin: true
  35. },
  36. async getLogin() {
  37. wx.login({
  38. success: (res) => {
  39. this.setData({
  40. onlyCode: res.code
  41. })
  42. request("/app-api/tutor/h5/getOpenId", "GET", {
  43. jsCode: res.code
  44. }).then(data => {
  45. wx.setStorageSync("openID", data)
  46. wx.hideToast()
  47. })
  48. },
  49. })
  50. },
  51. clickShowLogin() {
  52. this.setData({
  53. showLogin: !this.data.showLogin,
  54. phoneErr: " "
  55. })
  56. },
  57. onChangeUser(e) {
  58. let data = e.detail;
  59. this.setData({
  60. userName: data
  61. })
  62. },
  63. onChangePhone(e) {
  64. let data = e.detail;
  65. if (this.data.phone.length < 11) {
  66. this.setData({
  67. phone: data
  68. })
  69. } else {
  70. this.setData({
  71. phoneErr: "请检查手机号长度"
  72. })
  73. }
  74. },
  75. onChangePwd(e) {
  76. this.setData({
  77. pwd: e.detail
  78. })
  79. },
  80. onChangeonPwd(e) {
  81. this.setData({
  82. onPwd: e.detail
  83. })
  84. this.onPwdBlur()
  85. },
  86. userBlur(e) {
  87. // 是否为空
  88. let strL = this.data.userName.length
  89. if (this.data.userName === "") {
  90. this.setData({
  91. userErr: "用户名不能为空"
  92. })
  93. } else if (strL <= 3 || strL >= 10) {
  94. this.setData({
  95. userErr: "用户名长度为3-10字"
  96. })
  97. } else {
  98. this.setData({
  99. userErr: ''
  100. })
  101. }
  102. },
  103. phoneBlur(e) {
  104. // 是否为空
  105. let rex = /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/
  106. if (!rex.test(this.data.phone)) {
  107. this.setData({
  108. phoneErr: "请输入正确的手机号"
  109. })
  110. } else {
  111. this.setData({
  112. phoneErr: ""
  113. })
  114. }
  115. },
  116. pwdBlur() {
  117. let strl = this.data.pwd.length
  118. if (strl <= 3 || strl >= 10) {
  119. this.setData({
  120. pwdErr: "密码长度为3-10字"
  121. })
  122. } else {
  123. this.setData({
  124. pwdErr: ""
  125. })
  126. }
  127. },
  128. onPwdBlur() {
  129. if (this.data.pwd !== this.data.onPwd || this.data.onPwd.length === 0) {
  130. this.setData({
  131. onPwdErr: "请重新确认密码"
  132. })
  133. } else {
  134. this.setData({
  135. onPwdErr: ""
  136. })
  137. }
  138. },
  139. goSign: throttle(async function () {
  140. this.phoneBlur()
  141. if (this.data.phoneErr == "" && wx.getStorageSync("openID")) {
  142. const data = await request("/app-api/tutor/h5/getSystem", "GET", {
  143. openid: wx.getStorageSync("openID"),
  144. phone: this.data.phone
  145. })
  146. if (data.code == 9001) {
  147. // 出现错误
  148. this.setData({
  149. phoneErr: data.msg
  150. })
  151. } else if (data.code === 0) {
  152. // 无错误
  153. wx.showToast({
  154. title: '登录成功',
  155. })
  156. wx.setStorageSync('userInfo', data.data)
  157. this.setUserInfo(data.data)
  158. this.setData({
  159. showAuthorize: false,
  160. show: false
  161. })
  162. console.log(data.data.reserve, "data")
  163. if (data.data.reserve == "teacher") {
  164. wx.switchTab({
  165. url: '/pages/classes/classes',
  166. })
  167. }
  168. }
  169. }
  170. // 改手机号未注册
  171. }, 1000),
  172. goLogin: throttle(async function () {
  173. // 该手机号已注册,
  174. if (this.data.phoneErr == "" && wx.getStorageSync("openID") && this.data.phone !== "") {
  175. const data = await request("/app-api/tutor/h5/addSystem", "GET", {
  176. openid: wx.getStorageSync("openID"),
  177. phone: this.data.phone
  178. })
  179. if (data.code == 0) {
  180. wx.showToast({
  181. title: '注册成功!',
  182. icon: "success"
  183. })
  184. } else {
  185. Notify({
  186. type: 'warning',
  187. message: data.msg
  188. });
  189. }
  190. }
  191. }, 1000),
  192. clickClose() {
  193. this.setData({
  194. showAuthorize: !this.data.showAuthorize
  195. })
  196. },
  197. submitBtn(e) {
  198. let index = e.target.dataset.index
  199. // 验证是否登录
  200. let isUser = this.data.userInfo
  201. if (!isUser) {
  202. Notify({
  203. type: 'warning',
  204. message: '请先登录'
  205. });
  206. this.setData({
  207. showAuthorize: true
  208. })
  209. return;
  210. }
  211. if (index == 0) {
  212. wx.navigateTo({
  213. url: '/student/pages/notSubmit/notSubmit',
  214. })
  215. } else {
  216. wx.navigateTo({
  217. url: '/student/pages/submitWork/submitWork',
  218. })
  219. }
  220. // switch (index) {
  221. // case 0:
  222. // console.log(222)
  223. // wx.navigateTo({
  224. // url: '/student/pages/notSubmit/notSubmit',
  225. // })
  226. // break;
  227. // case 1:
  228. // console.log(11)
  229. // wx.navigateTo({
  230. // url: '/student/pages/notSubmit/notSubmit',
  231. // })
  232. // default:
  233. // break;
  234. // }
  235. },
  236. toAuthorize() {
  237. this.setData({
  238. show: true
  239. });
  240. },
  241. onClickHide() {
  242. this.setData({
  243. show: false,
  244. });
  245. wx.nextTick(() => {
  246. this.setData({
  247. userName: "",
  248. phone: "",
  249. pwd: "",
  250. onPwd: "",
  251. userErr: "",
  252. phoneErr: "",
  253. pwdErr: "",
  254. onPwdErr: ""
  255. })
  256. })
  257. },
  258. /**
  259. * 生命周期函数--监听页面加载
  260. */
  261. onLoad(options) {
  262. wx.getSystemInfo({
  263. success: (res) => {
  264. console.log(res)
  265. this.setData({
  266. pagHeight: res.windowHeight
  267. })
  268. }
  269. })
  270. this.storeBindings = createStoreBindings(this, {
  271. store,
  272. // 基础数据 计算属性
  273. fields: ["pageHeight", "userInfo"],
  274. // 修改方法
  275. actions: ["setPageHeight", "setUserInfo"]
  276. })
  277. },
  278. // 初始化用户信息
  279. initUserInfo() {
  280. console.log(this.data.userInfo)
  281. if (!this.data.userInfo?.id) {
  282. this.setUserInfo(wx.getStorageSync('userInfo'))
  283. }
  284. if (this.data.userInfo) {
  285. // 如果已登录
  286. this.setData({
  287. showAuthorize: false
  288. })
  289. if (this.data.userInfo.reserve == "teacher") {
  290. console.log(this.data.userInfo, "res")
  291. wx.switchTab({
  292. url: '/pages/classes/classes',
  293. })
  294. }
  295. } else {
  296. wx.showToast({
  297. title: '加载中...',
  298. icon: "loading"
  299. })
  300. this.setData({
  301. showAuthorize: true
  302. })
  303. this.getLogin()
  304. // 如果没有opid
  305. }
  306. },
  307. /**
  308. * 生命周期函数--监听页面初次渲染完成
  309. */
  310. onReady() {
  311. this.initUserInfo()
  312. },
  313. /**
  314. * 生命周期函数--监听页面显示
  315. */
  316. onShow() {
  317. this.initUserInfo()
  318. },
  319. /**
  320. * 生命周期函数--监听页面隐藏
  321. */
  322. onHide() {
  323. },
  324. /**
  325. * 生命周期函数--监听页面卸载
  326. */
  327. onUnload() {
  328. },
  329. /**
  330. * 页面相关事件处理函数--监听用户下拉动作
  331. */
  332. onPullDownRefresh() {
  333. },
  334. /**
  335. * 页面上拉触底事件的处理函数
  336. */
  337. onReachBottom() {
  338. },
  339. /**
  340. * 用户点击右上角分享
  341. */
  342. onShareAppMessage() {
  343. }
  344. })