Browse Source

图形验证码

wangxin 2 years atrás
parent
commit
cd231a9ef6

+ 58 - 49
miniprogram/app.json

@@ -1,52 +1,61 @@
 {
   "__usePrivacyCheck__": true,
-	"pages": [
-		"pages/index/index",
-		"pages/login/login",
-		"pages/service_agreement/service_agreement",
-		"pages/success/success",
-		"pages/person/person",
-		"pages/detail/detail",
-		"pages/userInfo/userInfo",
-		"pages/my-apply/my-apply",
-		"pages/my-order/my-order",
-		"pages/payment-records/payment-records",
-		"pages/payment-records-detail/payment-records-detail",
-		"pages/tax-certificate/tax-certificate",
-		"pages/common-setting/common-setting",
-		"pages/version/version",
-		"pages/web-view/web-view",
-		"pages/realNameAuthentication/realNameAuthentication",
-		"pages/realNameAuthentication/success",
-		"ocrsdk/pages/id-card/id-card",
-		"ocrsdk/pages/index/index"
-	],
-	"window": {
-		"backgroundTextStyle": "light",
-		"navigationBarBackgroundColor": "#1890FF",
-		"navigationBarTextStyle": "white"
-	},
-	"tabBar": {
-		"custom": true,
-		"color": "#000000",
-		"selectedColor": "#000000",
-		"backgroundColor": "#000000",
-		"list": [
-			{
-				"pagePath": "pages/my-order/my-order"
-			},
-			{
-				"pagePath": "pages/index/index"
-			},
-			{
-				"pagePath": "pages/person/person"
-			}
-		]
-	},
-	"lazyCodeLoading": "requiredComponents",
-	"navigateToMiniProgramAppIdList": [
-		"wx5a3a7366fd07e119","wxa023b292fd19d41d"
-	],
-	"embeddedAppIdList": ["wxa023b292fd19d41d"],
-	"sitemapLocation": "sitemap.json"
+  "pages": [
+    "pages/index/index",
+    "pages/login/login",
+    "pages/service_agreement/service_agreement",
+    "pages/success/success",
+    "pages/person/person",
+    "pages/detail/detail",
+    "pages/userInfo/userInfo",
+    "pages/my-apply/my-apply",
+    "pages/my-order/my-order",
+    "pages/payment-records/payment-records",
+    "pages/payment-records-detail/payment-records-detail",
+    "pages/tax-certificate/tax-certificate",
+    "pages/common-setting/common-setting",
+    "pages/version/version",
+    "pages/web-view/web-view",
+    "pages/realNameAuthentication/realNameAuthentication",
+    "pages/realNameAuthentication/success",
+    "ocrsdk/pages/id-card/id-card",
+    "ocrsdk/pages/index/index"
+  ],
+  "window": {
+    "backgroundTextStyle": "light",
+    "navigationBarBackgroundColor": "#1890FF",
+    "navigationBarTextStyle": "white"
+  },
+  "tabBar": {
+    "custom": true,
+    "color": "#000000",
+    "selectedColor": "#000000",
+    "backgroundColor": "#000000",
+    "list": [
+      {
+        "pagePath": "pages/my-order/my-order"
+      },
+      {
+        "pagePath": "pages/index/index"
+      },
+      {
+        "pagePath": "pages/person/person"
+      }
+    ]
+  },
+  "plugins": {
+    "captcha": {
+      "version": "2.0.0",
+      "provider": "wx1fe8d9a3cb067a75"
+    }
+  },
+  "lazyCodeLoading": "requiredComponents",
+  "navigateToMiniProgramAppIdList": [
+    "wx5a3a7366fd07e119",
+    "wxa023b292fd19d41d"
+  ],
+  "embeddedAppIdList": [
+    "wxa023b292fd19d41d"
+  ],
+  "sitemapLocation": "sitemap.json"
 }

+ 2 - 1
miniprogram/pages/login/login.json

@@ -1,6 +1,7 @@
 {
   "navigationStyle":"custom",
   "usingComponents": {
-    "privacy-popup": "/component/privacy-popup/privacy-popup"
+    "privacy-popup": "/component/privacy-popup/privacy-popup",
+    "t-captcha": "plugin://captcha/t-captcha"
   }
 }

+ 280 - 243
miniprogram/pages/login/login.ts

@@ -10,261 +10,298 @@ import { baseurl, captcha } from '../../config'
 import { getAuthCodeImpl, phoneLoginImpl } from "../../service/impl/hwUser.impl";
 Page({
 
-	/**
-	 * 页面的初始数据
-	 */
-	data: {
-		color: false, // 验证码字体颜色
-		code_message: '获取验证码', // 验证码文字
-		time, // 时间
-		canLogin: false, //是否可以登陆
-		phone: '', // 手机
-		formatPhone: '',
-		code: '', // 验证码
-		userAgreement: encodeURIComponent(baseurl + '/uploadfile/userAgreement.html'),
-		privacyPolicy: encodeURIComponent(baseurl + '/uploadfile/privacyPolicy.html'),
-		isCheck: false, // 是否勾选用户协议
-	},
-	/**
-	 * 勾选用户协议
-	 */
-	checked() {
-		this.setData({
-			isCheck: !this.data.isCheck,
-			canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && this.data.code && !this.data.isCheck),
-		})
-	},
-	// 打开用户协议 和 隐私政策
-	openWebView(e: any) {
-		wx.navigateTo({
-			url: '/pages/web-view/web-view?url=' + e.currentTarget.dataset.url,
-		})
-	},
-	/**
-	 * 
-	 * 时间倒计时
-	 */
-	countDown(options: any) {
-		const _time = --this.data.time;
-		this.setData({
-			code_message: `已发送(${_time}s)`,
-			color: true,
-			time: _time
-		})
-		setInter = setInterval(_ => {
-			let _time = this.data.time;
-			_time--;
-			if (_time > 0) {
-				this.setData({
-					code_message: `已发送(${_time}s)`,
-					color: true,
-					time: _time
-				})
-			} else {
-				clearInterval(setInter);
-				this.setData({
-					code_message: `重新发送`,
-					color: false,
-					time
-				});
-			}
-		}, speed)
-		getAuthCodeImpl({ phone: this.data.phone, ...options }).then(res => {
-			if (res.errCode === 0) {
-				wx.showToast({
-					icon: 'none',
-					title: '发送成功',
-				})
-			}
-		}).catch(() => {
-			clearInterval(setInter);
-			wx.showModal({
-				title: '提示',
-				content: '发送失败,请稍后重试',
-				showCancel: false,
-				success: () => {
-					this.setData({
-						code_message: `重新发送`,
-						color: false,
-						time
-					})
-				}
-			})
-		})
-	},
-	/**
-	 * 获取验证码
-	 * @phone 电话号码
-	 */
-	getCode() {
-		if (this.data.time === time) {
-			if (app.globalData.phone_zz.test(this.data.phone)) {
-				wx.navigateToMiniProgram({
-					appId: 'wx5a3a7366fd07e119',
-					path: '/pages/captcha/index',
-					extraData: {
-						appId: captcha//您申请的验证码的 appId
-					}
-				})
-			} else {
-				wx.showToast({
-					icon: 'none',
-					title: this.data.phone ? '手机号码格式错误!' : '请填写手机号码!',
-				})
-			}
-		}
-	},
-	/**
-	 * 检查是否可以登陆
-	 * @canLogin 是否可以登陆
-	 * @phone  电话号码
-	 */
-	checkPhone(e: any) {
-		const value: string = e.detail.value;
-		const phone = value.replace(/\s/g, '');
-		clearTimeout(phoneInter);
-		phoneInter = setTimeout(_ => {
-			let formatPhone: string
-			if (phone.length === 11) {
-				formatPhone = phone.replace(/^(.{3})(.*)(.{4})/, '$1 $2 $3');
-				this.setData({
-					formatPhone
-				})
-			}
-			this.setData({
-				canLogin: Boolean(app.globalData.phone_zz.test(value) && this.data.code && this.data.isCheck),
-				phone: phone,
-			})
-		}, interval)
-	},
-	/**
-	 * 检查验证码是否可以登陆
-	 * @canLogin 是否可以登陆
-	 * @code  验证码
-	 */
-	checkCode(e: any) {
-		const value = e.detail.value;
-		clearTimeout(codeInter);
-		codeInter = setTimeout(_ => {
-			this.setData({
-				canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && value && this.data.isCheck),
-				code: value
-			})
-		}, interval)
-	},
-	/**
-	 * 点击登录
-	 * @phone {number} 电话号码
-	 * @code {string} 验证码
-	 * @register_from {注册来源} 0 小程序 1 APP 2 后台
-	 */
+  /**
+   * 页面的初始数据
+   */
+  data: {
+    color: false, // 验证码字体颜色
+    code_message: '获取验证码', // 验证码文字
+    time, // 时间
+    canLogin: false, //是否可以登陆
+    phone: '', // 手机
+    formatPhone: '',
+    code: '', // 验证码
+    userAgreement: encodeURIComponent(baseurl + '/uploadfile/userAgreement.html'),
+    privacyPolicy: encodeURIComponent(baseurl + '/uploadfile/privacyPolicy.html'),
+    isCheck: false, // 是否勾选用户协议
+    captcha
+  },
+  /**
+   * 勾选用户协议
+   */
+  checked() {
+    this.setData({
+      isCheck: !this.data.isCheck,
+      canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && this.data.code && !this.data.isCheck),
+    })
+  },
+  // 打开用户协议 和 隐私政策
+  openWebView(e: any) {
+    wx.navigateTo({
+      url: '/pages/web-view/web-view?url=' + e.currentTarget.dataset.url,
+    })
+  },
+  /**
+   * 
+   * 时间倒计时
+   */
+  countDown(options: any) {
+    const _time = --this.data.time;
+    this.setData({
+      code_message: `已发送(${_time}s)`,
+      color: true,
+      time: _time
+    })
+    setInter = setInterval(_ => {
+      let _time = this.data.time;
+      _time--;
+      if (_time > 0) {
+        this.setData({
+          code_message: `已发送(${_time}s)`,
+          color: true,
+          time: _time
+        })
+      } else {
+        clearInterval(setInter);
+        this.setData({
+          code_message: `重新发送`,
+          color: false,
+          time
+        });
+      }
+    }, speed)
+    getAuthCodeImpl({ phone: this.data.phone, ...options }).then(res => {
+      if (res.errCode === 0) {
+        wx.showToast({
+          icon: 'none',
+          title: '发送成功',
+        })
+      }
+    }).catch(() => {
+      clearInterval(setInter);
+      wx.showModal({
+        title: '提示',
+        content: '发送失败,请稍后重试',
+        showCancel: false,
+        success: () => {
+          this.setData({
+            code_message: `重新发送`,
+            color: false,
+            time
+          })
+        }
+      })
+    })
+  },
+  /**
+   * 获取验证码
+   * @phone 电话号码
+   */
+  getCode() {
+    if (this.data.time === time) {
+      if (app.globalData.phone_zz.test(this.data.phone)) {
+        // wx.navigateToMiniProgram({
+        // 	appId: 'wx5a3a7366fd07e119',
+        // 	path: '/pages/captcha/index',
+        // 	extraData: {
+        // 		appId: captcha//您申请的验证码的 appId
+        // 	}
+        // })
+        this.selectComponent('#captcha').show()
+      } else {
+        wx.showToast({
+          icon: 'none',
+          title: this.data.phone ? '手机号码格式错误!' : '请填写手机号码!',
+        })
+      }
+    }
+  },
+  /**
+   * 检查是否可以登陆
+   * @canLogin 是否可以登陆
+   * @phone  电话号码
+   */
+  checkPhone(e: any) {
+    const value: string = e.detail.value;
+    const phone = value.replace(/\s/g, '');
+    clearTimeout(phoneInter);
+    phoneInter = setTimeout(_ => {
+      let formatPhone: string
+      if (phone.length === 11) {
+        formatPhone = phone.replace(/^(.{3})(.*)(.{4})/, '$1 $2 $3');
+        this.setData({
+          formatPhone
+        })
+      }
+      this.setData({
+        canLogin: Boolean(app.globalData.phone_zz.test(value) && this.data.code && this.data.isCheck),
+        phone: phone,
+      })
+    }, interval)
+  },
+  /**
+   * 检查验证码是否可以登陆
+   * @canLogin 是否可以登陆
+   * @code  验证码
+   */
+  checkCode(e: any) {
+    const value = e.detail.value;
+    clearTimeout(codeInter);
+    codeInter = setTimeout(_ => {
+      this.setData({
+        canLogin: Boolean(app.globalData.phone_zz.test(this.data.phone) && value && this.data.isCheck),
+        code: value
+      })
+    }, interval)
+  },
+  /**
+   * 点击登录
+   * @phone {number} 电话号码
+   * @code {string} 验证码
+   * @register_from {注册来源} 0 小程序 1 APP 2 后台
+   */
   submit() {
     // 首先判断是否可以登陆
     const status = this.data.canLogin;
-		if (status) {
-			phoneLoginImpl({
-				phone: this.data.phone,
-				code: this.data.code,
-				register_from: 0
-			}).then(res => {
-				if (res.errCode === 0) {
-					// 请求成功 存入userID和userToken 并根据来源跳转不同的页面  后台账号跳转首页
-					wx.setStorage({
-						key: 'userId',
-						data: res.data.userInfo.id,
-						success: () => {
-							wx.setStorage({
-								key: 'userToken',
-								data: res.data.userInfo.userToken,
-								success: () => {
-									wx.setStorageSync('userInfo', res.data.userInfo)
-									wx.setStorageSync('firstLogin', true)
-									if (!wx.getStorageInfoSync().keys.length) {
-										wx.setStorageSync('hasClear', true)
-									}
-									// 提交是否登录信息
-									if (res.data.userInfo.isDemonstrate) {
-										wx.reLaunch({
-											url: '/pages/index/index'
-										})
-									} else {
-										wx.reLaunch({
-											url: '/pages/my-order/my-order'
-										})
-									}
-								}
-							})
-						}
-					})
-				}
-			})
-		} else {
-			let content = ''
-			if (!this.data.isCheck) {
-				content = '请勾选用用户协议与隐私政策'
-			}
-			if (!this.data.code) {
-				content = '请填写验证码'
-			}
-			if (!this.data.phone || !app.globalData.phone_zz.test(this.data.phone)) {
-				content = '电话号码格式错误'
-			}
-			wx.showModal({
-				title: '登录失败',
-				content,
-				showCancel: false,
-				confirmColor: '#31364C',
-			})
-		}
+    if (status) {
+      phoneLoginImpl({
+        phone: this.data.phone,
+        code: this.data.code,
+        register_from: 0
+      }).then(res => {
+        if (res.errCode === 0) {
+          // 请求成功 存入userID和userToken 并根据来源跳转不同的页面  后台账号跳转首页
+          wx.setStorage({
+            key: 'userId',
+            data: res.data.userInfo.id,
+            success: () => {
+              wx.setStorage({
+                key: 'userToken',
+                data: res.data.userInfo.userToken,
+                success: () => {
+                  wx.setStorageSync('userInfo', res.data.userInfo)
+                  wx.setStorageSync('firstLogin', true)
+                  if (!wx.getStorageInfoSync().keys.length) {
+                    wx.setStorageSync('hasClear', true)
+                  }
+                  // 提交是否登录信息
+                  if (res.data.userInfo.isDemonstrate) {
+                    wx.reLaunch({
+                      url: '/pages/index/index'
+                    })
+                  } else {
+                    wx.reLaunch({
+                      url: '/pages/my-order/my-order'
+                    })
+                  }
+                }
+              })
+            }
+          })
+        }
+      })
+    } else {
+      let content = ''
+      if (!this.data.isCheck) {
+        content = '请勾选用用户协议与隐私政策'
+      }
+      if (!this.data.code) {
+        content = '请填写验证码'
+      }
+      if (!this.data.phone || !app.globalData.phone_zz.test(this.data.phone)) {
+        content = '电话号码格式错误'
+      }
+      wx.showModal({
+        title: '登录失败',
+        content,
+        showCancel: false,
+        confirmColor: '#31364C',
+      })
+    }
+  },
+  // 验证码验证结果回调
+  handlerVerify: function (ev: any) {
+    // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail
+    if (ev.detail.ret === 0) {
+      // 验证成功
+      console.log('randstr:', ev.detail.randstr)
+      console.log('ticket:', ev.detail.ticket)
+      this.countDown({ ticket: ev.detail.ticket, randstr: ev.detail.randstr })
+    } else {
+      // 验证失败
+      // 请不要在验证失败中调用refresh,验证码内部会进行相应处理
+    }
+  },
+  // 验证码准备就绪
+  handlerReady: function () {
+    console.log('验证码准备就绪')
   },
-	/**
-	 * 生命周期函数--监听页面加载
-	 */
-	onLoad: function (options) {
+  // 验证码弹框准备关闭
+  handlerClose: function (ev: any) {
+    // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail,ret为0是验证完成后自动关闭验证码弹窗,ret为2是用户主动点击了关闭按钮关闭验证码弹窗
+    if (ev && ev.detail.ret && ev.detail.ret === 2) {
+      console.log('点击了关闭按钮,验证码弹框准备关闭');
+    } else {
+      console.log('验证完成,验证码弹框准备关闭');
+    }
+  },
+  // 验证码出错
+  handlerError: function (ev: any) {
+    console.log(ev.detail.errMsg)
+    wx.showModal({
+      title: '提示',
+      content: ev.detail.errMsg,
+      showCancel: false
+    })
+  },
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad: function (options) {
     this.options = options;
-	},
+  },
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady: function () {
 
-	/**
-	 * 生命周期函数--监听页面初次渲染完成
-	 */
-	onReady: function () {
+  },
 
-	},
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow: function () {
+    wx.hideHomeButton();
+    // const captchaResult = app.globalData.captchaResult;
+    // console.log(captchaResult);
+    // if (captchaResult && captchaResult.ret === 0) {
+    // 	// 将验证码的结果返回至服务端校验
+    // 	const ticket = captchaResult.ticket;
+    // 	const randstr = captchaResult.randstr;
 
-	/**
-	 * 生命周期函数--监听页面显示
-	 */
-	onShow: function () {
-		wx.hideHomeButton();
-		const captchaResult = app.globalData.captchaResult;
-		console.log(captchaResult);
-		if (captchaResult && captchaResult.ret === 0) {
-			// 将验证码的结果返回至服务端校验
-			const ticket = captchaResult.ticket;
-			const randstr = captchaResult.randstr;
-			this.countDown({ ticket, randstr })
-			app.globalData.captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
-		}
-	},
+    // 	app.globalData.captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
+    // }
+  },
 
-	/**
-	 * 生命周期函数--监听页面隐藏
-	 */
-	onHide: function () {
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide: function () {
 
-	},
+  },
 
-	/**
-	 * 生命周期函数--监听页面卸载
-	 */
-	onUnload: function () {
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload: function () {
 
-	},
+  },
 
-	/**
-	 * 页面相关事件处理函数--监听用户下拉动作
-	 */
-	onPullDownRefresh: function () {
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh: function () {
 
-	}
+  }
 })

+ 35 - 37
miniprogram/pages/login/login.wxml

@@ -30,52 +30,50 @@
 </view> -->
 
 <view class="container safe-area-bottom">
-	<view class="container-top">
-		<image src="../../assets/img/login_top.png"></image>
-	</view>
-	<view class="content">
-		<view class="logo">
-			<image src="../../assets/img/login_logo.png"></image>
-		</view>
-		<view class="title">
-			欢迎使用 开心灵工
-		</view>
+  <view class="container-top">
+    <image src="../../assets/img/login_top.png"></image>
+  </view>
+  <view class="content">
+    <view class="logo">
+      <image src="../../assets/img/login_logo.png"></image>
+    </view>
+    <view class="title">
+      欢迎使用 开心灵工
+    </view>
 
-		<view class="content-main">
+    <view class="content-main">
 
-			<view class="content-main-input">
-				<input type="text" placeholder="请输入手机号码" value="{{formatPhone}}" bindinput="checkPhone" name='phone' maxlength="13"
-					placeholder-class="placeholder-class" />
-				<text class="code-text {{color&&'color'}}" bindtap="getCode">{{code_message}}</text>
-			</view>
+      <view class="content-main-input">
+        <input type="text" placeholder="请输入手机号码" value="{{formatPhone}}" bindinput="checkPhone" name='phone' maxlength="13" placeholder-class="placeholder-class" />
+        <text class="code-text {{color&&'color'}}" bindtap="getCode">{{code_message}}</text>
+      </view>
 
-			<view class="content-main-input">
-				<input type="number" placeholder="请输入手机验证码" bindinput="checkCode" name="code"
-					placeholder-class="placeholder-class" />
-			</view>
+      <view class="content-main-input">
+        <input type="number" placeholder="请输入手机验证码" bindinput="checkCode" name="code" placeholder-class="placeholder-class" />
+      </view>
 
-		</view>
+    </view>
 
-		<view class="tips" bindtap="checked">
-			<view class="checkbox">
-				<view class="checked" hidden="{{!isCheck}}"></view>
-			</view>
-			<view>登录即表示您已同意<text data-url="{{userAgreement}}" catchtap="openWebView"> 用户协议 </text>和<text
-					data-url="{{privacyPolicy}}" catchtap="openWebView"> 隐私政策 </text>
-			</view>
-		</view>
+    <view class="tips" bindtap="checked">
+      <view class="checkbox">
+        <view class="checked" hidden="{{!isCheck}}"></view>
+      </view>
+      <view>登录即表示您已同意<text data-url="{{userAgreement}}" catchtap="openWebView"> 用户协议 </text>和<text data-url="{{privacyPolicy}}" catchtap="openWebView"> 隐私政策 </text>
+      </view>
+    </view>
 
-	</view>
+  </view>
 
-	<view class="bottom">
+  <view class="bottom">
     <privacy-popup bind:privacySubmit="submit">
       <view class="button {{canLogin?'button-login':''}}">登录</view>
     </privacy-popup>
-		<text>仅内部授权用户可登录</text>
-	</view>
+    <text>仅内部授权用户可登录</text>
+  </view>
 
 
-	<view class="container-bottom">
-		<image src="../../assets/img/login_bottom.png"></image>
-	</view>
-</view>
+  <view class="container-bottom">
+    <image src="../../assets/img/login_bottom.png"></image>
+  </view>
+</view>
+<t-captcha id="captcha" app-id="{{captcha}}" bindverify="handlerVerify" bindready="handlerReady" bindclose="handlerClose" binderror="handlerError" />