login.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <view>
  3. <u-toast ref="uToast" />
  4. <view class="d-flex f-column a-center top">
  5. <image src="../../static/logo.png" mode=""></image>
  6. <text class="main-color-text f-weight-bold m2">欢迎使用设备管理系统!</text>
  7. </view>
  8. <view class="f-weight-bold name text-center">账号登录</view>
  9. <view class="input">
  10. <input
  11. type="text"
  12. v-model="userName"
  13. placeholder="请输入用户名"
  14. placeholder-class="placeholder-class"
  15. />
  16. <input
  17. type="password"
  18. v-model="passWord"
  19. placeholder="请输入密码"
  20. placeholder-class="placeholder-class"
  21. />
  22. </view>
  23. <view class="tip" @tap="forgetPass">
  24. 忘记密码
  25. <text class="mx1">
  26. <u-icon name="question"></u-icon>
  27. </text>
  28. </view>
  29. <view class=" mui-input-row mui-checkbox ">
  30. <checkbox-group @change="checkboxChange">
  31. <checkbox
  32. id="chkRem"
  33. type="checkbox"
  34. :checked="rememberPsw"
  35. @tap="rememberPsw = !rememberPsw"
  36. class="RememberCheck"
  37. >
  38. 记住密码
  39. </checkbox>
  40. </checkbox-group>
  41. </view>
  42. <button class="main-color-bg text-color-white login" @tap="login">登录</button>
  43. <view class="wechat p-fixed left0 right0" style="bottom: 60rpx;">
  44. <button open-type="getUserInfo" @click="getUserInfo" class="d-flex j-center a-center">
  45. <image src="/static/images/weixin.png" mode=""></image>
  46. </button>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import { encryptPassword } from '@/utils/password';
  52. export default {
  53. data() {
  54. return {
  55. userName: '', // 用户名
  56. passWord: '', // 密码
  57. rememberPsw: true, // 记住密码
  58. Code: '',
  59. session_key: '',
  60. openid: ''
  61. };
  62. },
  63. // 页面初始加载
  64. mounted() {
  65. const that = this;
  66. // 缓存的账号
  67. const cache_userName = uni.getStorageSync('cache_userName');
  68. // 缓存的密码
  69. const cache_passWord = uni.getStorageSync('cache_passWord');
  70. // 有缓存就赋值给文本没有就清空
  71. if (cache_userName && cache_passWord) {
  72. that.userName = cache_userName;
  73. that.passWord = cache_passWord;
  74. } else {
  75. that.userName = '';
  76. that.passWord = '';
  77. }
  78. },
  79. onShow() {
  80. if (this.$BaseTool.Util.isDev()) {
  81. this.userName = 'superadmin'
  82. this.passWord = '123456'
  83. }
  84. },
  85. methods: {
  86. // 找回密码
  87. forgetPass() {
  88. },
  89. // 复选框
  90. checkboxChange: function(e) {
  91. if (e.detail.value.length == 1) {
  92. // 获取缓存的账号
  93. uni.getStorageSync('cache_userName', this.userName);
  94. uni.getStorageSync('cache_passWord', this.passWord);
  95. } else {
  96. uni.removeStorageSync('cache_userName');
  97. uni.removeStorageSync('cache_passWord');
  98. }
  99. },
  100. // 登录
  101. login() {
  102. if (!this.userName) {
  103. return this.$refs.uToast.show({
  104. title: '请输入用户名',
  105. type: 'default',
  106. icon: false
  107. });
  108. } else if (!this.passWord) {
  109. return this.$refs.uToast.show({
  110. title: '请输入密码',
  111. type: 'default',
  112. icon: false
  113. });
  114. }
  115. this.$Http
  116. .mobileLogin({
  117. username: this.userName,
  118. password: encryptPassword(this.passWord),
  119. grant_type: 'password',
  120. scope: 'server'
  121. })
  122. .then(res => {
  123. // 缓存账号和密码
  124. if (this.rememberPsw) {
  125. uni.setStorageSync('cache_userName', this.userName);
  126. uni.setStorageSync('cache_passWord', this.passWord);
  127. } else {
  128. uni.removeStorageSync('cache_userName');
  129. uni.removeStorageSync('cache_passWord');
  130. }
  131. // 如果openId有,则绑定openId
  132. const openId = uni.getStorageSync('openId', this.userName);
  133. if (openId != null && openId !== '') {
  134. this.$Http
  135. .bindUserAndOpenId(openId)
  136. .then(res => {
  137. uni.removeStorageSync('openId');
  138. });
  139. }
  140. });
  141. },
  142. // 微信登录获取openid
  143. getUserInfo() {
  144. // 登录
  145. uni.login({
  146. provider: 'weixin',
  147. success: res => {
  148. // console.log('登录成功:', res);
  149. // 获取临时登录凭证code
  150. this.Code = res.code;
  151. // 获取openid,session_key
  152. const appid = 'wx38f46845be704faa' // 需替换
  153. const secret = '9bt1F6ojLU6JLv76Boo4qaeEU6PcW75nGjjLyauDtAF' // 需替换
  154. const url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret +
  155. '&js_code=' +
  156. this.Code + '&grant_type=authorization_code';
  157. uni.request({
  158. url: url, // 请求路径
  159. success: res => {
  160. // console.log('openid session_key:', res.data);
  161. this.openid = res.data.openid
  162. this.session_key = res.data.session_key
  163. console.log('openid:', this.openid)
  164. uni.showToast({ title: this.openid, icon: 'none' })
  165. },
  166. fail: err => {
  167. console.log('请求失败:', err)
  168. }
  169. });
  170. },
  171. fail: err => {
  172. console.log('登录失败:', err)
  173. }
  174. })
  175. }
  176. }
  177. };
  178. </script>
  179. <style lang="scss" scoped>
  180. .top {
  181. font-size: unit(28, rpx);
  182. padding-top: unit(90, rpx);
  183. image {
  184. width: unit(170, rpx);
  185. height: unit(123, rpx);
  186. }
  187. }
  188. .name {
  189. font-size: unit(42, rpx);
  190. color: #333333;
  191. margin: unit(80, rpx) 0 unit(80, rpx);
  192. }
  193. .input {
  194. width: unit(600, rpx);
  195. margin: 0 auto;
  196. input {
  197. border-bottom: unit(1, rpx) solid #e6e6e6;
  198. height: unit(100, rpx);
  199. line-height: unit(100, rpx);
  200. }
  201. }
  202. .login {
  203. width: unit(600, rpx);
  204. height: unit(100, rpx);
  205. margin: unit(100, rpx) auto 0;
  206. border-radius: unit(80, rpx);
  207. }
  208. .tip {
  209. width: unit(600, rpx);
  210. margin: unit(30, rpx) auto;
  211. text-align: right;
  212. font-size: unit(26, rpx);
  213. font-weight: 500;
  214. }
  215. .wechat{
  216. margin-top: unit(90, rpx);
  217. button{
  218. background-color: #FFFFFF;
  219. &::after{
  220. border: none;
  221. }
  222. image{
  223. width: unit(90, rpx);
  224. height: unit(90, rpx);
  225. }
  226. }
  227. }
  228. .mui-checkbox input[type='checkbox']:checked:before {
  229. color: #00bbb1;
  230. }
  231. .RememberPass {
  232. color: #adadad;
  233. margin-top: 5px;
  234. }
  235. .RememberCheck {
  236. margin-left: 30px;
  237. color: #adadad;
  238. }
  239. </style>