register.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="u-page">
  3. <u--form labelPosition="left" :model="registerData" ref="registerForm">
  4. <u-form-item label="电话" prop="phone" borderBottom>
  5. <u--input v-model="registerData.phone" border="none" placeholder="电话"></u--input>
  6. </u-form-item>
  7. <u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
  8. <u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
  9. <u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini" :disabled="disabled1">
  10. </u-button>
  11. </u-form-item>
  12. <u-form-item label="密码" prop="password" borderBottom>
  13. <u--input v-model="registerData.password" border="none" placeholder="请输入密码,6-16位字符"></u--input>
  14. </u-form-item>
  15. <u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true" @end="disabled1 = false">
  16. </u-code>
  17. </u--form>
  18. <u-button type="primary" @click='register'>注册</u-button>
  19. <view style='color:#6A6A6A;margin-top:10px;text-align:center;' class="flex">
  20. <u--image @click='consent' :src="consentStatus==true?'../../static/images/login/select@2x.png':'../../static/images/login/selected2.png'"
  21. width="40px" height="40px"></u--image>
  22. <!-- <image @click='consent' style='width:14px;height:14px;position:relative;top:2px;margin-right:5px;'
  23. :src="consentStatus==true?'../../static/img/login/select@2x.png':'../../static/img/login/selected2.png'"
  24. alt=""></image> -->
  25. 已阅读并同意
  26. <navigator url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/userAgreement.html"
  27. class="path" hover-class="navigator-hover">《服务协议》</navigator>和<navigator
  28. url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/screctAgreement.html"
  29. class="path" hover-class="navigator-hover">《隐私政策》</navigator>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. var _this
  35. import helper from '@/common/helper.js';
  36. export default {
  37. data() {
  38. return {
  39. disabled1: false,
  40. tips: '',
  41. consentStatus: false,
  42. codestatus: false,
  43. registerData: {
  44. phone: '',
  45. password: '',
  46. verifyCode: '',
  47. userName: ''
  48. },
  49. rules: {
  50. phone: {
  51. type: 'string',
  52. required: true,
  53. len: 11,
  54. message: '请填写11位手机号',
  55. trigger: ['blur']
  56. },
  57. verifyCode: {
  58. type: 'string',
  59. required: true,
  60. len: 6,
  61. message: '请填写6位验证码',
  62. trigger: ['blur']
  63. },
  64. }
  65. }
  66. },
  67. onReady() {
  68. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  69. this.$refs.registerForm.setRules(this.rules)
  70. },
  71. onLoad() {
  72. _this = this
  73. },
  74. methods: {
  75. codeChange(text) {
  76. this.tips = text;
  77. },
  78. // 获取验证码
  79. getCode() {
  80. if (/^0?1[3|4|5|6|7|8][0-9]\d{8}$/.test(this.registerData.phone)) {
  81. if (this.$refs.uCode.canGetCode) {
  82. // 模拟向后端请求验证码
  83. uni.showLoading({
  84. title: '正在获取验证码'
  85. })
  86. _this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
  87. phone: this.registerData.phone
  88. }).then(res => {
  89. uni.hideLoading();
  90. // 这里此提示会被this.start()方法中的提示覆盖
  91. uni.$u.toast('验证码已发送');
  92. // 通知验证码组件内部开始倒计时
  93. this.$refs.uCode.start();
  94. })
  95. .catch(res => {
  96. uni.$u.toast(res.message);
  97. });
  98. // setTimeout(() => {
  99. // }, 2000);
  100. } else {
  101. uni.$u.toast('倒计时结束后再发送');
  102. }
  103. } else {
  104. uni.$u.toast('请输入正确手机号');
  105. }
  106. },
  107. consent() {
  108. this.consentStatus = !this.consentStatus
  109. uni.setStorage({
  110. key: 'policyStorageKey',
  111. data: this.consentStatus
  112. });
  113. },
  114. // 随机数
  115. randomAccess(min, max) {
  116. return Math.floor(Math.random() * (min - max) + max)
  117. },
  118. getRandomName(NameLength) {
  119. let name = ""
  120. for (let i = 0; i < NameLength; i++) {
  121. let unicodeNum = ""
  122. unicodeNum = this.randomAccess(0x4e00, 0x9fa5).toString(16)
  123. name += this.decodeUnicode(unicodeNum)
  124. }
  125. return name
  126. },
  127. // 解码
  128. decodeUnicode(str) {
  129. //Unicode显示方式是\u4e00
  130. str = "\\u" + str
  131. str = str.replace(/\\/g, "%");
  132. //转换中文
  133. str = unescape(str);
  134. //将其他受影响的转换回原来
  135. str = str.replace(/%/g, "\\");
  136. return str;
  137. },
  138. //注册
  139. register() {
  140. if (!this.consentStatus) {
  141. uni.showToast({
  142. title: '请勾选协议',
  143. icon: 'none',
  144. duration: 2000
  145. })
  146. return
  147. }
  148. this.$refs.registerForm.validate().then(res => {debugger
  149. _this.registerData.userName = _this.getRandomName(3)
  150. uni.$u.toast('校验通过')
  151. _this.isLoading = true
  152. _this.$request.baseRequest('post', '/commonUser/register', _this.registerData).then(res => {
  153. if (res.data.code == '11002') {
  154. uni.$u.toast('验证码错误或过期,请重新获取')
  155. return
  156. } else if (res.data.code == '11004') {
  157. uni.$u.toast('该手机号已注册,可直接登录')
  158. return
  159. }
  160. uni.showLoading({
  161. title: '登录中',
  162. mask: true
  163. })
  164. debugger
  165. _this.$request.baseRequest('get', '/commonUser/login', {
  166. phone: _this.registerData.phone,
  167. password: _this.registerData.password,
  168. cid: uni.getStorageSync("clientId")
  169. }).then(res => {
  170. uni.hideLoading()
  171. uni.setStorageSync('userInfo', res.data)
  172. _this.$store.commit('login', res.data)
  173. uni.switchTab({
  174. url: '/pages/goodSource/index'
  175. });
  176. })
  177. .catch(res => {
  178. uni.$u.toast(res.message)
  179. });
  180. })
  181. .catch(res => {
  182. uni.$u.toast(res.message)
  183. });
  184. }).catch(errors => {
  185. uni.$u.toast('校验失败')
  186. })
  187. },
  188. },
  189. }
  190. </script>
  191. <style lang="scss">
  192. </style>