onClickLogin.vue 7.8 KB

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