http.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = str.replace(/CODABAR,/i, '');
  90. str = str.replace(/CODE_128,/i, '');
  91. resolve(str);
  92. },
  93. fail(err) {
  94. reject(err);
  95. }
  96. });
  97. });
  98. // #endif
  99. // #ifdef H5
  100. return new Promise((resolve, reject) => {
  101. let param = {
  102. requestUrl: location.href.split('#')[0]
  103. };
  104. post("/wechat/getJsConfig", param).then(res => {
  105. if (res) {
  106. wx.config({
  107. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  108. appId: res.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  109. timestamp: res.timestamp, // 必填,生成签名的时间戳
  110. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  111. signature: res.signature, // 必填,签名,见附录1
  112. jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  113. });
  114. wx.ready(function() {
  115. wx.scanQRCode({
  116. desc: "scanQRCode desc",
  117. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  118. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  119. success: function(res) {
  120. // 当needResult 为 1 时,扫码返回的结果
  121. let str = res.resultStr.replace(/[\s\/]/g, '') || 'none';
  122. str = str.replace(/CODABAR,/i, '');
  123. str = str.replace(/CODE_128,/i, '');
  124. resolve(str);
  125. },
  126. cancel(err){
  127. reject(err);
  128. }
  129. });
  130. });
  131. }
  132. })
  133. });
  134. // #endif
  135. }
  136. // 修改接口域名
  137. export function changeIP(ip) {
  138. console.log(ip)
  139. path = `${ip}/service`;
  140. appIp = ip;
  141. uni.setStorageSync('path', path);
  142. }
  143. // 建立websocket
  144. export function webHandle(cate, type, ipe) {
  145. // uni.closeSocket();
  146. // 屏蔽语音播报
  147. if (cate !== 'no') {
  148. uni.navigateTo({
  149. url: `../${cate}/${cate}`
  150. })
  151. }
  152. return;
  153. // 屏蔽语音播报
  154. if (getApp().$ws) {
  155. return;
  156. }
  157. console.log(getApp().$ws)
  158. let clientid, ip, wws;
  159. if (type === 'app') {
  160. clientid = uni.getStorageSync('clientid'); //获取cid
  161. ipe = ipe || appIp;
  162. ip = ipe.split(':')[1]; //过滤掉端口
  163. wws = ipe.split(':')[0] == 'http' ? 'ws' : 'wss';
  164. } else if (type === 'wx') {
  165. ip = document.domain; //过滤掉端口
  166. // ip = '192.168.3.108'; //过滤掉端口
  167. console.log(ip)
  168. wws = wsName;
  169. }
  170. console.log(getApp())
  171. getApp().$ws = uni.connectSocket({
  172. url: `${wws}://${ip}:8080/webSocket/message/app`,
  173. header: {
  174. 'content-type': 'application/json'
  175. },
  176. success(result) {
  177. console.log(result);
  178. // 监听WebSocket连接打开事件
  179. uni.onSocketOpen(res1 => {
  180. console.log(res1, 'websocket连接成功');
  181. // 通过 WebSocket 连接发送数据
  182. let obj = {};
  183. if (type === 'app') {
  184. obj = {
  185. userCount: uni.getStorageSync('userData').user.id,
  186. clientId: clientid
  187. };
  188. } else if (type === 'wx') {
  189. obj = {
  190. userCount: uni.getStorageSync('userData').user.id
  191. };
  192. }
  193. console.log(JSON.stringify(obj))
  194. uni.sendSocketMessage({
  195. data: JSON.stringify(obj),
  196. success(res2) {
  197. console.log(res2)
  198. }
  199. });
  200. // 监听WebSocket接受到服务器的消息事件
  201. uni.onSocketMessage(res3 => {
  202. console.log('收到服务器内容:' + res3.data);
  203. // 连接成功后跳转到待接单列表baba
  204. if (res3.data !== 'X') {
  205. let objData = JSON.parse(res3.data);
  206. if (objData.status == 200) {
  207. if (cate !== 'no') {
  208. uni.navigateTo({
  209. url: `../${cate}/${cate}`
  210. })
  211. }
  212. } else {
  213. // 播报 start
  214. let msg = objData.content;
  215. msg = msg.replace(/【[0-9]*】/g, function(word) {
  216. return word.substring(0, 2) + ' ' + word.substring(2);
  217. });
  218. console.log(getApp().audios)
  219. getApp().audios = getApp().audios || [];
  220. getApp().audios.push(msg);
  221. if (getApp().audios.length === 1) {
  222. startAudio();
  223. }
  224. // 播报 end
  225. }
  226. }
  227. });
  228. });
  229. uni.onSocketClose(function() {
  230. console.log('WebSocket 已关闭!');
  231. });
  232. }
  233. })
  234. }
  235. // 语音播放
  236. function startAudio() {
  237. let arr = getApp().audios;
  238. console.log(arr);
  239. if (arr.length === 0) {
  240. return;
  241. } else {
  242. const innerAudioContext = uni.createInnerAudioContext();
  243. innerAudioContext.autoplay = true;
  244. innerAudioContext.src = 'http://fanyi.baidu.com/gettts?lan=zh&text=' + arr[0] +
  245. '&spd=5&source=web';
  246. innerAudioContext.onEnded(() => {
  247. arr.shift();
  248. startAudio();
  249. })
  250. }
  251. // innerAudioContext.src = 'http://192.168.3.108/tts1.mp3';
  252. // uni.request({
  253. // url: 'http://fanyi.baidu.com/gettts?lan=zh&text=这是一个测试&spd=5&source=web',
  254. // success: (res) => {
  255. // console.log(res.data);
  256. // }
  257. // });
  258. }