setting-telphone.vue 6.1 KB

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