login.vue 9.0 KB

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