login.vue 7.4 KB

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