register.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <view class="app">
  3. <!-- logo -->
  4. <view class="header">
  5. <u-avatar src="/static/logo.png" size="200"></u-avatar>
  6. <view class="nickname"></view>
  7. </view>
  8. <!-- 输入框 -->
  9. <view class="body">
  10. <u-form :model="form1" ref="uForm" style="width: 100%;">
  11. <u-form-item label-width="0" prop="username" :borderBottom="false">
  12. <u-input border placeholder="请输入手机号" v-model="form1.username" @blur="phoneBlurHandle" type="text" height="90"></u-input>
  13. </u-form-item>
  14. <u-form-item label-width="0" prop="password" :borderBottom="false">
  15. <u-input border placeholder="请输入密码" v-model="form1.password" type="password" height="90"></u-input>
  16. </u-form-item>
  17. <u-form-item label-width="0" prop="re_password" :borderBottom="false">
  18. <u-input border placeholder="请重复输入密码" v-model="form1.re_password" type="password" height="90"></u-input>
  19. </u-form-item>
  20. <u-form-item label-width="0" prop="image_code" :borderBottom="false" v-if="isImgCoedShow">
  21. <u-input border placeholder="请输入右侧4位字符" v-model="form1.image_code" type="text" height="90"></u-input>
  22. <view slot="right" style="display: inline-block;" @click="getImgCode()">
  23. <u-image width="150rpx" height="90rpx" :src="imgCodeUrl"></u-image>
  24. </view>
  25. </u-form-item>
  26. <u-form-item label-width="0" prop="sms_code" :borderBottom="false">
  27. <u-input border placeholder="请输入短信验证码" v-model="form1.sms_code" type="text" height="90"></u-input>
  28. <view slot="right" style="display: inline-block;">
  29. <view v-if="smsTimer" class="scoped-sms-btn">{{smsTimeNum}}s</view>
  30. <view v-else class="scoped-sms-btn s" @click="smsHandle()">获取短信</view>
  31. </view>
  32. </u-form-item>
  33. </u-form>
  34. <u-gap></u-gap>
  35. <u-button class="bwin-btn-100 u-m-b-10" type="primary" @click="register">注册</u-button>
  36. <!-- <u-button class="bwin-btn-100">取消</u-button> -->
  37. </view>
  38. <!-- 忘记密码导航 -->
  39. <view class="function-row">
  40. <!-- <navigator url="/pages/user/login/login">忘记密码</navigator>
  41. <text style="margin: 0 16rpx;">|</text> -->
  42. <navigator url="/pages/user/login/login">直接登录</navigator>
  43. </view>
  44. <view class="footer">
  45. 注册、登录即代表同意
  46. <text class="agreement">《用户协议》</text>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. var that;
  52. export default {
  53. data() {
  54. // 页面数据变量
  55. return {
  56. isImgCoedShow: false,
  57. imgCodeUrl: '',
  58. smsTimer: null,
  59. smsTimeNum: 60,
  60. // init请求返回的数据
  61. data: {},
  62. // 表单请求数据
  63. form1: {
  64. username: null,
  65. password: '',
  66. re_password: '',
  67. image_code: '',
  68. sms_code: '',
  69. },
  70. rules: {
  71. username: [
  72. {
  73. required: true,
  74. message: '请输入账号',
  75. // 可以单个或者同时写两个触发验证方式
  76. trigger: 'blur,change'
  77. },
  78. ],
  79. password: [
  80. {
  81. min: 6,
  82. message: '密码不能少于6个字符',
  83. trigger: 'change'
  84. },
  85. ],
  86. re_password: [
  87. {
  88. min: 6,
  89. message: '密码不能少于6个字符',
  90. trigger: 'change'
  91. },
  92. {
  93. // 自定义验证函数,见上说明
  94. validator: (rule, value, callback) => {
  95. return that.form1.password == value;
  96. },
  97. message: '与上次输入的不一致',
  98. // 触发器可以同时用blur和change
  99. trigger: ['change', 'blur']
  100. }
  101. ],
  102. image_code: [
  103. {
  104. required: true,
  105. message: '请输入图形验证码',
  106. trigger: 'blur,change'
  107. }
  108. ],
  109. sms_code: [
  110. {
  111. required: true,
  112. message: '请输入短信验证码',
  113. trigger: 'blur,change'
  114. }
  115. ]
  116. },
  117. scrollTop: 0
  118. };
  119. },
  120. onPageScroll(e) {
  121. this.scrollTop = e.scrollTop;
  122. },
  123. // 监听 - 页面每次【加载时】执行(如:前进)
  124. onLoad(options = {}) {
  125. that = this;
  126. that.options = options;
  127. if (this.form1.username) {
  128. this.getImgCode()
  129. }
  130. // if (xxxx.checkToken()) {
  131. // xxxx.toast('您已登录', 'none', 1500, true, function() {
  132. // xxxx.navigateToHome();
  133. // });
  134. // }
  135. },
  136. // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快,会在页面进入动画完成前触发
  137. onReady() {
  138. this.$refs.uForm.setRules(this.rules);
  139. },
  140. /**
  141. * 监听 - 点击右上角转发时 文档 https://uniapp.dcloud.io/api/plugins/share?id=onshareappmessage
  142. * 如果删除onShareAppMessage函数,则微信小程序右上角转发按钮会自动变灰
  143. */
  144. onShareAppMessage(options) {},
  145. // 函数
  146. methods: {
  147. getImgCode () {
  148. this.imgCodeUrl = `${uni.baseUrl}api/captcha?phone=${this.form1.username}&r=${Math.random()}`
  149. },
  150. phoneBlurHandle () {
  151. if (this.form1.username && this.form1.username.length === 11) {
  152. uni.api.base.apicheckcaptcha({phone: this.form1.username}).then(res => {
  153. this.isImgCoedShow = res
  154. this.getImgCode()
  155. })
  156. } else {
  157. this.isImgCoedShow = false
  158. }
  159. },
  160. smsHandle () {
  161. if (this.form1.username && this.form1.username.length === 11) {
  162. let params = {
  163. phone: this.form1.username,
  164. user_type: 'wx',
  165. }
  166. if (this.isImgCoedShow) {
  167. if (!this.form1.image_code) {
  168. uni.$msg('请输入图形验证码')
  169. return
  170. }
  171. params.img_code = this.form1.image_code
  172. }
  173. uni.api.base.apisendsms(params).then(res => {
  174. this.smsTimer = setInterval(() => {
  175. this.smsTimeNum--
  176. if (this.smsTimeNum === 0) {
  177. this.smsTimeNum = 60
  178. clearInterval(this.smsTimer)
  179. this.smsTimer = null
  180. }
  181. }, 1000)
  182. })
  183. } else {
  184. uni.$msg('请输入正确的手机号')
  185. }
  186. },
  187. // 注册
  188. register() {
  189. this.$refs.uForm.validate(valid => {
  190. if (valid) {
  191. uni.api.base.apiregister({
  192. phone: this.form1.username,
  193. password: this.form1.password,
  194. sms_code : this.form1.sms_code,
  195. }).then(res => {
  196. uni.setStorageSync('MD_phone', this.form1.username)
  197. uni.$msgConfirm('注册成功~', () =>{
  198. uni.navigateTo({
  199. url: '/pages/user/login/login'
  200. })
  201. }, () => {
  202. uni.navigateTo({
  203. url: '/pages/user/login/login'
  204. })
  205. })
  206. })
  207. } else {
  208. console.log('验证失败');
  209. return;
  210. }
  211. });
  212. },
  213. },
  214. // 监听器
  215. watch: {},
  216. // 计算属性
  217. computed: {}
  218. };
  219. </script>
  220. <style lang="scss" scoped>
  221. .app {
  222. box-sizing: border-box;
  223. padding-bottom: 100rpx;
  224. }
  225. .header {
  226. padding-top: 15%;
  227. margin-bottom: 40rpx;
  228. display: flex;
  229. flex-direction: column;
  230. align-items: center;
  231. .avatar {
  232. width: 200rpx;
  233. height: 200rpx;
  234. display: flex;
  235. align-items: center;
  236. justify-content: center;
  237. margin-bottom: 20rpx;
  238. }
  239. .nickname {
  240. font-size: $u-h3;
  241. }
  242. }
  243. .body {
  244. width: 100%;
  245. display: flex;
  246. flex-direction: column;
  247. align-items: center;
  248. padding: 20rpx 100rpx;
  249. margin: 40rpx 0;
  250. .title {
  251. font-size: $u-p;
  252. margin-bottom: 20rpx;
  253. }
  254. .tips {
  255. font-size: $u-p2;
  256. color: $u-tips-color;
  257. margin-bottom: 60rpx;
  258. }
  259. }
  260. .third-login {
  261. display: flex;
  262. flex-direction: column;
  263. align-items: center;
  264. justify-content: center;
  265. padding: 32rpx;
  266. }
  267. .function-row {
  268. font-size: $u-p2;
  269. display: flex;
  270. align-items: center;
  271. justify-content: center;
  272. margin: 0 auto;
  273. width: 300rpx;
  274. }
  275. .footer {
  276. width: 100%;
  277. text-align: center;
  278. bottom: 20rpx;
  279. font-size: $u-p2;
  280. color: $u-content-color;
  281. .agreement {
  282. color: $u-theme-color;
  283. }
  284. }
  285. .scoped-sms-btn {
  286. width: 150rpx;
  287. height: 90rpx;
  288. font-size: 28rpx;
  289. border: 1PX solid #dcdcdc;
  290. border-radius: 10rpx;
  291. text-align: center;
  292. line-height: 90rpx;
  293. &.s {
  294. background: #2979ff;
  295. color: #fff;
  296. border: none;
  297. }
  298. }
  299. </style>