login.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. 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. try {
  151. this.isLogin = true
  152. await this.customLogin(this.params)
  153. this.isLogin = false
  154. this.hasProfile = false
  155. } catch (e) {
  156. this.isLogin = false
  157. this.hasProfile = false
  158. }
  159. },
  160. //获取手机验证码
  161. GetMobileCodeFn() {
  162. if (this.params.mobile == '') {
  163. this.$util.msg('请输入手机号', 2000)
  164. return
  165. }
  166. if (!this.$reg.isMobile(this.params.mobile)) {
  167. this.$util.msg('请输入正确的手机号', 2000)
  168. return
  169. }
  170. this.isMobileDisabled = true
  171. this.PublicService.GetheHeSend({
  172. mobile: this.params.mobile
  173. })
  174. .then(res => {
  175. const TIME_COUNT = 60
  176. this.$util.msg('验证短信已发送', 2000)
  177. if (!this.codeTime) {
  178. this.count = TIME_COUNT
  179. this.isMobileDisabled = true
  180. this.codeTime = setInterval(() => {
  181. if (this.count > 1 && this.count <= TIME_COUNT) {
  182. this.count--
  183. this.mobileCodeText = this.count + 's重新发送'
  184. } else {
  185. this.isMobileDisabled = false
  186. clearInterval(this.codeTime)
  187. this.codeTime = null
  188. this.mobileCodeText = '获取验证码'
  189. }
  190. }, 1000)
  191. }
  192. })
  193. .catch(error => {
  194. this.$util.msg(error.msg, 2000)
  195. this.isMobileDisabled = false
  196. })
  197. },
  198. handBlurInput(e) {
  199. console.log(e)
  200. if (e.detail.value) {
  201. this.isMobileDisabled = false
  202. } else {
  203. this.isMobileDisabled = true
  204. }
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss">
  210. .login {
  211. display: flex;
  212. align-items: center;
  213. flex-direction: column;
  214. .login-main,
  215. .login-title,
  216. .logo-message {
  217. width: 590rpx;
  218. }
  219. .login-main {
  220. display: flex;
  221. justify-content: center;
  222. margin: 180rpx 0 60rpx;
  223. .logo {
  224. width: 200rpx;
  225. height: 200rpx;
  226. display: block;
  227. }
  228. }
  229. .login-title {
  230. display: flex;
  231. justify-content: flex-start;
  232. image {
  233. width: 40rpx;
  234. height: 40rpx;
  235. margin-right: 6rpx;
  236. }
  237. }
  238. .login-btn {
  239. width: 600rpx;
  240. height: 90rpx;
  241. text-align: center;
  242. line-height: 90rpx;
  243. background: $btn-confirm;
  244. border-radius: 45rpx;
  245. color: #fff !important;
  246. margin-top: 40rpx;
  247. &.disabled {
  248. background: #e1e1e1;
  249. color: #999999;
  250. }
  251. }
  252. .login-input {
  253. margin: 10rpx 0;
  254. width: 600rpx;
  255. height: 90rpx;
  256. box-sizing: border-box;
  257. padding: 25rpx 0;
  258. border-bottom: 1px solid #e1e1e1;
  259. .input {
  260. width: 100%;
  261. height: 40rpx;
  262. left: 40rpx;
  263. font-size: $font-size-28;
  264. color: #333333;
  265. }
  266. .input_code {
  267. width: 420rpx;
  268. height: 40rpx;
  269. left: 40rpx;
  270. font-size: $font-size-28;
  271. color: #333333;
  272. float: left;
  273. }
  274. .code-btn {
  275. width: 180rpx;
  276. height: 40rpx;
  277. line-height: 40rpx;
  278. font-size: $font-size-28;
  279. color: #333333;
  280. float: right;
  281. text-align: center;
  282. .button {
  283. width: 180rpx;
  284. height: 40rpx;
  285. line-height: 40rpx;
  286. padding: 0;
  287. color: $color-system;
  288. }
  289. &.disabled {
  290. .button {
  291. color: #b2b2b2;
  292. }
  293. }
  294. }
  295. }
  296. .logo-message {
  297. font-size: 24rpx;
  298. line-height: 44rpx;
  299. color: #fc464c;
  300. text-align: left;
  301. }
  302. }
  303. </style>