C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

323 行
9.7 KiB

  1. let config = require("./config/config.js");
  2. const Http = require("./utils/HttpBasics");
  3. const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  4. App({
  5. data: {
  6. newArr:[],//存放分享二维进入消息信息
  7. },
  8. onLaunch: function(options) {
  9. var that = this;
  10. if(options.query.scene){
  11. that.globalData.newArr = decodeURIComponent(options.query.scene).split(":");
  12. }
  13. console.log(that.globalData.newArr,"我是app.js")
  14. that.globalData.sceneAddress = options.scene;
  15. /**
  16. * 小程序版本更新
  17. */
  18. that.autoUpdate();
  19. /**
  20. * 用户登录
  21. */
  22. that.userLogin(options.scene);
  23. wx.getSystemInfo({
  24. success: res => {
  25. that.statusBarHeight = res.statusBarHeight
  26. }
  27. })
  28. },
  29. statusBarHeight: 0,
  30. /**
  31. * 用户登录
  32. */
  33. userLogin: function(sceneAddress) {
  34. let that = this;
  35. // 登录
  36. wx.login({
  37. success: ({
  38. code
  39. }) => {
  40. let usrdata = {
  41. appId: config.weapp.AppId,
  42. code: code,
  43. sceneAddress: sceneAddress
  44. }
  45. if (that.globalData.newArr&&that.globalData.newArr[1]=='in_u'){
  46. usrdata.UId = that.globalData.newArr[2]
  47. } else if(that.globalData.newArr && that.globalData.newArr[1] == 'md'){
  48. usrdata.MId = that.globalData.newArr[2]
  49. }
  50. console.log(usrdata,"?????usrdata")
  51. Http.post({
  52. url: config.api.login,
  53. data: usrdata
  54. })
  55. .then(res => {
  56. wx.setStorageSync('openId',res.data.openId)
  57. if (res.data.subMalls){
  58. const squareList = JSON.parse(res.data.subMalls)
  59. wx.setStorageSync("squareList", squareList)
  60. wx.setStorageSync("selectedMall", res.data.selectedMall)
  61. }
  62. if (res.data && res.data.score) {
  63. if (res.data.score != 0) {
  64. that.globalData.score = res.data.score;
  65. }
  66. }
  67. Http.setToken(res.data.token);
  68. that.globalData.token = res.data.token;
  69. console.log(that.globalData.token)
  70. if (that.tokenCallback) {
  71. that.tokenCallback(res.data.token);
  72. }
  73. })
  74. .catch(err => {
  75. console.log(err)
  76. wx.showModal({
  77. title: '提示',
  78. showCancel: false,
  79. content: '登录失败,请重新尝试',
  80. success: function(res) {
  81. if (res.cancel) {
  82. //点击取消,默认隐藏弹框
  83. } else {
  84. //点击确定
  85. wx.reLaunch({
  86. url: '/pages/index/index',
  87. })
  88. }
  89. }
  90. })
  91. })
  92. }
  93. })
  94. },
  95. autoUpdate: function() {
  96. let that = this;
  97. if (wx.canIUse('getUpdateManager')) {
  98. const updateManager = wx.getUpdateManager()
  99. // 1. 检查小程序是否有新版本发布
  100. updateManager.onCheckForUpdate(function(res) {
  101. // 请求完新版本信息的回调
  102. if (res.hasUpdate) {
  103. // 检测到新版本,需要更新,给出提示
  104. wx.showModal({
  105. title: '更新提示',
  106. content: '检测到新版本,是否下载新版本并重启小程序?',
  107. success: function (res) {
  108. if (res.confirm) {
  109. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  110. that.downLoadAndUpdate(updateManager)
  111. } else if (res.cancel) {
  112. //用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
  113. wx.showModal({
  114. title: '温馨提示~',
  115. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  116. showCancel: false,//隐藏取消按钮
  117. confirmText: "确定更新",//只保留确定更新按钮
  118. success: function (res) {
  119. if (res.confirm) {
  120. //下载新版本,并重新应用
  121. that.downLoadAndUpdate(updateManager)
  122. }
  123. }
  124. })
  125. }
  126. }
  127. })
  128. }
  129. })
  130. } else {
  131. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  132. wx.showModal({
  133. title: '提示',
  134. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  135. })
  136. }
  137. },
  138. /**
  139. * 下载小程序新版本并重启应用
  140. */
  141. downLoadAndUpdate: function(updateManager) {
  142. let that = this
  143. wx.showLoading();
  144. //静默下载更新小程序新版本
  145. updateManager.onUpdateReady(function() {
  146. wx.hideLoading()
  147. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  148. updateManager.applyUpdate()
  149. })
  150. updateManager.onUpdateFailed(function() {
  151. // 新的版本下载失败
  152. wx.showModal({
  153. title: '已经有新版本了哟~',
  154. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  155. })
  156. })
  157. },
  158. globalData: {
  159. typeLsit: [],//初始哈样式
  160. scene: "",
  161. // token
  162. token: null,
  163. // user openId
  164. openId: null,
  165. // 渠道
  166. sceneAddress: null,
  167. // location info
  168. locationInfo: null,
  169. // 二维码参数
  170. scene: null,
  171. // 支持智慧停车, 用户名下有车
  172. phone: null,
  173. supportCar: false,
  174. parkVendor: 1, // 1-ETCP, 2-TJD停简单 3-Dahua大华, 4-ShangAn尚安, 5-BoLink泊链
  175. // ETCP token
  176. etcpToken: null,
  177. carLogin: false,
  178. // 当前商场信息
  179. market: {
  180. name: ""
  181. },
  182. // websocket
  183. socketClient: null,
  184. socketReceiver: function(e) {}, //收到消息回调
  185. curHtml: '',//富文本相关,由于浏览器传参长度有限制
  186. },
  187. // 初始化websocket
  188. initSocket: function() {
  189. let that = this;
  190. // socket是否连接
  191. let socketConnected = false;
  192. // 待发送的消息队列
  193. let messageQueue = [];
  194. // 是否断线重连
  195. let reconnect = true;
  196. // 发送消息
  197. function sendSocketMessage(msg) {
  198. // console.log(msg);
  199. // 如果socket已连接则发送消息
  200. if (socketConnected) {
  201. wx.sendSocketMessage({
  202. data: msg
  203. })
  204. } else {
  205. // socket没有连接将消息放入队列中
  206. messageQueue.push(msg);
  207. }
  208. }
  209. // 关闭连接
  210. function close() {
  211. if (socketConnected) {
  212. wx.closeSocket()
  213. socketConnected = false;
  214. }
  215. }
  216. // 符合WebSocket定义的对象
  217. var ws = {
  218. send: sendSocketMessage,
  219. close: close
  220. }
  221. // 创建一个 WebSocket 连接
  222. function connect() {
  223. wx.connectSocket({
  224. url: config.wsurl,
  225. header: {
  226. token: that.globalData.token //从服务端获取一个token,服务端验证token是否允许连接,案例中没做限制
  227. }
  228. })
  229. }
  230. connect();
  231. // 监听 WebSocket 连接打开事件
  232. wx.onSocketOpen(function(res) {
  233. console.log("WebSocket 连接成功")
  234. socketConnected = true;
  235. ws.onopen();
  236. // 连接成功后,将队列中的消息发送出去
  237. let queueLength = messageQueue.length
  238. for (let i = 0; i < queueLength; i++) {
  239. sendSocketMessage(messageQueue.shift())
  240. }
  241. })
  242. // 监听 WebSocket 接受到服务器的消息事件
  243. wx.onSocketMessage(function(res) {
  244. ws.onmessage(res);
  245. })
  246. // 监听 WebSocket 错误事件
  247. wx.onSocketError(function(res) {
  248. console.log("WebSocket 错误事件")
  249. if (!socketConnected) {
  250. // 断线重连
  251. if (reconnect) {
  252. connect();
  253. }
  254. }
  255. })
  256. // 监听 WebSocket 连接关闭事件
  257. wx.onSocketClose(function(res) {
  258. console.log("WebSocket 连接关闭")
  259. socketConnected = false;
  260. // 断线重连
  261. if (reconnect) {
  262. connect();
  263. }
  264. })
  265. const Stomp = require('/utils/stomp.min.js').Stomp;
  266. /**
  267. * 定期发送心跳或检测服务器心跳
  268. * The heart-beating is using window.setInterval() to regularly send heart-beats and/or check server heart-beats.
  269. * 可看stomp.js的源码(195,207,489行),由于小程序没有window对象,所以我们要调用小程序的定时器api实现
  270. */
  271. Stomp.setInterval = function(interval, f) {
  272. return setInterval(f, interval);
  273. };
  274. // 结束定时器的循环调用
  275. Stomp.clearInterval = function(id) {
  276. return clearInterval(id);
  277. };
  278. const stompClient = Stomp.over(ws);
  279. that.globalData.socketClient = stompClient;
  280. stompClient.connect({}, function(callback) {
  281. // 订阅自己的
  282. stompClient.subscribe('/user/' + config.weapp.AppId + '/' + that.globalData.openId + '/message', function(message, headers) {
  283. console.log('收到只发送给我的消息:', message);
  284. that.globalData.socketReceiver(JSON.parse(message.body));
  285. // 通知服务端收到消息
  286. message.ack();
  287. });
  288. stompClient.subscribe('/topic/' + config.weapp.AppId, function(message, headers) {
  289. console.log('收到TOPIC的消息:', message);
  290. that.globalData.socketReceiver(JSON.parse(message.body));
  291. // 通知服务端收到消息
  292. //message.ack();
  293. });
  294. // 向服务端发送消息
  295. stompClient.send("/app/message", {}, JSON.stringify({
  296. 'msg': 'client: ' + that.globalData.openId
  297. }));
  298. })
  299. },
  300. //统一发送消息
  301. sendSocketMessage: function(msg) {
  302. this.globalData.socketClient.send("/app/message", {}, JSON.stringify(msg));
  303. }
  304. });