login.js 6.5 KB

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