login.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. <cm-loading text="登录中..." :visible="isLogin"></cm-loading>
  48. </view>
  49. </template>
  50. <script>
  51. import authorize from '@/common/authorize.js'
  52. import CmLoading from '@/components/cm-module/cm-loading/cm-loading.vue'
  53. import { mapGetters, mapActions } from 'vuex'
  54. export default {
  55. components: {
  56. CmLoading
  57. },
  58. data() {
  59. return {
  60. invitationCode: '', //获取用户登录的邀请码
  61. loginMessage: '未注册的手机号验证后自动创建呵呵商城账户', //登录信息反馈
  62. StaticUrl: this.$Static,
  63. disabled: false,
  64. params: {
  65. mobile: '',
  66. headImgUrl: '', //微信头像
  67. nickName: '', //微信昵称
  68. openId: '', //微信openId
  69. verificationCode: '' //短信验证码
  70. },
  71. count: '', //倒计时
  72. isMobileDisabled: true, //获取手机短信按钮
  73. mobileCodeText: '获取验证码',
  74. codeTime: null,
  75. isLogin: false,
  76. hasProfile: false
  77. }
  78. },
  79. onLoad() {
  80. if (this.hasLogin) return this.$api.switchTabTo('/pages/tabBar/index/index')
  81. },
  82. created() {
  83. this.loginInit()
  84. },
  85. computed: {
  86. ...mapGetters(['hasLogin'])
  87. },
  88. methods: {
  89. ...mapActions('user', ['customLogin']),
  90. // 初始化登录状态
  91. loginInit() {
  92. const userInfo = uni.getStorageSync('userInfo')
  93. if (userInfo) {
  94. this.hasProfile = true
  95. this.params.nickName = userInfo.nickName
  96. this.params.headImgUrl = userInfo.avatarUrl
  97. } else {
  98. this.hasProfile = false
  99. }
  100. },
  101. // 登录
  102. SubMitLogins() {
  103. if (!this.params.mobile) {
  104. this.$util.msg('请输入手机号', 2000)
  105. return
  106. }
  107. if (!this.$reg.isMobile(this.params.mobile)) {
  108. this.$util.msg('请输入正确的手机号', 2000)
  109. return
  110. }
  111. if (this.params.verificationCode == '') {
  112. this.$util.msg('请输入短信验证码', 2000)
  113. return
  114. }
  115. this.params.openId = uni.getStorageSync('openId')
  116. // 直接登录 / 授权再登录
  117. this.hasProfile ? this.LoginGetUser() : this.ProfileAndLogin()
  118. },
  119. // 获取用户信息
  120. GetUserProfile() {
  121. return new Promise((resolve, reject) => {
  122. wx.getUserProfile({
  123. desc: '呵呵商城小程序获取您的信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  124. success: res => {
  125. this.params.nickName = res.userInfo.nickName
  126. this.params.headImgUrl = res.userInfo.avatarUrl
  127. this.hasProfile = true
  128. // 缓存用户信息
  129. uni.setStorageSync('userInfo', res.userInfo)
  130. resolve()
  131. },
  132. fail: () => {
  133. this.$util.msg('授权失败', 2000)
  134. this.hasProfile = false
  135. reject()
  136. }
  137. })
  138. })
  139. },
  140. // 授权并且登录
  141. ProfileAndLogin() {
  142. this.GetUserProfile().then(() => {
  143. this.LoginGetUser()
  144. })
  145. },
  146. // 登录
  147. async LoginGetUser() {
  148. try {
  149. this.isLogin = true
  150. await this.customLogin(this.params)
  151. this.isLogin = false
  152. } catch (e) {
  153. this.isLogin = false
  154. }
  155. },
  156. //获取手机验证码
  157. GetMobileCodeFn() {
  158. if (this.params.mobile == '') {
  159. this.$util.msg('请输入手机号', 2000)
  160. return
  161. }
  162. if (!this.$reg.isMobile(this.params.mobile)) {
  163. this.$util.msg('请输入正确的手机号', 2000)
  164. return
  165. }
  166. this.isMobileDisabled = true
  167. this.PublicService.GetheHeSend({
  168. mobile: this.params.mobile
  169. })
  170. .then(res => {
  171. const TIME_COUNT = 60
  172. this.$util.msg('验证短信已发送', 2000)
  173. if (!this.codeTime) {
  174. this.count = TIME_COUNT
  175. this.isMobileDisabled = true
  176. this.codeTime = setInterval(() => {
  177. if (this.count > 1 && this.count <= TIME_COUNT) {
  178. this.count--
  179. this.mobileCodeText = this.count + 's重新发送'
  180. } else {
  181. this.isMobileDisabled = false
  182. clearInterval(this.codeTime)
  183. this.codeTime = null
  184. this.mobileCodeText = '获取验证码'
  185. }
  186. }, 1000)
  187. }
  188. })
  189. .catch(error => {
  190. this.$util.msg(error.msg, 2000)
  191. this.isMobileDisabled = false
  192. })
  193. },
  194. handBlurInput(e) {
  195. console.log(e)
  196. if (e.detail.value) {
  197. this.isMobileDisabled = false
  198. } else {
  199. this.isMobileDisabled = true
  200. }
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. .login {
  207. display: flex;
  208. align-items: center;
  209. flex-direction: column;
  210. .login-main,
  211. .login-title,
  212. .logo-message {
  213. width: 590rpx;
  214. }
  215. .login-main {
  216. display: flex;
  217. justify-content: center;
  218. margin: 180rpx 0 60rpx;
  219. .logo {
  220. width: 200rpx;
  221. height: 200rpx;
  222. display: block;
  223. }
  224. }
  225. .login-title {
  226. display: flex;
  227. justify-content: flex-start;
  228. image {
  229. width: 40rpx;
  230. height: 40rpx;
  231. margin-right: 6rpx;
  232. }
  233. }
  234. .login-btn {
  235. width: 600rpx;
  236. height: 90rpx;
  237. text-align: center;
  238. line-height: 90rpx;
  239. background: $btn-confirm;
  240. border-radius: 45rpx;
  241. color: #fff !important;
  242. margin-top: 40rpx;
  243. &.disabled {
  244. background: #e1e1e1;
  245. color: #999999;
  246. }
  247. }
  248. .login-input {
  249. margin: 10rpx 0;
  250. width: 600rpx;
  251. height: 90rpx;
  252. box-sizing: border-box;
  253. padding: 25rpx 0;
  254. border-bottom: 1px solid #e1e1e1;
  255. .input {
  256. width: 100%;
  257. height: 40rpx;
  258. left: 40rpx;
  259. font-size: $font-size-28;
  260. color: #333333;
  261. }
  262. .input_code {
  263. width: 420rpx;
  264. height: 40rpx;
  265. left: 40rpx;
  266. font-size: $font-size-28;
  267. color: #333333;
  268. float: left;
  269. }
  270. .code-btn {
  271. width: 180rpx;
  272. height: 40rpx;
  273. line-height: 40rpx;
  274. font-size: $font-size-28;
  275. color: #333333;
  276. float: right;
  277. text-align: center;
  278. .button {
  279. width: 180rpx;
  280. height: 40rpx;
  281. line-height: 40rpx;
  282. padding: 0;
  283. color: $color-system;
  284. }
  285. &.disabled {
  286. .button {
  287. color: #b2b2b2;
  288. }
  289. }
  290. }
  291. }
  292. .logo-message {
  293. font-size: 24rpx;
  294. line-height: 44rpx;
  295. color: #fc464c;
  296. text-align: left;
  297. }
  298. }
  299. </style>