login.vue 8.2 KB

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