login.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="logo-content align-center justify-center flex">
  4. <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
  5. </image>
  6. <text class="title">若依移动端登录</text>
  7. </view>
  8. <view class="login-form-content">
  9. <view class="input-item flex align-center">
  10. <view class="iconfont icon-user icon"></view>
  11. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
  12. </view>
  13. <view class="input-item flex align-center">
  14. <view class="iconfont icon-password icon"></view>
  15. <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
  16. </view>
  17. <view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
  18. <view class="iconfont icon-code icon"></view>
  19. <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
  20. <view class="login-code">
  21. <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
  22. </view>
  23. </view>
  24. <view class="action-btn">
  25. <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
  26. <button @click="wxHandleLogin" class="login-btn cu-btn block bg-green lg round">微信授权登录</button>
  27. </view>
  28. <view class="reg text-center" v-if="register">
  29. <text class="text-grey1">没有账号?</text>
  30. <text @click="handleUserRegister" class="text-blue">立即注册</text>
  31. </view>
  32. <view class="xieyi text-center">
  33. <text class="text-grey1">登录即代表同意</text>
  34. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  35. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { getCodeImg } from '@/api/login'
  42. export default {
  43. data() {
  44. return {
  45. codeUrl: "",
  46. captchaEnabled: true,
  47. // 用户注册开关
  48. register: false,
  49. globalConfig: getApp().globalData.config,
  50. loginForm: {
  51. username: "admin",
  52. password: "admin123",
  53. code: "",
  54. uuid: ""
  55. },
  56. //微信登录
  57. wxLoginForm:{
  58. code:"",
  59. encryptedIv:"",
  60. encrytedData:""
  61. }
  62. }
  63. },
  64. created() {
  65. this.getCode()
  66. },
  67. methods: {
  68. // 用户注册
  69. handleUserRegister() {
  70. this.$tab.redirectTo(`/pages/register`)
  71. },
  72. // 隐私协议
  73. handlePrivacy() {
  74. let site = this.globalConfig.appInfo.agreements[0]
  75. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  76. },
  77. // 用户协议
  78. handleUserAgrement() {
  79. let site = this.globalConfig.appInfo.agreements[1]
  80. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  81. },
  82. // 获取图形验证码
  83. getCode() {
  84. getCodeImg().then(res => {
  85. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  86. if (this.captchaEnabled) {
  87. this.codeUrl = 'data:image/gif;base64,' + res.img
  88. this.loginForm.uuid = res.uuid
  89. }
  90. })
  91. },
  92. //微信授权登录
  93. async wxHandleLogin() {
  94. console.log("微信小程序发起授权登录")
  95. this.$modal.loading("登录中,请耐心等待...")
  96. //获取服务商信息
  97. uni.getProvider({
  98. service:'oauth',
  99. success:(res)=>{
  100. console.log(res);
  101. if(~res.provider.indexOf("weixin")){
  102. //登录
  103. uni.login({
  104. provider:'weixin',
  105. success: (loginRes) => {
  106. console.log("登录",loginRes)
  107. this.wxLoginForm.code=loginRes.code;
  108. //获取用户信息
  109. uni.getUserInfo({
  110. success: (resInfo) => {
  111. console.log("用户信息",resInfo)
  112. //设置偏移量和加密数据
  113. this.wxLoginForm.encryptedIv=resInfo.iv;
  114. this.wxLoginForm.encrytedData=resInfo.encryptedData;
  115. //向后端发送登录请求 TODO
  116. this.sendWxLoginFormToLocalService()
  117. }
  118. })
  119. }
  120. })
  121. }
  122. }
  123. })
  124. },
  125. sendWxLoginFormToLocalService(){
  126. console.log("向后端发起登录请求......")
  127. this.$store.dispatch('WxLogin', this.wxLoginForm).then(() => {
  128. this.$modal.closeLoading()
  129. this.loginSuccess()
  130. }).catch(() => {
  131. this.$modal.msgError("微信登录失败")
  132. // if (this.captchaEnabled) {
  133. // this.getCode()
  134. // }
  135. })
  136. },
  137. // 登录方法
  138. async handleLogin() {
  139. if (this.loginForm.username === "") {
  140. this.$modal.msgError("请输入账号")
  141. } else if (this.loginForm.password === "") {
  142. this.$modal.msgError("请输入密码")
  143. } else if (this.loginForm.code === "" && this.captchaEnabled) {
  144. this.$modal.msgError("请输入验证码")
  145. } else {
  146. this.$modal.loading("登录中,请耐心等待...")
  147. this.pwdLogin()
  148. }
  149. },
  150. // 密码登录
  151. async pwdLogin() {
  152. this.$store.dispatch('Login', this.loginForm).then(() => {
  153. this.$modal.closeLoading()
  154. this.loginSuccess()
  155. }).catch(() => {
  156. if (this.captchaEnabled) {
  157. this.getCode()
  158. }
  159. })
  160. },
  161. // 登录成功后,处理函数
  162. loginSuccess(result) {
  163. // 设置用户信息
  164. this.$store.dispatch('GetInfo').then(res => {
  165. this.$tab.reLaunch('/pages/index')
  166. })
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. page {
  173. background-color: #ffffff;
  174. }
  175. .normal-login-container {
  176. width: 100%;
  177. .logo-content {
  178. width: 100%;
  179. font-size: 21px;
  180. text-align: center;
  181. padding-top: 15%;
  182. image {
  183. border-radius: 4px;
  184. }
  185. .title {
  186. margin-left: 10px;
  187. }
  188. }
  189. .login-form-content {
  190. text-align: center;
  191. margin: 20px auto;
  192. margin-top: 15%;
  193. width: 80%;
  194. .input-item {
  195. margin: 20px auto;
  196. background-color: #f5f6f7;
  197. height: 45px;
  198. border-radius: 20px;
  199. .icon {
  200. font-size: 38rpx;
  201. margin-left: 10px;
  202. color: #999;
  203. }
  204. .input {
  205. width: 100%;
  206. font-size: 14px;
  207. line-height: 20px;
  208. text-align: left;
  209. padding-left: 15px;
  210. }
  211. }
  212. .login-btn {
  213. margin-top: 40px;
  214. height: 45px;
  215. }
  216. .reg {
  217. margin-top: 15px;
  218. }
  219. .xieyi {
  220. color: #333;
  221. margin-top: 20px;
  222. }
  223. .login-code {
  224. height: 38px;
  225. float: right;
  226. .login-code-img {
  227. height: 38px;
  228. position: absolute;
  229. margin-left: 10px;
  230. width: 200rpx;
  231. }
  232. }
  233. }
  234. }
  235. </style>