金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.3 KiB

  1. <template>
  2. <view id="bindingphone">
  3. <view class="tips" :hidden="!message">{{message}}</view>
  4. <view class="phone__warning">
  5. <view class="phone__num">
  6. <input type="text" placeholder="手机号码" @input="changeCode"></input>
  7. </view>
  8. <view class="phone__code">
  9. <view class="input">
  10. <input type="text" placeholder="验证码" @input="changeIdentifyCode"></input>
  11. </view>
  12. <view class="code__get code-send" :style="'background: ' + config.mainColor" @tap="getCode">
  13. {{code.codeText}}
  14. </view>
  15. </view>
  16. </view>
  17. <view class="phone__box">
  18. <view class="phone__btn phone-confirm phone-binding" :style="'background: ' + config.mainColor" @tap="submit">
  19. 确定绑定
  20. </view>
  21. <view class="phone__btn phone-cancel" @tap="back">
  22. 取消
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  29. export default {
  30. data() {
  31. return {
  32. code: {
  33. total: 60,
  34. access_token: null,
  35. codeText: "获取验证码"
  36. },
  37. sending: false,
  38. tellphone: '',
  39. identifyingcode: '',
  40. url: '',
  41. config: '',
  42. message:''
  43. };
  44. },
  45. onLoad(e) {
  46. // 第三方平台配置颜色
  47. var bgConfig = this.$cookieStorage.get('globalConfig') || '';
  48. this.setData({
  49. config: bgConfig
  50. });
  51. if (e.url) {
  52. this.setData({
  53. url: e.url
  54. });
  55. }
  56. },
  57. methods: {
  58. getCode() {
  59. if (this.sending) return;
  60. var randoms = this.random();
  61. this.setData({
  62. sending: true,
  63. 'code.codeText': "短信发送中",
  64. 'code.access_token': randoms
  65. });
  66. var fn;
  67. fn = this.getLoginCode;
  68. fn(() => {
  69. var total = this.code.total;
  70. this.setData({
  71. 'code.codeText': total + "秒后再发送"
  72. });
  73. var timer = setInterval(() => {
  74. total--;
  75. this.setData({
  76. 'code.codeText': total + "秒后再发送"
  77. });
  78. if (total < 1) {
  79. this.setData({
  80. sending: false,
  81. 'code.codeText': "获取验证码"
  82. });
  83. clearInterval(timer);
  84. }
  85. }, 1000);
  86. }, () => {
  87. // this.setData({
  88. // sending: false,
  89. // 'code.codeText': "获取验证码"
  90. // });
  91. this.sending=false;
  92. this.code.codeText="获取验证码";
  93. });
  94. },
  95. changeCode(e) {
  96. this.setData({
  97. tellphone: e.detail.value
  98. });
  99. },
  100. changeIdentifyCode(e) {
  101. this.setData({
  102. identifyingcode: e.detail.value
  103. });
  104. },
  105. random() {
  106. return Math.random().toString(36).substr(2, 24);
  107. },
  108. getLoginCode(resolve, reject) {
  109. var message = null;
  110. if (!is.has(this.tellphone)) {
  111. message = "请输入您的手机号";
  112. } else if (!is.mobile(this.tellphone)) {
  113. message = '手机号格式不正确,请重新输入';
  114. }
  115. if (message) {
  116. this.message=message;
  117. reject();
  118. setTimeout(() => {
  119. this.message='';
  120. }, 3000);
  121. return;
  122. }
  123. this.$http.post({
  124. api: "api/sms/verify-code",
  125. data: {
  126. mobile: this.tellphone,
  127. access_token: this.code.access_token
  128. }
  129. }).then(res => {
  130. if (res.data.success) {
  131. resolve();
  132. } else {
  133. reject();
  134. }
  135. }); // resolve();
  136. },
  137. submit() {
  138. var message = null;
  139. if (!is.has(this.tellphone)) {
  140. message = "请输入您的手机号";
  141. } else if (!is.mobile(this.tellphone)) {
  142. message = '手机号格式不正确,请重新输入';
  143. } else if (!is.has(this.identifyingcode)) {
  144. message = "请填写验证码";
  145. }
  146. if (message) {
  147. this.message=message;
  148. setTimeout(() => {
  149. this.message='';
  150. }, 3000);
  151. return;
  152. }
  153. this.showLoading=true;
  154. this.bindUserMobile();
  155. },
  156. bindUserMobile() {
  157. this.$http.post({
  158. api: 'api/users/update/mobile',
  159. data: {
  160. mobile: this.tellphone,
  161. code: this.identifyingcode,
  162. access_token: this.code.access_token
  163. },
  164. header: {
  165. Authorization: this.$cookieStorage.get('user_token')
  166. }
  167. }).then(res => {
  168. wx.hideLoading();
  169. if (res.statusCode == 200) {
  170. res = res.data;
  171. if (res.status) {
  172. wx.showModal({
  173. content: '绑定成功',
  174. showCancel: false,
  175. success: res => {
  176. if (res.confirm || !res.cancel && !res.confirm) {
  177. if (this.url) {
  178. wx.redirectTo({
  179. url: '/' + decodeURIComponent(this.url)
  180. });
  181. } else {
  182. wx.switchTab({
  183. url: '/pages/user/personal/personal'
  184. });
  185. }
  186. }
  187. }
  188. });
  189. } else {
  190. wx.showModal({
  191. content: res.message || '绑定失败',
  192. showCancel: false
  193. });
  194. }
  195. } else {
  196. wx.showModal({
  197. content: '请求失败,请重试',
  198. showCancel: false
  199. });
  200. }
  201. }).catch(rej => {
  202. res = res.data;
  203. wx.showModal({
  204. content: '请求失败,请重试',
  205. showCancel: false
  206. });
  207. });
  208. },
  209. back() {
  210. if (this.url) {
  211. wx.redirectTo({
  212. url: '/' + decodeURIComponent(this.url)
  213. });
  214. } else {
  215. wx.navigateBack();
  216. }
  217. },
  218. setData: function (obj) {
  219. let that = this;
  220. let keys = [];
  221. let val, data;
  222. Object.keys(obj).forEach(function (key) {
  223. keys = key.split('.');
  224. val = obj[key];
  225. data = that.$data;
  226. keys.forEach(function (key2, index) {
  227. if (index + 1 == keys.length) {
  228. that.$set(data, key2, val);
  229. } else {
  230. if (!data[key2]) {
  231. that.$set(data, key2, {});
  232. }
  233. }
  234. data = data[key2];
  235. });
  236. });
  237. }
  238. },
  239. computed: {},
  240. watch: {}
  241. };
  242. </script>
  243. <style rel="stylesheet/less" lang="less">
  244. @import "bindingphone";
  245. </style>