loginBind.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // pages/login/login.js
  2. const utils = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. uid: '',
  9. account: '',
  10. smsCode: '',
  11. canSend: false, //是否可以发送手机验证码(true为不行,false为可以)
  12. time: 60, //倒计时
  13. timeText: '获取验证码', //倒计时文字
  14. mobileActive: false, //是否激活
  15. codeActive: false, //是否激活
  16. mLine: false, //下划线
  17. cLine: false, //下划线
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. this.setData({
  24. uid: options.uid
  25. })
  26. },
  27. //登录请求
  28. login() {
  29. let params = {
  30. account: utils.trimAll(this.data.account),
  31. smsCode: utils.trimAll(this.data.smsCode),
  32. reqChannel: 5,
  33. loginType: 2
  34. }
  35. if (this.data.uid) {
  36. params.unionId = this.data.uid
  37. }
  38. if (params.account === '') {
  39. utils.toast('手机号码不能为空')
  40. return
  41. }
  42. if (!utils.testMobile(params.account)) {
  43. utils.toast('请填写正确的手机号码')
  44. return
  45. }
  46. if (params.smsCode === '') {
  47. utils.toast('验证码不能为空')
  48. return
  49. }
  50. /* 12345678901服务商 12345678902企业 12345678903运营商*/
  51. // let params = {
  52. // account: '12345678902',
  53. // password: '123456',
  54. // reqChannel: 5,
  55. // loginType: 1
  56. // }
  57. utils.axios({
  58. method: 'post',
  59. url: '/login',
  60. data: params,
  61. sendBefore() {
  62. wx.showLoading({
  63. title: '登录中...',
  64. mask: true
  65. })
  66. },
  67. complete() {
  68. wx.hideLoading()
  69. },
  70. success: res => {
  71. this.loginSuccess(res.data)
  72. }
  73. })
  74. },
  75. //登录成功后的执行函数
  76. loginSuccess(result) {
  77. utils.toast('登录成功')
  78. let userInfo = {
  79. roles: result.usertype,
  80. mobile: result.account,
  81. userName: result.userName,
  82. roleName: result.roleName
  83. }
  84. wx.setStorageSync('expirationTime', result.expirationTime)
  85. wx.setStorageSync('userInfo', userInfo)
  86. wx.setStorageSync('token', result.token)
  87. wx.reLaunch({
  88. url: '/pages/index/index'
  89. })
  90. },
  91. //获取验证码
  92. sendCode(e) {
  93. let params = {
  94. phoneNo: utils.trimAll(this.data.account),
  95. }
  96. if (params.phoneNo === '') {
  97. utils.toast('手机号码不能为空')
  98. return
  99. }
  100. if (!utils.testMobile(params.phoneNo)) {
  101. utils.toast('请填写正确的手机号码')
  102. return
  103. }
  104. utils.axios({
  105. method: 'post',
  106. url: '/sms/getSmscode',
  107. data: params,
  108. sendBefore() {
  109. wx.showLoading({
  110. title: '验证码发送中...',
  111. mask: true
  112. })
  113. },
  114. complete() {
  115. wx.hideLoading()
  116. },
  117. success: () => {
  118. this.countDown()
  119. utils.toast('发送成功')
  120. }
  121. })
  122. },
  123. //获取手机
  124. getAccount(e) {
  125. this.setData({
  126. account: e.detail.value
  127. })
  128. },
  129. //获取验证码
  130. getSmsCode(e) {
  131. this.setData({
  132. smsCode: e.detail.value
  133. })
  134. },
  135. //控制发送验证码按钮是否可以点击
  136. controlSend() {
  137. if (utils.testMobile(this.data.account)) {
  138. this.setData({
  139. canSend: false
  140. })
  141. } else {
  142. this.setData({
  143. canSend: true
  144. })
  145. }
  146. },
  147. //倒计时
  148. countDown() {
  149. let time = this.data.time
  150. this.setData({
  151. canSend: true
  152. })
  153. let clearTimer = setInterval(() => {
  154. if (time <= 0) {
  155. clearInterval(clearTimer)
  156. this.setData({
  157. time: 60,
  158. timeText: '重发验证码',
  159. canSend: false
  160. })
  161. } else {
  162. time--
  163. this.setData({
  164. time: time,
  165. timeText: time + '秒后可重发'
  166. })
  167. }
  168. }, 1000)
  169. },
  170. //聚焦失焦
  171. mfocusInput() {
  172. this.setData({
  173. mobileActive: true,
  174. mLine: true
  175. })
  176. },
  177. mblurInput(e) {
  178. this.setData({
  179. mLine: false
  180. })
  181. if (!utils.trimAll(this.data.account)) {
  182. this.setData({
  183. mobileActive: false
  184. })
  185. }
  186. },
  187. cfocusInput() {
  188. this.setData({
  189. codeActive: true,
  190. cLine: true
  191. })
  192. },
  193. cblurInput(e) {
  194. this.setData({
  195. cLine: false
  196. })
  197. if (!utils.trimAll(this.data.smsCode)) {
  198. this.setData({
  199. codeActive: false
  200. })
  201. }
  202. },
  203. /**
  204. * 生命周期函数--监听页面初次渲染完成
  205. */
  206. onReady: function () {
  207. },
  208. /**
  209. * 生命周期函数--监听页面显示
  210. */
  211. onShow: function () {
  212. },
  213. /**
  214. * 生命周期函数--监听页面隐藏
  215. */
  216. onHide: function () {
  217. },
  218. /**
  219. * 生命周期函数--监听页面卸载
  220. */
  221. onUnload: function () {
  222. },
  223. /**
  224. * 页面相关事件处理函数--监听用户下拉动作
  225. */
  226. onPullDownRefresh: function () {
  227. },
  228. /**
  229. * 页面上拉触底事件的处理函数
  230. */
  231. onReachBottom: function () {
  232. },
  233. /**
  234. * 用户点击右上角分享
  235. */
  236. onShareAppMessage: function (res) {
  237. return {
  238. title: '诺信云',
  239. path: '/pages/index/index'
  240. }
  241. }
  242. })