http.js 7.7 KB

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