login.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="login" style="background: url(../../static/login-bg.png);background-size: 100% 100%;">
  3. <view style="height:120rpx;width: 100%;">
  4. </view>
  5. <view class="login-form">
  6. <image src="../../static/logo.png" mode=""></image>
  7. <view class="">
  8. <uni-forms :modelValue="loginForm">
  9. <uni-forms-item required label="账号" name="name">
  10. <uni-easyinput type="text" v-model="loginForm.username" placeholder="请输入账号" />
  11. </uni-forms-item>
  12. <uni-forms-item required name="hobby" label="密码">
  13. <uni-easyinput type="password" v-model="loginForm.password" placeholder="请输入密码" />
  14. </uni-forms-item>
  15. </uni-forms>
  16. <button type="default" @click="lodingFn">登录</button>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import Vue from 'vue'
  23. import {
  24. TOKEN_NAME,
  25. OPENID
  26. } from '../../util/cacheConstant.js'
  27. import {
  28. usernameLogin,
  29. isBind
  30. } from "@/util/api.js";
  31. export default {
  32. data() {
  33. return {
  34. image: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
  35. loginForm: {
  36. //租户ID
  37. tenantId: '849380',
  38. //部门ID
  39. deptId: '',
  40. //角色ID
  41. roleId: '',
  42. //用户名
  43. username: '',
  44. //密码
  45. password: '',
  46. //账号类型
  47. type: 'account',
  48. //验证码的值
  49. code: '',
  50. //验证码的索引
  51. key: '',
  52. grant_type: 'captcha',
  53. scope: 'all',
  54. type:'storageApplet',
  55. jsCode:""
  56. //预加载白色背景
  57. },
  58. }
  59. },
  60. onLoad() {
  61. setTimeout(() => {
  62. this.init()
  63. }, 500)
  64. },
  65. methods: {
  66. init() {
  67. const v = this
  68. //v.getCodeImg()
  69. Vue.prototype.$token = null
  70. wx.login({
  71. success: res => {
  72. v.loginForm.jsCode = res.code
  73. function filter(str) { // 特殊字符转义
  74. str += ''; // 隐式转换
  75. str = str.replace(/%/g, '%25');
  76. str = str.replace(/\+/g, '%2B');
  77. str = str.replace(/ /g, '%20');
  78. str = str.replace(/\//g, '%2F');
  79. str = str.replace(/\?/g, '%3F');
  80. str = str.replace(/&/g, '%26');
  81. str = str.replace(/\=/g, '%3D');
  82. str = str.replace(/#/g, '%23');
  83. return str;
  84. }
  85. function formateObjToParamStr(paramObj) {
  86. const sdata = [];
  87. for (let attr in paramObj) {
  88. sdata.push(`${attr}=${filter(paramObj[attr])}`);
  89. }
  90. return sdata.join('&');
  91. }
  92. const loginForm = {...v.loginForm}
  93. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  94. uni.request({
  95. url: 'https://cfm.bytesail.cn/api' + '/blade-auth/oauth/token?' + formateObjToParamStr(loginForm),
  96. method: 'POST',
  97. header: {
  98. 'Tenant-Id': v.loginForm.tenantId,
  99. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  100. 'Content-Type':'application/json;charset=UTF-8',
  101. },
  102. success: (res) => {
  103. if(res.statusCode == 200){
  104. wx.setStorageSync(TOKEN_NAME, res.data)
  105. Vue.prototype.$token = res.data
  106. wx.reLaunch({
  107. url: '/pages/home'
  108. })
  109. }else{
  110. uni.showToast({
  111. icon: "error",
  112. title: res.data.error_description,
  113. duration: 2000
  114. });
  115. }
  116. },
  117. fail: (err) => {
  118. }
  119. });
  120. },
  121. fail: (response) => {
  122. wx.showToast({
  123. title: response,
  124. icon: 'none',
  125. duration: 2000
  126. });
  127. }
  128. })
  129. },
  130. getCodeImg() {
  131. const v = this
  132. v.$get('/blade-auth/oauth/captcha', {}).then(data => {
  133. v.loginForm.key = data.key
  134. v.image = data.image
  135. })
  136. },
  137. lodingFn() {
  138. const v = this
  139. const loginForm = {...v.loginForm}
  140. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  141. function filter(str) { // 特殊字符转义
  142. str += ''; // 隐式转换
  143. str = str.replace(/%/g, '%25');
  144. str = str.replace(/\+/g, '%2B');
  145. str = str.replace(/ /g, '%20');
  146. str = str.replace(/\//g, '%2F');
  147. str = str.replace(/\?/g, '%3F');
  148. str = str.replace(/&/g, '%26');
  149. str = str.replace(/\=/g, '%3D');
  150. str = str.replace(/#/g, '%23');
  151. return str;
  152. }
  153. function formateObjToParamStr(paramObj) {
  154. const sdata = [];
  155. for (let attr in paramObj) {
  156. sdata.push(`${attr}=${filter(paramObj[attr])}`);
  157. }
  158. return sdata.join('&');
  159. }
  160. wx.login({
  161. success: res => {
  162. v.loginForm.jsCode = res.code
  163. const loginForm = {...v.loginForm}
  164. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  165. uni.request({
  166. url: 'https://cfm.bytesail.cn/api' + '/blade-auth/oauth/token?' + formateObjToParamStr(loginForm),
  167. method: 'POST',
  168. header: {
  169. 'Captcha-Key': v.loginForm.key,
  170. 'Captcha-Code': v.loginForm.code,
  171. 'Tenant-Id': v.loginForm.tenantId,
  172. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  173. 'Content-Type':'application/json;charset=UTF-8',
  174. },
  175. success: (res) => {
  176. console.log(res)
  177. if(res.statusCode == 200){
  178. wx.setStorageSync(TOKEN_NAME, res.data)
  179. Vue.prototype.$token = res.data
  180. wx.reLaunch({
  181. url: '/pages/home'
  182. })
  183. }else{
  184. uni.showToast({
  185. icon: "none",
  186. title: res.data.error_description,
  187. duration: 2000
  188. });
  189. }
  190. },
  191. fail: (err) => {
  192. uni.showToast({
  193. title: '失败' + err,
  194. duration: 12000
  195. });
  196. }
  197. });
  198. }
  199. })
  200. },
  201. },
  202. }
  203. </script>
  204. <style lang="less">
  205. .login {
  206. height: 100vh;
  207. width: 100vw;
  208. background-size: 100% 100%;
  209. image {
  210. width: 550rpx;
  211. height: 125rpx;
  212. margin: 0 auto 100rpx;
  213. display: block;
  214. }
  215. .login-form {
  216. margin: 0 auto;
  217. width: 80%;
  218. background: #fff;
  219. border-radius: 10rpx;
  220. padding: 150rpx 50rpx;
  221. }
  222. }
  223. </style>