海鲜小程序

app.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //app.js
  2. var http = require('utils/util.js');
  3. var md5 = require('utils/md5.js');
  4. var commonJs = require('utils/common.js')
  5. let livePlayer = requirePlugin('live-player-plugin')
  6. App({
  7. data:{
  8. windowWidth:0,//屏幕的宽度
  9. windowHeight:0,//屏幕的高度
  10. share_open_id:'',//分享用户的opneid
  11. sale_id:'',//销售id
  12. systemInfo:{},
  13. safeAreaTop:30
  14. },
  15. onLaunch: function () {
  16. var _this = this;
  17. wx.setStorageSync('is_guide_mask', false)
  18. this.checkSession()
  19. wx.getSystemInfo({
  20. success (res) {
  21. _this.data.systemInfo = res
  22. if(res.platform == "ios"){
  23. _this.data.safeAreaTop=res.statusBarHeight + 10
  24. }else if(res.platform == "android"){
  25. _this.data.safeAreaTop=res.statusBarHeight + 10
  26. }else{
  27. _this.data.safeAreaTop=res.statusBarHeight + 10
  28. }
  29. }
  30. })
  31. },
  32. onShow(options) {
  33. console.log('onShow',options)
  34. if (options.scene == 1007 || options.scene == 1008 || options.scene == 1044) {
  35. livePlayer.getShareParams()
  36. .then(res => {
  37. // 房间号
  38. console.log('get room id', res.room_id)
  39. // 用户openid
  40. console.log('get openid', res.openid)
  41. // 分享者openid,分享卡片进入场景才有
  42. console.log('get share openid', res.share_openid)
  43. if(res.share_openid){
  44. this.data.share_open_id = res.share_openid
  45. this.checkSession(()=>{
  46. this.reportInviteInfo()
  47. })
  48. }
  49. // 开发者在跳转进入直播间页面时,页面路径上携带的自定义参数,这里传回给开发者
  50. console.log('get custom params', res.custom_params)
  51. }).catch(err => {
  52. console.log('get share params', err)
  53. })
  54. }
  55. if(options.query.openid){//上报用户关系
  56. this.data.share_open_id = options.query.openid
  57. this.checkSession(()=>{
  58. this.reportInviteInfo()
  59. })
  60. }
  61. if(options.query.scene){//记录销售用户关系
  62. var obj = this.getQueryVariable(decodeURIComponent(options.query.scene))
  63. console.log(obj)
  64. if(obj.sale_id){
  65. this.data.sale_id = obj.sale_id
  66. this.checkSession(()=>{
  67. this.salerShare()
  68. })
  69. }
  70. if(obj.room_id){//去直播间
  71. let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/index/index', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数(如示例中的path和pid参数),后续可以在分享卡片链接和跳转至商详页时获取,详见【获取自定义参数】、【直播间到商详页面携带参数】章节(上限600个字符,超过部分会被截断)
  72. wx.navigateTo({
  73. url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${obj.room_id}&custom_params=${customParams}`
  74. })// 其中wx2b03c6e691cd7370是直播组件appid不能修改
  75. }
  76. }
  77. },
  78. checkSession (cb) {//判断微信登录是否过期
  79. var _this = this;
  80. wx.checkSession({
  81. success () {
  82. //session_key 未过期,并且在本生命周期一直有效
  83. console.log(wx.getStorageSync('openid'),'openid')
  84. if(!wx.getStorageSync('openid')){
  85. _this.login(cb)
  86. }else{//已在登录状态
  87. console.log("已在登录状态")
  88. cb?cb():''
  89. }
  90. },
  91. fail() {
  92. console.log('过期,重新登录')
  93. // session_key 已经失效,需要重新执行登录流程
  94. _this.login(cb)
  95. }
  96. })
  97. },
  98. login(cb){//获取openid
  99. var _this = this;
  100. wx.login({
  101. success (res) {
  102. if (res.code) {
  103. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  104. wx.setStorageSync('isSpringBox', false)
  105. http.req('/api/wx/auth', 'get', {
  106. code: res.code,
  107. }, function (res) {
  108. if(res && res.errno == 200){
  109. wx.setStorageSync('openid',res.rst.openid)
  110. wx.setStorageSync('session_key',res.rst.session_key)
  111. cb?cb():''
  112. }else{
  113. }
  114. },()=>{
  115. })
  116. } else {
  117. console.log('登录失败!' + res.errMsg)
  118. }
  119. }
  120. })
  121. },
  122. reportInviteInfo() {//上报分享关系
  123. if(this.data.share_open_id == wx.getStorageSync('openid')){//排除自己邀请自己的情况
  124. return;
  125. }
  126. http.req('/api/wxuser/share', 'post', {
  127. share_open_id: this.data.share_open_id,
  128. current_open_id: wx.getStorageSync('openid')
  129. }, (res) => {
  130. console.log('上报用户关系接口',res)
  131. if(res && res.errno == 200){
  132. }else{
  133. }
  134. },()=>{
  135. })
  136. },
  137. salerShare(){//记录销售关系
  138. http.req('/api/wxuser/salerShare', 'post', {
  139. saler_id: this.data.sale_id,
  140. openid: wx.getStorageSync('openid')
  141. }, (res) => {
  142. console.log('记录销售关系接口',res)
  143. if(res && res.errno == 200){
  144. }else{
  145. }
  146. },()=>{
  147. })
  148. },
  149. shareEvent(res){//分享
  150. var pages = getCurrentPages() //获取加载的页面
  151. var currentPage = pages[pages.length-1] //获取当前页面的对象
  152. var url = currentPage.route //当前页面url
  153. var options = currentPage.options
  154. options = Object.assign(options,{'openid':wx.getStorageSync('openid')})
  155. console.log(url,options)
  156. var path = '';
  157. for(var i in options){
  158. path += '&' + i + '=' + options[i]
  159. }
  160. var path2 = url + "?" + path.substr(1)
  161. console.log(path2)
  162. console.log(res)
  163. if(res.from === 'button'){
  164. //来自页面内转发按钮
  165. }
  166. return{
  167. title:'海鲜阿嫂',
  168. path: path2
  169. }
  170. },
  171. getQueryVariable(query){
  172. var vars = query.split("&");
  173. var arr = {}
  174. vars.forEach((item,index)=>{
  175. var pair = item.split("=")
  176. Object.assign(arr,{[pair[0]]:pair[1]})
  177. })
  178. return arr;
  179. },
  180. func: {
  181. req: http.req,
  182. md5: md5.hex_md5,
  183. getDay: commonJs.getDay,
  184. NumberHandle: commonJs.NumberHandle
  185. },
  186. })