login.vue 8.3 KB

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