register.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="u-page content">
  3. <u-icon class="back-btn" name="arrow-left" color="black" size="20" @click="navBack"></u-icon>
  4. <view class="wrapper">
  5. <h2 class="Semibold">手机号注册</h2>
  6. <u--form labelPosition="left" :model="registerData" ref="registerForm" labelWidth="80">
  7. <u-form-item label="电话" prop="phone" borderBottom>
  8. <u--input v-model="registerData.phone" border="none" placeholder="请输入手机号码"></u--input>
  9. </u-form-item>
  10. <u-form-item label="验证码" prop="verifyCode" labelWidth="80" borderBottom>
  11. <u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
  12. <u-button slot="right" @tap="getCode" :text="tips" size="mini" :disabled="disabled1"
  13. class="code-style">
  14. </u-button>
  15. </u-form-item>
  16. <u-form-item label="密码" prop="password" borderBottom>
  17. <u--input v-model="registerData.password" border="none" placeholder="请输入密码,6-16位字符"></u--input>
  18. </u-form-item>
  19. <u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true"
  20. @end="disabled1 = false">
  21. </u-code>
  22. </u--form>
  23. <u-button class="code-style register" @click='register'>注册</u-button>
  24. <view class="flex service align-center">
  25. <u--image @click='consent'
  26. :src="consentStatus==true?'../../static/images/mine/duihao@2x.png':'../../static/images/mine/wxz.png'"
  27. width="12px" height="12px"></u--image>
  28. 我已阅读并同意
  29. <navigator
  30. url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/userAgreement.html"
  31. class="path" hover-class="navigator-hover">《服务协议》</navigator>和<navigator
  32. url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/screctAgreement.html"
  33. class="path" hover-class="navigator-hover">《隐私政策》</navigator>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. var _this
  40. import helper from '@/common/helper.js';
  41. export default {
  42. data() {
  43. return {
  44. disabled1: false,
  45. tips: '',
  46. consentStatus: false,
  47. codestatus: false,
  48. registerData: {
  49. phone: '',
  50. password: '',
  51. verifyCode: '',
  52. },
  53. rules: {
  54. phone: [{
  55. required: true,
  56. message: '请输入手机号',
  57. trigger: ['change', 'blur'],
  58. },
  59. {
  60. // 自定义验证函数,见上说明
  61. validator: (rule, value, callback) => {
  62. // 上面有说,返回true表示校验通过,返回false表示不通过
  63. // uni.$u.test.mobile()就是返回true或者false的
  64. return uni.$u.test.mobile(value);
  65. },
  66. message: '手机号码不正确',
  67. // 触发器可以同时用blur和change
  68. trigger: ['blur'],
  69. }
  70. ],
  71. verifyCode: {
  72. type: 'string',
  73. required: true,
  74. len: 6,
  75. message: '请填写6位验证码',
  76. trigger: ['blur'],
  77. },
  78. password: {
  79. type: 'string',
  80. required: true,
  81. min: 6,
  82. max: 16,
  83. message: '长度在6-16位字符之间',
  84. trigger: ['blur'],
  85. }
  86. }
  87. }
  88. },
  89. onReady() {
  90. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  91. this.$refs.registerForm.setRules(this.rules)
  92. },
  93. onLoad() {
  94. _this = this
  95. },
  96. methods: {
  97. navBack() {
  98. uni.navigateBack();
  99. },
  100. codeChange(text) {
  101. this.tips = text;
  102. },
  103. // 获取验证码
  104. getCode() {
  105. if (/^0?1[3|4|5|6|7|8][0-9]\d{8}$/.test(this.registerData.phone)) {
  106. if (this.$refs.uCode.canGetCode) {
  107. // 模拟向后端请求验证码
  108. uni.showLoading({
  109. title: '正在获取验证码'
  110. })
  111. _this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
  112. phone: this.registerData.phone
  113. }).then(res => {
  114. uni.hideLoading();
  115. // 这里此提示会被this.start()方法中的提示覆盖
  116. uni.$u.toast('验证码已发送');
  117. // 通知验证码组件内部开始倒计时
  118. this.$refs.uCode.start();
  119. })
  120. .catch(res => {
  121. uni.$u.toast(res.message);
  122. });
  123. // setTimeout(() => {
  124. // }, 2000);
  125. } else {
  126. uni.$u.toast('倒计时结束后再发送');
  127. }
  128. } else {
  129. uni.$u.toast('请输入正确手机号');
  130. }
  131. },
  132. consent() {
  133. this.consentStatus = !this.consentStatus
  134. uni.setStorage({
  135. key: 'policyStorageKey',
  136. data: this.consentStatus
  137. });
  138. },
  139. // 随机数
  140. randomAccess(min, max) {
  141. return Math.floor(Math.random() * (min - max) + max)
  142. },
  143. getRandomName(NameLength) {
  144. let name = ""
  145. for (let i = 0; i < NameLength; i++) {
  146. let unicodeNum = ""
  147. unicodeNum = this.randomAccess(0x4e00, 0x9fa5).toString(16)
  148. name += this.decodeUnicode(unicodeNum)
  149. }
  150. return name
  151. },
  152. // 解码
  153. decodeUnicode(str) {
  154. //Unicode显示方式是\u4e00
  155. str = "\\u" + str
  156. str = str.replace(/\\/g, "%");
  157. //转换中文
  158. str = unescape(str);
  159. //将其他受影响的转换回原来
  160. str = str.replace(/%/g, "\\");
  161. return str;
  162. },
  163. //注册
  164. register() {
  165. if (!this.consentStatus) {
  166. uni.showToast({
  167. title: '请勾选协议',
  168. icon: 'none',
  169. duration: 2000
  170. })
  171. return
  172. }
  173. this.$refs.registerForm.validate().then(res => {
  174. _this.registerData.userName = _this.getRandomName(3)
  175. uni.$u.toast('校验通过')
  176. _this.isLoading = true
  177. _this.$request.baseRequest('post', '/commonUser/register', _this.registerData).then(res => {
  178. if (res.data.code == '11002') {
  179. uni.$u.toast('验证码错误或过期,请重新获取')
  180. return
  181. } else if (res.data.code == '11004') {
  182. uni.$u.toast('该手机号已注册,可直接登录')
  183. return
  184. }
  185. uni.showLoading({
  186. title: '登录中',
  187. mask: true
  188. })
  189. _this.$request.baseRequest('get', '/commonUser/login', {
  190. phone: _this.registerData.phone,
  191. password: _this.registerData.password,
  192. cid: uni.getStorageSync("clientId")
  193. }).then(res => {
  194. uni.hideLoading()
  195. uni.setStorageSync('userInfo', res.data)
  196. _this.$store.commit('login', res.data)
  197. uni.switchTab({
  198. url: '/pages/goodSource/index'
  199. });
  200. })
  201. .catch(res => {
  202. uni.$u.toast(res.message)
  203. });
  204. })
  205. .catch(res => {
  206. uni.$u.toast(res.message)
  207. });
  208. }).catch(errors => {
  209. uni.$u.toast('校验失败')
  210. })
  211. },
  212. },
  213. }
  214. </script>
  215. <style lang="scss">
  216. .content {
  217. background: url(../../static/images/mine/bg@2x.png);
  218. background-size: 100% 100%;
  219. height: 100vh;
  220. padding-top: 85px;
  221. position: relative;
  222. }
  223. .back-btn {
  224. position: absolute;
  225. left: 40upx;
  226. z-index: 9999;
  227. padding-top: var(--status-bar-height);
  228. top: 40upx;
  229. font-size: 40upx;
  230. color: red;
  231. }
  232. .wrapper {
  233. position: relative;
  234. z-index: 90;
  235. padding-bottom: 40upx;
  236. height: 100%;
  237. padding: 0 30px;
  238. }
  239. .code-style {
  240. background: #2772FB;
  241. color: white;
  242. }
  243. .register {
  244. margin-top: 50rpx;
  245. }
  246. .service {
  247. font-size: 24rpx;
  248. margin: 20rpx;
  249. justify-content: center;
  250. /deep/.u-image {
  251. margin: 0 20rpx;
  252. }
  253. }
  254. .path {
  255. color: #2772FB;
  256. }
  257. .Semibold {
  258. margin-bottom: 50rpx;
  259. }
  260. </style>