setting-telphone.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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="uCode2"
  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. setPhone(null,{data:this.userInfo}).then((res)=>{
  126. uni.showToast({
  127. title: '绑定成功'
  128. })
  129. setTimeout(function() {
  130. // 选择完成后返回上一页
  131. uni.navigateBack({
  132. delta: 1,
  133. success: function(res) {
  134. console.log('返回成功');
  135. }
  136. });
  137. }, 2000); // 延迟2秒执行
  138. }).catch(() =>{
  139. uni.showToast({
  140. title: "操作失败"
  141. })
  142. });
  143. },
  144. // 去绑定账号密码页面
  145. setPhone(){
  146. let that = this;
  147. this.$refs.form1.validate().then(res => {
  148. that.saveSetPhoneData();
  149. }).catch(errors => {
  150. uni.$u.toast('校验失败,请认真填写')
  151. })
  152. },
  153. codeChange2(text) {
  154. this.tips2 = text;
  155. },
  156. getCode2() {
  157. let that = this;
  158. if (this.$refs.uCode2.canGetCode) {
  159. that.getSmsCode();
  160. } else {
  161. uni.$u.toast('倒计时结束后再发送');
  162. }
  163. },
  164. // 获取手机验证码
  165. getSmsCode(){
  166. let that = this;
  167. if(this.$isDataEmpty(this.userInfo.phonenumber)){
  168. uni.$u.toast('请认真填写手机号');
  169. return;
  170. }
  171. // 验证电话号码是否存在
  172. phoneIsExist(null,{data:this.userInfo}).then((res)=>{
  173. if(res.phoneIsExist){ // 存在
  174. that.errTipShow = true;
  175. setTimeout(() => {
  176. that.errTipShow = false;
  177. // 在这里编写延迟执行的代码
  178. that.userInfo.phonenumber = null;
  179. }, 3000);
  180. } else {
  181. that.getPhoneCode();
  182. }
  183. }).catch(() =>{
  184. uni.showToast({
  185. title: "操作失败",
  186. duration: 3000
  187. })
  188. });
  189. },
  190. // 获取验证码
  191. getPhoneCode(){
  192. let that = this;
  193. // 模拟向后端请求验证码
  194. uni.showLoading({
  195. title: '正在获取验证码'
  196. })
  197. getSmsCode({data:{phonenumber:this.userInfo.phonenumber,auth: true}}).then((res)=>{
  198. uni.hideLoading();
  199. // 这里此提示会被this.start()方法中的提示覆盖
  200. uni.$u.toast('验证码已发送');
  201. // 通知验证码组件内部开始倒计时
  202. that.$refs.uCode2.start();
  203. }).catch(() =>{
  204. uni.showToast({
  205. title: "操作失败"
  206. })
  207. });
  208. },
  209. }
  210. }
  211. </script>
  212. <style lang="scss">
  213. page,body{
  214. background: #fff;
  215. }
  216. .h-err{
  217. color: red;
  218. text-align: center;
  219. font-size: 14px;
  220. }
  221. .h-popo-content{
  222. height: 170px;
  223. padding: 12px;
  224. width: 300px;
  225. text-align: center;
  226. font-size: 14px;
  227. font-family: PingFangSC-Semibold, PingFang SC;
  228. .h-img{
  229. display: flex;
  230. justify-content: center;
  231. }
  232. .h-text{
  233. margin-top: 12px;
  234. font-weight: 400;
  235. color: #666666;
  236. }
  237. .text{
  238. margin-top: 6px;
  239. font-weight: 400;
  240. color: #666666;
  241. }
  242. .h-btn-wrap{
  243. display: flex;
  244. justify-content: space-between;
  245. color: #333333;
  246. text-align: center;
  247. margin-top: 14px;
  248. .h-left-btn{
  249. height: 32px;
  250. line-height: 30px;
  251. background: #EEEEEE;
  252. width: 100px;
  253. text-align: center;
  254. border-radius: 16px;
  255. margin: 0 auto;
  256. }
  257. .h-right-btn{
  258. height: 32px;
  259. line-height: 30px;
  260. background: #FFE05C;
  261. width: 100px;
  262. border-radius: 16px;
  263. margin: 0 auto;
  264. }
  265. }
  266. }
  267. .container{
  268. background: #fff;
  269. height: 300px;
  270. .login-wrap {
  271. display: flex;
  272. justify-content: center;
  273. .login-form{
  274. width: 90%;
  275. }
  276. .account{
  277. display: flex;
  278. }
  279. .identifying-code{
  280. }
  281. }
  282. .op-btn-wrap{
  283. margin-top: 30px;
  284. display: flex;
  285. justify-content: center;
  286. align-items: center;
  287. .h-btn{
  288. text-align: center;
  289. width: 343px;
  290. height: 42px;
  291. background: #FFE05C;
  292. border-radius: 27px;
  293. line-height: 42px;
  294. }
  295. }
  296. }
  297. </style>