login.vue 9.7 KB

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