setPhone.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <template>
  2. <view class="container">
  3. <!-- #ifdef H5-->
  4. <uni-nav-bar v-if="!$isWxBrowser()" :fixed="true" background-color="#FFE05C" :border="false" :statusBar="false"
  5. title="注册手机号"/>
  6. <!-- #endif -->
  7. <view class="login-wrap">
  8. <view class="login-form">
  9. <u--form
  10. labelPosition="left"
  11. :model="userInfo"
  12. :rules="rules"
  13. ref="form1"
  14. >
  15. <u-form-item
  16. label="手机号"
  17. prop="phonenumber"
  18. labelWidth="auto"
  19. labelAlign="right"
  20. >
  21. <u--input
  22. v-model="userInfo.phonenumber"
  23. placeholder="请输入手机号"
  24. border="bottom"
  25. type="number"
  26. maxlength="11"
  27. ></u--input>
  28. </u-form-item>
  29. <u-form-item
  30. labelWidth="auto"
  31. labelAlign="right"
  32. label="验证码"
  33. prop="smsCode"
  34. width="120"
  35. >
  36. <u--input
  37. v-model="userInfo.smsCode"
  38. disabledColor="#ffffff"
  39. placeholder="请输入验证码"
  40. border="bottom"
  41. maxlength="4"
  42. ></u--input>
  43. <template #right>
  44. <view class="identifying-code">
  45. <u-code
  46. ref="uCode3"
  47. @change="codeChange2"
  48. keep-running
  49. start-text="获取验证码"
  50. ></u-code>
  51. <text
  52. @tap="getCode2"
  53. :text="tips2"
  54. class="u-page__code-text"
  55. >{{ tips2 }}
  56. </text>
  57. </view>
  58. </template>
  59. </u-form-item>
  60. </u--form>
  61. </view>
  62. </view>
  63. <view v-if="errTipShow" class="h-err">
  64. <text>你输入的手机号码已存在,请换一个</text>
  65. </view>
  66. <view @click="setPhone()" class="op-btn-wrap">
  67. <view class="h-btn">
  68. <text>跳转到小程序</text>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. export default {
  75. data() {
  76. return {
  77. errTipShow: false,
  78. userInfo: {
  79. phonenumber: '',
  80. smsCode: '',
  81. },
  82. tips2: '',
  83. storageUserInfo:{},
  84. rules: {
  85. phonenumber: [
  86. {
  87. required: true,
  88. min: 11,
  89. max: 11,
  90. message: '请输入11位电话号码',
  91. trigger: ['blur', 'change'],
  92. },
  93. {
  94. // 此为同步验证,可以直接返回true或者false,如果是异步验证,稍微不同,见下方说明
  95. validator: (rule, value, callback) => {
  96. // 调用uView自带的js验证规则,详见:https://www.uviewui.com/js/test.html
  97. return !this.$isDataEmpty(value);
  98. },
  99. message: "请认真填写电话号码",
  100. trigger: ['blur', 'change'],
  101. }
  102. ],
  103. smsCode: {
  104. required: true,
  105. min: 4,
  106. max: 6,
  107. message: '请输入4位验证码',
  108. trigger: ['blur', 'change'],
  109. },
  110. },
  111. }
  112. },
  113. onLoad(option) {
  114. this.storageUserInfo = uni.getStorageSync('userInfo')
  115. },
  116. onReady() {
  117. this.$refs.form1.setRules(this.rules)
  118. },
  119. methods: {
  120. getUserInfo() {
  121. this.$api.getUserInfo().then(res => {
  122. uni.setStorageSync('userInfo', res.data.data)
  123. })
  124. },
  125. // 验证电话号码是否存在
  126. saveSetPhoneData() {
  127. let that = this;
  128. // 验证电话号码是否存在
  129. this.$api.setPhone(this.userInfo).then((res) => {
  130. uni.showToast({
  131. title: '绑定成功'
  132. })
  133. this.getUserInfo()
  134. setTimeout(function () {
  135. // 绑定成功,返回上一页
  136. uni.navigateBack({
  137. delta: 1,
  138. });
  139. }, 2000); // 延迟2秒执行
  140. })
  141. },
  142. // 去绑定账号密码页面
  143. setPhone() {
  144. let that = this;
  145. this.$refs.form1.validate().then(res => {
  146. that.saveSetPhoneData();
  147. }).catch(errors => {
  148. uni.$u.toast('校验失败,请认真填写')
  149. })
  150. },
  151. codeChange2(text) {
  152. this.tips2 = text;
  153. },
  154. getCode2() {
  155. let that = this;
  156. if (this.$refs.uCode3.canGetCode) {
  157. that.getSmsCode();
  158. } else {
  159. uni.$u.toast('倒计时结束后再发送');
  160. }
  161. },
  162. // 获取手机验证码
  163. getSmsCode() {
  164. let that = this;
  165. if (!this.userInfo.phonenumber) {
  166. uni.$u.toast('请认真填写手机号');
  167. return;
  168. }
  169. // 验证电话号码是否存在
  170. this.$api.service.phoneIsExist(this.userInfo).then((res) => {
  171. console.log(res.data.data.phoneIsExist)
  172. if (res.data.data.phoneIsExist) { // 存在
  173. that.errTipShow = true;
  174. setTimeout(() => {
  175. that.errTipShow = false;
  176. // 在这里编写延迟执行的代码
  177. that.userInfo.phonenumber = null;
  178. }, 3000);
  179. } else {
  180. that.getPhoneCode();
  181. }
  182. })
  183. },
  184. // 获取验证码
  185. getPhoneCode() {
  186. let that = this;
  187. // 模拟向后端请求验证码
  188. uni.showLoading({
  189. title: '正在获取验证码'
  190. })
  191. this.$api.service.getSmsCodeByType({phonenumber: this.userInfo.phonenumber, auth: true, type: 0}).then((res) => {
  192. uni.hideLoading();
  193. // 这里此提示会被this.start()方法中的提示覆盖
  194. uni.$u.toast('验证码已发送');
  195. // 通知验证码组件内部开始倒计时
  196. that.$refs.uCode3.start();
  197. })
  198. },
  199. }
  200. }
  201. </script>
  202. <style lang="scss">
  203. page, body {
  204. background: #fff;
  205. }
  206. .h-err {
  207. color: red;
  208. text-align: center;
  209. font-size: 14px;
  210. }
  211. .h-popo-content {
  212. height: 170px;
  213. padding: 12px;
  214. width: 300px;
  215. text-align: center;
  216. font-size: 14px;
  217. font-family: PingFangSC-Semibold, PingFang SC;
  218. .h-img {
  219. display: flex;
  220. justify-content: center;
  221. }
  222. .h-text {
  223. margin-top: 12px;
  224. font-weight: 400;
  225. color: #666666;
  226. }
  227. .text {
  228. margin-top: 6px;
  229. font-weight: 400;
  230. color: #666666;
  231. }
  232. .h-btn-wrap {
  233. display: flex;
  234. justify-content: space-between;
  235. color: #333333;
  236. text-align: center;
  237. margin-top: 14px;
  238. .h-left-btn {
  239. height: 32px;
  240. line-height: 30px;
  241. background: #EEEEEE;
  242. width: 100px;
  243. text-align: center;
  244. border-radius: 16px;
  245. margin: 0 auto;
  246. }
  247. .h-right-btn {
  248. height: 32px;
  249. line-height: 30px;
  250. background: #FFE05C;
  251. width: 100px;
  252. border-radius: 16px;
  253. margin: 0 auto;
  254. }
  255. }
  256. }
  257. .container {
  258. background: #fff;
  259. height: 300px;
  260. .login-wrap {
  261. display: flex;
  262. justify-content: center;
  263. .login-form {
  264. width: 90%;
  265. }
  266. .account {
  267. display: flex;
  268. }
  269. .identifying-code {
  270. }
  271. }
  272. .op-btn-wrap {
  273. margin-top: 30px;
  274. display: flex;
  275. justify-content: center;
  276. align-items: center;
  277. .h-btn {
  278. text-align: center;
  279. width: 343px;
  280. height: 42px;
  281. background: #FFE05C;
  282. border-radius: 27px;
  283. line-height: 42px;
  284. }
  285. }
  286. }
  287. .popup-content {
  288. width: 622rpx;
  289. background-color: #FFFFFF;
  290. border-radius: 20rpx;
  291. padding: 24rpx;
  292. .popupItem {
  293. width: 558rpx;
  294. height: 100rpx;
  295. background-color: #FAFAFA;
  296. border-radius: 16rpx;
  297. margin:24rpx ;
  298. display: flex;
  299. align-items: center;
  300. justify-content: space-around;
  301. }
  302. .popupItemSelect {
  303. width: 558rpx;
  304. height: 100rpx;
  305. background-color: #FAFAFA;
  306. border-radius: 16rpx;
  307. border: 4rpx solid #FFE05C;
  308. margin:24rpx ;
  309. display: flex;
  310. align-items: center;
  311. justify-content: space-around;
  312. box-sizing: border-box;
  313. }
  314. .popupItemSelect::after {
  315. content: '√';
  316. color: #fff;
  317. z-index: 1000;
  318. display: inline-block;
  319. width: 32rpx;
  320. height: 32rpx;
  321. transform: rotate(20deg);
  322. position: absolute;
  323. top: 0;
  324. right: 0;
  325. }
  326. }
  327. .button-popup {
  328. height: 60px;
  329. display: flex;
  330. justify-content: space-between;
  331. background: #fff;
  332. .h-btn {
  333. margin: 0 auto;
  334. margin-top: 8px;
  335. width: 240rpx;
  336. height: 84rpx;
  337. border-radius: 27px;
  338. display: flex;
  339. justify-content: center;
  340. align-items: center;
  341. font-size: 14px;
  342. }
  343. }
  344. </style>