login.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. _that.getUserInfo()
  157. })
  158. } else {
  159. console.log('验证失败');
  160. return;
  161. }
  162. });
  163. // 发送登录请求
  164. // vk.userCenter.login({
  165. // data: {
  166. // username: that.form1.username,
  167. // password: that.form1.password
  168. // },
  169. // success: res => {
  170. // // 成功后的逻辑
  171. // vk.callFunction({
  172. // url: 'client/agent/kh/getAgentInfo',
  173. // needAlert: false
  174. // }).then(res => {
  175. // vk.vuex.set('$user.agentInfo', res.info);
  176. // vk.alert('登录成功', '确定', '提示', function() {
  177. // vk.navigateToHome();
  178. // });
  179. // });
  180. // }
  181. // });
  182. },
  183. getUserInfo () {
  184. uni.api.base.apiuserinfo().then(res => {
  185. uni.reLaunch({
  186. url: '/pages/index/index'
  187. })
  188. uni.setStorageSync('MD_userInfo2', res)
  189. })
  190. uni.api.base.apidicttree().then(res => {
  191. const cObj = res || {}
  192. let newDict = {}
  193. for (let k in cObj) {
  194. const cArr = cObj[k].map(item => {
  195. return {
  196. ...item,
  197. key: item.dict_label,
  198. val: item.dict_value
  199. }
  200. })
  201. newDict[k] = cArr
  202. }
  203. uni.setStorageSync('MD_dict', newDict)
  204. })
  205. },
  206. // 微信登录
  207. loginByWeixin() {
  208. // vk.userCenter.loginByWeixin({
  209. // data: {
  210. // type: 'login' // 无账号不注册
  211. // },
  212. // success: data => {
  213. // // 成功后的逻辑
  214. // vk.callFunction({
  215. // url: 'client/agent/kh/getAgentInfo',
  216. // needAlert: false
  217. // }).then(res => {
  218. // vk.vuex.set('$user.agentInfo', res.info);
  219. // vk.alert('登录成功', '确定', '提示', function() {
  220. // vk.navigateToHome();
  221. // });
  222. // });
  223. // }
  224. // });
  225. }
  226. },
  227. // 监听器
  228. watch: {},
  229. // 计算属性
  230. computed: {}
  231. };
  232. </script>
  233. <style lang="scss" scoped>
  234. .header {
  235. padding-top: 15%;
  236. margin-bottom: 40rpx;
  237. display: flex;
  238. flex-direction: column;
  239. align-items: center;
  240. .avatar {
  241. width: 200rpx;
  242. height: 200rpx;
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. margin-bottom: 20rpx;
  247. }
  248. .nickname {
  249. font-size: $u-h3;
  250. }
  251. }
  252. .body {
  253. width: 100%;
  254. display: flex;
  255. flex-direction: column;
  256. align-items: center;
  257. padding: 20rpx 100rpx;
  258. margin: 40rpx 0;
  259. .title {
  260. font-size: $u-p;
  261. margin-bottom: 20rpx;
  262. }
  263. .tips {
  264. font-size: $u-p2;
  265. color: $u-tips-color;
  266. margin-bottom: 60rpx;
  267. }
  268. }
  269. .third-login {
  270. display: flex;
  271. flex-direction: column;
  272. align-items: center;
  273. justify-content: center;
  274. padding: 32rpx;
  275. }
  276. .function-row {
  277. font-size: $u-p2;
  278. display: flex;
  279. align-items: center;
  280. justify-content: center;
  281. margin: 0 auto;
  282. width: 300rpx;
  283. }
  284. .footer {
  285. position: absolute;
  286. width: 100%;
  287. text-align: center;
  288. bottom: 20rpx;
  289. font-size: $u-p2;
  290. color: $u-content-color;
  291. .agreement {
  292. color: $u-theme-color;
  293. }
  294. }
  295. .scoped-login-btn {
  296. background: transparent;
  297. border: 0;
  298. outline:0px;
  299. -webkit-appearance: none;
  300. &::after {
  301. display: none;
  302. }
  303. }
  304. </style>