login.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.verificationCode"
  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"
  37. :class="[isLoginStatus ? 'disabled' : '']"
  38. type="primary"
  39. open-type="getUserInfo"
  40. size="small"
  41. @click="SubMitLogins"
  42. lang="zh_CN">
  43. 登录
  44. </button>
  45. </view>
  46. </template>
  47. <script>
  48. import authorize from '@/common/config/authorize.js'
  49. import wxLogin from '@/services/wxLogin.js'
  50. import { mapState, mapMutations } from 'vuex'
  51. export default {
  52. data() {
  53. return {
  54. invitationCode: '', //获取用户登录的邀请码
  55. loginMessage: '未注册的手机号验证后自动创建呵呵商城账户' ,//登录信息反馈
  56. StaticUrl:this.$Static,
  57. disabled:false,
  58. params:{
  59. mobile:'',
  60. headImgUrl:'', //微信头像
  61. nickName:'', //微信昵称
  62. openId:'', //微信openId
  63. verificationCode:''//短信验证码
  64. },
  65. count: '', //倒计时
  66. isMobileDisabled:true, //获取手机短信按钮
  67. mobileCodeText: '获取验证码',
  68. codeTime: null,
  69. isLoginStatus:false
  70. }
  71. },
  72. onLoad(option) {},
  73. computed: {
  74. ...mapState(['isWxAuthorize', 'isLoginType', 'isLoginProductId', 'isLoginOrderId']),
  75. },
  76. methods: {
  77. ...mapMutations(['login', 'wxLogin']),
  78. // 登录
  79. SubMitLogins() {
  80. // const WechatInfo = uni.getStorageSync('wechatUserInfo')
  81. if(this.isLoginStatus){return}
  82. if(!this.params.mobile){
  83. this.$util.msg('请输入手机号',2000);
  84. return
  85. }
  86. if(!this.$reg.isMobile(this.params.mobile)){
  87. this.$util.msg('请输入正确的手机号',2000);
  88. return
  89. }
  90. if( this.params.verificationCode == ''){
  91. this.$util.msg('请输入短信验证码',2000);
  92. return
  93. }
  94. this.params.openId = uni.getStorageSync('openId');
  95. this.GetUserProfile();
  96. },
  97. GetUserProfile(){
  98. const self = this
  99. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
  100. wx.getUserProfile({
  101. desc: '呵呵商城小程序获取您的信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  102. success(res) {
  103. console.log('新API',res)
  104. self.wxLogin(res.userInfo)
  105. self.params.nickName = res.userInfo.nickName
  106. self.params.headImgUrl = res.userInfo.avatarUrl
  107. self.isLoginStatus = true
  108. self.LoginGetUser()
  109. },
  110. fail() {
  111. self.$util.msg('授权失败', 2000)
  112. }
  113. })
  114. },
  115. LoginGetUser(){
  116. this.UserService.UserMobileLogin(this.params)
  117. .then(response => {
  118. // 保存用户信息
  119. const _data = JSON.parse(response.data);
  120. this.login(_data)
  121. this.$store.commit('updateStatus',_data)
  122. // 登录成功提示
  123. this.$util.msg('登录成功', 1500, false, 'success')
  124. this.isLoginStatus = false
  125. setTimeout(() => {
  126. this.$api.switchTabTo(`/pages/tabBar/index/index`)
  127. }, 1500)
  128. })
  129. .catch(error => {
  130. this.$util.msg(error.msg,2000)
  131. this.isLoginStatus = false
  132. })
  133. },
  134. GetMobileCodeFn(){//获取手机验证码
  135. if( this.params.mobile == ''){
  136. this.$util.msg('请输入手机号',2000);
  137. return
  138. }
  139. if(!this.$reg.isMobile(this.params.mobile)){
  140. this.$util.msg('请输入正确的手机号',2000);
  141. return
  142. }
  143. this.isMobileDisabled = true;
  144. this.PublicService.GetheHeSend(
  145. {
  146. mobile:this.params.mobile,
  147. }
  148. )
  149. .then(res =>{
  150. const TIME_COUNT = 60;
  151. this.$util.msg('验证短信已发送',2000)
  152. if (!this.codeTime) {
  153. this.count = TIME_COUNT;
  154. this.isMobileDisabled = true;
  155. this.codeTime = setInterval(() => {
  156. if (this.count > 1 && this.count <= TIME_COUNT) {
  157. this.count--
  158. this.mobileCodeText = this.count +'s重新发送'
  159. } else {
  160. this.isMobileDisabled = false;
  161. clearInterval(this.codeTime)
  162. this.codeTime = null
  163. this.mobileCodeText = '获取验证码'
  164. }
  165. },1000)
  166. }
  167. })
  168. .catch( error =>{
  169. this.$util.msg(error.msg,2000)
  170. this.isMobileDisabled = false;
  171. })
  172. },
  173. handBlurInput(e){
  174. console.log(e)
  175. if(e.detail.value){
  176. this.isMobileDisabled = false
  177. }else{
  178. this.isMobileDisabled = true
  179. }
  180. }
  181. },
  182. onShow() {
  183. wxLogin.wxLoginAuthorize()
  184. }
  185. }
  186. </script>
  187. <style lang="scss">
  188. .login {
  189. display: flex;
  190. align-items: center;
  191. flex-direction: column;
  192. .login-main,
  193. .login-title,
  194. .logo-message {
  195. width: 590rpx;
  196. }
  197. .login-main {
  198. display: flex;
  199. justify-content: center;
  200. margin: 180rpx 0 60rpx;
  201. .logo {
  202. width: 200rpx;
  203. height: 200rpx;
  204. display: block;
  205. }
  206. }
  207. .login-title {
  208. display: flex;
  209. justify-content: flex-start;
  210. image {
  211. width: 40rpx;
  212. height: 40rpx;
  213. margin-right: 6rpx;
  214. }
  215. }
  216. .login-btn {
  217. width: 600rpx;
  218. height: 90rpx;
  219. text-align: center;
  220. line-height: 90rpx;
  221. background:$btn-confirm ;
  222. border-radius: 45rpx;
  223. color: #fff !important;
  224. margin-top: 40rpx;
  225. &.disabled{
  226. background: #E1E1E1;
  227. color: #999999;
  228. }
  229. }
  230. .login-input {
  231. margin: 10rpx 0;
  232. width: 600rpx;
  233. height: 90rpx;
  234. box-sizing: border-box;
  235. padding: 25rpx 0;
  236. border-bottom: 1px solid #E1E1E1;
  237. .input{
  238. width: 100%;
  239. height: 40rpx;
  240. left: 40rpx;
  241. font-size: $font-size-28;
  242. color: #333333;
  243. }
  244. .input_code{
  245. width: 420rpx;
  246. height: 40rpx;
  247. left: 40rpx;
  248. font-size: $font-size-28;
  249. color: #333333;
  250. float: left;
  251. }
  252. .code-btn{
  253. width: 180rpx;
  254. height: 40rpx;
  255. line-height: 40rpx;
  256. font-size: $font-size-28;
  257. color: #333333;
  258. float: right;
  259. text-align: center;
  260. .button{
  261. width: 180rpx;
  262. height: 40rpx;
  263. line-height: 40rpx;
  264. padding: 0;
  265. color: $color-system;
  266. }
  267. &.disabled{
  268. .button{
  269. color: #b2b2b2;
  270. }
  271. }
  272. }
  273. }
  274. .logo-message {
  275. font-size: 24rpx;
  276. line-height: 44rpx;
  277. color: #fc464c;
  278. text-align: left;
  279. }
  280. }
  281. </style>