bind-mobile.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view>
  3. <view @touchmove.prevent.stop>
  4. <uni-popup ref="bindMobile" :custom="true" :mask-click="true">
  5. <view class="bind-wrap">
  6. <!-- #ifdef H5 -->
  7. <view class="head color-base-bg">检测到您还未绑定手机请立即绑定您的手机号</view>
  8. <view class="form-wrap">
  9. <view class="label">手机号码</view>
  10. <view class="form-item"><input type="number" placeholder="请输入您的手机号码" v-model="formData.mobile" /></view>
  11. <block v-if="captchaConfig">
  12. <view class="label">验证码</view>
  13. <view class="form-item">
  14. <input type="number" placeholder="请输入验证码" v-model="formData.vercode" />
  15. <image :src="captcha.img" class="captcha" @click="getCaptcha"></image>
  16. </view>
  17. </block>
  18. <view class="label">动态码</view>
  19. <view class="form-item">
  20. <input type="number" placeholder="请输入动态码" v-model="formData.dynacode" />
  21. <view class="send color-base-text" @click="sendMobileCode">{{ dynacodeData.codeText }}</view>
  22. </view>
  23. </view>
  24. <view class="footer">
  25. <view @click="cancel">取消</view>
  26. <view class="color-base-text" @click="confirm">确定</view>
  27. </view>
  28. <!-- #endif -->
  29. <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
  30. <view class="bind-tip-icon"><image :src="$util.img('/upload/uniapp/member/login.png')" mode="widthFix"></image></view>
  31. <view class="bind-tips">为了方便您接收订单等信息,需要绑定您的手机号码</view>
  32. <button open-type="getPhoneNumber" class="auth-login ns-btn-default-all color-base-bg" @getphonenumber="mobileAuthLogin"><text>点击绑定手机号码</text></button>
  33. <!-- #endif -->
  34. </view>
  35. </uni-popup>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import uniPopup from '../uni-popup/uni-popup.vue';
  41. import validate from 'common/js/validate.js';
  42. export default {
  43. name: 'bind-mobile',
  44. components: {
  45. uniPopup
  46. },
  47. mixins: [validate],
  48. data() {
  49. return {
  50. captchaConfig: 0,
  51. captcha: {
  52. id: '',
  53. img: ''
  54. },
  55. dynacodeData: {
  56. seconds: 120,
  57. timer: null,
  58. codeText: '获取动态码',
  59. isSend: false
  60. },
  61. formData: {
  62. key: '',
  63. mobile: '',
  64. vercode: '',
  65. dynacode: ''
  66. },
  67. isSub: false
  68. };
  69. },
  70. created() {
  71. this.getCaptchaConfig();
  72. },
  73. methods: {
  74. open() {
  75. this.$refs.bindMobile.open();
  76. },
  77. cancel() {
  78. this.$refs.bindMobile.close();
  79. },
  80. confirm() {
  81. let authData = uni.getStorageSync('authInfo');
  82. let data = {
  83. mobile: this.formData.mobile,
  84. key: this.formData.key,
  85. code: this.formData.dynacode
  86. };
  87. if (this.captcha.id != '') {
  88. data.captcha_id = this.captcha.id;
  89. data.captcha_code = this.formData.vercode;
  90. }
  91. if (Object.keys(authData).length) {
  92. Object.assign(data, authData);
  93. }
  94. if (authData.avatarUrl) data.headimg = authData.avatarUrl;
  95. if (authData.nickName) data.nickname = authData.nickName;
  96. if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member');
  97. if (this.verify(data)) {
  98. if (this.isSub) return;
  99. this.isSub = true;
  100. this.$api.sendRequest({
  101. url: '/api/tripartite/mobile',
  102. data,
  103. success: res => {
  104. if (res.code >= 0) {
  105. uni.setStorage({
  106. key: 'token',
  107. data: res.data.token,
  108. success: () => {
  109. uni.removeStorageSync('loginLock');
  110. uni.removeStorageSync('unbound');
  111. uni.removeStorageSync('authInfo');
  112. }
  113. });
  114. this.$store.commit('setToken', res.data.token);
  115. this.$store.dispatch('getCartNumber');
  116. this.$refs.bindMobile.close();
  117. if (res.data.is_register && this.$refs.registerReward.getReward()) {
  118. this.$refs.registerReward.open();
  119. }
  120. } else {
  121. this.isSub = false;
  122. this.getCaptcha();
  123. this.$util.showToast({ title: res.message });
  124. }
  125. },
  126. fail: res => {
  127. this.isSub = false;
  128. this.getCaptcha();
  129. }
  130. });
  131. }
  132. },
  133. /**
  134. * 表单验证
  135. * @param {Object} data
  136. */
  137. verify(data) {
  138. let rule = [{ name: 'mobile', checkType: 'required', errorMsg: '请输入手机号' }, { name: 'mobile', checkType: 'phoneno', errorMsg: '请输入正确的手机号' }];
  139. if (this.captchaConfig == 1) {
  140. if (this.captcha.id != '') rule.push({ name: 'captcha_code', checkType: 'required', errorMsg: this.$lang('captchaPlaceholder') });
  141. }
  142. rule.push({ name: 'code', checkType: 'required', errorMsg: this.$lang('dynacodePlaceholder') });
  143. var checkRes = validate.check(data, rule);
  144. if (checkRes) {
  145. return true;
  146. } else {
  147. this.$util.showToast({ title: validate.error });
  148. return false;
  149. }
  150. },
  151. /**
  152. * 获取验证码配置
  153. */
  154. getCaptchaConfig() {
  155. this.$api.sendRequest({
  156. url: '/api/config/getCaptchaConfig',
  157. success: res => {
  158. if (res.code >= 0) {
  159. this.captchaConfig = res.data.data.value.shop_reception_login;
  160. if (this.captchaConfig) this.getCaptcha();
  161. }
  162. }
  163. });
  164. },
  165. /**
  166. * 获取验证码
  167. */
  168. getCaptcha() {
  169. this.$api.sendRequest({
  170. url: '/api/captcha/captcha',
  171. data: {
  172. captcha_id: this.captcha.id
  173. },
  174. success: res => {
  175. if (res.code >= 0) {
  176. this.captcha = res.data;
  177. this.captcha.img = this.captcha.img.replace(/\r\n/g, '');
  178. }
  179. }
  180. });
  181. },
  182. /**
  183. * 发送手机动态码
  184. */
  185. sendMobileCode() {
  186. if (this.dynacodeData.seconds != 120) return;
  187. var data = {
  188. mobile: this.formData.mobile,
  189. captcha_id: this.captcha.id,
  190. captcha_code: this.formData.vercode
  191. };
  192. var rule = [{ name: 'mobile', checkType: 'required', errorMsg: '请输入手机号' }, { name: 'mobile', checkType: 'phoneno', errorMsg: '请输入正确的手机号' }];
  193. if (this.captchaConfig == 1) {
  194. rule.push({ name: 'captcha_code', checkType: 'required', errorMsg: '请输入验证码' });
  195. }
  196. var checkRes = validate.check(data, rule);
  197. if (!checkRes) {
  198. this.$util.showToast({ title: validate.error });
  199. return;
  200. }
  201. if (this.dynacodeData.isSend) return;
  202. this.dynacodeData.isSend = true;
  203. this.$api.sendRequest({
  204. url: '/api/tripartite/mobileCode',
  205. data: data,
  206. success: res => {
  207. this.dynacodeData.isSend = false;
  208. if (res.code >= 0) {
  209. this.formData.key = res.data.key;
  210. if (this.dynacodeData.seconds == 120 && this.dynacodeData.timer == null) {
  211. this.dynacodeData.timer = setInterval(() => {
  212. this.dynacodeData.seconds--;
  213. this.dynacodeData.codeText = this.dynacodeData.seconds + 's后可重新获取';
  214. }, 1000);
  215. }
  216. } else {
  217. this.$util.showToast({ title: res.message });
  218. }
  219. },
  220. fail: () => {
  221. this.$util.showToast({ title: 'request:fail' });
  222. this.dynacodeData.isSend = false;
  223. }
  224. });
  225. },
  226. mobileAuthLogin(e) {
  227. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  228. let authData = uni.getStorageSync('authInfo');
  229. var data = {
  230. iv: e.detail.iv,
  231. encryptedData: e.detail.encryptedData
  232. };
  233. if (Object.keys(authData).length) {
  234. Object.assign(data, authData);
  235. }
  236. if (authData.avatarUrl) data.headimg = authData.avatarUrl;
  237. if (authData.nickName) data.nickname = authData.nickName;
  238. if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member');
  239. if (this.isSub) return;
  240. this.isSub = true;
  241. this.$api.sendRequest({
  242. url: '/api/tripartite/mobileauth',
  243. data,
  244. success: res => {
  245. if (res.code >= 0) {
  246. uni.setStorage({
  247. key: 'token',
  248. data: res.data.token,
  249. success: () => {
  250. uni.removeStorageSync('loginLock');
  251. uni.removeStorageSync('unbound');
  252. uni.removeStorageSync('authInfo');
  253. this.$store.dispatch('getCartNumber');
  254. }
  255. });
  256. this.$store.commit('setToken', res.data.token);
  257. this.$refs.bindMobile.close();
  258. if (res.data.is_register && this.$refs.registerReward.getReward()) {
  259. this.$refs.registerReward.open();
  260. }
  261. } else {
  262. this.isSub = false;
  263. this.$util.showToast({ title: res.message });
  264. }
  265. },
  266. fail: res => {
  267. this.isSub = false;
  268. this.$util.showToast({ title: 'request:fail' });
  269. }
  270. });
  271. }
  272. }
  273. },
  274. watch: {
  275. 'dynacodeData.seconds': {
  276. handler(newValue, oldValue) {
  277. if (newValue == 0) {
  278. clearInterval(this.dynacodeData.timer);
  279. this.dynacodeData = {
  280. seconds: 120,
  281. timer: null,
  282. codeText: '获取动态码',
  283. isSend: false
  284. };
  285. }
  286. },
  287. immediate: true,
  288. deep: true
  289. }
  290. }
  291. };
  292. </script>
  293. <style lang="scss">
  294. .bind-wrap {
  295. width: 600rpx;
  296. background: #fff;
  297. box-sizing: border-box;
  298. border-radius: 20rpx;
  299. overflow: hidden;
  300. .head {
  301. text-align: center;
  302. height: 90rpx;
  303. line-height: 90rpx;
  304. color: #fff;
  305. font-size: $font-size-tag;
  306. }
  307. .form-wrap {
  308. padding: 30rpx 40rpx;
  309. .label {
  310. color: #000;
  311. font-size: $font-size-base;
  312. line-height: 1.3;
  313. }
  314. .form-item {
  315. margin: 20rpx 0;
  316. display: flex;
  317. padding-bottom: 10rpx;
  318. border-bottom: 1px solid #eee;
  319. align-items: center;
  320. input {
  321. font-size: $font-size-tag;
  322. flex: 1;
  323. }
  324. .send {
  325. font-size: $font-size-tag;
  326. line-height: 1;
  327. }
  328. .captcha {
  329. margin: 4rpx;
  330. height: 52rpx;
  331. width: 140rpx;
  332. }
  333. }
  334. }
  335. .footer {
  336. border-top: 1px solid #eee;
  337. display: flex;
  338. view {
  339. flex: 1;
  340. height: 100rpx;
  341. line-height: 100rpx;
  342. text-align: center;
  343. &:first-child {
  344. font-size: 28rpx;
  345. border-right: 1px solid #eee;
  346. }
  347. }
  348. }
  349. .bind-tips {
  350. color: #aaa;
  351. font-size: $font-size-base;
  352. padding: 20rpx 50rpx;
  353. text-align: center;
  354. }
  355. .auth-login {
  356. width: 300rpx;
  357. margin: 20rpx auto 60rpx auto;
  358. }
  359. .bind-tip-icon {
  360. padding-top: 80rpx;
  361. width: 100%;
  362. text-align: center;
  363. image {
  364. width: 300rpx;
  365. }
  366. }
  367. }
  368. .ns-btn-default-all {
  369. width: 100%;
  370. height: 70rpx;
  371. border-radius: 70rpx;
  372. text-align: center;
  373. line-height: 70rpx;
  374. color: #ffffff;
  375. font-size: $font-size-base;
  376. }
  377. </style>