init.js 3.8 KB

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