commonPageMethod.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var app = getApp(); //获取应用实例
  2. const util = require('@/static/utils/util.js');
  3. const config = require('@/static/config.js');
  4. import requestConfig from '@/static/lib/requestConfig';
  5. export default {
  6. data() {
  7. return {
  8. isSendImg: false, //当前页面是否正在处于选择本地图片的状态-选择图片会触发小程序的隐藏事件,需要单独处理
  9. previewFlag: false, //当前页面是否预览了图片-预览图片会触发小程序的隐藏事件,需要单独处理
  10. loadPromise: new Promise((resolve) => {
  11. this.loadedCompleteFun = function() {
  12. console.log('onload结束');
  13. resolve()
  14. }
  15. }),
  16. }
  17. },
  18. watch: {},
  19. async onLoad(options) {
  20. let shareCity = ''; //分享携带的城市信息
  21. let houseId = ''; //分享携带的项目id
  22. let shareUserId = ''; //分享携带的助力发起人id
  23. if (!options) { //如果不存在则直接返回
  24. this.loadedCompleteFun();//通告加载已经结束
  25. return false;
  26. }
  27. // let lastPage = getCurrentPages()[getCurrentPages().length-2] ? getCurrentPages()[getCurrentPages().length-2].$vm : null;
  28. // if(lastPage){
  29. // if(lastPage.componentId){
  30. // options.componentId = lastPage.componentId;
  31. // }
  32. // if(lastPage.componentType){
  33. // options.componentType = lastPage.componentType;
  34. // }
  35. // if(lastPage.diyArea){
  36. // options.diyArea = lastPage.diyArea;
  37. // }
  38. // }
  39. if(app.globalData.componentId){
  40. options.componentId = app.globalData.componentId;
  41. options.componentType = app.globalData.componentType;
  42. options.diyArea = app.globalData.diyArea;
  43. app.globalData.componentId = '';//清空该值
  44. app.globalData.componentType = '';//清空该值
  45. app.globalData.diyArea = false;//清空该值
  46. }
  47. this.pvCurPageParams = JSON.stringify(options);
  48. if (options.gdt_vid) { //从朋友圈广告进入的带有clickid
  49. app.globalData.clickId = options.gdt_vid;
  50. }
  51. if (options.scene) { //如果存在该参数,则说明是通过数量无限制的小程序码进来的
  52. const scene = decodeURIComponent(options.scene); // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  53. options.shareToken = scene; //说明用户携带分享信息
  54. console.log('被scene先覆盖了')
  55. }
  56. //将渠道存至全局,留电接口需要用
  57. if (options.shareToken && options.shareToken != "null" && options.shareToken != "undefined") {
  58. app.globalData.fromChannel = options.shareToken;
  59. // #ifdef H5
  60. var userInfo = uni.getStorageSync('userInfo');
  61. if(userInfo){
  62. userInfo.fromChannel = options.shareToken;
  63. uni.setStorageSync('userInfo', userInfo);
  64. }
  65. // #endif
  66. }
  67. // 解密对应的客户来源-同时防止重复解密
  68. if (app.globalData.fromChannel && app.globalData.oldFromChannel != app.globalData.fromChannel) {
  69. console.log("***开始解密来源信息***", app.globalData.fromChannel);
  70. var param = {
  71. shareSign: app.globalData.fromChannel
  72. };
  73. const res = await requestConfig('decryptShareSign', param, true);
  74. console.log("***decryptHandle***", res);
  75. if (res && res.success) {
  76. app.globalData.oldFromChannel = app.globalData.fromChannel;//解密成功后记录
  77. console.log("***解密来源信息成功***", res.single);
  78. if (res.single) {
  79. // app.globalData.shareId = res.single.customerId;
  80. if (res.single.attrs) {// attrs 是字符串形式存储在数据库服务器上
  81. res.single.attrs = JSON.parse(res.single.attrs);
  82. }
  83. shareCity = res.single.attrs ? res.single.attrs.shareCity : '';//赋值分享者城市
  84. shareUserId = res.single.attrs ? res.single.attrs.shareUserId : '';
  85. houseId = res.single.houseId || options.houseId || '';
  86. app.globalData.exchangedFromChannel = JSON.stringify(res.single);//解密后的明文
  87. app.globalData.isZhuanFaFromProject = houseId;
  88. }
  89. } else {
  90. let trackparam = {
  91. type: 'mini-program-Error', //埋点类型
  92. pvCurPageName: 'app.js-decrypt', //当前页面名称
  93. expand: JSON.stringify(res) + ";brandId=" + config.brandId + ";param=" + JSON.stringify(
  94. param), //扩展字段
  95. };
  96. util.trackRequest(trackparam);
  97. }
  98. }
  99. //检查如果不触发解密的情况(相邻几次的秘钥是一样的)
  100. //或者 不存在解密时 即 fromChannel 和 oldFromChannel 都为空
  101. //则比对最新的城市解密记录情况,匹配的上,则取最新解密记录的城市刷新当前城市
  102. if (app.globalData.oldFromChannel == app.globalData.fromChannel) {
  103. let single = app.globalData.exchangedFromChannel ? JSON.parse(app.globalData.exchangedFromChannel) : {};
  104. shareCity = single.attrs ? single.attrs.shareCity : '';
  105. shareUserId = single.attrs ? single.attrs.shareUserId : '';
  106. }
  107. if(options.shareCity){
  108. shareCity = options.shareCity;
  109. }
  110. if(shareCity){//存在分享城市,则更新为分享人所在的城市
  111. app.addCustomerCity(shareCity);
  112. }
  113. if(options.houseId){
  114. houseId = options.houseId;
  115. }
  116. let currPage = getCurrentPages()[getCurrentPages().length - 1] ? getCurrentPages()[getCurrentPages().length - 1].$vm : null;
  117. if(currPage){
  118. currPage.shareUserId = shareUserId ? shareUserId : '';
  119. currPage.houseId = houseId ? houseId : '';
  120. }
  121. let shareOpenId = options.shareOpenId ? options.shareOpenId : '';
  122. if (app.globalData.exchangedFromChannel) {
  123. var _t = JSON.parse(app.globalData.exchangedFromChannel);
  124. if (_t) {
  125. _t.shareOpenId = shareOpenId;
  126. app.globalData.exchangedFromChannel = JSON.stringify(_t);
  127. }
  128. } else {
  129. let params = {
  130. shareOpenId: shareOpenId
  131. };
  132. app.globalData.exchangedFromChannel = JSON.stringify(params);
  133. }
  134. console.warn("---loadedCompleteFun---")
  135. this.loadedCompleteFun();//通告加载已经结束
  136. },
  137. onShow() {
  138. // this.isSendImg = false; //还原默认值-这里不能还原,因为微信在处理拍照选择图片时,是分两个阶段的
  139. // 阶段1:拍照,开始时,会把小程序退到后台,会触发onhide;拍照结束后,会回到小程序的当前页面-页面执行onshow
  140. // 阶段2:选择图片,会进入选择图片的原生组件-小程序退到后台执行onhide;图片选择完毕后,会执行onshow
  141. // 上述两个阶段存在onshow事件,重置的话,会导致阶段2会执行断开socket的方法-详见App.vue 的onhide方法
  142. },
  143. onUnload() {
  144. console.warn("---onUnload---")
  145. app.hidePage();
  146. },
  147. /**
  148. * 生命周期函数--监听页面隐藏
  149. */
  150. onHide: function() {
  151. console.warn("---onhide---")
  152. app.hidePage();
  153. },
  154. methods: {
  155. // 通告加载完成事件
  156. loadedCompleteFun() {},
  157. /**
  158. * 获取当前页面的数据,为埋点用
  159. */
  160. getCurrentPageParam() {
  161. return this.pvCurPageParams
  162. },
  163. // 处理分享路径的公共方法
  164. shareInitPath(){
  165. let path = '';
  166. let shareToken = app.globalData.shareToken || '';//当前用户的openid
  167. let shareOpenId = app.globalData.openid || '';//当前用户的openid
  168. let houseId = this.houseId || '';//当前用户的openid
  169. let pageId = this.pageId || '';//当前页面的pageId
  170. let activityId = this.activityId || '';//当前页面的activityId
  171. let liveStreams = this.liveStreams || '';//当前页面的直播地址
  172. let layoutId = this.layoutId || '';//当前页面的户型id
  173. // 当前的分享参数
  174. // 为了判断返回是到项目首页 还是集团页面 同时,也具有判断当前用户是来自分享的功能
  175. let fromProject = houseId ? app.globalData.projectShare : app.globalData.brandShare;
  176. path = "shareToken=" + shareToken
  177. + "&shareOpenId=" + shareOpenId
  178. + "&houseId=" + houseId
  179. + '&fromProject=' + fromProject
  180. + "&pageId=" + pageId;
  181. if(activityId){
  182. path += "&activityId=" + activityId;
  183. }
  184. if(liveStreams){
  185. path += "&liveStreams=" + liveStreams;
  186. }
  187. if(layoutId){
  188. path += "&layoutId=" + layoutId;
  189. }
  190. console.warn("***shareInitPath***",path);
  191. return path;
  192. },
  193. }
  194. }