setting-telphone.vue 5.9 KB

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