onClickLogin.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="form">
  3. <u-form ref="validateCodeForm">
  4. <div class="login-list">
  5. <!-- 循环出当前可使用的第三方登录模式 -->
  6. <div class="login-item" v-for="(item,index) in loginList" :key="index">
  7. <u-icon :color="item.color" size="80" :name="item.icon" @click="navigateLogin(item)"></u-icon>
  8. <div>{{item.title}}</div>
  9. </div>
  10. </div>
  11. <view class="text-tips cell" @click="clickCodeLogin">账号密码登录</view>
  12. </u-form>
  13. </div>
  14. </template>
  15. <script>
  16. import { webConnect, openIdLogin } from "@/api/connect.js";
  17. import { whetherNavigate } from "@/utils/Foundation"; //登录跳转
  18. import { getUserInfo } from "@/api/members";
  19. import storage from "@/utils/storage.js";
  20. import api from "@/config/api.js";
  21. export default {
  22. data() {
  23. return {
  24. loginList: [
  25. {
  26. icon: "weixin-circle-fill",
  27. color: "#00a327",
  28. title: "微信",
  29. code: "WECHAT",
  30. },
  31. {
  32. icon: "qq-circle-fill",
  33. color: "#38ace9",
  34. title: "QQ",
  35. code: "QQ",
  36. },
  37. {
  38. icon: "apple-fill",
  39. color: "#000000",
  40. title: "Apple",
  41. code: "APPLE",
  42. },
  43. ],
  44. tips: "",
  45. };
  46. },
  47. props: ["status"],
  48. mounted() {
  49. //#ifdef APP-PLUS
  50. /**如果是app 加载支持的登录方式*/
  51. let _this = this;
  52. uni.getProvider({
  53. service: "oauth",
  54. success: (result) => {
  55. _this.loginList = result.provider.map((value) => {
  56. //展示title
  57. let title = "";
  58. //系统code
  59. let code = "";
  60. //颜色
  61. let color = "#8b8b8b";
  62. //图标
  63. let icon = "";
  64. //uni 联合登录 code
  65. let appcode = "";
  66. switch (value) {
  67. case "weixin":
  68. icon = "weixin-circle-fill";
  69. color = "#00a327";
  70. title = "微信";
  71. code = "WECHAT";
  72. break;
  73. case "qq":
  74. icon = "qq-circle-fill";
  75. color = "#38ace9";
  76. title = "QQ";
  77. code = "QQ";
  78. break;
  79. case "apple":
  80. icon = "apple-fill";
  81. color = "#000000";
  82. title = "Apple";
  83. code = "APPLE";
  84. break;
  85. }
  86. return {
  87. title: title,
  88. code: code,
  89. color: color,
  90. icon: icon,
  91. appcode: value,
  92. };
  93. });
  94. },
  95. fail: (error) => {
  96. uni.showToast({
  97. title: "获取登录通道失败" + error,
  98. duration: 2000,
  99. icon: "none",
  100. });
  101. },
  102. });
  103. //#endif
  104. //特殊平台,登录方式需要过滤
  105. // #ifdef H5
  106. this.methodFilter(["QQ"]);
  107. // #endif
  108. //微信小程序,只支持微信登录
  109. // #ifdef MP-WEIXIN
  110. this.methodFilter(["WECHAT"]);
  111. // #endif
  112. },
  113. methods: {
  114. /** 根据参数显示登录模块 */
  115. methodFilter(code) {
  116. let way = [];
  117. this.loginList.forEach((item) => {
  118. if (code.length != 0) {
  119. code.forEach((val) => {
  120. if (item.code == val) {
  121. way.push(item);
  122. }
  123. });
  124. } else {
  125. uni.showToast({
  126. title: '配置有误请联系管理员',
  127. duration: 2000,
  128. icon:"none"
  129. });
  130. }
  131. });
  132. this.loginList = way;
  133. },
  134. /**跳转到登录页面 */
  135. navigateLogin(connectLogin) {
  136. if (!this.status) {
  137. uni.showToast({
  138. title: "请您阅读并同意用户协议以及隐私政策",
  139. duration: 2000,
  140. icon: "none",
  141. });
  142. return false;
  143. }
  144. // #ifdef H5
  145. let code = connectLogin.code;
  146. let buyer = api.buyer;
  147. window.open(buyer+`/connect/login/web/`+code, "_self");
  148. // #endif
  149. // #ifdef APP-PLUS
  150. this.nonH5OpenId(connectLogin);
  151. // #endif
  152. },
  153. // 跳转到一键登录
  154. clickCodeLogin() {
  155. this.$emit("open", "code");
  156. },
  157. //非h5 获取openid
  158. async nonH5OpenId(item) {
  159. let _this = this;
  160. //获取各个openid
  161. await uni.login({
  162. provider: item.appcode,
  163. // #ifdef MP-ALIPAY
  164. scopes: "auth_user", //支付宝小程序需设置授权类型
  165. // #endif
  166. success: function (res) {
  167. uni.setStorageSync("type", item.code);
  168. //微信小程序意外的其它方式直接在storage中写入openid
  169. // #ifndef MP-WEIXIN
  170. uni.setStorageSync("openid", res.authResult.openid);
  171. // #endif
  172. },
  173. fail(e) {
  174. uni.showToast({
  175. title: "第三方登录暂不可用!",
  176. icon: "none",
  177. duration: 3000,
  178. });
  179. },
  180. complete(e) {
  181. //获取用户信息
  182. uni.getUserInfo({
  183. provider: item.appcode,
  184. success: function (infoRes) {
  185. //写入用户信息
  186. uni.setStorageSync("nickname", infoRes.userInfo.nickName);
  187. uni.setStorageSync("avatar", infoRes.userInfo.avatarUrl);
  188. // #ifdef MP-WEIXIN
  189. //微信小程序获取openid 需要特殊处理 如需获取openid请参考uni-id: https://uniapp.dcloud.net.cn/uniCloud/uni-id
  190. _this.weixinMPOpenID(res).then((res) => {
  191. //这里需要先行获得openid,再使用openid登录,小程序登录需要两步,所以这里特殊编译
  192. _this.goOpenidLogin("WECHAT_MP");
  193. });
  194. // #endif
  195. // #ifndef MP-WEIXIN
  196. _this.goOpenidLogin("APP");
  197. //#endif
  198. },
  199. });
  200. },
  201. });
  202. },
  203. //openid 登录
  204. goOpenidLogin(clientType) {
  205. let _this = this;
  206. // 获取准备好的参数,进行登录系统
  207. let params = {
  208. uuid: uni.getStorageSync("openid"), //联合登陆id
  209. source: uni.getStorageSync("type"), //联合登陆类型
  210. nickname: uni.getStorageSync("nickname"), // 昵称
  211. avatar: uni.getStorageSync("avatar"), // 头像
  212. uniAccessToken: uni.getStorageSync("uni_access_token"), //第三方token
  213. };
  214. openIdLogin(params, clientType).then((res) => {
  215. if (!res.data.success) {
  216. let errormessage = "第三方登录暂不可用";
  217. uni.showToast({
  218. // title: '未绑定第三方账号',
  219. title: errormessage,
  220. icon: "none",
  221. duration: 3000,
  222. });
  223. return;
  224. } else {
  225. storage.setAccessToken(res.data.result.accessToken);
  226. storage.setRefreshToken(res.data.result.refreshToken);
  227. // 登录成功
  228. uni.showToast({
  229. title: "第三方登录成功!",
  230. icon: "none",
  231. });
  232. // 执行登录
  233. getUserInfo().then((user) => {
  234. storage.setUserInfo(user.data.result);
  235. storage.setHasLogin(true);
  236. });
  237. whetherNavigate()
  238. }
  239. });
  240. },
  241. //微信小程序获取openid
  242. async weixinMPOpenID(res) {
  243. await miniProgramLogin(res.code).then((res) => {
  244. uni.setStorageSync("openid", res.data);
  245. });
  246. },
  247. },
  248. };
  249. </script>
  250. <style lang="scss" scoped>
  251. @import url("./login.scss");
  252. .submit {
  253. margin: 80rpx 0 40rpx 0;
  254. }
  255. .login-list {
  256. display: flex;
  257. padding: 40rpx 0;
  258. justify-content: space-between;
  259. .login-item {
  260. font-size: 24rpx;
  261. text-align: center;
  262. > * {
  263. margin: 4rpx 0;
  264. }
  265. }
  266. }
  267. </style>