login.js 19 KB

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