init.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import carhelp from '@/apis/utils/mixin.js'
  2. import * as API_WeiXin from '@/apis/weixin.js'
  3. import {
  4. getUrlParam,
  5. getWeixinRedirectURI,
  6. isWeiXin
  7. } from '@/apis/utils'
  8. var checkOpenId = true; //是否需要获取openId
  9. var openId = carhelp.getOpenId();
  10. var uid = carhelp.getUID();
  11. var app = {
  12. init: function() {
  13. // isWeiXin()||
  14. const jpuid = getUrlParam('unionid');
  15. const otherUserId = getUrlParam('otherUserId');
  16. const unionPhone = getUrlParam('unionPhone');
  17. if(jpuid){
  18. var obj=carhelp.get("xpgj_wx_user_info")
  19. if(obj==null||obj==''){
  20. obj={
  21. unionId:"",
  22. otherUserId:""
  23. }
  24. }
  25. obj.unionId=jpuid
  26. if(obj.otherUserId!=otherUserId){
  27. carhelp.set("token_tdate",'');
  28. }
  29. obj.otherUserId=otherUserId
  30. obj.unionPhone=unionPhone
  31. carhelp.set("xpgj_wx_user_info",obj)
  32. uid = carhelp.getUID();
  33. }
  34. if (isWeiXin()||(process.env.NODE_ENV === "development")) {
  35. if (checkOpenId) {
  36. if (!openId&&!uid) {
  37. this.getOpenId()
  38. }else {
  39. this.getPersion(openId)
  40. }
  41. }
  42. } else{
  43. var NODE_NAME= process.env['NODE_NAME']
  44. if(NODE_NAME=="production"){
  45. alert("请使用微信打开本页面")
  46. }
  47. }
  48. },
  49. getPersion(thisopenId){
  50. console.log("init")
  51. var tdate=new Date( +new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,15).replace("T"," ")
  52. var token_tdate=carhelp.get("token_tdate");//每10分钟存储一次token
  53. if(token_tdate&&token_tdate==tdate){
  54. return
  55. }
  56. var loginurl="/mobile/tenantUser/findByOpenId"
  57. var uid2=carhelp.getUID()
  58. var getOtherUserId=carhelp.getOtherUserId()
  59. uni.request({
  60. method:'get',
  61. url: process.car.BASE_URL + loginurl,
  62. data: {
  63. openId:uid2?uid:thisopenId,
  64. otherUserId:getOtherUserId
  65. },
  66. header: {
  67. 'Content-Type': 'application/x-www-form-urlencoded',
  68. 'X-Requested-With': 'XMLHttpRequest',
  69. //'Authorization':token
  70. }
  71. }).then((response) => {
  72. let [error, res] = response;
  73. if (res.data.code == 200&&res.data.result) {
  74. var token = res ? res.data.data.token : '';
  75. carhelp.setPersonInfo(res.data.data.tenantInfo );
  76. carhelp.setPersonInfoPlus(res.data.data );
  77. carhelp.setToken(token);
  78. }else{
  79. carhelp.logoff();
  80. }
  81. }).catch(error => {
  82. })
  83. },
  84. getOpenId(bl,uid) {
  85. if (process.env.NODE_ENV === "development"){
  86. carhelp.setOpenId(process.car.openId)
  87. location.reload();
  88. return
  89. }
  90. const code = getUrlParam('code');
  91. var openId = carhelp.getOpenId()
  92. if (!openId) {
  93. if (!code) {
  94. var url = document.URL;
  95. var getUrl= getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);
  96. window.location.href = getUrl;
  97. } else {
  98. API_WeiXin.getDataByCode(code).then(response => {
  99. carhelp.setOpenId(response.data.openid)
  100. openId = response.data.openid;
  101. carhelp.set("xpgj_wx_user_info",response.data)
  102. if(bl&&uid){
  103. API_WeiXin.saveWechatInfo({
  104. openId:openId,
  105. unionId:uid
  106. }).then(response => {
  107. location.reload();
  108. }).catch(error => {
  109. console.log(error);
  110. });
  111. }else{
  112. location.reload();
  113. }
  114. }).catch(error => {
  115. console.log(error);
  116. });
  117. }
  118. }
  119. }
  120. }
  121. module.exports = app