login.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view class="app">
  3. <!-- logo -->
  4. <view class="header">
  5. <u-avatar src="/static/logo.png" size="200"></u-avatar>
  6. <view class="nickname">登录</view>
  7. </view>
  8. <!-- 输入框 -->
  9. <view class="body">
  10. <u-form :model="form1" ref="uForm" style="width: 100%;">
  11. <u-form-item label-width="0" prop="username" :borderBottom="false">
  12. <u-input border placeholder="请输入用户名" v-model="form1.username" type="text" height="90"></u-input>
  13. </u-form-item>
  14. <u-form-item label-width="0" prop="password" :borderBottom="false">
  15. <u-input border placeholder="请输入密码" v-model="form1.password" type="password" height="90"></u-input>
  16. </u-form-item>
  17. </u-form>
  18. <u-gap></u-gap>
  19. <u-button class="bwin-btn-100 u-m-b-10" type="primary" @click="loginHandle">登录</u-button>
  20. <!-- <u-button class="bwin-btn-100">取消</u-button> -->
  21. </view>
  22. <!-- 忘记密码导航 -->
  23. <view class="function-row">
  24. <!-- <navigator url="/pages/user/login/login">忘记密码</navigator>
  25. <text style="margin: 0 16rpx;">|</text> -->
  26. <navigator url="/pages/user/login/register">立即注册</navigator>
  27. </view>
  28. <!-- 三方登录 -->
  29. <!-- #ifdef MP-WEIXIN -->
  30. <u-line margin="40rpx 0 0 0"></u-line>
  31. <view class="third-login u-m-b-40">
  32. <u-line margin="0 0 40rpx 0"></u-line>
  33. <button class="login-icon-item scoped-login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  34. <u-icon name="weixin-fill" size="64" color="#19be6b" label="微信一键登录" labelPos="bottom" labelSize="24"></u-icon>
  35. </button>
  36. </view>
  37. <u-modal
  38. v-model="loginByWeixinModalShow"
  39. title="提示"
  40. content="未注册用户请先完成注册并绑定微信后再使用微信登录~"
  41. showCancelButton
  42. showConfirmButton
  43. confirmText="继续微信登录"
  44. @confirm="loginByWeixin()"
  45. ></u-modal>
  46. <!-- #endif -->
  47. <view class="footer">
  48. 登录即代表同意
  49. <text class="agreement">《用户协议》</text>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. var that;
  55. export default {
  56. data() {
  57. // 页面数据变量
  58. return {
  59. loginByWeixinModalShow: false, // 微信登录提醒
  60. // init请求返回的数据
  61. data: {},
  62. // 表单请求数据
  63. form1: {
  64. username: '',
  65. password: ''
  66. },
  67. rules: {
  68. username: [
  69. {
  70. required: true,
  71. message: '请输入账号',
  72. // 可以单个或者同时写两个触发验证方式
  73. trigger: 'blur,change'
  74. },
  75. ],
  76. password: [
  77. {
  78. min: 6,
  79. message: '密码不能少于6个字符',
  80. trigger: 'change'
  81. },
  82. ]
  83. },
  84. scrollTop: 0
  85. };
  86. },
  87. onPageScroll(e) {
  88. this.scrollTop = e.scrollTop;
  89. },
  90. // 监听 - 页面每次【加载时】执行(如:前进)
  91. onLoad(options = {}) {
  92. that = this;
  93. that.options = options;
  94. // if (xxx.checkToken()) {
  95. // xxx.toast('您已登录', 'none', 1500, true, function() {
  96. // xxx.navigateToHome();
  97. // });
  98. // }
  99. that.init(options);
  100. },
  101. // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快,会在页面进入动画完成前触发
  102. onReady() {
  103. this.$refs.uForm.setRules(this.rules);
  104. },
  105. /**
  106. * 监听 - 点击右上角转发时 文档 https://uniapp.dcloud.io/api/plugins/share?id=onshareappmessage
  107. * 如果删除onShareAppMessage函数,则微信小程序右上角转发按钮会自动变灰
  108. */
  109. onShareAppMessage(options) {},
  110. // 函数
  111. methods: {
  112. getPhoneNumber (e) {
  113. const dtlObj = e.detail || {}
  114. let that = this
  115. uni.login({
  116. success: function (res) {
  117. if (res.code) {
  118. uni.api.base.apiwxlogin({
  119. code: res.code,
  120. phone_code: dtlObj.code,
  121. iv: dtlObj.iv,
  122. encrypted_data: dtlObj.encryptedData,
  123. }).then(cData => {
  124. uni.setStorageSync('MD_token', cData.token)
  125. uni.setStorageSync('MD_userInfo', cData)
  126. uni.setStorageSync('MD_phone', cData.phone)
  127. uni.$msg('登录成功~')
  128. uni.reLaunch({
  129. url: '/pages/index/index'
  130. })
  131. }).catch(err => {
  132. console.log(err)
  133. })
  134. } else {
  135. console.log('登录失败!' + res.errMsg)
  136. }
  137. }
  138. })
  139. },
  140. // 页面数据初始化函数
  141. async init(options = {}) {
  142. const username = uni.getStorageSync('MD_phone')
  143. this.form1.username = username
  144. },
  145. // 账密登录
  146. loginHandle() {
  147. this.$refs.uForm.validate(valid => {
  148. if (valid) {
  149. uni.api.base.apilogin({
  150. phone: this.form1.username,
  151. password: this.form1.password,
  152. }).then(res => {
  153. uni.setStorageSync('MD_userInfo', res)
  154. uni.setStorageSync('MD_phone', res.phone)
  155. uni.setStorageSync('MD_token', res.token)
  156. uni.$msg('登录成功~')
  157. uni.navigateTo({
  158. url: '/pages/index/index'
  159. })
  160. })
  161. } else {
  162. console.log('验证失败');
  163. return;
  164. }
  165. });
  166. // 发送登录请求
  167. // vk.userCenter.login({
  168. // data: {
  169. // username: that.form1.username,
  170. // password: that.form1.password
  171. // },
  172. // success: res => {
  173. // // 成功后的逻辑
  174. // vk.callFunction({
  175. // url: 'client/agent/kh/getAgentInfo',
  176. // needAlert: false
  177. // }).then(res => {
  178. // vk.vuex.set('$user.agentInfo', res.info);
  179. // vk.alert('登录成功', '确定', '提示', function() {
  180. // vk.navigateToHome();
  181. // });
  182. // });
  183. // }
  184. // });
  185. },
  186. // 微信登录
  187. loginByWeixin() {
  188. // vk.userCenter.loginByWeixin({
  189. // data: {
  190. // type: 'login' // 无账号不注册
  191. // },
  192. // success: data => {
  193. // // 成功后的逻辑
  194. // vk.callFunction({
  195. // url: 'client/agent/kh/getAgentInfo',
  196. // needAlert: false
  197. // }).then(res => {
  198. // vk.vuex.set('$user.agentInfo', res.info);
  199. // vk.alert('登录成功', '确定', '提示', function() {
  200. // vk.navigateToHome();
  201. // });
  202. // });
  203. // }
  204. // });
  205. }
  206. },
  207. // 监听器
  208. watch: {},
  209. // 计算属性
  210. computed: {}
  211. };
  212. </script>
  213. <style lang="scss" scoped>
  214. .header {
  215. padding-top: 15%;
  216. margin-bottom: 40rpx;
  217. display: flex;
  218. flex-direction: column;
  219. align-items: center;
  220. .avatar {
  221. width: 200rpx;
  222. height: 200rpx;
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. margin-bottom: 20rpx;
  227. }
  228. .nickname {
  229. font-size: $u-h3;
  230. }
  231. }
  232. .body {
  233. width: 100%;
  234. display: flex;
  235. flex-direction: column;
  236. align-items: center;
  237. padding: 20rpx 100rpx;
  238. margin: 40rpx 0;
  239. .title {
  240. font-size: $u-p;
  241. margin-bottom: 20rpx;
  242. }
  243. .tips {
  244. font-size: $u-p2;
  245. color: $u-tips-color;
  246. margin-bottom: 60rpx;
  247. }
  248. }
  249. .third-login {
  250. display: flex;
  251. flex-direction: column;
  252. align-items: center;
  253. justify-content: center;
  254. padding: 32rpx;
  255. }
  256. .function-row {
  257. font-size: $u-p2;
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. margin: 0 auto;
  262. width: 300rpx;
  263. }
  264. .footer {
  265. position: absolute;
  266. width: 100%;
  267. text-align: center;
  268. bottom: 20rpx;
  269. font-size: $u-p2;
  270. color: $u-content-color;
  271. .agreement {
  272. color: $u-theme-color;
  273. }
  274. }
  275. .scoped-login-btn {
  276. background: transparent;
  277. border: 0;
  278. outline:0px;
  279. -webkit-appearance: none;
  280. &::after {
  281. display: none;
  282. }
  283. }
  284. </style>