login.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. import {
  2. login,
  3. loginByWeChat,
  4. setPhone,
  5. getUserInfo,
  6. updatePushId
  7. } from '@/api/user'
  8. let univerifyManager = null;
  9. let registrationID = uni.getStorageSync("registrationID");
  10. let userPhone = "";
  11. let unionid = "";
  12. let loginType = 3; //1账号密码 2:微信登录 3:手机号一键登录
  13. export async function showLoginPage(provider) {
  14. // #ifdef APP-PLUS
  15. // if(this.$qconfig.applyLogin){
  16. // return;
  17. // }
  18. if (registrationID == null || registrationID == "") {
  19. try {
  20. registrationID = await getRegistrationID();
  21. console.log('推送ID:', registrationID);
  22. } catch (err) {
  23. registrationID = ''
  24. console.error('获取失败:', err);
  25. }
  26. }
  27. // 微信一键登录
  28. if (provider == "weixin") {
  29. weixinLogin()
  30. } else {
  31. // 手机号一键登录
  32. let that = this;
  33. univerifyManager = uni.getUniverifyManager();
  34. univerifyLogin(that);
  35. const loginCallBack = (res) => {
  36. // 获取一键登录弹框协议勾选状态
  37. univerifyManager.getCheckBoxState({
  38. success(res) {
  39. // that.$qconfig.applyLogin=false;
  40. if (!res.state) {
  41. uni.showToast({
  42. title: "请同意服务条款和隐私政策",
  43. icon: 'none',
  44. position: 'bottom'
  45. });
  46. return;
  47. }
  48. loginType = 2;
  49. uni.login({
  50. provider: "weixin",
  51. onlyAuthorize: false,
  52. success: async (res) => {
  53. console.log('login success:成功', JSON.stringify(res));
  54. let that = this;
  55. uni.showLoading({
  56. title: "处理中..."
  57. });
  58. const params = {
  59. code: res.code,
  60. jpushId: registrationID
  61. };
  62. loginByWeChatAct(111, params);
  63. },
  64. fail: (err) => {
  65. console.log('login fail:', JSON.stringify(err));
  66. // 未开通
  67. if (err.code == 1000) {
  68. uni.showModal({
  69. title: '登录失败',
  70. content: `${err.errMsg}\n,错误码:${err.code}`,
  71. confirmText: '开通指南',
  72. cancelText: '确定',
  73. success: (res) => {
  74. if (res.confirm) {
  75. setTimeout(() => {
  76. plus.runtime.openWeb(
  77. 'https://ask.dcloud.net.cn/article/37965'
  78. )
  79. }, 500)
  80. }
  81. }
  82. });
  83. return;
  84. }
  85. if (err.code == '-100') {
  86. uni.showToast({
  87. title: "授权拒绝",
  88. icon: "none"
  89. });
  90. return;
  91. }
  92. // 一键登录预登陆失败
  93. if (err.code == '30005') {
  94. uni.showModal({
  95. showCancel: false,
  96. title: '预登录失败',
  97. content: err.errMsg
  98. });
  99. return;
  100. }
  101. if (err.code == 30006) {
  102. uni.showToast({
  103. title: "登录已取消,请重新操作",
  104. icon: "none"
  105. });
  106. return;
  107. }
  108. // 一键登录用户关闭验证界面
  109. if (err.code != '30003') {
  110. uni.showModal({
  111. showCancel: false,
  112. title: '登录失败',
  113. content: JSON.stringify(err)
  114. });
  115. }
  116. },
  117. complete: () => {
  118. }
  119. });
  120. // 关闭一键登录弹框
  121. univerifyManager.close();
  122. }
  123. });
  124. }
  125. // 订阅自定义按钮点击事件
  126. univerifyManager.onButtonsClick(loginCallBack);
  127. // 取消订阅自定义按钮点击事件
  128. //univerifyManager.offButtonsClick(loginCallBack);
  129. // this.$qconfig.applyLogin=true;
  130. }
  131. // #endif
  132. // #ifdef H5
  133. if (String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger") {
  134. let url = window.location.href;
  135. let urlPaths = url.split("/h5");
  136. uni.setStorageSync('beforLoginPage', urlPaths[1]);
  137. uni.navigateTo({
  138. url: "/pages/auth/h5WxLogin"
  139. });
  140. } else {
  141. uni.navigateTo({
  142. url: "/pages/auth/passwordLogin"
  143. });
  144. }
  145. // #endif
  146. }
  147. const univerifyLogin = function(that) {
  148. univerifyManager.login({
  149. provider: 'univerify',
  150. univerifyStyle: { // 自定义登录框样式
  151. fullScreen: true,
  152. icon: {
  153. "path": "static/logo.png", // 自定义显示在授权框中的logo,仅支持本地图片 默认显示App logo
  154. "width": "80px", //图标宽度 默认值:60px
  155. "height": "80px", //图标高度 默认值:60px
  156. },
  157. authButton: {
  158. "title": "本机号码一键登录",
  159. "normalColor": "#018C39",
  160. "highlightColor": "#018C39",
  161. },
  162. otherLoginButton: {
  163. // "title": "微信快捷登录",
  164. "visible": "true",
  165. "borderRadius": "24px",
  166. "borderColor": "#ECECEC",
  167. },
  168. closeIcon: {
  169. "path": "static/images/icon_close.png",
  170. "width": "160px", //图标宽度 默认值:60px (HBuilderX 4.0支持)
  171. "height": "160px" //图标高度 默认值:60px (HBuilderX 4.0支持)
  172. },
  173. buttons: {
  174. "iconWidth": "55px", // 图标宽度(高度等比例缩放) 默认值:45px
  175. "list": [
  176. // {
  177. // "provider": "apple",
  178. // "iconPath": "/static/apple.png" // 图标路径仅支持本地图片
  179. // },
  180. {
  181. "provider": "weixin",
  182. "iconPath": "/static/images/wechat.png" // 图标路径仅支持本地图片
  183. }
  184. ]
  185. },
  186. privacyTerms: {
  187. "defaultCheckBoxState": false, // 条款勾选框初始状态 默认值: true
  188. "isCenterHint": false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  189. "uncheckedImage": "/static/icons/radio_default_icon.png", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  190. "checkedImage": "/static/icons/radio_choose_icon.png", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  191. "checkBoxSize": 13, // 可选 条款勾选框大小
  192. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  193. "termsColor": "#018C39", // 协议文字颜色 默认值: #5496E3
  194. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  195. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  196. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  197. {
  198. "url": "https://userapp.zhengzefeng.com/h5/userAgreement", // 点击跳转的协议详情页面
  199. "title": "用户服务协议" // 协议名称
  200. },
  201. {
  202. "url": "https://userapp.zhengzefeng.com/h5/privacyPolicy", // 点击跳转的协议详情页面
  203. "title": "隐私保护" // 协议名称
  204. }
  205. // {
  206. // "url": "https://userapp.his.cdwjyyh.com/h5/healthCustomerService", // 点击跳转的协议详情页面
  207. // "title": "健康客服协议" // 协议名称
  208. // }
  209. ]
  210. },
  211. },
  212. success(res) { // 登录成功
  213. console.log("qxj univerify login:" + JSON.stringify(res
  214. .authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'}
  215. //uni.hideLoading();
  216. // that.$qconfig.applyLogin=false;
  217. loginType = 3;
  218. loginByUniverify(res.authResult, 0);
  219. },
  220. fail(res) {
  221. //uni.hideLoading();
  222. // that.$qconfig.applyLogin=false;
  223. if (res.code == 30002) {
  224. // uni.closeAuthView()
  225. uni.navigateTo({
  226. url: "/pages/auth/passwordLogin"
  227. })
  228. } else if (res.code == 30008) { //用户点击了自定义按钮
  229. uni.getCheckBoxState({
  230. success(res) {
  231. console.log(res.state) // Boolean 用户是否勾选了选框
  232. console.log(res.errMsg)
  233. if (!res.state) {
  234. uni.showToast({
  235. title: "请同意服务条款和隐私政策",
  236. icon: 'none',
  237. position: 'bottom'
  238. });
  239. return;
  240. }
  241. loginType = 2;
  242. uni.login({
  243. provider: "weixin",
  244. onlyAuthorize: false,
  245. success: async (res) => {
  246. console.log('login success:', JSON.stringify(res));
  247. let that = this;
  248. uni.showLoading({
  249. title: "处理中..."
  250. });
  251. const params = {
  252. code: res.code
  253. };
  254. console.log('微信', params)
  255. loginByWeChatAct(params);
  256. },
  257. fail: (err) => {
  258. console.log('login fail:', JSON.stringify(err));
  259. // 未开通
  260. if (err.code == 1000) {
  261. uni.showModal({
  262. title: '登录失败',
  263. content: `${err.errMsg}\n,错误码:${err.code}`,
  264. confirmText: '开通指南',
  265. cancelText: '确定',
  266. success: (res) => {
  267. if (res.confirm) {
  268. setTimeout(() => {
  269. plus.runtime
  270. .openWeb(
  271. 'https://ask.dcloud.net.cn/article/37965'
  272. )
  273. }, 500)
  274. }
  275. }
  276. });
  277. return;
  278. }
  279. if (err.code == '-100') {
  280. uni.showToast({
  281. title: "授权拒绝",
  282. icon: "none"
  283. });
  284. return;
  285. }
  286. // 一键登录预登陆失败
  287. if (err.code == '30005') {
  288. uni.showModal({
  289. showCancel: false,
  290. title: '预登录失败',
  291. content: err.errMsg
  292. });
  293. return;
  294. }
  295. // 一键登录用户关闭验证界面
  296. if (err.code != '30003') {
  297. uni.showModal({
  298. showCancel: false,
  299. title: '登录失败',
  300. content: JSON.stringify(err)
  301. });
  302. }
  303. },
  304. complete: () => {
  305. }
  306. });
  307. },
  308. fail(res) {
  309. console.log(res.errCode)
  310. console.log(res.errMsg)
  311. }
  312. });
  313. console.log("----qxj 一键登录调用2-----");
  314. } else if (res.code == 30003) { //关闭验证界面
  315. return false;
  316. } else if (res.code == 30005) { //预登录失败
  317. //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  318. setTimeout(function() {
  319. uni.navigateTo({
  320. url: "/pages/auth/passwordLogin"
  321. });
  322. }, 1800);
  323. } else {
  324. }
  325. //登录失败
  326. console.log("微信一键登录failRes:" + JSON.stringify(res));
  327. }
  328. });
  329. };
  330. export function loginByWeChatAct(params) {
  331. loginByWeChat(params).then(res => {
  332. if (res.code == 200) {
  333. if (res.user != null) {
  334. uni.hideLoading();
  335. uni.setStorageSync('AppToken', res.token);
  336. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  337. uni.closeAuthView();
  338. uni.$emit('refreshUserInfo', {});
  339. // // #ifdef APP-PLUS
  340. // const jyJPush = uni.requireNativePlugin('JY-JPush');
  341. // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
  342. // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致
  343. // // 注:若没有返回任何数据,考虑是否初始化完成
  344. // console.log("qxj userAlias "+JSON.stringify(result));
  345. // });
  346. // // #endif
  347. uni.reLaunch({
  348. url: "/pages/home/index"
  349. })
  350. } else {
  351. unionid = res.unionid;
  352. if (res.isNew) {
  353. bindMobile();
  354. }
  355. }
  356. } else {
  357. uni.hideLoading();
  358. console.log(res)
  359. uni.showToast({
  360. title: res.msg,
  361. icon: 'none'
  362. });
  363. }
  364. },
  365. rej => {}
  366. );
  367. }
  368. export function bindMobile() {
  369. // #ifdef APP-PLUS
  370. //uni.showLoading({title:""});
  371. uni.login({
  372. provider: 'univerify',
  373. univerifyStyle: { // 自定义登录框样式
  374. fullScreen: false,
  375. authButton: {
  376. "title": "绑定手机号",
  377. "normalColor": "#018C39",
  378. "highlightColor": "#018C39",
  379. },
  380. privacyTerms: {
  381. "defaultCheckBoxState": true, // 条款勾选框初始状态 默认值: true
  382. "isCenterHint": false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  383. "uncheckedImage": "", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  384. "checkedImage": "", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  385. "checkBoxSize": 18, // 可选 条款勾选框大小
  386. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  387. "termsColor": "#018C39", // 协议文字颜色 默认值: #5496E3
  388. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  389. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  390. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  391. ]
  392. },
  393. },
  394. success(res) { //
  395. console.log("qxj univerify login:" + JSON.stringify(res
  396. .authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'}
  397. loginByUniverify(res.authResult, 1);
  398. },
  399. fail(res) {
  400. uni.hideLoading();
  401. uni.closeAuthView();
  402. if (res.errCode == 30005) { //预登录失败
  403. //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  404. //setTimeout(function() {
  405. uni.navigateTo({
  406. url: "/pages/auth/passwordLogin"
  407. });
  408. //}, 1800);
  409. }
  410. console.log("qxj failRes:" + JSON.stringify(res));
  411. }
  412. });
  413. console.log("----qxj 一键登录调用3-----");
  414. // #endif
  415. // #ifdef H5
  416. uni.navigateTo({
  417. url: "/pages/auth/passwordLogin"
  418. });
  419. // #endif
  420. };
  421. export function loginByUniverify(authResult, type) {
  422. uniCloud.callFunction({
  423. name: 'loginByMobile', // 你的云函数名称
  424. data: {
  425. access_token: authResult.access_token, // 客户端一键登录接口返回的access_token
  426. openid: authResult.openid // 客户端一键登录接口返回的openid
  427. }
  428. }).then(res => {
  429. console.log("qxj doUniverify:", JSON.stringify(res));
  430. if (res.result.errCode == 0) {
  431. userPhone = res.result.phoneNumber;
  432. console.log("电话:", res.result.phoneNumber);
  433. if (type == 0) {
  434. loginActionAct();
  435. } else {
  436. setPhoneAct(userPhone, unionid);
  437. }
  438. } else {
  439. uni.showToast({
  440. title: res.result.message,
  441. icon: 'none'
  442. });
  443. }
  444. }).catch(err => {
  445. // 处理错误
  446. console.error('调用云函数失败:', err);
  447. // 详细记录错误信息
  448. console.error('错误信息:', err.errMsg);
  449. })
  450. }
  451. export function loginActionAct() {
  452. if (loginType == 3 && userPhone == null) {
  453. uni.showToast({
  454. title: "请授权手机号登录",
  455. icon: 'none'
  456. });
  457. return
  458. }
  459. var data = {
  460. phone: userPhone,
  461. jpushId: registrationID,
  462. loginType: loginType
  463. };
  464. var that = this;
  465. uni.showLoading({
  466. title: "处理中..."
  467. });
  468. login(data).then(res => {
  469. uni.hideLoading();
  470. if (res.code == 200) {
  471. uni.setStorageSync('AppToken', res.token);
  472. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  473. uni.closeAuthView();
  474. // // #ifdef APP-PLUS
  475. // const jyJPush = uni.requireNativePlugin('JY-JPush');
  476. // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
  477. // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致
  478. // // 注:若没有返回任何数据,考虑是否初始化完成
  479. // console.log("qxj userAlias "+JSON.stringify(result));
  480. // });
  481. // // #endif
  482. uni.reLaunch({
  483. url: "/pages/home/index"
  484. })
  485. } else {
  486. console.log("loginActionAct error", res)
  487. uni.showToast({
  488. title: res.msg,
  489. icon: 'none'
  490. });
  491. }
  492. },
  493. rej => {}
  494. );
  495. }
  496. export function setPhoneAct(phone, unionId) {
  497. let params = {
  498. phone: phone,
  499. unionId: unionId
  500. };
  501. setPhone(params).then(res => {
  502. uni.hideLoading();
  503. if (res.code == 200) {
  504. uni.setStorageSync('AppToken', res.token);
  505. uni.setStorageSync('userInfo', JSON.stringify(res.user));
  506. uni.closeAuthView();
  507. // // #ifdef APP-PLUS
  508. // const jyJPush = uni.requireNativePlugin('JY-JPush');
  509. // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
  510. // // 设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致
  511. // // 注:若没有返回任何数据,考虑是否初始化完成
  512. // console.log("qxj userAlias "+JSON.stringify(result));
  513. // });
  514. // // #endif
  515. } else {
  516. uni.showToast({
  517. title: res.msg,
  518. icon: 'none'
  519. });
  520. }
  521. },
  522. rej => {}
  523. );
  524. }
  525. export function getRegistrationID() {
  526. // #ifdef APP-PLUS
  527. return new Promise((resolve, reject) => {
  528. if (plus.runtime.isAgreePrivacy()) {
  529. uni.getPushClientId({
  530. success: (res) => {
  531. resolve(res.cid)
  532. uni.setStorageSync("registrationID", res.cid);
  533. upPushClientId(res.cid)
  534. }, // 返回推送标识 cid
  535. fail: (err) => {
  536. console.log("getRegistrationID error:", err)
  537. reject(err)
  538. }
  539. });
  540. } else {
  541. resolve('')
  542. }
  543. });
  544. // #endif
  545. }
  546. export function weixinLogin() {
  547. loginType = 2;
  548. uni.login({
  549. provider: "weixin",
  550. onlyAuthorize: false,
  551. success: async (res) => {
  552. console.log('login success:', JSON.stringify(res));
  553. let that = this;
  554. uni.showLoading({
  555. title: "处理中..."
  556. });
  557. const params = {
  558. code: res.code,
  559. jpushId: registrationID
  560. };
  561. console.log('login微信:', params);
  562. loginByWeChatAct(params);
  563. },
  564. fail: (err) => {
  565. console.log('login fail:', JSON.stringify(err));
  566. // 未开通
  567. if (err.code == 1000) {
  568. uni.showModal({
  569. title: '登录失败',
  570. content: `${err.errMsg}\n,错误码:${err.code}`,
  571. confirmText: '开通指南',
  572. cancelText: '确定',
  573. success: (res) => {
  574. if (res.confirm) {
  575. setTimeout(() => {
  576. plus.runtime.openWeb(
  577. 'https://ask.dcloud.net.cn/article/37965')
  578. }, 500)
  579. }
  580. }
  581. });
  582. return;
  583. }
  584. // 授权拒绝
  585. if (err.code == '-100') {
  586. uni.showToast({
  587. title: "授权拒绝",
  588. icon: "none"
  589. });
  590. return;
  591. }
  592. // 一键登录预登陆失败
  593. if (err.code == '30005') {
  594. uni.showModal({
  595. showCancel: false,
  596. title: '预登录失败',
  597. content: err.errMsg
  598. });
  599. return;
  600. }
  601. if (err.code == 30006) {
  602. uni.showToast({
  603. title: "登录已取消,请重新操作",
  604. icon: "none"
  605. });
  606. return;
  607. }
  608. // 一键登录用户关闭验证界面
  609. if (err.code != '30003') {
  610. uni.showModal({
  611. showCancel: false,
  612. title: '登录失败',
  613. content: JSON.stringify(err)
  614. });
  615. }
  616. },
  617. complete: () => {
  618. }
  619. });
  620. }
  621. function upPushClientId(pushId) {
  622. updatePushId({
  623. pushId: pushId
  624. })
  625. }