phoneLogin.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="wrap">
  3. <view class="content">
  4. <view class="title">手机号登录</view>
  5. <input class="u-border-bottom" type="number" maxlength="11" v-model="tel" placeholder="请输入手机号" />
  6. <view class="u-text-center u-type-error u-m-t-20" v-if="telError">手机号输入错误</view>
  7. <button @tap="submit" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button>
  8. <u-message-input v-if="show" :focus="true" :value="messageCode" @change="change" @finish="finish" mode="bottomLine" :maxlength="codelength"></u-message-input>
  9. </view>
  10. <!-- <view class="u-text-center u-type-error" v-if="phoneError">手机号输入错误</view> -->
  11. <view class="captcha">
  12. <!-- <text v-if="show&&this.messageDisable==false" @tap="noCaptcha">收不到验证码点这里</text> -->
  13. <text v-if="messageShow">{{ second }}秒后可重新获取验证码</text>
  14. </view>
  15. <view class="buttom">
  16. <view class="hint u-text-center">
  17. 登录代表同意
  18. <text class="link">隐私政策,</text>
  19. 并授权使用您的账号信息(如昵称、头像、收货地址)以便您统一管理
  20. </view>
  21. </view>
  22. <u-toast ref="uToast" />
  23. </view>
  24. </template>
  25. <script>
  26. import getUrlParams from "./../../../utils/getUrlParams.js";
  27. import { config } from '@/common/config.js';
  28. export default {
  29. data() {
  30. return {
  31. tel: '',
  32. messageCode:'',
  33. messageShow: false,
  34. messageDisable: false,
  35. codelength: 4,
  36. show: false,
  37. second:60,
  38. toastMsg:'',
  39. toastUrl:'',
  40. toastType:'',
  41. accessToken:'',
  42. userId:'',
  43. telError:false,
  44. // messageError:false
  45. }
  46. },
  47. onLoad() {
  48. },
  49. computed: {
  50. inputStyle() {
  51. let style = {};
  52. if(this.tel.length==11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  53. style.color = "#fff";
  54. style.backgroundColor = '#5295F5';
  55. this.telError = false;
  56. // style.backgroundColor = this.$u.color['warning'];
  57. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  58. this.telError = true;
  59. }
  60. return style;
  61. }
  62. },
  63. methods: {
  64. showToast() {
  65. this.$refs.uToast.show({
  66. title: this.toastMsg,
  67. type: this.toastType,
  68. url: this.toastUrl
  69. })
  70. },
  71. submit() {
  72. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  73. let that = this;
  74. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  75. .then(res =>{
  76. this.messageDisable = true;
  77. this.messageShow = true;
  78. this.show = true;
  79. let interval = setInterval(() => {
  80. that.second--;
  81. if (that.second <= 0) {
  82. that.messageDisable=false
  83. that.messageShow = false;
  84. if (that.messageCode.lenth != 4) {
  85. // this.messageError = true;
  86. }
  87. clearInterval(interval);
  88. that.second=60;
  89. }
  90. }, 1000);
  91. console.log('getcode res',res);
  92. this.accessToken = res.data.accessToken;
  93. this.$u.vuex('vuex_token', res.data.accessToken);
  94. this.userId = res.data.userId;
  95. }).catch(err=>{
  96. console.log('err',err)
  97. this.toastMsg = err.code + ":" + err.msg;
  98. this.showToast();
  99. console.log('getcode err',err)
  100. });
  101. }
  102. },
  103. // 收不到验证码选择时的选择
  104. // noCaptcha() {
  105. // uni.showActionSheet({
  106. // itemList: ['重新获取验证码', '接听语音验证码'],
  107. // success: function(res) {
  108. // },
  109. // fail: function(res) {
  110. // }
  111. // });
  112. // },
  113. // change事件侦听
  114. change(value) {
  115. // console.log('change', value);
  116. },
  117. // 输入完验证码最后一位执行
  118. finish(value) {
  119. let params = {
  120. accessToken:this.accessToken,
  121. userId:this.userId,
  122. code:value
  123. };
  124. this.$u.api.phoneLoginAuth(params)
  125. .then(res =>{
  126. if(res.code=='200'){
  127. // console.log('finish res',res);
  128. this.$u.vuex('vuex_user', res.data);
  129. this.$u.vuex('vuex_hasLogin', true);
  130. this.wechatLogin()
  131. // location.href = backUrl;
  132. // this.$refs.uToast.show({
  133. // title: '操作成功',
  134. // url: backUrl,
  135. // // isTab:true,
  136. // callback :function(err){
  137. // console.log('err',err)
  138. // }
  139. // });
  140. // this.$u.api.getuseinfo().then(res => {
  141. // console.log('getuseinfo',res.retBody);
  142. // this.$u.vuex('vuex_user', res.retBody);
  143. // this.toastMsg = '操作成功';
  144. // this.showToast();
  145. // setTimeout(() => {
  146. // this.$u.route({
  147. // url: 'pages/template/wxCenter/index'
  148. // })
  149. // }, 2000);
  150. // });
  151. }else{
  152. this.toastMsg = res.msg;
  153. this.showToast();
  154. }
  155. }).catch(err=>{
  156. console.log('finish err',err);
  157. this.toastMsg = err.msg;
  158. this.showToast();
  159. });
  160. },
  161. // 微信登录
  162. wechatLogin() {
  163. const openId = this.$store.state.vuex_wxinfo.openId
  164. if (openId) {
  165. this.jumpIndex()
  166. } else {
  167. this.getCode()
  168. }
  169. },
  170. // 微信已登录则跳转到首页
  171. jumpIndex() {
  172. let storage = window.sessionStorage;
  173. let backUrl = storage.getItem('backUrl');
  174. if (backUrl) {
  175. location.href = backUrl;
  176. } else {
  177. location.href = config.jumpHomeUrl
  178. }
  179. },
  180. // 获取code
  181. getCode () {
  182. var local = window.location.href // 获取页面url
  183. let locationLocaturl = window.location.search;
  184. this.code = getUrlParams(locationLocaturl,"code"); // 截取code
  185. if (this.code == null || this.code === '') { // 如果没有code,则去请求
  186. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
  187. } else {
  188. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  189. }
  190. },
  191. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  192. handleGetWXInfo (code) {
  193. this.$u.api.getWXInfo(code).then((res) => {
  194. if (res.code === 200 ) {
  195. this.$u.vuex('vuex_wxinfo', res.data);
  196. this.jumpIndex()
  197. }
  198. }).catch((err) => {
  199. this.$refs.uToast.show({
  200. title: err.msg,
  201. type: 'error',
  202. });
  203. })
  204. },
  205. }
  206. };
  207. </script>
  208. <style lang="scss" scoped>
  209. .hide{display: none!important;}
  210. .wrap {
  211. font-size: 28rpx;
  212. .content {
  213. width: 600rpx;
  214. margin: 80rpx auto 0;
  215. .title {
  216. text-align: left;
  217. font-size: 60rpx;
  218. font-weight: 500;
  219. margin-bottom: 100rpx;
  220. }
  221. input {
  222. text-align: left;
  223. margin-bottom: 10rpx;
  224. padding-bottom: 20rpx;
  225. border-bottom: 1px solid #ddd;
  226. }
  227. .getCaptcha {
  228. margin: 45rpx auto 130rpx;
  229. background-color: #a8c6f1;
  230. color: $u-tips-color;
  231. border: none;
  232. font-size: 30rpx;
  233. padding: 12rpx 0;
  234. &::after {
  235. border: none;
  236. }
  237. }
  238. }
  239. .buttom {
  240. .hint {
  241. padding: 20rpx 40rpx;
  242. font-size: 20rpx;
  243. color: $u-tips-color;
  244. .link {
  245. color: $u-type-warning;
  246. }
  247. }
  248. }
  249. .captcha {
  250. color: $u-type-warning;
  251. font-size: 30rpx;
  252. margin-top: 40rpx;
  253. text-align: center;
  254. }
  255. }
  256. </style>