login.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. const v = this
  62. uni.getStorage({
  63. key: 'automaticLogin',
  64. success: function (res) {
  65. if(res.data) v.init()
  66. }
  67. });
  68. },
  69. methods: {
  70. init() {
  71. const v = this
  72. //v.getCodeImg()
  73. Vue.prototype.$token = null
  74. wx.login({
  75. success: res => {
  76. v.loginForm.jsCode = res.code
  77. function filter(str) { // 特殊字符转义
  78. str += ''; // 隐式转换
  79. str = str.replace(/%/g, '%25');
  80. str = str.replace(/\+/g, '%2B');
  81. str = str.replace(/ /g, '%20');
  82. str = str.replace(/\//g, '%2F');
  83. str = str.replace(/\?/g, '%3F');
  84. str = str.replace(/&/g, '%26');
  85. str = str.replace(/\=/g, '%3D');
  86. str = str.replace(/#/g, '%23');
  87. return str;
  88. }
  89. function formateObjToParamStr(paramObj) {
  90. const sdata = [];
  91. for (let attr in paramObj) {
  92. sdata.push(`${attr}=${filter(paramObj[attr])}`);
  93. }
  94. return sdata.join('&');
  95. }
  96. const loginForm = {...v.loginForm}
  97. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  98. uni.request({
  99. url: 'https://cfm.bytesail.cn/api' + '/blade-auth/oauth/token?' + formateObjToParamStr(loginForm),
  100. method: 'POST',
  101. header: {
  102. 'Tenant-Id': v.loginForm.tenantId,
  103. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  104. 'Content-Type':'application/json;charset=UTF-8',
  105. },
  106. success: (res) => {
  107. if(res.statusCode == 200){
  108. wx.setStorageSync(TOKEN_NAME, res.data)
  109. Vue.prototype.$token = res.data
  110. wx.reLaunch({
  111. url: '/pages/home'
  112. })
  113. }else{
  114. uni.showToast({
  115. icon: "error",
  116. title: res.data.error_description,
  117. duration: 2000
  118. });
  119. }
  120. },
  121. fail: (err) => {
  122. }
  123. });
  124. },
  125. fail: (response) => {
  126. wx.showToast({
  127. title: response,
  128. icon: 'none',
  129. duration: 2000
  130. });
  131. }
  132. })
  133. },
  134. getCodeImg() {
  135. const v = this
  136. v.$get('/blade-auth/oauth/captcha', {}).then(data => {
  137. v.loginForm.key = data.key
  138. v.image = data.image
  139. })
  140. },
  141. lodingFn() {
  142. const v = this
  143. const loginForm = {...v.loginForm}
  144. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  145. function filter(str) { // 特殊字符转义
  146. str += ''; // 隐式转换
  147. str = str.replace(/%/g, '%25');
  148. str = str.replace(/\+/g, '%2B');
  149. str = str.replace(/ /g, '%20');
  150. str = str.replace(/\//g, '%2F');
  151. str = str.replace(/\?/g, '%3F');
  152. str = str.replace(/&/g, '%26');
  153. str = str.replace(/\=/g, '%3D');
  154. str = str.replace(/#/g, '%23');
  155. return str;
  156. }
  157. function formateObjToParamStr(paramObj) {
  158. const sdata = [];
  159. for (let attr in paramObj) {
  160. sdata.push(`${attr}=${filter(paramObj[attr])}`);
  161. }
  162. return sdata.join('&');
  163. }
  164. wx.login({
  165. success: res => {
  166. v.loginForm.jsCode = res.code
  167. const loginForm = {...v.loginForm}
  168. loginForm.password = this.$w_md5.hex_md5_32(loginForm.password)
  169. uni.request({
  170. url: 'https://cfm.bytesail.cn/api' + '/blade-auth/oauth/token?' + formateObjToParamStr(loginForm),
  171. method: 'POST',
  172. header: {
  173. 'Captcha-Key': v.loginForm.key,
  174. 'Captcha-Code': v.loginForm.code,
  175. 'Tenant-Id': v.loginForm.tenantId,
  176. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  177. 'Content-Type':'application/json;charset=UTF-8',
  178. },
  179. success: (res) => {
  180. console.log(res)
  181. uni.setStorage({
  182. key: 'automaticLogin',
  183. data: true,
  184. success: function () {
  185. }
  186. });
  187. if(res.statusCode == 200){
  188. wx.setStorageSync(TOKEN_NAME, res.data)
  189. Vue.prototype.$token = res.data
  190. wx.reLaunch({
  191. url: '/pages/home'
  192. })
  193. }else{
  194. uni.showToast({
  195. icon: "none",
  196. title: res.data.error_description,
  197. duration: 2000
  198. });
  199. }
  200. },
  201. fail: (err) => {
  202. uni.showToast({
  203. title: '失败' + err,
  204. duration: 12000
  205. });
  206. }
  207. });
  208. }
  209. })
  210. },
  211. },
  212. }
  213. </script>
  214. <style lang="less">
  215. .login {
  216. height: 100vh;
  217. width: 100vw;
  218. background-size: 100% 100%;
  219. image {
  220. width: 550rpx;
  221. height: 125rpx;
  222. margin: 0 auto 100rpx;
  223. display: block;
  224. }
  225. .login-form {
  226. margin: 0 auto;
  227. width: 80%;
  228. background: #fff;
  229. border-radius: 10rpx;
  230. padding: 150rpx 50rpx;
  231. }
  232. }
  233. </style>