login.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // pages/login/login.js
  2. import { $wuxCountDown, $wuxForm,$wuxToptips} from '../../miniprogram_npm/wux-weapp/index'
  3. const api = require('../../api/index')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. layout: 'horizontal',
  10. validateMessages: {
  11. required: '%s 字段为必填',
  12. },
  13. loginMessage:{
  14. phone:'',
  15. code:'',
  16. checkCode:''
  17. },
  18. showDialog:false,
  19. imgURL:null,
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. },
  26. /**
  27. * 生命周期函数--监听页面初次渲染完成
  28. */
  29. onReady() {
  30. },
  31. /**
  32. * 生命周期函数--监听页面显示
  33. */
  34. onShow() {
  35. },
  36. /**
  37. * 页面方法集合
  38. */
  39. //表单栏位值发生变化时
  40. onValuesChange(e) {
  41. console.log(e)
  42. const { changedValues, allValues } = e.detail
  43. // this.setValues(changedValues)
  44. Object.keys(changedValues).forEach((field) => {
  45. this.setData({
  46. ['loginMessage.'+field]: changedValues[field],
  47. })
  48. })
  49. console.log('onValuesChange \n', changedValues, allValues)
  50. },
  51. //获取手机号并打开弹窗
  52. onSendCode(){
  53. var value = this.data.loginMessage.phone
  54. if(!value){
  55. $wuxToptips().error({
  56. hidden: false,
  57. text: '请输入手机号',
  58. duration: 3000,
  59. success() {},
  60. })
  61. return
  62. }
  63. const isTel = (value) => /^1[34578]\d{9}$/.test(value)
  64. if(!isTel(value)){
  65. $wuxToptips().error({
  66. hidden: false,
  67. text: '手机号格式不正确',
  68. duration: 3000,
  69. success() {},
  70. })
  71. return
  72. }
  73. this.changeVerifyCode()
  74. },
  75. //获取输入的校验码
  76. changeInput(e) {
  77. this.setData({
  78. ['loginMessage.checkCode']: e.detail.value,
  79. })
  80. },
  81. //关闭弹窗
  82. closeDialog(){
  83. this.setData(
  84. {
  85. showDialog:false
  86. }
  87. )
  88. },
  89. //获取验证码
  90. getCheckCode(){
  91. if (this.c2 && this.c2.interval) return !1
  92. var data = {
  93. phoneNum:this.data.loginMessage.phone,
  94. checkCode:this.data.loginMessage.checkCode
  95. }
  96. api.sendCode(data).then(res=>{
  97. if(res.code == 200){
  98. this.CountDown()
  99. this.closeDialog()
  100. }
  101. }).catch(error=>{
  102. console.log(error)
  103. // $wuxToptips().error({
  104. // hidden: false,
  105. // text: '获取验证码失败',
  106. // duration: 3000,
  107. // success() {},
  108. // })
  109. })
  110. },
  111. //获取校验码
  112. changeVerifyCode(){
  113. var data = {
  114. phoneNum:this.data.loginMessage.phone
  115. }
  116. api.verifyCode(data).then(res=>{
  117. if(res.code == 200){
  118. console.log(res)
  119. this.setData(
  120. {
  121. imgURL:res.data.captcha,
  122. showDialog:true
  123. }
  124. )
  125. }
  126. }).catch(error=>{
  127. })
  128. },
  129. //倒计时
  130. CountDown(phoneCode='1234'){
  131. this.c2 = new $wuxCountDown({
  132. date: +new Date() + 60000,
  133. onEnd() {
  134. this.setData({
  135. c2: '重新获取验证码',
  136. })
  137. },
  138. render(date) {
  139. const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
  140. date.sec !== 0 &&
  141. this.setData({
  142. c2: sec,
  143. })
  144. },
  145. })
  146. },
  147. //手机号校验
  148. checkPhone(rule, value){
  149. if(!value){
  150. return Promise.reject(new Error('手机号不能为空!'))
  151. }
  152. const isTel = (value) => /^1[34578]\d{9}$/.test(value)
  153. if(!isTel(value)){
  154. return Promise.reject(new Error('手机号输入错误!'))
  155. }
  156. return Promise.resolve()
  157. },
  158. //验证码校验
  159. checkCode(rule, value){
  160. if(!value){
  161. return Promise.reject(new Error('验证码不能为空!'))
  162. }
  163. return Promise.resolve()
  164. },
  165. //登录
  166. onSubmit(){
  167. console.log(this.data.loginMessage)
  168. const { validateFields } = $wuxForm()
  169. validateFields((err, values) => {
  170. if (!err) {
  171. var data = {
  172. phoneNum:this.data.loginMessage.phone,
  173. phoneCode:this.data.loginMessage.code
  174. }
  175. api.loginByPhone(data).then(res=>{
  176. console.log(res)
  177. if(res.code == 200){
  178. this.isLogin(res.data)
  179. }
  180. }).catch(error=>{
  181. })
  182. }
  183. })
  184. },
  185. //登录后操作
  186. isLogin({token,id,isFirst,phone}){
  187. wx.navigateBack()
  188. wx.setStorageSync('token',token)
  189. },
  190. //跳转到注册界面
  191. register(){
  192. wx.navigateTo({
  193. url: '/pages/register/register',
  194. })
  195. },
  196. //微信一键登录
  197. fastLogin(e){
  198. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  199. // 用户同意授权,获取加密信息
  200. const encryptedData = e.detail.encryptedData;
  201. const iv = e.detail.iv;
  202. const code = e.detail.code
  203. // 将 encryptedData 和 iv 发送到后端服务器
  204. var data= {
  205. encryptedData: encryptedData,
  206. iv: iv,
  207. code:code
  208. }
  209. api.loginByWeChat(data).then(res=>{
  210. if(res.code == 200){
  211. this.isLogin(res.data)
  212. }
  213. }).catch(error=>{
  214. })
  215. } else if (e.detail.errMsg === 'getPhoneNumber:fail') {
  216. // 用户拒绝授权
  217. console.log('获取手机号失败');
  218. }
  219. },
  220. /**
  221. * 生命周期函数--监听页面隐藏
  222. */
  223. onHide() {
  224. },
  225. /**
  226. * 生命周期函数--监听页面卸载
  227. */
  228. onUnload() {
  229. },
  230. /**
  231. * 页面相关事件处理函数--监听用户下拉动作
  232. */
  233. onPullDownRefresh() {
  234. },
  235. /**
  236. * 页面上拉触底事件的处理函数
  237. */
  238. onReachBottom() {
  239. },
  240. /**
  241. * 用户点击右上角分享
  242. */
  243. onShareAppMessage() {
  244. }
  245. })