login.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const app = getApp();
  2. Page({
  3. data: {
  4. // loginName:"13153864796",
  5. // loginPwd:"123456",
  6. isVivo:false,
  7. loginNav:"pwd",
  8. canClick:false,
  9. codeBtnTxt:"发送验证码",
  10. code:'',
  11. },
  12. onLoad: function (options) {
  13. var that = this
  14. //获取手机型号,vivo手机安全键盘bug
  15. wx.getSystemInfo({
  16. success:function(res){
  17. if(res.brand=="vivo"){
  18. that.data.isVivo = true;
  19. }
  20. }
  21. });
  22. wx.removeStorageSync("token")
  23. wx.removeStorageSync("userInfo")
  24. if(options.phoneNumber){
  25. this.setData({
  26. loginName:options.phoneNumber,
  27. loginPwd:options.password
  28. })
  29. }
  30. this.getCaptchaImage();
  31. },
  32. setNav:function(e){//登录方式切换
  33. this.setData({
  34. loginNav:e.currentTarget.dataset.type
  35. })
  36. this.getCaptchaImage();
  37. },
  38. setLoginName: function (e) {//手机号输入校验
  39. var loginName = e.detail.value.replace(/[^\d]/g, "").substr(0,11)
  40. //验证手机号
  41. var myreg = /^[1][0-9]{10}$/;
  42. this.setData({
  43. loginName,
  44. canClick : myreg.test(loginName)
  45. })
  46. },
  47. setLoginPwd: function (e) {//密码输入绑定
  48. var loginPwd = e.detail.value
  49. this.setData({
  50. loginPwd: loginPwd
  51. })
  52. if(this.data.isVivo){//vivo密码键盘bug,超过1秒没输入收起键盘
  53. if(this.timer)clearTimeout(this.timer)
  54. this.timer = setTimeout(function(){
  55. wx.hideKeyboard();
  56. },1000)
  57. }
  58. },
  59. getValidCode:function(e){
  60. // if(!this.data.canClick){
  61. // return
  62. // }
  63. //验证手机号
  64. var myreg = /^[1][0-9]{10}$/;
  65. if (!myreg.test(this.data.loginName)) {
  66. app.showToptip(this, "worning", "手机号输入有误")
  67. return;
  68. }
  69. var that=this;
  70. wx.showLoading({
  71. title: '发送中...',
  72. mask: true
  73. })
  74. app.requestP({
  75. url: "/sms/getSmsCodeByMobile",
  76. needToken:false,
  77. contentType:' application/json',
  78. data: {
  79. mobile: that.data.loginName
  80. }
  81. }).then((res) => {
  82. app.showToptip(that, "success","发送成功")
  83. that.data.lastSendTime = Date.now()
  84. that.setData({
  85. canClick: false
  86. })
  87. //定时器触发倒计时,
  88. var times = 60
  89. var i = setInterval(function () {
  90. times--
  91. if (times == 0) {
  92. that.setData({
  93. canClick: true,
  94. codeBtnTxt: "重新获取",
  95. })
  96. clearInterval(i)
  97. } else {
  98. that.setData({
  99. codeBtnTxt: times + "S",
  100. })
  101. }
  102. }, 1000)
  103. }).catch(function(r){
  104. app.showToptip(that, "error",r.data.msg)
  105. })
  106. },
  107. setCode:function(e){//验证码输入绑定
  108. this.setData({
  109. code: e.detail.value.replace(/[^\d]/g, "").substr(0,6)
  110. })
  111. },
  112. setCaptChaImage:function(e){
  113. this.setData({
  114. tCaptChaCode:e.detail.value
  115. })
  116. },
  117. loginIn: function (e) {//登录
  118. const that=this
  119. let url = ""
  120. let obj = {}
  121. //验证手机号
  122. var myreg = /^[1][0-9]{10}$/;
  123. if (!myreg.test(this.data.loginName)) {
  124. app.showToptip(this, "worning", "手机号输入有误")
  125. return;
  126. }
  127. if(that.data.loginNav == "pwd"){
  128. url = "/login"
  129. //验证密码必填
  130. if (!that.data.loginPwd) {
  131. app.showToptip(this, "worning", "请输入密码")
  132. return;
  133. }
  134. obj= {
  135. "password": that.data.loginPwd,
  136. "username": that.data.loginName,
  137. "uuid":that.data.uuid,
  138. "code":that.data.tCaptChaCode
  139. }
  140. }
  141. if(that.data.loginNav == "code"){
  142. url = "/login/mobileLogin"
  143. //验证码必填
  144. if (!that.data.code) {
  145. app.showToptip(this, "worning", "请输入验证码")
  146. return;
  147. }
  148. obj= {
  149. code: that.data.code,
  150. mobile: that.data.loginName,
  151. }
  152. }
  153. wx.showLoading({
  154. title: '正在登录...',
  155. mask: true
  156. })
  157. app.requestP({
  158. url : url,
  159. needToken:false,
  160. data: obj,
  161. contentType:' application/json'
  162. }).then((res) => {
  163. wx.setStorageSync("token",res.data.token)
  164. app.requestP({
  165. method:"post",
  166. url: "/user/getUserInfo",
  167. }).then(function(r){
  168. var data = r.data;
  169. wx.setStorageSync('userInfo',r.data.data)
  170. wx.switchTab({
  171. url: '/pages/index/index',
  172. })
  173. })
  174. }).catch((err) => {
  175. that.getCaptchaImage();
  176. app.showToptip(that,"error", err.data.msg)
  177. });
  178. },
  179. getCaptchaImage(){
  180. var that = this;
  181. app.requestP({
  182. url: "/captchaImage",
  183. needToken:false,
  184. method:'get',
  185. // contentType:' application/json',
  186. }).then(res=>{
  187. that.setData({
  188. uuid:res.data.uuid,
  189. img:'data:image/gif;base64,'+res.data.img
  190. })
  191. })
  192. }
  193. })