http.js 7.5 KB

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