index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <text class="text">欢迎登录耗材小程序</text>
  5. <view class="one_1">
  6. <uni-forms ref="valiForm" :rules="rules" :modelValue="form">
  7. <uni-forms-item label="电话号" required name="tel">
  8. <uni-easyinput v-model="form.tel" placeholder="请输入电话号" />
  9. </uni-forms-item>
  10. <uni-forms-item label="密码" required name="password">
  11. <uni-easyinput type="password" v-model="form.password" placeholder="请输入密码" />
  12. </uni-forms-item>
  13. </uni-forms>
  14. <button class="button" type="primary" @click="submit('valiForm')">登录</button>
  15. <view class="other">
  16. <view class="other_1" @tap="toLogin">
  17. <text class="iconfont icon-weixin"></text>
  18. <text>直接使用微信登录</text>
  19. </view>
  20. <view @tap="toRegister">注册</view>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. form: {},
  31. // 校验规则
  32. rules: {
  33. tel: {
  34. rules: [{
  35. required: true,
  36. errorMessage: '电话号不能为空'
  37. }, {
  38. format: 'number',
  39. errorMessage: '电话号只能输入数字'
  40. }]
  41. },
  42. password: {
  43. rules: [{
  44. required: true,
  45. errorMessage: '密码不能为空'
  46. }]
  47. }
  48. },
  49. }
  50. },
  51. onLoad() {
  52. },
  53. methods: {
  54. // 登录
  55. submit(ref) {
  56. const that = this;
  57. that.$refs[ref].validate().then(async form => {
  58. form.tel = form.tel.toString()
  59. const res = await that.$api(`/User/login`, 'POST', form);
  60. if (res.errcode == '0') {
  61. const token = await that.$token(`/tool/token`, 'GET', {
  62. token: res.data
  63. });
  64. if (token.errcode == '0') {
  65. uni.setStorage({
  66. key: 'token',
  67. data: token.data,
  68. success: function(res) {
  69. let url = `/pages/home/index`;
  70. uni.reLaunch({
  71. url
  72. })
  73. },
  74. fail: function(err) {
  75. console.log(err);
  76. }
  77. })
  78. }
  79. } else {
  80. uni.showToast({
  81. title: res.errmsg,
  82. icon: 'none'
  83. })
  84. }
  85. })
  86. },
  87. // 微信登录
  88. toLogin() {
  89. const that = this;
  90. uni.getStorage({
  91. key: 'openid',
  92. success: async function(res) {
  93. const aee = await that.$api(`/User/openid`, 'GET', {
  94. openid: res.data
  95. });
  96. if (aee.errcode == '0') {
  97. const token = await that.$token(`/tool/token`, 'GET', {
  98. token: aee.data
  99. });
  100. if (token.errcode == '0') {
  101. uni.setStorage({
  102. key: 'token',
  103. data: token.data,
  104. success: function(res) {
  105. let url = `/pages/home/index`;
  106. uni.reLaunch({
  107. url
  108. })
  109. },
  110. fail: function(err) {
  111. console.log(err);
  112. }
  113. })
  114. }
  115. } else {
  116. uni.showToast({
  117. title: aee.errmsg + ` 请注册账号!`,
  118. icon: 'none'
  119. })
  120. }
  121. },
  122. fail: async function(err) {
  123. uni.showToast({
  124. title: err,
  125. icon: 'none'
  126. })
  127. }
  128. })
  129. },
  130. // 注册
  131. toRegister() {
  132. let url = `/pages/register/index`;
  133. uni.navigateTo({
  134. url
  135. })
  136. },
  137. }
  138. }
  139. </script>
  140. <style lang="scss">
  141. .content {
  142. height: 100vh;
  143. display: flex;
  144. flex-direction: column;
  145. align-items: center;
  146. justify-content: center;
  147. background-image: url('../../static/login.jpeg');
  148. background-repeat: no-repeat;
  149. background-size: cover;
  150. background-position: center;
  151. .one {
  152. text-align: center;
  153. background-color: var(--mainColor);
  154. padding: 5vw 12vw;
  155. .text {
  156. padding: 5px;
  157. font-family: "宋体";
  158. font-size: var(--font16Size);
  159. font-weight: bold;
  160. border-bottom: 1px solid var(--f3CColor);
  161. }
  162. .one_1 {
  163. margin: 5vw 0 0 0;
  164. .button {
  165. background-color: var(--f3CColor);
  166. color: var(--mainColor);
  167. font-size: var(--font14Size);
  168. }
  169. .other {
  170. display: flex;
  171. justify-content: space-around;
  172. padding: 2vw 0 0 0;
  173. font-size: var(--font13Size);
  174. color: var(--fFB1Color);
  175. .other_1 {
  176. display: flex;
  177. align-items: center;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. </style>