login.vue 7.4 KB

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