login.vue 7.7 KB

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