codeLogin.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <!-- 验证码登录 -->
  3. <view>
  4. <u-navbar title="" autoBack placeholder></u-navbar>
  5. <view class="card_login">
  6. <view class="card_head_logo">
  7. <image class="mine_image" src="../static/logo.png" mode=""></image>
  8. <view class="welcome_title">欢迎登录宝智达气瓶</view>
  9. </view>
  10. <u--form labelPosition="left" :model="formList" :rules="rules" ref="uForm">
  11. <u-form-item label="手机号" prop="phone" labelWidth="80" borderBottom ref="item1">
  12. <u--input v-model="formList.phone" border="none" placeholder="请填写手机号"></u--input>
  13. </u-form-item>
  14. <u-form-item label="验证码" prop="verifyCode" labelWidth="80" borderBottom>
  15. <u--input v-model="formList.verifyCode" border="none" placeholder="请填写验证码"></u--input>
  16. <u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini"
  17. :disabled="disabled1"></u-button>
  18. </u-form-item>
  19. </u--form>
  20. <u-code ref="uCode" @change="codeChange" seconds="90" @start="disabled1 = true"
  21. @end="disabled1 = false"></u-code>
  22. <view class="btn_login">
  23. <view class="title_register" @click="posswordLogin">密码登录</view>
  24. <view class="title_register" @click="goRegister">去注册</view>
  25. </view>
  26. <view class="card_hint">
  27. <view class="card_checkbox">
  28. <u-checkbox-group @change="checkboxChange">
  29. <u-checkbox name="asgas" shape="circle"></u-checkbox>
  30. </u-checkbox-group>
  31. <span class="message" :style="{display:hintShow ? 'block' : 'none'}">
  32. 请先阅读并同意协议
  33. </span>
  34. </view>
  35. <view class="agreement_title">我已阅读并同意<span class="protocol_title">《用户协议》</span>、<span
  36. class="protocol_title">《隐私政策》</span>,
  37. 并授权宝智达气瓶使用该宝智达气瓶账号信息(如昵称、头像、收货地址)进行统一管理
  38. </view>
  39. </view>
  40. <view class="card_btn">
  41. <u-button type="primary" :disabled="disabled" text="登录" @click="getLogin"></u-button>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. formList: {
  51. phone: '',
  52. verifyCode: '',
  53. },
  54. checked: false,
  55. hintShow: false,
  56. userInfo: {},
  57. tips: '',
  58. disabled1: false,
  59. rules: {
  60. phone: {
  61. type: 'string',
  62. required: true,
  63. message: '请填写手机号',
  64. trigger: ['blur']
  65. },
  66. verifyCode: {
  67. type: 'string',
  68. required: true,
  69. message: '请填写验证码',
  70. trigger: ['blur']
  71. },
  72. },
  73. }
  74. },
  75. computed: {
  76. disabled() {
  77. if (this.formList.phone == '' || this.formList.verifyCode == '') {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. },
  84. // beforeCreate() {
  85. // var token = this.$cache.getToken()
  86. // if (token) {
  87. // uni.redirectTo({
  88. // url: '/pages/indexRouter'
  89. // })
  90. // }
  91. // },
  92. methods: {
  93. checkboxChange() {
  94. if (this.checked) {
  95. this.checked = false
  96. } else {
  97. this.checked = true
  98. this.hintShow = false
  99. }
  100. },
  101. getLogin() {
  102. if (!this.checked) {
  103. this.hintShow = true
  104. } else {
  105. uni.showLoading({
  106. title: '登录中...'
  107. });
  108. this.$api.post('/api/applet/login', {
  109. phone: this.formList.phone,
  110. verifyCode: this.formList.verifyCode
  111. }).then((res) => {
  112. if (res.code == 200) {
  113. this.$cache.setToken(res.data.token)
  114. this.getUserInfo()
  115. } else {
  116. uni.showToast({
  117. title: res.data.msg,
  118. icon: 'none'
  119. });
  120. }
  121. uni.hideLoading();
  122. })
  123. }
  124. },
  125. getUserInfo() {
  126. this.$api.get('/api/user/profile').then(res => {
  127. if (res.code == 200) {
  128. this.userInfo = res.data
  129. this.$cache.setCache('userInfo', this.userInfo)
  130. uni.redirectTo({
  131. url: '/pages/indexRouter'
  132. })
  133. } else {
  134. uni.showToast({
  135. title: '当前用户不存在',
  136. icon: 'none'
  137. });
  138. }
  139. })
  140. },
  141. getCode() {
  142. if (this.$refs.uCode.canGetCode) {
  143. // 模拟向后端请求验证码
  144. if (this.formList.phone) {
  145. uni.showLoading({
  146. title: '正在获取验证码'
  147. })
  148. // 通知验证码组件内部开始倒计时
  149. this.$refs.uCode.start();
  150. this.$api.get('/api/verify-code', {
  151. phone: this.formList.phone,
  152. }).then(res => {
  153. if (res.code == 200) {
  154. uni.hideLoading();
  155. // 这里此提示会被this.start()方法中的提示覆盖
  156. uni.$u.toast('验证码已发送');
  157. }
  158. })
  159. } else {
  160. uni.$u.toast('请先输入手机号');
  161. }
  162. } else {
  163. uni.$u.toast('倒计时结束后再发送');
  164. }
  165. },
  166. codeChange(text) {
  167. this.tips = text;
  168. },
  169. // 密码登录
  170. posswordLogin() {
  171. uni.redirectTo({
  172. url: '/pages/login'
  173. })
  174. },
  175. // 注册
  176. goRegister() {
  177. uni.navigateTo({
  178. url: '/pages/register'
  179. })
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .welcome_title {
  186. font-size: 46rpx;
  187. font-weight: 600;
  188. margin-bottom: 60rpx;
  189. margin-top: 10rpx;
  190. }
  191. .card_head_logo {
  192. margin-top: 50rpx;
  193. display: flex;
  194. flex-direction: column;
  195. justify-content: center;
  196. align-items: center;
  197. }
  198. .card_login {
  199. padding: 20rpx 40rpx;
  200. }
  201. .mine_image {
  202. width: 100rpx;
  203. height: 100rpx;
  204. }
  205. .login_input_box {
  206. margin-bottom: 30rpx;
  207. }
  208. .title_register {
  209. display: flex;
  210. justify-content: flex-end;
  211. font-size: 28rpx;
  212. color: #494949;
  213. }
  214. .card_hint {
  215. margin-top: 60rpx;
  216. display: flex;
  217. align-items: flex-start;
  218. }
  219. .agreement_title {
  220. font-size: 26rpx;
  221. color: #333;
  222. }
  223. .protocol_title {
  224. color: #1177ff;
  225. }
  226. .card_btn {
  227. margin-top: 20rpx;
  228. }
  229. .card_checkbox {
  230. margin-top: 5rpx;
  231. position: relative;
  232. cursor: pointer;
  233. }
  234. .message {
  235. display: none;
  236. position: absolute;
  237. padding: 5px 8px;
  238. border: 1px solid #494949;
  239. border-radius: 9px;
  240. top: -36px;
  241. font-size: 12px;
  242. width: 110px;
  243. background: #494949;
  244. color: #fff;
  245. left: -6px;
  246. }
  247. .message::after {
  248. content: '';
  249. position: absolute;
  250. left: 8px;
  251. top: 100%;
  252. border: 8px solid transparent;
  253. border-top: 6px solid #494949;
  254. margin-left: -2px;
  255. }
  256. .btn_login {
  257. margin-top: 20rpx;
  258. display: flex;
  259. justify-content: space-between;
  260. }
  261. </style>