http.js 7.6 KB

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