changePhone.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view class="wrap">
  3. <view class="main">
  4. <view class="user-info">
  5. <view class="b-item">
  6. <view class="s-tit">旧手机号</view>
  7. <text class="s-input">{{ form.oldPhone }}</text>
  8. </view>
  9. <template v-if="loginMethods.indexOf('phone_captcha') !== -1">
  10. <view class="b-item">
  11. <view class="s-tit">新手机号</view>
  12. <input class="s-input" :value="form.newPhone" minlength="11" maxlength="11" @input="onInput" data-field="newPhone" placeholder="请输入新手机号" />
  13. </view>
  14. <view class="b-item">
  15. <view class="s-tit">验证码</view>
  16. <input class="s-input" :value="form.newCaptcha" maxlength="12" @input="onInput" data-field="newCaptcha" placeholder="请输入验证码" />
  17. <view class="s-captcha" :class="{ disable: captcha.second > 0 }" @click="onPhoneCaptcha">
  18. <text v-if="!captcha.retry && !captcha.second">获取验证码</text>
  19. <text v-else-if="captcha.second > 0">重新获取 {{ captcha.second }}秒</text>
  20. <text v-else-if="captcha.retry">重新获取</text>
  21. </view>
  22. </view>
  23. </template>
  24. </view>
  25. <template v-if="loginMethods.indexOf('phone_captcha') !== -1">
  26. <view class="btn-save" :class="{ disable: disable.submit }" @click="onSave">保存修改</view>
  27. </template>
  28. <!-- #ifdef MP-WEIXIN -->
  29. <button class="btn-save f-wechat open-data-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" v-if="loginMethods.indexOf('wechat_phone') !== -1">
  30. 授权微信手机号
  31. </button>
  32. <!-- #endif -->
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import { getUserInfo, sendPhoneCaptcha, changePhone, changeWechatPhone } from '@/service/api/user.js';
  38. import mixinsCommon from '@/mixins/common.js';
  39. import mixinsAuth from '@/mixins/auth.js';
  40. export default {
  41. mixins: [mixinsCommon, mixinsAuth],
  42. data() {
  43. return {
  44. form: {
  45. oldPhone: '',
  46. newPhone: '',
  47. newCaptcha: ''
  48. },
  49. disable: {
  50. submit: false
  51. },
  52. captcha: {
  53. retry: false,
  54. second: 0
  55. }
  56. };
  57. },
  58. onLoad() {
  59. getUserInfo().then(([err, res]) => {
  60. console.log('getUserInfo', err, res);
  61. if (!err) {
  62. this.form.oldPhone = res.phone;
  63. }
  64. });
  65. },
  66. computed: {
  67. loginMethods() {
  68. return this.$store.getters.globalConfig.login_methods || [];
  69. }
  70. },
  71. methods: {
  72. onInput(e) {
  73. console.log('onInput', e);
  74. const field = e.target.dataset.field;
  75. this.form[field] = e.detail.value;
  76. },
  77. onPhoneCaptcha() {
  78. if (this.disable.captcha) {
  79. return;
  80. }
  81. if (!this.form.newPhone) {
  82. return this.$logic.showToast('新手机号不能为空');
  83. }
  84. if (this.form.newPhone.length != 11) {
  85. return this.$logic.showToast('新手机号长度必须为11位');
  86. }
  87. if (this.form.newPhone == this.form.oldPhone) {
  88. return this.$logic.showToast('未修改手机号');
  89. }
  90. this.disable.captcha = true;
  91. uni.login().then(([err, res]) => {
  92. console.log('uni.login', err, res);
  93. sendPhoneCaptcha(this.form.newPhone, res ? res.code : '', false).then(([err, res]) => {
  94. console.log('sendPhoneCaptcha', err, res);
  95. if (!err) {
  96. uni.showToast({
  97. title: '发送成功'
  98. });
  99. // 验证码倒计时
  100. this.captcha.second = 60;
  101. var interval = setInterval(() => {
  102. this.captcha.second--;
  103. }, 1000);
  104. setTimeout(() => {
  105. clearInterval(interval);
  106. this.captcha.retry = true;
  107. this.captcha.second = 0;
  108. this.disable.captcha = false;
  109. }, this.captcha.second * 1000);
  110. } else {
  111. this.disable.captcha = false;
  112. }
  113. });
  114. });
  115. },
  116. onSave() {
  117. if (this.disable.submit) {
  118. return;
  119. }
  120. if (!this.form.newPhone) {
  121. return this.$logic.showToast('新手机号不能为空');
  122. }
  123. if (this.form.newPhone.length != 11) {
  124. return this.$logic.showToast('新手机号长度必须为11位');
  125. }
  126. if (this.form.newPhone == this.form.oldPhone) {
  127. return this.$logic.showToast('未修改手机号');
  128. }
  129. if (!this.form.newCaptcha) {
  130. return this.$logic.showToast('验证码不能为空');
  131. }
  132. this.disable.submit = true;
  133. changePhone(this.form.newPhone, this.form.newCaptcha).then(([err, res]) => {
  134. console.log('changePhone', err, res);
  135. this.disable.submit = false;
  136. if (!err) {
  137. this.$logic.showToast('修改成功').then(([err, res]) => {
  138. uni.reLaunch({
  139. url: '/answer_pages/user/mine'
  140. });
  141. });
  142. }
  143. });
  144. },
  145. onGetPhoneNumber(e) {
  146. console.log('onGetPhoneNumber', e);
  147. if (e.detail.errMsg.indexOf(':ok') === -1) {
  148. // fail user deny
  149. this.$logic.showToast(e.detail.errMsg.replace('getPhoneNumber:', ''));
  150. return;
  151. }
  152. uni.showLoading({
  153. title: '修改手机号中'
  154. });
  155. uni.login().then(([err, res]) => {
  156. console.log('uni.login', err, res);
  157. changeWechatPhone(e.detail.encryptedData, e.detail.iv, e.detail.code || '', res ? res.code : '').then(([err, res]) => {
  158. console.log('changeWechatPhone', err, res);
  159. uni.hideLoading();
  160. if (!err) {
  161. this.$logic.showToast('修改成功').then(([err, res]) => {
  162. uni.reLaunch({
  163. url: '/answer_pages/user/mine'
  164. });
  165. });
  166. }
  167. });
  168. });
  169. }
  170. }
  171. };
  172. </script>
  173. <style lang="scss">
  174. page {
  175. padding-bottom: env(safe-area-inset-bottom);
  176. background: $pq-bg-color;
  177. }
  178. .wrap {
  179. padding: 0 40upx;
  180. }
  181. .user-info {
  182. margin-top: 40upx;
  183. background: #fff;
  184. border-radius: 20upx;
  185. .b-item {
  186. padding: 0 40upx;
  187. border-bottom: 1upx solid $pq-bg-color;
  188. height: 100upx;
  189. display: flex;
  190. align-items: center;
  191. position: relative;
  192. .s-tit {
  193. color: #000000;
  194. font-size: 26upx;
  195. }
  196. .s-input {
  197. flex: 1;
  198. color: #808080;
  199. text-align: right;
  200. font-size: 26upx;
  201. }
  202. .s-captcha {
  203. margin-left: 20upx;
  204. padding: 0 30upx;
  205. background: #fff;
  206. border: 2upx #ccc solid;
  207. height: 64upx;
  208. line-height: 64upx;
  209. border-radius: 32upx;
  210. color: #808080;
  211. font-size: 24upx;
  212. &.disable {
  213. color: #999;
  214. }
  215. }
  216. }
  217. }
  218. .btn-save {
  219. margin-top: 50upx;
  220. height: 100upx;
  221. line-height: 100upx;
  222. text-align: center;
  223. color: #fff;
  224. font-size: 32upx;
  225. background: #da5650;
  226. border-radius: 50upx;
  227. letter-spacing: 10upx;
  228. &.f-wechat {
  229. background: #35c773;
  230. }
  231. }
  232. </style>