login.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. _that.getUserInfo()
  129. }).catch(err => {
  130. console.log(err)
  131. })
  132. } else {
  133. console.log('登录失败!' + res.errMsg)
  134. }
  135. }
  136. })
  137. },
  138. // 页面数据初始化函数
  139. async init(options = {}) {
  140. const username = uni.getStorageSync('MD_phone')
  141. this.form1.username = username
  142. },
  143. // 账密登录
  144. loginHandle() {
  145. const _that = this
  146. this.$refs.uForm.validate(valid => {
  147. if (valid) {
  148. uni.api.base.apilogin({
  149. phone: this.form1.username,
  150. password: this.form1.password,
  151. }).then(res => {
  152. uni.setStorageSync('MD_userInfo', res)
  153. uni.setStorageSync('MD_phone', res.phone)
  154. uni.setStorageSync('MD_token', res.token)
  155. uni.$msg('登录成功~')
  156. console.log(_that)
  157. _that.getUserInfo()
  158. })
  159. } else {
  160. console.log('验证失败');
  161. return;
  162. }
  163. });
  164. // 发送登录请求
  165. // vk.userCenter.login({
  166. // data: {
  167. // username: that.form1.username,
  168. // password: that.form1.password
  169. // },
  170. // success: res => {
  171. // // 成功后的逻辑
  172. // vk.callFunction({
  173. // url: 'client/agent/kh/getAgentInfo',
  174. // needAlert: false
  175. // }).then(res => {
  176. // vk.vuex.set('$user.agentInfo', res.info);
  177. // vk.alert('登录成功', '确定', '提示', function() {
  178. // vk.navigateToHome();
  179. // });
  180. // });
  181. // }
  182. // });
  183. },
  184. getUserInfo () {
  185. uni.api.base.apiuserinfo().then(res => {
  186. uni.setStorageSync('MD_userInfo2', res)
  187. uni.reLaunch({
  188. url: '/pages/index/index'
  189. })
  190. })
  191. },
  192. // 微信登录
  193. loginByWeixin() {
  194. // vk.userCenter.loginByWeixin({
  195. // data: {
  196. // type: 'login' // 无账号不注册
  197. // },
  198. // success: data => {
  199. // // 成功后的逻辑
  200. // vk.callFunction({
  201. // url: 'client/agent/kh/getAgentInfo',
  202. // needAlert: false
  203. // }).then(res => {
  204. // vk.vuex.set('$user.agentInfo', res.info);
  205. // vk.alert('登录成功', '确定', '提示', function() {
  206. // vk.navigateToHome();
  207. // });
  208. // });
  209. // }
  210. // });
  211. }
  212. },
  213. // 监听器
  214. watch: {},
  215. // 计算属性
  216. computed: {}
  217. };
  218. </script>
  219. <style lang="scss" scoped>
  220. .header {
  221. padding-top: 15%;
  222. margin-bottom: 40rpx;
  223. display: flex;
  224. flex-direction: column;
  225. align-items: center;
  226. .avatar {
  227. width: 200rpx;
  228. height: 200rpx;
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. margin-bottom: 20rpx;
  233. }
  234. .nickname {
  235. font-size: $u-h3;
  236. }
  237. }
  238. .body {
  239. width: 100%;
  240. display: flex;
  241. flex-direction: column;
  242. align-items: center;
  243. padding: 20rpx 100rpx;
  244. margin: 40rpx 0;
  245. .title {
  246. font-size: $u-p;
  247. margin-bottom: 20rpx;
  248. }
  249. .tips {
  250. font-size: $u-p2;
  251. color: $u-tips-color;
  252. margin-bottom: 60rpx;
  253. }
  254. }
  255. .third-login {
  256. display: flex;
  257. flex-direction: column;
  258. align-items: center;
  259. justify-content: center;
  260. padding: 32rpx;
  261. }
  262. .function-row {
  263. font-size: $u-p2;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. margin: 0 auto;
  268. width: 300rpx;
  269. }
  270. .footer {
  271. position: absolute;
  272. width: 100%;
  273. text-align: center;
  274. bottom: 20rpx;
  275. font-size: $u-p2;
  276. color: $u-content-color;
  277. .agreement {
  278. color: $u-theme-color;
  279. }
  280. }
  281. .scoped-login-btn {
  282. background: transparent;
  283. border: 0;
  284. outline:0px;
  285. -webkit-appearance: none;
  286. &::after {
  287. display: none;
  288. }
  289. }
  290. </style>