http.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // #ifdef H5
  2. import wx from 'weixin-jsapi'
  3. // #endif
  4. let path = "";
  5. let appIp = "";
  6. // #ifdef H5
  7. let domainName = location.host; //域名+端口
  8. let protocolName = document.location.protocol; //http协议
  9. let wsName = protocolName === 'http:' ? 'ws' : 'wss'; //ws协议
  10. path = `${protocolName}//${domainName}/service`
  11. // #endif
  12. uni.setStorageSync('path', path);
  13. // get方法
  14. export function get(url, data = {}) {
  15. url = path + url;
  16. return new Promise((resolve, reject) => {
  17. uni.request({
  18. url,
  19. data,
  20. header: {
  21. 'Cache-Control': 'no-cache'
  22. },
  23. success(res) {
  24. resolve(res.data);
  25. },
  26. fail(err) {
  27. uni.showToast({
  28. icon: 'none',
  29. title: '请求数据失败!'
  30. });
  31. }
  32. })
  33. });
  34. }
  35. // post方法
  36. export function post(url, data = {}) {
  37. url = path + url;
  38. return new Promise((resolve, reject) => {
  39. uni.request({
  40. method: 'POST',
  41. url,
  42. data,
  43. header: {
  44. 'Cache-Control': 'no-cache'
  45. },
  46. success(res) {
  47. resolve(res.data);
  48. },
  49. fail(err) {
  50. uni.showToast({
  51. icon: 'none',
  52. title: '请求数据失败!'
  53. });
  54. }
  55. })
  56. });
  57. }
  58. // delete方法
  59. export function deleteIt(url, data = {}) {
  60. url = path + url;
  61. return new Promise((resolve, reject) => {
  62. uni.request({
  63. method: 'DELETE',
  64. url,
  65. data,
  66. header: {
  67. 'Cache-Control': 'no-cache'
  68. },
  69. success(res) {
  70. resolve(res.data);
  71. },
  72. fail(err) {
  73. uni.showToast({
  74. icon: 'none',
  75. title: '请求数据失败!'
  76. });
  77. }
  78. })
  79. });
  80. }
  81. // 扫一扫
  82. export function SM() {
  83. // #ifndef H5
  84. return new Promise((resolve, reject) => {
  85. uni.scanCode({
  86. onlyFromCamera: true,
  87. success: function(res) {
  88. let str = res.result.replace(/[\s\/]/g, '') || 'none';
  89. str.replace(/CODABAR,/i, '');
  90. resolve(str);
  91. },
  92. fail(err) {
  93. console.error(err)
  94. }
  95. });
  96. });
  97. // #endif
  98. // #ifdef H5
  99. return new Promise((resolve, reject) => {
  100. let param = {
  101. requestUrl: location.href.split('#')[0]
  102. };
  103. post("/wechat/getJsConfig", param).then(res => {
  104. if (res) {
  105. wx.config({
  106. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  107. appId: res.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  108. timestamp: res.timestamp, // 必填,生成签名的时间戳
  109. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  110. signature: res.signature, // 必填,签名,见附录1
  111. jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  112. });
  113. wx.ready(function() {
  114. wx.scanQRCode({
  115. desc: "scanQRCode desc",
  116. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  117. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  118. success: function(res) {
  119. // 当needResult 为 1 时,扫码返回的结果
  120. let str = res.resultStr.replace(/[\s\/]/g, '') || 'none';
  121. resolve(str);
  122. }
  123. });
  124. });
  125. }
  126. })
  127. });
  128. // #endif
  129. }
  130. // 修改接口域名
  131. export function changeIP(ip) {
  132. console.log(ip)
  133. path = `${ip}/service`;
  134. appIp = ip;
  135. uni.setStorageSync('path', path);
  136. }
  137. // 建立websocket
  138. export function webHandle(cate, type, ipe) {
  139. // uni.closeSocket();
  140. // 屏蔽语音播报
  141. if (cate !== 'no') {
  142. uni.navigateTo({
  143. url: `../${cate}/${cate}`
  144. })
  145. }
  146. return;
  147. // 屏蔽语音播报
  148. if (getApp().$ws) {
  149. return;
  150. }
  151. console.log(getApp().$ws)
  152. let clientid, ip, wws;
  153. if (type === 'app') {
  154. clientid = uni.getStorageSync('clientid'); //获取cid
  155. ipe = ipe || appIp;
  156. ip = ipe.split(':')[1]; //过滤掉端口
  157. wws = ipe.split(':')[0] == 'http' ? 'ws' : 'wss';
  158. } else if (type === 'wx') {
  159. ip = document.domain; //过滤掉端口
  160. // ip = '192.168.3.108'; //过滤掉端口
  161. console.log(ip)
  162. wws = wsName;
  163. }
  164. console.log(getApp())
  165. getApp().$ws = uni.connectSocket({
  166. url: `${wws}://${ip}:8080/webSocket/message/app`,
  167. header: {
  168. 'content-type': 'application/json'
  169. },
  170. success(result) {
  171. console.log(result);
  172. // 监听WebSocket连接打开事件
  173. uni.onSocketOpen(res1 => {
  174. console.log(res1, 'websocket连接成功');
  175. // 通过 WebSocket 连接发送数据
  176. let obj = {};
  177. if (type === 'app') {
  178. obj = {
  179. userCount: uni.getStorageSync('userData').user.id,
  180. clientId: clientid
  181. };
  182. } else if (type === 'wx') {
  183. obj = {
  184. userCount: uni.getStorageSync('userData').user.id
  185. };
  186. }
  187. console.log(JSON.stringify(obj))
  188. uni.sendSocketMessage({
  189. data: JSON.stringify(obj),
  190. success(res2) {
  191. console.log(res2)
  192. }
  193. });
  194. // 监听WebSocket接受到服务器的消息事件
  195. uni.onSocketMessage(res3 => {
  196. console.log('收到服务器内容:' + res3.data);
  197. // 连接成功后跳转到待接单列表baba
  198. if (res3.data !== 'X') {
  199. let objData = JSON.parse(res3.data);
  200. if (objData.status == 200) {
  201. if (cate !== 'no') {
  202. uni.navigateTo({
  203. url: `../${cate}/${cate}`
  204. })
  205. }
  206. } else {
  207. // 播报 start
  208. let msg = objData.content;
  209. msg = msg.replace(/【[0-9]*】/g, function(word) {
  210. return word.substring(0, 2) + ' ' + word.substring(2);
  211. });
  212. console.log(getApp().audios)
  213. getApp().audios = getApp().audios || [];
  214. getApp().audios.push(msg);
  215. if (getApp().audios.length === 1) {
  216. startAudio();
  217. }
  218. // 播报 end
  219. }
  220. }
  221. });
  222. });
  223. uni.onSocketClose(function() {
  224. console.log('WebSocket 已关闭!');
  225. });
  226. }
  227. })
  228. }
  229. // 语音播放
  230. function startAudio() {
  231. let arr = getApp().audios;
  232. console.log(arr);
  233. if (arr.length === 0) {
  234. return;
  235. } else {
  236. const innerAudioContext = uni.createInnerAudioContext();
  237. innerAudioContext.autoplay = true;
  238. innerAudioContext.src = 'http://fanyi.baidu.com/gettts?lan=zh&text=' + arr[0] +
  239. '&spd=5&source=web';
  240. innerAudioContext.onEnded(() => {
  241. arr.shift();
  242. startAudio();
  243. })
  244. }
  245. // innerAudioContext.src = 'http://192.168.3.108/tts1.mp3';
  246. // uni.request({
  247. // url: 'http://fanyi.baidu.com/gettts?lan=zh&text=这是一个测试&spd=5&source=web',
  248. // success: (res) => {
  249. // console.log(res.data);
  250. // }
  251. // });
  252. }