mobile.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="page" style="padding-top: 10px;">
  3. <ul class="profile" v-if="user.mobile && step==1">
  4. <li>手机号:</li>
  5. <li>
  6. {{mobileshow}}
  7. </li>
  8. </ul>
  9. <ul class="profile" v-else>
  10. <li>新手机号:</li>
  11. <li>
  12. <input type="text" class="input1" v-model="mobile" placeholder="输入新手机号码" style="width: 150px;" />
  13. </li>
  14. </ul>
  15. <ul class="profile">
  16. <li>验证码:</li>
  17. <li><input type="text" class="input1" id="randcode" v-model="randcode" placeholder="短信验证码" maxlength="6"
  18. style="width:80px;height:30px;line-height: 30px;display: inline-block;vertical-align: middle;" />
  19. <span class="sendbtn" @tap="sendsms()" style="display: inline-block;margin-left: 5px;"
  20. v-if='!disabled'>发送短信</span>
  21. <span class="sendbtn" style="display: inline-block;margin-left: 5px;" v-else>
  22. <block v-if="issending">发送中...</block>
  23. <block v-else>
  24. {{getCodeTime}}秒后再获取
  25. </block>
  26. </span>
  27. </li>
  28. </ul>
  29. <view style="margin:15px auto;display:block;width: 80%;">
  30. <button class="button1" @tap="click_mobile()">
  31. <block v-if="user.mobile && step==1">更换手机号</block>
  32. <block v-else>确认绑定</block>
  33. </button>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import http from "../../library/http.js"
  39. import config from "../../config.js"
  40. import uniIcons from '../../components/uni-icons/uni-icons.vue'
  41. export default {
  42. components: {
  43. uniIcons
  44. },
  45. data() {
  46. return {
  47. user: uni.getStorageSync('userInfo'),
  48. system: uni.getStorageSync('system'),
  49. step: 1,
  50. mobile: '',
  51. mobileshow: '',
  52. type: 'bind',
  53. timer: null,
  54. getCodeTime: 60,
  55. disabled: false,
  56. issending: false,
  57. randcode: '',
  58. }
  59. },
  60. methods: {
  61. sendsms() {
  62. var mobile = this.mobile;
  63. var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
  64. if (reg.test(mobile)) {
  65. this.disabled = true;
  66. this.issending = true;
  67. http.setWait(true).post('user.php?act=sendCode', {
  68. mobile: mobile,
  69. type: this.type
  70. }).then(res => {
  71. this.issending = false;
  72. if (res.data.method != 'error') {
  73. var that = this;
  74. clearInterval(that.timer);
  75. that.timer = setInterval(function() {
  76. that.getCodeTime--;
  77. that.disabled = true;
  78. if (that.getCodeTime <= 0) {
  79. that.disabled = false;
  80. that.getCodeTime = 60;
  81. clearInterval(that.timer);
  82. }
  83. }, 1000)
  84. } else {
  85. var that = this;
  86. that.disabled = false;
  87. that.getCodeTime = 60;
  88. clearInterval(that.timer);
  89. uni.showToast({
  90. icon: 'none',
  91. title: '该手机号已经被其他用户使用',
  92. duration: 1000
  93. })
  94. }
  95. })
  96. } else {
  97. uni.showToast({
  98. icon: 'none',
  99. title: '手机号码格式错误',
  100. duration: 1000
  101. })
  102. return false;
  103. }
  104. },
  105. click_mobile() {
  106. var that = this;
  107. var mobile = this.mobile;
  108. if (mobile == '') {
  109. uni.showToast({
  110. icon: 'none',
  111. title: '手机号不能为空',
  112. duration: 1000
  113. })
  114. return false;
  115. }
  116. var reg = /^0?1[3|4|5|6|7|8|9][0-9]\d{8}$/;
  117. if (!reg.test(mobile)) {
  118. uni.showToast({
  119. icon: 'none',
  120. title: '手机号码格式错误',
  121. duration: 1000
  122. })
  123. return false;
  124. }
  125. if (this.randcode == '') {
  126. uni.showToast({
  127. icon: 'none',
  128. title: '短信验证码不能为空',
  129. duration: 1000
  130. })
  131. return false;
  132. }
  133. http.setWait(true).post('user.php?act=mobile', {
  134. id: this.user.id,
  135. mobile: mobile,
  136. randcode: this.randcode
  137. }).then(res => {
  138. if (res.code == '200') {
  139. if (this.type == 'change') {
  140. this.step = 2;
  141. this.type = 'new';
  142. this.mobile = '';
  143. this.randcode = '';
  144. uni.setNavigationBarTitle({
  145. title: '设置新手机号'
  146. })
  147. } else {
  148. this.getuserinfo();
  149. uni.showToast({
  150. icon: 'none',
  151. title: '手机绑定成功',
  152. duration: 1000
  153. })
  154. that.disabled = false;
  155. that.getCodeTime = 60;
  156. clearInterval(that.timer);
  157. }
  158. } else {
  159. uni.showToast({
  160. icon: 'none',
  161. title: res.message,
  162. duration: 1000
  163. })
  164. }
  165. })
  166. },
  167. setuserdata() {
  168. if (this.user.mobile) {
  169. this.mobile = this.user.mobile;
  170. this.mobileshow = this.mobile.substr(0, 3) + "*****" + this.mobile.substr(this.mobile.length - 3,
  171. 3);
  172. this.type = 'change';
  173. uni.setNavigationBarTitle({
  174. title: '更换手机号'
  175. })
  176. } else {
  177. this.type = 'bind';
  178. uni.setNavigationBarTitle({
  179. title: '绑定手机号'
  180. })
  181. }
  182. },
  183. getuserinfo() {
  184. var postdata = {
  185. id: uni.getStorageSync('access_token')
  186. };
  187. http.setWait(false).post('user.php?act=userinfo', postdata).then(res => {
  188. this.user = res.data;
  189. uni.setStorageSync('userInfo', this.user)
  190. this.setuserdata();
  191. })
  192. },
  193. },
  194. onShow() {
  195. this.setuserdata();
  196. this.getuserinfo();
  197. }
  198. }
  199. </script>
  200. <style>
  201. @import "@/static/css/user.css";
  202. .page {
  203. background-color: #fafafa;
  204. }
  205. .profile {
  206. background-color: #fff;
  207. margin: 10px auto;
  208. width: 100%;
  209. }
  210. .profile li {
  211. vertical-align: middle;
  212. }
  213. </style>