login.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <view class="container login">
  3. <!-- logo区域 -->
  4. <view class="login-main">
  5. <image class="logo" :src="StaticUrl+'icon-logo@2x.png'" mode="widthFix"></image>
  6. </view>
  7. <!-- 邀请码 -->
  8. <view class="login-input">
  9. <input class="input"
  10. type="number"
  11. v-model="params.mobile"
  12. @blur="handBlurInput"
  13. placeholder="请输入手机号"
  14. maxlength="11"
  15. />
  16. </view>
  17. <view class="login-input">
  18. <input class="input_code"
  19. type="number"
  20. v-model="params.msgCode"
  21. placeholder="请输入短信验证码"
  22. maxlength="6"
  23. />
  24. <view class="code-btn" :class="[isMobileDisabled ? 'disabled' : '']">
  25. <button type="button"
  26. @click.stop="GetMobileCodeFn"
  27. :disabled="isMobileDisabled"
  28. class="button"
  29. >
  30. {{ mobileCodeText }}
  31. </button>
  32. </view>
  33. </view>
  34. <!-- 提示信息 -->
  35. <view class="logo-message"><text>{{ loginMessage }}</text></view>
  36. <button class="login-btn" @click="goLogin" :disabled="isCodeEmpty">登录</button>
  37. </view>
  38. </template>
  39. <script>
  40. import authorize from '@/common/config/authorize.js'
  41. import wxLogin from '@/common/config/wxLogin.js'
  42. import { mapState, mapMutations } from 'vuex'
  43. export default {
  44. data() {
  45. return {
  46. invitationCode: '', //获取用户登录的邀请码
  47. loginMessage: '未注册的手机号验证后自动创建呵呵商城账户' ,//登录信息反馈
  48. StaticUrl:this.$Static,
  49. params:{
  50. mobile:'',
  51. msgCode:''
  52. },
  53. count: '', //倒计时
  54. isMobileDisabled:true, //获取手机短信按钮
  55. mobileCodeText: '获取验证码',
  56. codeTime: null,
  57. }
  58. },
  59. onLoad(option) {},
  60. computed: {
  61. ...mapState(['isWxAuthorize', 'isLoginType', 'isLoginProductId', 'isLoginOrderId']),
  62. // 邀请码长度是否符合要求
  63. isCodeEmpty() {
  64. return this.invitationCode.trim().length < 6
  65. },
  66. hasError() {
  67. return this.loginMessage.length > 0
  68. }
  69. },
  70. methods: {
  71. ...mapMutations(['login', 'wxLogin']),
  72. getUserProfile() {
  73. const _that = this
  74. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  75. return new Promise((resolve, reject) => {
  76. wx.getUserProfile({
  77. desc: '用于完善会员资料',
  78. success(res) {
  79. // 更新用户信息
  80. _that.wxLogin(res.userInfo)
  81. uni.setStorageSync('profileFlag', true)
  82. resolve(res.userInfo)
  83. },
  84. fail() {
  85. _that.$util.msg('授权失败', 2000)
  86. }
  87. })
  88. })
  89. },
  90. // 登录
  91. async goLogin() {
  92. const _that = this
  93. let wechatUserInfo = uni.getStorageSync('wechatUserInfo')
  94. let profileFlag = uni.getStorageSync('profileFlag')
  95. console.log(profileFlag);
  96. if (!profileFlag) {
  97. wechatUserInfo = await this.getUserProfile()
  98. }
  99. // 验证邀请码
  100. console.log(this.isWxAuthorize)
  101. this.UserService.userInvitation({
  102. invitationCode: this.invitationCode,
  103. nickName: wechatUserInfo.nickName,
  104. openid: uni.getStorageSync('openid')
  105. })
  106. .then(response => {
  107. console.log(response)
  108. // 保存用户信息
  109. _that.login(response.data)
  110. _that.$store.commit('updateStatus', response.data)
  111. // 登录成功提示
  112. _that.$util.msg('登录成功', 1500, false, 'success')
  113. setTimeout(() => {
  114. _that.$api.navigateTo(`/pages/index/index`)
  115. }, 1500)
  116. })
  117. .catch(error => {
  118. _that.loginMessage = error.msg
  119. _that.isUserInfo = false
  120. setTimeout(() => {
  121. _that.loginMessage = ''
  122. }, 2000)
  123. })
  124. },
  125. GetMobileCodeFn(){//获取手机验证码
  126. if( this.params.mobile == ''){
  127. this.$util.msg('请输入手机号',2000);
  128. return
  129. }
  130. if(!this.$reg.isMobile(this.params.mobile)){
  131. this.$util.msg('请输入正确的手机号',2000);
  132. return
  133. }
  134. let params = {
  135. mobile:this.params.mobile,
  136. platformType:2,
  137. isCheckCaptcha:0,
  138. }
  139. this.isMobileDisabled = true;
  140. this.PublicService.GetBindMobileCode(params)
  141. .then(res =>{
  142. const TIME_COUNT = 60;
  143. this.$util.msg('验证短信已发送',2000)
  144.       if (!this.codeTime) {
  145.         this.count = TIME_COUNT;
  146. this.isMobileDisabled = true;
  147.         this.codeTime = setInterval(() => {
  148.         if (this.count > 1 && this.count <= TIME_COUNT) {
  149.           this.count--
  150.           this.mobileCodeText = this.count +'s重新发送'
  151.          } else {
  152.           this.isMobileDisabled = false;
  153.           clearInterval(this.codeTime)
  154.           this.codeTime = null
  155. this.mobileCodeText = '获取验证码'
  156.          }
  157.         },1000)
  158.        }
  159. })
  160. .catch( error =>{
  161. this.$util.msg(error.msg,2000)
  162. this.isMobileDisabled = false;
  163. })
  164. },
  165. handBlurInput(e){
  166. console.log(e)
  167. if(e.detail.value){
  168. this.isMobileDisabled = false
  169. }else{
  170. this.isMobileDisabled = true
  171. }
  172. }
  173. },
  174. onShow() {}
  175. }
  176. </script>
  177. <style lang="scss">
  178. .login {
  179. display: flex;
  180. align-items: center;
  181. flex-direction: column;
  182. .login-main,
  183. .login-title,
  184. .logo-message {
  185. width: 590rpx;
  186. }
  187. .login-main {
  188. display: flex;
  189. justify-content: center;
  190. margin: 180rpx 0 60rpx;
  191. .logo {
  192. width: 200rpx;
  193. height: 200rpx;
  194. display: block;
  195. }
  196. }
  197. .login-title {
  198. display: flex;
  199. justify-content: flex-start;
  200. image {
  201. width: 40rpx;
  202. height: 40rpx;
  203. margin-right: 6rpx;
  204. }
  205. }
  206. .login-btn {
  207. width: 600rpx;
  208. height: 90rpx;
  209. text-align: center;
  210. line-height: 90rpx;
  211. background:$btn-confirm ;
  212. border-radius: 45rpx;
  213. color: #fff !important;
  214. margin-top: 40rpx;
  215. }
  216. .login-input {
  217. margin: 10rpx 0;
  218. width: 600rpx;
  219. height: 90rpx;
  220. box-sizing: border-box;
  221. padding: 25rpx 0;
  222. border-bottom: 1px solid #E1E1E1;
  223. .input{
  224. width: 100%;
  225. height: 40rpx;
  226. left: 40rpx;
  227. font-size: $font-size-28;
  228. color: #333333;
  229. }
  230. .input_code{
  231. width: 420rpx;
  232. height: 40rpx;
  233. left: 40rpx;
  234. font-size: $font-size-28;
  235. color: #333333;
  236. float: left;
  237. }
  238. .code-btn{
  239. width: 180rpx;
  240. height: 40rpx;
  241. line-height: 40rpx;
  242. font-size: $font-size-28;
  243. color: #333333;
  244. float: right;
  245. text-align: center;
  246. .button{
  247. width: 180rpx;
  248. height: 40rpx;
  249. line-height: 40rpx;
  250. padding: 0;
  251. color: $color-system;
  252. }
  253. &.disabled{
  254. .button{
  255. color: #b2b2b2;
  256. }
  257. }
  258. }
  259. }
  260. .logo-message {
  261. font-size: 24rpx;
  262. line-height: 44rpx;
  263. color: #fc464c;
  264. text-align: left;
  265. }
  266. }
  267. </style>